1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
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 // fixme - this needs a bitter location..
26 // they where in a string class before, but overriding String methods is not a good normally a good idea..
28 rtrim : function (s,toTrim) {
29 if (s.substr(s.length - toTrim.length) == toTrim) {
30 return s.slice(0, s.length - toTrim.length);
35 trim : function (s,toTrim) {
36 var out = s.ltrim(toTrim);
37 out = out.rtrim(toTrim);
41 ltrim : function (s, toTrim) {
42 if (s.substr(0, toTrim.length) == toTrim) {
43 return s.slice(toTrim.length);
51 for (var i = 0; i < arguments.length; i++) {
53 out += this.rtrim(arguments[i], File.SEPARATOR);
55 else if (i == arguments.length - 1) {
56 out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
59 out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
67 var ar = s.split(File.SEPARATOR);
69 return ar.join(File.SEPARATOR);
71 basename : function(s)
73 var ar = s.split(File.SEPARATOR);
77 read : function (path) {
79 GLib.file_get_contents(path, out, null, null);
81 //print(JSON.stringify(out));
86 isFile : function (path) {
87 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
89 exists : function (path) {
90 return GLib.file_test(path, GLib.FileTest.EXISTS);
92 isDirectory : function (path) {
93 return GLib.file_test(path, GLib.FileTest.IS_DIR);
96 list : function (path) {
99 var f = Gio.file_new_for_path(String(path));
100 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
102 var next_file = null;
104 while ((next_file = file_enum.next_file(null)) != null) {
105 listing.push(next_file.get_display_name());
108 file_enum.close(null);
115 mtime : function (path) {
116 var f = Gio.file_new_for_path(String(path));
117 var mtime = new GLib.TimeVal();
119 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
120 info.get_modification_time(mtime);
122 return new Date(mtime.tv_sec * 1000);
126 * resolve the real path
127 * @arg path {String} Path to resolve
128 * @returns {String} the resolved path path.
131 realpath : function (path) {
132 return this.canonical(path);
134 canonical : function (path) {
135 var f = Gio.file_new_for_path(String(path));
136 var can = f.resolve_relative_path('');
137 return can.get_path();
140 * write a string to a file
141 * @arg path {String} File to write to alwasy overwrites.
142 * @arg string {String} Contents of file.
145 write : function (path, string) {
147 var f = Gio.file_new_for_path(String(path));
148 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
149 data_out.put_string(string, null);
150 data_out.close(null);
151 print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
156 * @arg path {String} File to write to
157 * @arg string {String} string to append to file.
160 append : function (path, string) {
161 var f = Gio.file_new_for_path(String(path));
162 var data_out = new Gio.DataOutputStream({
163 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
165 data_out.put_string(string, null);
166 data_out.close(null);
171 * @arg path {String} File to remove
175 remove : function (path)
177 var f = Gio.file_new_for_path(String(path));
178 return f['delete']();
180 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
181 silentRecursiveCopy : function (fromDir, toDir) {
182 var filesToCopy = File.recursiveListing(fromDir);
183 var srcPath, destPath, src, dest;
185 for (var index in filesToCopy) {
186 srcPath = File.join(String(fromDir), filesToCopy[index]);
187 destPath = File.join(String(toDir), filesToCopy[index]);
189 if (File.isFile(srcPath) && !File.isFile(destPath)) {
190 File.copyFile(srcPath, destPath);
192 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
193 File.mkdir(destPath);
199 * Make a symbolic link
200 * @arg new_link {String} The new link
201 * @arg target {String} Where it links to.
203 link : function (new_link, target) {
204 var dest = Gio.file_new_for_path(String(new_link));
205 return dest.make_symbolic_link(target, null);
209 * FIXME - needs perms setting..
211 * @arg directory {String} Directory to make
214 mkdir : function (destPath) {
215 var dest = Gio.file_new_for_path(String(destPath));
216 return dest.make_directory(null);
220 * Copy a file or (directory maybe?)
221 * @arg srcPath {String} source file
222 * @arg destPath {String} destination file
223 * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
225 copy : function (srcPath, destPath, flags) {
226 return this.copyFile(srcPath, destPath, flags);
228 copyFile : function (srcPath, destPath, flags) {
230 flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
231 var dest = Gio.file_new_for_path(String(destPath));
232 var src = Gio.file_new_for_path(String(srcPath));
234 // a bit of a hack for the fact that Gio.File.copy arguments
235 // can be nulled, but not according to the GIR file
236 return src.copy(dest, flags);
242 recursiveListing : function (dir) {
244 function recursiveListingInternal(prefix, listing, dir) {
245 var entries = File.list(dir);
248 for (var index in entries) {
249 next = entries[index];
250 fullPath = File.join(prefix, dir, next);
252 if (File.isDirectory(fullPath)) {
254 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
258 next = File.join(prefix, next);
267 return recursiveListingInternal('', [], dir);