1 // <script type ="text/Javascript">
2 const GLib = imports.gi.GLib;
3 const Gio = imports.gi.Gio;
5 var String = imports.String.String;
9 * Library to wrap GLib and Gio basic File related methods
13 * File = import.File.File;
15 * var contents = File.read("/tmp/test.txt");
24 // fixme - this needs a bitter location..
25 // they where in a string class before, but overriding String methods is not a good normally a good idea..
27 rtrim : function (s,toTrim) {
28 if (s.substr(s.length - toTrim.length) == toTrim) {
29 return s.slice(0, s.length - toTrim.length);
34 trim : function (s,toTrim) {
35 var out = this.ltrim(s,toTrim);
36 out = this.rtrim(out,toTrim);
40 ltrim : function (s, toTrim) {
41 if (s.substr(0, toTrim.length) == toTrim) {
42 return s.slice(toTrim.length);
50 for (var i = 0; i < arguments.length; i++) {
52 out += this.rtrim(arguments[i], File.SEPARATOR);
54 else if (i == arguments.length - 1) {
55 out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
58 out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
63 read : function (path) {
66 var ret = GLib.file_get_contents(path, out, null, null);
69 return '' + ((typeof(ret) == 'object') ? ret[1] : out['value']);
72 isFile : function (path) {
73 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
75 exists : function (path) {
76 return GLib.file_test(path, GLib.FileTest.EXISTS);
78 isDirectory : function (path) {
79 return GLib.file_test(path, GLib.FileTest.IS_DIR);
82 list : function (path) {
85 var f = Gio.file_new_for_path('' +(path));
86 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
90 while ((next_file = file_enum.next_file(null)) != null) {
91 listing.push(next_file.get_display_name());
94 file_enum.close(null);
101 mtime : function (path) {
102 var f = Gio.file_new_for_path('' +(path));
103 var mtime = new GLib.TimeVal();
105 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
106 info.get_modification_time(mtime);
108 return new Date(mtime.tv_sec * 1000);
111 canonical : function (path) {
112 var f = Gio.file_new_for_path('' +(path));
113 var can = f.resolve_relative_path('');
114 return can.get_path();
119 * @arg path {String} File to write to
120 * @arg string {String} Contents of file.
123 write : function (path, string) {
124 var f = Gio.file_new_for_path('' +(path));
125 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
126 data_out.put_string(string, null);
127 data_out.close(null);
131 * @arg path {String} File to write to
132 * @arg string {String} string to append to file.
135 append : function (path, string) {
136 var f = Gio.file_new_for_path('' +(path));
137 var data_out = new Gio.DataOutputStream({
138 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
140 data_out.put_string(string, null);
141 data_out.close(null);
146 * @arg path {String} File to remove
150 remove : function (path)
152 var f = Gio.file_new_for_path('' +(path));
153 var c = new Gio.Cancellable();
154 return f['delete'](c);
157 * silentRecursiveCopy
158 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
159 * unless you select overwrite..
160 * @arg {String} src source path
161 * @arg {String} dest destination path
162 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
163 * otherwise they will not be copied
166 silentRecursiveCopy : function (fromDir, toDir, opts) {
168 var filesToCopy = File.recursiveListing(fromDir);
169 var srcPath, destPath, src, dest;
170 if (typeof(opts) =='undefined') {
171 opts = Gio.FileCopyFlags.NONE;
174 for (var index in filesToCopy) {
175 srcPath = File.join('' +(fromDir), filesToCopy[index]);
176 destPath = File.join('' +(toDir), filesToCopy[index]);
178 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
179 File.mkdir(destPath);
182 // source is not file..?!?!?
183 if (!File.isFile(srcPath)) {
186 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
187 // do not overwrite.. - if file exists and we are not flaged to overwrite.
191 File.copyFile(srcPath, destPath, opts);
199 * @arg {String} dstPath directory to make
201 mkdir : function (destPath) {
202 var dest = Gio.file_new_for_path('' +(destPath));
204 return dest.make_directory(null, null);
208 * @arg {String} src source path
209 * @arg {String} dest destination path
210 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
213 copyFile : function (srcPath, destPath, opts) {
214 if (typeof(opts) =='undefined') {
215 opts = Gio.FileCopyFlags.NONE;
217 var dest = Gio.file_new_for_path('' +(destPath));
218 var src = Gio.file_new_for_path('' +(srcPath));
220 // a bit of a hack for the fact that Gio.File.copy arguments
221 // can be nulled, but not according to the GIR file
222 return src.copy(dest, opts);
225 recursiveListing : function (dir) {
227 function recursiveListingInternal(prefix, listing, dir) {
228 var entries = File.list(dir);
231 for (var index in entries) {
232 next = entries[index];
233 fullPath = File.join(prefix, dir, next);
235 if (File.isDirectory(fullPath)) {
237 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
241 next = File.join(prefix, next);
250 return recursiveListingInternal('', [], dir);