1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
5 imports['String.js'].load(String);
13 for (var i = 0; i < arguments.length; i++) {
15 out += arguments[i].rtrim(File.SEPARATOR);
17 else if (i == arguments.length - 1) {
18 out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
21 out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
27 read : function (path) {
29 GLib.file_get_contents(path, out, null, null);
33 isFile : function (path) {
34 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
36 exists : function (path) {
37 return GLib.file_test(path, GLib.FileTest.EXISTS);
39 isDirectory : function (path) {
40 return GLib.file_test(path, GLib.FileTest.IS_DIR);
43 list : function (path) {
46 var f = Gio.file_new_for_path(String(path));
47 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
51 while ((next_file = file_enum.next_file(null)) != null) {
52 listing.push(next_file.get_display_name());
55 file_enum.close(null);
62 mtime : function (path) {
63 var f = Gio.file_new_for_path(String(path));
64 var mtime = new GLib.TimeVal();
66 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
67 info.get_modification_time(mtime);
69 return new Date(mtime.tv_sec * 1000);
72 canonical : function (path) {
73 var f = Gio.file_new_for_path(String(path));
74 var can = f.resolve_relative_path('');
75 return can.get_path();
78 write : function (path, string) {
79 var f = Gio.file_new_for_path(String(path));
80 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
81 data_out.put_string(string, null);
85 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
86 silentRecursiveCopy : function (fromDir, toDir) {
87 var filesToCopy = File.recursiveListing(fromDir);
88 var srcPath, destPath, src, dest;
90 for (var index in filesToCopy) {
91 srcPath = File.join(String(fromDir), filesToCopy[index]);
92 destPath = File.join(String(toDir), filesToCopy[index]);
94 if (File.isFile(srcPath) && !File.isFile(destPath)) {
95 File.copyFile(srcPath, destPath);
97 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
104 mkdir : function (destPath) {
105 var dest = Gio.file_new_for_path(String(destPath));
106 return dest.make_directory(null, null);
109 copyFile : function (srcPath, destPath) {
110 var dest = Gio.file_new_for_path(String(destPath));
111 var src = Gio.file_new_for_path(String(srcPath));
113 // a bit of a hack for the fact that Gio.File.copy arguments
114 // can be nulled, but not according to the GIR file
115 return src.copy(dest, Gio.FileCopyFlags.NONE);
118 recursiveListing : function (dir) {
120 function recursiveListingInternal(prefix, listing, dir) {
121 var entries = File.list(dir);
124 for (var index in entries) {
125 next = entries[index];
126 fullPath = File.join(prefix, dir, next);
128 if (File.isDirectory(fullPath)) {
130 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
134 next = File.join(prefix, next);
143 return recursiveListingInternal('', [], dir);