1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
5 String = imports.String.String;
11 * Library to wrap GLib and Gio basic File related methods
15 * File = import.File.File;
17 * var contents = File.read("/tmp/test.txt");
26 * @type {String} File seperator. (should be dynamic....)
32 // fixme - this needs a bitter location..
33 // they where in a string class before, but overriding String methods is not a good normally a good idea..
36 * Right trim (in here to reduce dependancies)
37 * @param {String} s the string to trim
38 * @param {String} string to trim off right..
39 * @return {String} the trimmed string
41 rtrim : function (s,toTrim) {
42 if (s.substr(s.length - toTrim.length) == toTrim) {
43 return s.slice(0, s.length - toTrim.length);
49 * Trim a string (in here to reduce dependancies)
50 * @param {String} s the string to trim
51 * @param {String} string to trim off right..
52 * @return {String} the trimmed string
54 trim : function (s,toTrim) {
55 var out = this.ltrim(s,toTrim);
56 out = this.rtrim(out,toTrim);
60 * Left Trim a string (in here to reduce dependancies)
61 * @param {String} s the string to trim
62 * @param {String} string to trim off right..
63 * @return {String} the trimmed string
65 ltrim : function (s, toTrim) {
66 if (s.substr(0, toTrim.length) == toTrim) {
67 return s.slice(toTrim.length);
73 * Get the base name of a path.
74 * @param {String} path
75 * @returns {String} basename
77 basename : function(path)
79 return path.split(File.SEPARATOR).pop();
83 * Get the directory name of a path. (could use Glib really)
84 * @param {String} path
85 * @returns {String} dirname
87 dirname : function(path)
89 var r = path.split(File.SEPARATOR)
91 return r.join(File.SEPARATOR);
96 * Join a path with the Correct File seperator (unix only at present...)
97 * Takes a variable number of arguments, and joins them together.
98 * @return {String} the joined path
102 for (var i = 0; i < arguments.length; i++) {
104 out += this.rtrim(arguments[i], File.SEPARATOR);
106 else if (i == arguments.length - 1) {
107 out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
110 out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
116 * Read a file and return as string
117 * @param {String} path The file location
118 * @return {String} the joined path
120 read : function (path) {
122 GLib.file_get_contents(path, out, null, null);
126 * Check if a path points to a file.
127 * @param {String} path The location
128 * @return {Boolean} true if it's a file
130 isFile : function (path) {
131 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
134 * Check if a path points to a file, directory or link..
135 * @param {String} path The location
136 * @return {Boolean} true if it exists
138 exists : function (path) {
139 return GLib.file_test(path, GLib.FileTest.EXISTS);
142 * Check if a path points to a directory.
143 * @param {String} path The location
144 * @return {Boolean} true if it's a directory
146 isDirectory : function (path) {
147 return GLib.file_test(path, GLib.FileTest.IS_DIR);
150 * list files in a directory.
151 * @param {String} path The directory
152 * @return {Array} list of files (with full path?)
154 list : function (path) {
157 var f = Gio.file_new_for_path(String(path));
158 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
160 var next_file = null;
162 while ((next_file = file_enum.next_file(null)) != null) {
163 listing.push(next_file.get_display_name());
166 file_enum.close(null);
173 * Get the last modification time of a file
174 * @param {String} path The location
175 * @return {Date} when the file was last modified
177 mtime : function (path) {
178 var f = Gio.file_new_for_path(String(path));
179 var mtime = new GLib.TimeVal();
181 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
182 info.get_modification_time(mtime);
184 return new Date(mtime.tv_sec * 1000);
187 * Resovle the absolute path of a file
188 * @param {String} path The location (relative) to current working directory?
189 * @return {String} the full path
191 canonical : function (path) {
192 var f = Gio.file_new_for_path(String(path));
193 var can = f.resolve_relative_path('');
194 return can.get_path();
198 * write a string to file
199 * @param {String} pathFile to write to
200 * @param {String} string Contents of file.
203 write : function (path, string) {
204 var f = Gio.file_new_for_path(String(path));
205 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
206 data_out.put_string(string, null);
207 data_out.close(null);
210 * write a string to file
211 * @param {String} pathFile to write to
212 * @param {String} string Contents of file.
215 writeBinaryArray : function (path, stringAr) {
216 var f = Gio.file_new_for_path(String(path));
217 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
218 for(var i =0; i < stringAr.length; i++) {
219 data_out.put_byte(stringAr[i], null);
220 if (i % 10000 == 0) {
221 print("wrote " + i + "/" + stringAr.length + "\n");
225 data_out.close(null);
228 * append a string to a file
229 * @param {String} path File to write to
230 * @param {String} string string to append to file.
233 append : function (path, string) {
234 var f = Gio.file_new_for_path(String(path));
235 var data_out = new Gio.DataOutputStream({
236 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
238 data_out.put_string(string, null);
239 data_out.close(null);
243 * @param {String} path File to remove
245 remove : function (path)
247 var f = Gio.file_new_for_path(String(path));
248 return f['delete']();
251 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
252 * unless you select overwrite..
253 * @param {String} src source path
254 * @param {String} dest destination path
255 * @param {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
256 * otherwise they will not be copied
259 silentRecursiveCopy : function (fromDir, toDir, opts) {
261 var filesToCopy = File.recursiveListing(fromDir);
262 var srcPath, destPath, src, dest;
263 if (typeof(opts) =='undefined') {
264 opts = Gio.FileCopyFlags.NONE;
267 for (var index in filesToCopy) {
268 srcPath = File.join(String(fromDir), filesToCopy[index]);
269 destPath = File.join(String(toDir), filesToCopy[index]);
271 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
272 File.mkdir(destPath);
275 // source is not file..?!?!?
276 if (!File.isFile(srcPath)) {
279 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
280 // do not overwrite.. - if file exists and we are not flaged to overwrite.
284 File.copyFile(srcPath, destPath, opts);
291 * @param {String} dstPath directory to make
293 mkdir : function (destPath) {
294 var dest = Gio.file_new_for_path(String(destPath));
297 return dest.make_directory(null, null);
305 * @param {String} dstPath directory to make
307 mkdirall : function (destPath) {
308 var parent = File.dirname(destPath);
309 if (!File.exists(parent)) {
310 File.mkdirall(parent);
312 if (!File.exists(destPath)) {
313 return File.mkdir(destPath);
320 * @param {String} src source path
321 * @param {String} dest destination path
322 * @param {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
325 copyFile : function (srcPath, destPath, opts) {
326 if (typeof(opts) =='undefined') {
327 opts = Gio.FileCopyFlags.NONE;
329 var dest = Gio.file_new_for_path(String(destPath));
330 var src = Gio.file_new_for_path(String(srcPath));
332 // a bit of a hack for the fact that Gio.File.copy arguments
333 // can be nulled, but not according to the GIR file
334 return src.copy(dest, opts);
337 * recursively list files in a directory.
338 * @param {String} path The directory
339 * @return {Array} list of files (with full path?)
341 recursiveListing : function (dir) {
343 function recursiveListingInternal(prefix, listing, dir) {
344 var entries = File.list(dir);
347 for (var index in entries) {
348 next = entries[index];
349 fullPath = File.join(prefix, dir, next);
351 if (File.isDirectory(fullPath)) {
353 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
357 next = File.join(prefix, next);
366 return recursiveListingInternal('', [], dir);