1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
5 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) {
65 GLib.file_get_contents(path, out, null, null);
69 isFile : function (path) {
70 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
72 exists : function (path) {
73 return GLib.file_test(path, GLib.FileTest.EXISTS);
75 isDirectory : function (path) {
76 return GLib.file_test(path, GLib.FileTest.IS_DIR);
79 list : function (path) {
82 var f = Gio.file_new_for_path(String(path));
83 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
87 while ((next_file = file_enum.next_file(null)) != null) {
88 listing.push(next_file.get_display_name());
91 file_enum.close(null);
98 mtime : function (path) {
99 var f = Gio.file_new_for_path(String(path));
100 var mtime = new GLib.TimeVal();
102 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
103 info.get_modification_time(mtime);
105 return new Date(mtime.tv_sec * 1000);
108 canonical : function (path) {
109 var f = Gio.file_new_for_path(String(path));
110 var can = f.resolve_relative_path('');
111 return can.get_path();
116 * @arg path {String} File to write to
117 * @arg string {String} Contents of file.
120 write : function (path, string) {
121 var f = Gio.file_new_for_path(String(path));
122 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
123 data_out.put_string(string, null);
124 data_out.close(null);
128 * @arg path {String} File to write to
129 * @arg string {String} string to append to file.
132 append : function (path, string) {
133 var f = Gio.file_new_for_path(String(path));
134 var data_out = new Gio.DataOutputStream({
135 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
137 data_out.put_string(string, null);
138 data_out.close(null);
143 * @arg path {String} File to remove
147 remove : function (path)
149 var f = Gio.file_new_for_path(String(path));
150 return f['delete']();
153 * silentRecursiveCopy
154 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
155 * unless you select overwrite..
156 * @arg {String} src source path
157 * @arg {String} dest destination path
158 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
159 * otherwise they will not be copied
162 silentRecursiveCopy : function (fromDir, toDir, opts) {
164 var filesToCopy = File.recursiveListing(fromDir);
165 var srcPath, destPath, src, dest;
166 if (typeof(opts) =='undefined') {
167 opts = Gio.FileCopyFlags.NONE;
170 for (var index in filesToCopy) {
171 srcPath = File.join(String(fromDir), filesToCopy[index]);
172 destPath = File.join(String(toDir), filesToCopy[index]);
174 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
175 File.mkdir(destPath);
178 // source is not file..?!?!?
179 if (!File.isFile(srcPath)) {
182 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
183 // do not overwrite.. - if file exists and we are not flaged to overwrite.
187 File.copyFile(srcPath, destPath, opts);
195 * @arg {String} dstPath directory to make
197 mkdir : function (destPath) {
198 var dest = Gio.file_new_for_path(String(destPath));
200 return dest.make_directory(null, null);
204 * @arg {String} src source path
205 * @arg {String} dest destination path
206 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
209 copyFile : function (srcPath, destPath, opts) {
210 if (typeof(opts) =='undefined') {
211 opts = Gio.FileCopyFlags.NONE;
213 var dest = Gio.file_new_for_path(String(destPath));
214 var src = Gio.file_new_for_path(String(srcPath));
216 // a bit of a hack for the fact that Gio.File.copy arguments
217 // can be nulled, but not according to the GIR file
218 return src.copy(dest, opts);
221 recursiveListing : function (dir) {
223 function recursiveListingInternal(prefix, listing, dir) {
224 var entries = File.list(dir);
227 for (var index in entries) {
228 next = entries[index];
229 fullPath = File.join(prefix, dir, next);
231 if (File.isDirectory(fullPath)) {
233 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
237 next = File.join(prefix, next);
246 return recursiveListingInternal('', [], dir);