1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
5 String = imports.String.String;
9 * Library to wrap GLib and Gio basic File related methods
13 * File = import.File.File;
15 * var contents = File.read("/tmp/test.txt");
26 for (var i = 0; i < arguments.length; i++) {
28 out += arguments[i].rtrim(File.SEPARATOR);
30 else if (i == arguments.length - 1) {
31 out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
34 out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
40 read : function (path) {
42 GLib.file_get_contents(path, out, null, null);
46 isFile : function (path) {
47 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
49 exists : function (path) {
50 return GLib.file_test(path, GLib.FileTest.EXISTS);
52 isDirectory : function (path) {
53 return GLib.file_test(path, GLib.FileTest.IS_DIR);
56 list : function (path) {
59 var f = Gio.file_new_for_path(String(path));
60 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
64 while ((next_file = file_enum.next_file(null)) != null) {
65 listing.push(next_file.get_display_name());
68 file_enum.close(null);
75 mtime : function (path) {
76 var f = Gio.file_new_for_path(String(path));
77 var mtime = new GLib.TimeVal();
79 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
80 info.get_modification_time(mtime);
82 return new Date(mtime.tv_sec * 1000);
85 canonical : function (path) {
86 var f = Gio.file_new_for_path(String(path));
87 var can = f.resolve_relative_path('');
88 return can.get_path();
93 * @arg path {String} File to write to
94 * @arg string {String} Contents of file.
97 write : function (path, string) {
98 var f = Gio.file_new_for_path(String(path));
99 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
100 data_out.put_string(string, null);
101 data_out.close(null);
105 * @arg path {String} File to write to
106 * @arg string {String} string to append to file.
109 append : function (path, string) {
110 var f = Gio.file_new_for_path(String(path));
111 var data_out = new Gio.DataOutputStream({
112 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
114 data_out.put_string(string, null);
115 data_out.close(null);
120 * @arg path {String} File to remove
124 remove : function (path)
126 var f = Gio.file_new_for_path(String(path));
127 return f['delete']();
130 * silentRecursiveCopy
131 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
132 * unless you select overwrite..
133 * @arg {String} src source path
134 * @arg {String} dest destination path
135 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
136 * otherwise they will not be copied
139 silentRecursiveCopy : function (fromDir, toDir, opts) {
141 var filesToCopy = File.recursiveListing(fromDir);
142 var srcPath, destPath, src, dest;
143 if (typeof(opts) =='undefined') {
144 opts = Gio.FileCopyFlags.NONE;
147 for (var index in filesToCopy) {
148 srcPath = File.join(String(fromDir), filesToCopy[index]);
149 destPath = File.join(String(toDir), filesToCopy[index]);
151 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
152 File.mkdir(destPath);
155 // source is not file..?!?!?
156 if (!File.isFile(srcPath)) {
159 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
160 // do not overwrite.. - if file exists and we are not flaged to overwrite.
164 File.copyFile(srcPath, destPath, opts);
172 * @arg {String} dstPath directory to make
174 mkdir : function (destPath) {
175 var dest = Gio.file_new_for_path(String(destPath));
176 return dest.make_directory(null, null);
180 * @arg {String} src source path
181 * @arg {String} dest destination path
182 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
185 copyFile : function (srcPath, destPath, opts) {
186 if (typeof(opts) =='undefined') {
187 opts = Gio.FileCopyFlags.NONE;
189 var dest = Gio.file_new_for_path(String(destPath));
190 var src = Gio.file_new_for_path(String(srcPath));
192 // a bit of a hack for the fact that Gio.File.copy arguments
193 // can be nulled, but not according to the GIR file
194 return src.copy(dest, opts);
197 recursiveListing : function (dir) {
199 function recursiveListingInternal(prefix, listing, dir) {
200 var entries = File.list(dir);
203 for (var index in entries) {
204 next = entries[index];
205 fullPath = File.join(prefix, dir, next);
207 if (File.isDirectory(fullPath)) {
209 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
213 next = File.join(prefix, next);
222 return recursiveListingInternal('', [], dir);