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);
55 for (var i = 0; i < arguments.length; i++) {
57 out += arguments[i].rtrim(File.SEPARATOR);
59 else if (i == arguments.length - 1) {
60 out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
63 out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
69 read : function (path) {
71 GLib.file_get_contents(path, out, null, null);
75 isFile : function (path) {
76 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
78 exists : function (path) {
79 return GLib.file_test(path, GLib.FileTest.EXISTS);
81 isDirectory : function (path) {
82 return GLib.file_test(path, GLib.FileTest.IS_DIR);
85 list : function (path) {
88 var f = Gio.file_new_for_path(String(path));
89 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
93 while ((next_file = file_enum.next_file(null)) != null) {
94 listing.push(next_file.get_display_name());
97 file_enum.close(null);
104 mtime : function (path) {
105 var f = Gio.file_new_for_path(String(path));
106 var mtime = new GLib.TimeVal();
108 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
109 info.get_modification_time(mtime);
111 return new Date(mtime.tv_sec * 1000);
115 * resolve the real path
116 * @arg path {String} Path to resolve
117 * @returns {String} the resolved path path.
120 realpath : function (path) {
121 return this.canonical(path);
123 canonical : function (path) {
124 var f = Gio.file_new_for_path(String(path));
125 var can = f.resolve_relative_path('');
126 return can.get_path();
129 * write a string to a file
130 * @arg path {String} File to write to alwasy overwrites.
131 * @arg string {String} Contents of file.
134 write : function (path, string) {
136 var f = Gio.file_new_for_path(String(path));
137 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
138 data_out.put_string(string, null);
139 data_out.close(null);
140 print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
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 data_out = new Gio.DataOutputStream({
152 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
154 data_out.put_string(string, null);
155 data_out.close(null);
160 * @arg path {String} File to remove
164 remove : function (path)
166 var f = Gio.file_new_for_path(String(path));
167 return f['delete']();
169 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
170 silentRecursiveCopy : function (fromDir, toDir) {
171 var filesToCopy = File.recursiveListing(fromDir);
172 var srcPath, destPath, src, dest;
174 for (var index in filesToCopy) {
175 srcPath = File.join(String(fromDir), filesToCopy[index]);
176 destPath = File.join(String(toDir), filesToCopy[index]);
178 if (File.isFile(srcPath) && !File.isFile(destPath)) {
179 File.copyFile(srcPath, destPath);
181 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
182 File.mkdir(destPath);
188 * Make a symbolic link
189 * @arg new_link {String} The new link
190 * @arg target {String} Where it links to.
192 link : function (new_link, target) {
193 var dest = Gio.file_new_for_path(String(new_link));
194 return dest.make_symbolic_link(target, null);
198 * FIXME - needs perms setting..
200 * @arg directory {String} Directory to make
203 mkdir : function (destPath) {
204 var dest = Gio.file_new_for_path(String(destPath));
205 return dest.make_directory(null);
209 * Copy a file or (directory maybe?)
210 * @arg srcPath {String} source file
211 * @arg destPath {String} destination file
212 * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
214 copy : function (srcPath, destPath, flags) {
215 return this.copyFile(srcPath, destPath, flags);
217 copyFile : function (srcPath, destPath, flags) {
219 flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
220 var dest = Gio.file_new_for_path(String(destPath));
221 var src = Gio.file_new_for_path(String(srcPath));
223 // a bit of a hack for the fact that Gio.File.copy arguments
224 // can be nulled, but not according to the GIR file
225 return src.copy(dest, flags);
231 recursiveListing : function (dir) {
233 function recursiveListingInternal(prefix, listing, dir) {
234 var entries = File.list(dir);
237 for (var index in entries) {
238 next = entries[index];
239 fullPath = File.join(prefix, dir, next);
241 if (File.isDirectory(fullPath)) {
243 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
247 next = File.join(prefix, next);
256 return recursiveListingInternal('', [], dir);