1 // <script type ="text/Javascript">
2 const GLib = imports.gi.GLib;
3 const Gio = imports.gi.Gio;
5 //const String = imports.String.String;
6 const console = imports.console.console;
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 // fixme - this needs a bitter location..
27 // 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);
36 trim : function (s,toTrim) {
37 var out = this.ltrim(s,toTrim);
38 out = this.rtrim(out,toTrim);
42 ltrim : function (s, toTrim) {
43 if (s.substr(0, toTrim.length) == toTrim) {
44 return s.slice(toTrim.length);
52 for (var i = 0; i < arguments.length; i++) {
54 out += this.rtrim(arguments[i], File.SEPARATOR);
56 else if (i == arguments.length - 1) {
57 out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
60 out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
66 read : function (path) {
68 if (typeof(Seed) == 'undefined') {
71 [ret, out, err] = GLib.file_get_contents(path, out, null, null);
74 GLib.file_get_contents(path, out, null);
79 isFile : function (path) {
80 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
83 exists : function (path) {
84 return GLib.file_test(path, GLib.FileTest.EXISTS);
87 isDirectory : function (path) {
88 return GLib.file_test(path, GLib.FileTest.IS_DIR);
91 list : function (path) {
94 //var f = Gio.file_new_for_path(String(path));
95 var f = Gio.file_new_for_path(path);
96 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
100 while ((next_file = file_enum.next_file(null)) != null) {
101 listing.push(next_file.get_display_name());
104 file_enum.close(null);
111 mtime : function (path) {
112 //var f = Gio.file_new_for_path(String(path));
113 var f = Gio.file_new_for_path(path);
114 var mtime = new GLib.TimeVal();
116 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
117 info.get_modification_time(mtime);
119 return new Date(mtime.tv_sec * 1000);
122 canonical : function (path) {
123 //var f = Gio.file_new_for_path(String(path));
124 var f = Gio.file_new_for_path(path);
125 var can = f.resolve_relative_path('');
126 return can.get_path();
131 * @arg path {String} File to write to
132 * @arg string {String} Contents of file.
135 write : function (path, string) {
136 //var f = Gio.file_new_for_path(String(path));
137 var f = Gio.file_new_for_path(path);
138 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
139 data_out.put_string(string, null);
140 data_out.close(null);
145 * @arg path {String} File to write to
146 * @arg string {String} string to append to file.
149 append : function (path, string) {
150 //var f = Gio.file_new_for_path(String(path));
151 var f = Gio.file_new_for_path(path);
152 var data_out = new Gio.DataOutputStream({
153 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
155 data_out.put_string(string, null);
156 data_out.close(null);
162 * @arg path {String} File to remove
166 remove : function (path)
168 //var f = Gio.file_new_for_path(String(path));
169 var f = Gio.file_new_for_path(path);
170 return f['delete']();
174 * silentRecursiveCopy
175 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
176 * unless you select overwrite..
177 * @arg {String} src source path
178 * @arg {String} dest destination path
179 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
180 * otherwise they will not be copied
183 silentRecursiveCopy : function (fromDir, toDir, opts) {
185 var filesToCopy = File.recursiveListing(fromDir);
186 var srcPath, destPath, src, dest;
187 if (typeof(opts) =='undefined') {
188 opts = Gio.FileCopyFlags.NONE;
191 for (var index in filesToCopy) {
192 //srcPath = File.join(String(fromDir), filesToCopy[index]);
193 //destPath = File.join(String(toDir), filesToCopy[index]);
194 srcPath = File.join(fromDir, filesToCopy[index]);
195 destPath = File.join(toDir, filesToCopy[index]);
197 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
198 File.mkdir(destPath);
202 // source is not file..?!?!?
203 if (!File.isFile(srcPath)) {
207 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
208 // do not overwrite.. - if file exists and we are not flaged to overwrite.
212 File.copyFile(srcPath, destPath, opts);
219 * @arg {String} dstPath directory to make
221 mkdir : function (destPath) {
222 //var dest = Gio.file_new_for_path(String(destPath));
223 var dest = Gio.file_new_for_path(destPath);
225 return dest.make_directory(null, null);
230 * @arg {String} src source path
231 * @arg {String} dest destination path
232 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
235 copyFile : function (srcPath, destPath, opts) {
236 if (typeof(opts) =='undefined') {
237 opts = Gio.FileCopyFlags.NONE;
239 var dest = Gio.file_new_for_path(String(destPath));
240 //var dest = Gio.file_new_for_path(destPath);
241 var src = Gio.file_new_for_path(String(srcPath));
242 //var src = Gio.file_new_for_path(srcPath);
244 // a bit of a hack for the fact that Gio.File.copy arguments
245 // can be nulled, but not according to the GIR file
246 //return src.copy(dest, opts);
247 if (typeof(Seed) != 'undefined')
248 return src.copy(dest, opts, null);
250 return src.copy(dest, opts, null, null);
254 recursiveListing : function (dir) {
256 function recursiveListingInternal(prefix, listing, dir) {
257 var entries = File.list(dir);
260 for (var index in entries) {
261 next = entries[index];
262 fullPath = File.join(prefix, dir, next);
264 if (File.isDirectory(fullPath)) {
266 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
270 next = File.join(prefix, next);
279 return recursiveListingInternal('', [], dir);