1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
5 String = imports.String.String;
10 * Library to wrap GLib and Gio basic File related methods
14 * File = import.File.File;
16 * var contents = File.read("/tmp/test.txt");
25 * @type {String} File seperator. (should be dynamic....)
31 // fixme - this needs a bitter location..
32 // they where in a string class before, but overriding String methods is not a good normally a good idea..
35 * Right trim (in here to reduce dependancies)
36 * @param {String} s the string to trim
37 * @param {String} string to trim off right..
38 * @return {String} the trimmed string
40 rtrim : function (s,toTrim) {
41 if (s.substr(s.length - toTrim.length) == toTrim) {
42 return s.slice(0, s.length - toTrim.length);
48 * Trim a string (in here to reduce dependancies)
49 * @param {String} s the string to trim
50 * @param {String} string to trim off right..
51 * @return {String} the trimmed string
53 trim : function (s,toTrim) {
54 var out = this.ltrim(s,toTrim);
55 out = this.rtrim(out,toTrim);
59 * Left Trim a string (in here to reduce dependancies)
60 * @param {String} s the string to trim
61 * @param {String} string to trim off right..
62 * @return {String} the trimmed string
64 ltrim : function (s, toTrim) {
65 if (s.substr(0, toTrim.length) == toTrim) {
66 return s.slice(toTrim.length);
72 * Get the base name of a path.
73 * @param {String} path
74 * @returns {String} basename
76 basename : function(path)
78 return path.split(File.SEPARATOR).pop();
82 * Get the directory name of a path. (could use Glib really)
83 * @param {String} path
84 * @returns {String} dirname
86 dirname : function(path)
88 var r = path.split(File.SEPARATOR)
90 return r.join(File.SEPARATOR);
95 * Join a path with the Correct File seperator (unix only at present...)
96 * Takes a variable number of arguments, and joins them together.
97 * @return {String} the joined path
101 for (var i = 0; i < arguments.length; i++) {
103 out += this.rtrim(arguments[i], File.SEPARATOR);
105 else if (i == arguments.length - 1) {
106 out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
109 out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
115 * Read a file and return as string
116 * @param {String} path The file location
117 * @return {String} the joined path
119 read : function (path) {
121 GLib.file_get_contents(path, out, null, null);
125 * Check if a path points to a file.
126 * @param {String} path The location
127 * @return {Boolean} true if it's a file
129 isFile : function (path) {
130 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
133 * Check if a path points to a file, directory or link..
134 * @param {String} path The location
135 * @return {Boolean} true if it exists
137 exists : function (path) {
138 return GLib.file_test(path, GLib.FileTest.EXISTS);
141 * Check if a path points to a directory.
142 * @param {String} path The location
143 * @return {Boolean} true if it's a directory
145 isDirectory : function (path) {
146 return GLib.file_test(path, GLib.FileTest.IS_DIR);
149 * list files in a directory.
150 * @param {String} path The directory
151 * @return {Array} list of files (with full path?)
153 list : function (path) {
156 var f = Gio.file_new_for_path(String(path));
157 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
159 var next_file = null;
161 while ((next_file = file_enum.next_file(null)) != null) {
162 listing.push(next_file.get_display_name());
165 file_enum.close(null);
172 * Get the last modification time of a file
173 * @param {String} path The location
174 * @return {Date} when the file was last modified
176 mtime : function (path) {
177 var f = Gio.file_new_for_path(String(path));
178 var mtime = new GLib.TimeVal();
180 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
181 info.get_modification_time(mtime);
183 return new Date(mtime.tv_sec * 1000);
186 * Resovle the absolute path of a file
187 * @param {String} path The location (relative) to current working directory?
188 * @return {String} the full path
190 canonical : function (path) {
191 var f = Gio.file_new_for_path(String(path));
192 var can = f.resolve_relative_path('');
193 return can.get_path();
197 * write a string to file
198 * @param {String} pathFile to write to
199 * @param {String} string Contents of file.
202 write : function (path, string) {
203 var f = Gio.file_new_for_path(String(path));
204 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
205 data_out.put_string(string, null);
206 data_out.close(null);
209 * append a string to a file
210 * @param {String} path File to write to
211 * @param {String} string string to append to file.
214 append : function (path, string) {
215 var f = Gio.file_new_for_path(String(path));
216 var data_out = new Gio.DataOutputStream({
217 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
219 data_out.put_string(string, null);
220 data_out.close(null);
224 * @param {String} path File to remove
226 remove : function (path)
228 var f = Gio.file_new_for_path(String(path));
229 return f['delete']();
232 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
233 * unless you select overwrite..
234 * @param {String} src source path
235 * @param {String} dest destination path
236 * @param {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
237 * otherwise they will not be copied
240 silentRecursiveCopy : function (fromDir, toDir, opts) {
242 var filesToCopy = File.recursiveListing(fromDir);
243 var srcPath, destPath, src, dest;
244 if (typeof(opts) =='undefined') {
245 opts = Gio.FileCopyFlags.NONE;
248 for (var index in filesToCopy) {
249 srcPath = File.join(String(fromDir), filesToCopy[index]);
250 destPath = File.join(String(toDir), filesToCopy[index]);
252 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
253 File.mkdir(destPath);
256 // source is not file..?!?!?
257 if (!File.isFile(srcPath)) {
260 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
261 // do not overwrite.. - if file exists and we are not flaged to overwrite.
265 File.copyFile(srcPath, destPath, opts);
272 * @param {String} dstPath directory to make
274 mkdir : function (destPath) {
275 var dest = Gio.file_new_for_path(String(destPath));
277 return dest.make_directory(null, null);
281 * @param {String} src source path
282 * @param {String} dest destination path
283 * @param {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
286 copyFile : function (srcPath, destPath, opts) {
287 if (typeof(opts) =='undefined') {
288 opts = Gio.FileCopyFlags.NONE;
290 var dest = Gio.file_new_for_path(String(destPath));
291 var src = Gio.file_new_for_path(String(srcPath));
293 // a bit of a hack for the fact that Gio.File.copy arguments
294 // can be nulled, but not according to the GIR file
295 return src.copy(dest, opts);
298 * recursively list files in a directory.
299 * @param {String} path The directory
300 * @return {Array} list of files (with full path?)
302 recursiveListing : function (dir) {
304 function recursiveListingInternal(prefix, listing, dir) {
305 var entries = File.list(dir);
308 for (var index in entries) {
309 next = entries[index];
310 fullPath = File.join(prefix, dir, next);
312 if (File.isDirectory(fullPath)) {
314 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
318 next = File.join(prefix, next);
327 return recursiveListingInternal('', [], dir);