--- /dev/null
+// <script type ="text/Javascript">
+GLib = imports.gi.GLib;
+Gio = imports.gi.Gio;
+
+imports['String.js'].load(String);
+
+var File = {
+
+ SEPARATOR : '/',
+
+ join : function () {
+ var out = "";
+ for (var i = 0; i < arguments.length; i++) {
+ if (i == 0) {
+ out += arguments[i].rtrim(File.SEPARATOR);
+ }
+ else if (i == arguments.length - 1) {
+ out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
+ }
+ else {
+ out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
+ }
+ }
+ return out;
+ },
+
+ read : function (path) {
+ var out = {};
+ GLib.file_get_contents(path, out, null, null);
+ return out['value'];
+ },
+
+ isFile : function (path) {
+ return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
+ },
+ exists : function (path) {
+ return GLib.file_test(path, GLib.FileTest.EXISTS);
+ },
+ isDirectory : function (path) {
+ return GLib.file_test(path, GLib.FileTest.IS_DIR);
+ },
+
+ list : function (path) {
+ var listing = [];
+
+ var f = Gio.file_new_for_path(String(path));
+ var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
+
+ var next_file = null;
+
+ while ((next_file = file_enum.next_file(null)) != null) {
+ listing.push(next_file.get_display_name());
+ }
+
+ file_enum.close(null);
+
+ listing.sort();
+
+ return listing;
+ },
+
+ mtime : function (path) {
+ var f = Gio.file_new_for_path(String(path));
+ var mtime = new GLib.TimeVal();
+
+ var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
+ info.get_modification_time(mtime);
+
+ return new Date(mtime.tv_sec * 1000);
+ },
+
+ canonical : function (path) {
+ var f = Gio.file_new_for_path(String(path));
+ var can = f.resolve_relative_path('');
+ return can.get_path();
+ },
+
+ write : function (path, string) {
+ var f = Gio.file_new_for_path(String(path));
+ var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
+ data_out.put_string(string, null);
+ data_out.close(null);
+ },
+
+ // copy files recursively from fromDir, silently ignore them if they already exist in toDir
+ silentRecursiveCopy : function (fromDir, toDir) {
+ var filesToCopy = File.recursiveListing(fromDir);
+ var srcPath, destPath, src, dest;
+
+ for (index in filesToCopy) {
+ srcPath = File.join(String(fromDir), filesToCopy[index]);
+ destPath = File.join(String(toDir), filesToCopy[index]);
+
+ if (File.isFile(srcPath) && !File.isFile(destPath)) {
+ File.copyFile(srcPath, destPath);
+ }
+ else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
+ File.mkdir(destPath);
+ }
+
+ }
+ },
+
+ mkdir : function (destPath) {
+ var dest = Gio.file_new_for_path(String(destPath));
+ return dest.make_directory(null, null);
+ },
+
+ copyFile : function (srcPath, destPath) {
+ var dest = Gio.file_new_for_path(String(destPath));
+ var src = Gio.file_new_for_path(String(srcPath));
+
+ // a bit of a hack for the fact that Gio.File.copy arguments
+ // can be nulled, but not according to the GIR file
+ return src.copy(dest, Gio.FileCopyFlags.NONE, null, function(_a,_b,_c) {return true;}, true);
+ },
+
+ recursiveListing : function (dir) {
+
+ function recursiveListingInternal(prefix, listing, dir) {
+ var entries = File.list(dir);
+ var next, fullPath;
+
+ for (var index in entries) {
+ next = entries[index];
+ fullPath = File.join(prefix, dir, next);
+
+ if (File.isDirectory(fullPath)) {
+ listing.push(next);
+ listing = listing.concat(recursiveListingInternal(next, [], fullPath));
+ }
+ else {
+ if (prefix) {
+ next = File.join(prefix, next);
+ }
+ listing.push(next);
+ }
+ }
+
+ return listing;
+ }
+
+ return recursiveListingInternal('', [], dir);
+ }
+
+};
\ No newline at end of file