home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / firefox / filesystem.js < prev    next >
Text File  |  2006-08-07  |  5KB  |  147 lines

  1. var G_File = {};
  2. G_File.getHomeFile = function(opt_file) {
  3. return this.getSpecialFile("Home", opt_file);
  4. }
  5. G_File.getProfileFile = function(opt_file) {
  6. return this.getSpecialFile("ProfD", opt_file);
  7. }
  8. G_File.getTempFile = function(opt_file) {
  9. return this.getSpecialFile("TmpD", opt_file);
  10. }
  11. G_File.getSpecialFile = function(loc, opt_file) {
  12. var file = Cc["@mozilla.org/file/directory_service;1"]
  13. .getService(Ci.nsIProperties)
  14. .get(loc, Ci.nsILocalFile);
  15. if (opt_file) {
  16. file.append(opt_file);
  17. }
  18. return file;
  19. }
  20. G_File.createUniqueTempFile = function(opt_baseName) {
  21. var baseName = (opt_baseName || (new Date().getTime())) + ".tmp";
  22. var file = this.getSpecialFile("TmpD", baseName);
  23. file.createUnique(file.NORMAL_FILE_TYPE, 0644);
  24. return file;
  25. }
  26. G_File.createUniqueTempDir = function(opt_baseName) {
  27. var baseName = (opt_baseName || (new Date().getTime())) + ".tmp";
  28. var dir = this.getSpecialFile("TmpD", baseName);
  29. dir.createUnique(dir.DIRECTORY_TYPE, 0744);
  30. return dir;
  31. }
  32. G_File.fromFileURI = function(uri) {
  33. if (uri.indexOf("file://") != 0)
  34. throw new Error("File path must be a file:// URL");
  35. var fileHandler = Cc["@mozilla.org/network/protocol;1?name=file"]
  36. .getService(Ci.nsIFileProtocolHandler);
  37. return fileHandler.getFileFromURLSpec(uri);
  38. }
  39. G_File.PR_RDONLY = 0x01;      // read-only
  40. G_File.PR_WRONLY = 0x02;      // write only
  41. G_File.PR_RDWR = 0x04;        // reading and writing
  42. G_File.PR_CREATE_FILE = 0x08; // create if it doesn't exist
  43. G_File.PR_APPEND = 0x10;      // file pntr reset to end prior to writes
  44. G_File.PR_TRUNCATE = 0x20;    // file exists its length is set to zero
  45. G_File.PR_SYNC = 0x40;        // writes wait for data to be physically written
  46. G_File.PR_EXCL = 0x80;        // file does not exist ? created : no action
  47. G_File.__defineGetter__("LINE_END_CHAR", function() {
  48. var end_char;
  49. if ("@mozilla.org/xre/app-info;1" in Cc) {
  50. end_char = Cc["@mozilla.org/xre/app-info;1"]
  51. .getService(Ci.nsIXULRuntime)
  52. .OS == "WINNT" ? "\r\n" : "\n";
  53. } else {
  54. end_char = Cc["@mozilla.org/network/protocol;1?name=http"]
  55. .getService(Ci.nsIHttpProtocolHandler)
  56. .platform.toLowerCase().indexOf("win") == 0 ? "\r\n" : "\n";
  57. }
  58. G_File.__defineGetter__("LINE_END_CHAR", function() { return end_char; });
  59. return end_char;
  60. });
  61. function G_FileReader(file) {
  62. this.file_ = isString(file) ? G_File.fromFileURI(file) : file;
  63. }
  64. G_FileReader.readAll = function(file) {
  65. var reader = new G_FileReader(file);
  66. try {
  67. return reader.read();
  68. } finally {
  69. reader.close();
  70. }
  71. }
  72. G_FileReader.prototype.read = function(opt_maxBytes) {
  73. if (!this.stream_) {
  74. var fs = Cc["@mozilla.org/network/file-input-stream;1"]
  75. .createInstance(Ci.nsIFileInputStream);
  76. fs.init(this.file_, G_File.PR_RDONLY, 0444, 0);
  77. this.stream_ = Cc["@mozilla.org/scriptableinputstream;1"]
  78. .createInstance(Ci.nsIScriptableInputStream);
  79. this.stream_.init(fs);
  80. }
  81. if (!isDef(opt_maxBytes)) {
  82. opt_maxBytes = this.stream_.available();
  83. }
  84. return this.stream_.read(opt_maxBytes);
  85. }
  86. G_FileReader.prototype.close = function(opt_maxBytes) {
  87. if (this.stream_) {
  88. this.stream_.close();
  89. this.stream_ = null;
  90. }
  91. }
  92. function G_FileWriter(file, opt_append) {
  93. this.file_ = typeof file == "string" ? G_File.fromFileURI(file) : file;
  94. this.append_ = !!opt_append;
  95. }
  96. G_FileWriter.writeAll = function(file, data, opt_append) {
  97. var writer = new G_FileWriter(file, opt_append);
  98. try {
  99. return writer.write(data);
  100. } finally {
  101. writer.close();
  102. return 0;
  103. }
  104. }
  105. G_FileWriter.prototype.write = function(data) {
  106. if (!this.stream_) {
  107. this.stream_ = Cc["@mozilla.org/network/file-output-stream;1"]
  108. .createInstance(Ci.nsIFileOutputStream);
  109. var flags = G_File.PR_WRONLY |
  110. G_File.PR_CREATE_FILE |
  111. (this.append_ ? G_File.PR_APPEND : G_File.PR_TRUNCATE);
  112. this.stream_.init(this.file_,
  113. flags,
  114. -1 /* default perms */,
  115. 0 /* no special behavior */);
  116. }
  117. return this.stream_.write(data, data.length);
  118. }
  119. G_FileWriter.prototype.writeLine = function(data) {
  120. this.write(data + G_File.LINE_END_CHAR);
  121. }
  122. G_FileWriter.prototype.close = function() {
  123. if (this.stream_) {
  124. this.stream_.close();
  125. this.stream_ = null;
  126. }
  127. }
  128. function TEST_G_File() {
  129. if (G_GDEBUG) {
  130. var z = "gfile UNITTEST";
  131. G_debugService.enableZone(z);
  132. G_Debug(z, "Starting");
  133. try {
  134. var dir = G_File.createUniqueTempDir();
  135. } catch(e) {
  136. G_Debug(z, e);
  137. G_Assert(z, false, "Failed to create temp dir");
  138. }
  139. G_Assert(z, dir.exists(), "Temp dir doesn't exist: " + dir.path);
  140. G_Assert(z, dir.isDirectory(), "Temp dir isn't a directory: " + dir.path);
  141. G_Assert(z, dir.isReadable(), "Temp dir isn't readable: " + dir.path);
  142. G_Assert(z, dir.isWritable(), "Temp dir isn't writable: " + dir.path);
  143. dir.remove(true /* recurse */);
  144. G_Debug(z, "PASS");
  145. }
  146. }
  147.