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");
30 // fixme - this needs a bitter location..
31 // they where in a string class before, but overriding String methods is not a good normally a good idea..
34 * Right trim (in here to reduce dependancies)
35 * @param {String} s the string to trim
36 * @param {String} string to trim off right..
37 * @return {String} the trimmed string
39 rtrim : function (s,toTrim) {
40 if (s.substr(s.length - toTrim.length) == toTrim) {
41 return s.slice(0, s.length - toTrim.length);
47 * Trim a string (in here to reduce dependancies)
48 * @param {String} s the string to trim
49 * @param {String} string to trim off right..
50 * @return {String} the trimmed string
52 trim : function (s,toTrim) {
53 var out = this.ltrim(s,toTrim);
54 out = this.rtrim(out,toTrim);
58 * Left Trim a string (in here to reduce dependancies)
59 * @param {String} s the string to trim
60 * @param {String} string to trim off right..
61 * @return {String} the trimmed string
63 ltrim : function (s, toTrim) {
64 if (s.substr(0, toTrim.length) == toTrim) {
65 return s.slice(toTrim.length);
71 * Join a path with the Correct File seperator (unix only at present...)
72 * Takes a variable number of arguments, and joins them together.
73 * @return {String} the joined path
77 for (var i = 0; i < arguments.length; i++) {
79 out += this.rtrim(arguments[i], File.SEPARATOR);
81 else if (i == arguments.length - 1) {
82 out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
85 out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
91 * Read a file and return as string
92 * @param {String} path The file location
93 * @return {String} the joined path
95 read : function (path) {
97 GLib.file_get_contents(path, out, null, null);
101 * Check if a path points to a file.
102 * @param {String} path The location
103 * @return {Boolean} true if it's a file
105 isFile : function (path) {
106 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
109 * Check if a path points to a file, directory or link..
110 * @param {String} path The location
111 * @return {Boolean} true if it exists
113 exists : function (path) {
114 return GLib.file_test(path, GLib.FileTest.EXISTS);
117 * Check if a path points to a directory.
118 * @param {String} path The location
119 * @return {Boolean} true if it's a directory
121 isDirectory : function (path) {
122 return GLib.file_test(path, GLib.FileTest.IS_DIR);
125 * list files in a directory.
126 * @param {String} path The directory
127 * @return {Array} list of files (with full path?)
129 list : function (path) {
132 var f = Gio.file_new_for_path(String(path));
133 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
135 var next_file = null;
137 while ((next_file = file_enum.next_file(null)) != null) {
138 listing.push(next_file.get_display_name());
141 file_enum.close(null);
148 * Get the last modification time of a file
149 * @param {String} path The location
150 * @return {Date} when the file was last modified
152 mtime : function (path) {
153 var f = Gio.file_new_for_path(String(path));
154 var mtime = new GLib.TimeVal();
156 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
157 info.get_modification_time(mtime);
159 return new Date(mtime.tv_sec * 1000);
162 * Resovle the absolute path of a file
163 * @param {String} path The location (relative) to current working directory?
164 * @return {String} the full path
166 canonical : function (path) {
167 var f = Gio.file_new_for_path(String(path));
168 var can = f.resolve_relative_path('');
169 return can.get_path();
173 * write a string to file
174 * @param path {String} File to write to
175 * @param string {String} Contents of file.
178 write : function (path, string) {
179 var f = Gio.file_new_for_path(String(path));
180 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
181 data_out.put_string(string, null);
182 data_out.close(null);
185 * append a string to a file
186 * @param path {String} File to write to
187 * @param string {String} string to append to file.
190 append : function (path, string) {
191 var f = Gio.file_new_for_path(String(path));
192 var data_out = new Gio.DataOutputStream({
193 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
195 data_out.put_string(string, null);
196 data_out.close(null);
200 * @param path {String} File to remove
202 remove : function (path)
204 var f = Gio.file_new_for_path(String(path));
205 return f['delete']();
208 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
209 * unless you select overwrite..
210 * @param {String} src source path
211 * @param {String} dest destination path
212 * @param {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
213 * otherwise they will not be copied
216 silentRecursiveCopy : function (fromDir, toDir, opts) {
218 var filesToCopy = File.recursiveListing(fromDir);
219 var srcPath, destPath, src, dest;
220 if (typeof(opts) =='undefined') {
221 opts = Gio.FileCopyFlags.NONE;
224 for (var index in filesToCopy) {
225 srcPath = File.join(String(fromDir), filesToCopy[index]);
226 destPath = File.join(String(toDir), filesToCopy[index]);
228 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
229 File.mkdir(destPath);
232 // source is not file..?!?!?
233 if (!File.isFile(srcPath)) {
236 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
237 // do not overwrite.. - if file exists and we are not flaged to overwrite.
241 File.copyFile(srcPath, destPath, opts);
248 * @param {String} dstPath directory to make
250 mkdir : function (destPath) {
251 var dest = Gio.file_new_for_path(String(destPath));
253 return dest.make_directory(null, null);
257 * @param {String} src source path
258 * @param {String} dest destination path
259 * @param {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
262 copyFile : function (srcPath, destPath, opts) {
263 if (typeof(opts) =='undefined') {
264 opts = Gio.FileCopyFlags.NONE;
266 var dest = Gio.file_new_for_path(String(destPath));
267 var src = Gio.file_new_for_path(String(srcPath));
269 // a bit of a hack for the fact that Gio.File.copy arguments
270 // can be nulled, but not according to the GIR file
271 return src.copy(dest, opts);
274 * recursively list files in a directory.
275 * @param {String} path The directory
276 * @return {Array} list of files (with full path?)
278 recursiveListing : function (dir) {
280 function recursiveListingInternal(prefix, listing, dir) {
281 var entries = File.list(dir);
284 for (var index in entries) {
285 next = entries[index];
286 fullPath = File.join(prefix, dir, next);
288 if (File.isDirectory(fullPath)) {
290 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
294 next = File.join(prefix, next);
303 return recursiveListingInternal('', [], dir);