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 * @property {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 * Join a path with the Correct File seperator (unix only at present...)
73 * Takes a variable number of arguments, and joins them together.
74 * @return {String} the joined path
78 for (var i = 0; i < arguments.length; i++) {
80 out += this.rtrim(arguments[i], File.SEPARATOR);
82 else if (i == arguments.length - 1) {
83 out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
86 out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
92 * Read a file and return as string
93 * @param {String} path The file location
94 * @return {String} the joined path
96 read : function (path) {
98 GLib.file_get_contents(path, out, null, null);
102 * Check if a path points to a file.
103 * @param {String} path The location
104 * @return {Boolean} true if it's a file
106 isFile : function (path) {
107 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
110 * Check if a path points to a file, directory or link..
111 * @param {String} path The location
112 * @return {Boolean} true if it exists
114 exists : function (path) {
115 return GLib.file_test(path, GLib.FileTest.EXISTS);
118 * Check if a path points to a directory.
119 * @param {String} path The location
120 * @return {Boolean} true if it's a directory
122 isDirectory : function (path) {
123 return GLib.file_test(path, GLib.FileTest.IS_DIR);
126 * list files in a directory.
127 * @param {String} path The directory
128 * @return {Array} list of files (with full path?)
130 list : function (path) {
133 var f = Gio.file_new_for_path(String(path));
134 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
136 var next_file = null;
138 while ((next_file = file_enum.next_file(null)) != null) {
139 listing.push(next_file.get_display_name());
142 file_enum.close(null);
149 * Get the last modification time of a file
150 * @param {String} path The location
151 * @return {Date} when the file was last modified
153 mtime : function (path) {
154 var f = Gio.file_new_for_path(String(path));
155 var mtime = new GLib.TimeVal();
157 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
158 info.get_modification_time(mtime);
160 return new Date(mtime.tv_sec * 1000);
163 * Resovle the absolute path of a file
164 * @param {String} path The location (relative) to current working directory?
165 * @return {String} the full path
167 canonical : function (path) {
168 var f = Gio.file_new_for_path(String(path));
169 var can = f.resolve_relative_path('');
170 return can.get_path();
174 * write a string to file
175 * @param {String} pathFile to write to
176 * @param {String} string Contents of file.
179 write : function (path, string) {
180 var f = Gio.file_new_for_path(String(path));
181 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
182 data_out.put_string(string, null);
183 data_out.close(null);
186 * append a string to a file
187 * @param {String} path File to write to
188 * @param {String} string string to append to file.
191 append : function (path, string) {
192 var f = Gio.file_new_for_path(String(path));
193 var data_out = new Gio.DataOutputStream({
194 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
196 data_out.put_string(string, null);
197 data_out.close(null);
201 * @param {String} path File to remove
203 remove : function (path)
205 var f = Gio.file_new_for_path(String(path));
206 return f['delete']();
209 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
210 * unless you select overwrite..
211 * @param {String} src source path
212 * @param {String} dest destination path
213 * @param {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
214 * otherwise they will not be copied
217 silentRecursiveCopy : function (fromDir, toDir, opts) {
219 var filesToCopy = File.recursiveListing(fromDir);
220 var srcPath, destPath, src, dest;
221 if (typeof(opts) =='undefined') {
222 opts = Gio.FileCopyFlags.NONE;
225 for (var index in filesToCopy) {
226 srcPath = File.join(String(fromDir), filesToCopy[index]);
227 destPath = File.join(String(toDir), filesToCopy[index]);
229 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
230 File.mkdir(destPath);
233 // source is not file..?!?!?
234 if (!File.isFile(srcPath)) {
237 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
238 // do not overwrite.. - if file exists and we are not flaged to overwrite.
242 File.copyFile(srcPath, destPath, opts);
249 * @param {String} dstPath directory to make
251 mkdir : function (destPath) {
252 var dest = Gio.file_new_for_path(String(destPath));
254 return dest.make_directory(null, null);
258 * @param {String} src source path
259 * @param {String} dest destination path
260 * @param {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
263 copyFile : function (srcPath, destPath, opts) {
264 if (typeof(opts) =='undefined') {
265 opts = Gio.FileCopyFlags.NONE;
267 var dest = Gio.file_new_for_path(String(destPath));
268 var src = Gio.file_new_for_path(String(srcPath));
270 // a bit of a hack for the fact that Gio.File.copy arguments
271 // can be nulled, but not according to the GIR file
272 return src.copy(dest, opts);
275 * recursively list files in a directory.
276 * @param {String} path The directory
277 * @return {Array} list of files (with full path?)
279 recursiveListing : function (dir) {
281 function recursiveListingInternal(prefix, listing, dir) {
282 var entries = File.list(dir);
285 for (var index in entries) {
286 next = entries[index];
287 fullPath = File.join(prefix, dir, next);
289 if (File.isDirectory(fullPath)) {
291 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
295 next = File.join(prefix, next);
304 return recursiveListingInternal('', [], dir);