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);
83 isFile : function (path) {
84 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
86 exists : function (path) {
87 return GLib.file_test(path, GLib.FileTest.EXISTS);
89 isDirectory : function (path) {
90 return GLib.file_test(path, GLib.FileTest.IS_DIR);
93 list : function (path) {
96 var f = Gio.file_new_for_path(String(path));
97 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
101 while ((next_file = file_enum.next_file(null)) != null) {
102 listing.push(next_file.get_display_name());
105 file_enum.close(null);
112 mtime : function (path) {
113 var f = Gio.file_new_for_path(String(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);
123 * resolve the real path
124 * @arg path {String} Path to resolve
125 * @returns {String} the resolved path path.
128 realpath : function (path) {
129 return this.canonical(path);
131 canonical : function (path) {
132 var f = Gio.file_new_for_path(String(path));
133 var can = f.resolve_relative_path('');
134 return can.get_path();
137 * write a string to a file
138 * @arg path {String} File to write to alwasy overwrites.
139 * @arg string {String} Contents of file.
142 write : function (path, string) {
144 var f = Gio.file_new_for_path(String(path));
145 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
146 data_out.put_string(string, null);
147 data_out.close(null);
148 print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
153 * @arg path {String} File to write to
154 * @arg string {String} string to append to file.
157 append : function (path, string) {
158 var f = Gio.file_new_for_path(String(path));
159 var data_out = new Gio.DataOutputStream({
160 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
162 data_out.put_string(string, null);
163 data_out.close(null);
168 * @arg path {String} File to remove
172 remove : function (path)
174 var f = Gio.file_new_for_path(String(path));
175 return f['delete']();
177 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
178 silentRecursiveCopy : function (fromDir, toDir) {
179 var filesToCopy = File.recursiveListing(fromDir);
180 var srcPath, destPath, src, dest;
182 for (var index in filesToCopy) {
183 srcPath = File.join(String(fromDir), filesToCopy[index]);
184 destPath = File.join(String(toDir), filesToCopy[index]);
186 if (File.isFile(srcPath) && !File.isFile(destPath)) {
187 File.copyFile(srcPath, destPath);
189 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
190 File.mkdir(destPath);
196 * Make a symbolic link
197 * @arg new_link {String} The new link
198 * @arg target {String} Where it links to.
200 link : function (new_link, target) {
201 var dest = Gio.file_new_for_path(String(new_link));
202 return dest.make_symbolic_link(target, null);
206 * FIXME - needs perms setting..
208 * @arg directory {String} Directory to make
211 mkdir : function (destPath) {
212 var dest = Gio.file_new_for_path(String(destPath));
213 return dest.make_directory(null);
217 * Copy a file or (directory maybe?)
218 * @arg srcPath {String} source file
219 * @arg destPath {String} destination file
220 * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
222 copy : function (srcPath, destPath, flags) {
223 return this.copyFile(srcPath, destPath, flags);
225 copyFile : function (srcPath, destPath, flags) {
227 flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
228 var dest = Gio.file_new_for_path(String(destPath));
229 var src = Gio.file_new_for_path(String(srcPath));
231 // a bit of a hack for the fact that Gio.File.copy arguments
232 // can be nulled, but not according to the GIR file
233 return src.copy(dest, flags);
239 recursiveListing : function (dir) {
241 function recursiveListingInternal(prefix, listing, dir) {
242 var entries = File.list(dir);
245 for (var index in entries) {
246 next = entries[index];
247 fullPath = File.join(prefix, dir, next);
249 if (File.isDirectory(fullPath)) {
251 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
255 next = File.join(prefix, next);
264 return recursiveListingInternal('', [], dir);