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);
65 read : function (path) {
67 GLib.file_get_contents(path, out, null, null);
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(String(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);
100 mtime : function (path) {
101 var f = Gio.file_new_for_path(String(path));
102 var mtime = new GLib.TimeVal();
104 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
105 info.get_modification_time(mtime);
107 return new Date(mtime.tv_sec * 1000);
111 * resolve the real path
112 * @arg path {String} Path to resolve
113 * @returns {String} the resolved path path.
116 realpath : function (path) {
117 return imports.os.realpath(path);
119 canonical : function (path) {
120 var f = Gio.file_new_for_path(String(path));
121 var can = f.resolve_relative_path('');
122 return can.get_path();
125 * write a string to a file
126 * @arg path {String} File to write to alwasy overwrites.
127 * @arg string {String} Contents of file.
130 write : function (path, string) {
132 var f = Gio.file_new_for_path(String(path));
133 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
134 data_out.put_string(string, null);
135 data_out.close(null);
136 print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
141 * @arg path {String} File to write to
142 * @arg string {String} string to append to file.
145 append : function (path, string) {
146 var f = Gio.file_new_for_path(String(path));
147 var data_out = new Gio.DataOutputStream({
148 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
150 data_out.put_string(string, null);
151 data_out.close(null);
156 * @arg path {String} File to remove
160 remove : function (path)
162 var f = Gio.file_new_for_path(String(path));
163 return f['delete']();
165 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
166 silentRecursiveCopy : function (fromDir, toDir) {
167 var filesToCopy = File.recursiveListing(fromDir);
168 var srcPath, destPath, src, dest;
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.isFile(srcPath) && !File.isFile(destPath)) {
175 File.copyFile(srcPath, destPath);
177 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
178 File.mkdir(destPath);
184 * Make a symbolic link
185 * @arg new_link {String} The new link
186 * @arg target {String} Where it links to.
188 link : function (new_link, target) {
189 var dest = Gio.file_new_for_path(String(new_link));
190 return dest.make_symbolic_link(target, null);
194 * FIXME - needs perms setting..
196 * @arg directory {String} Directory to make
197 * @arg recursive {Boolean}
200 mkdir : function (destPath, rec) {
201 //print("mkdir: " + destPath);
203 if (rec && !File.isDirectory(GLib.path_get_dirname(destPath))) {
204 File.mkdir(GLib.path_get_dirname(destPath),true);
207 var dest = Gio.file_new_for_path(String(destPath));
208 return dest.make_directory(null);
212 * Copy a file or (directory maybe?)
213 * @arg srcPath {String} source file
214 * @arg destPath {String} destination file
215 * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
217 copy : function (srcPath, destPath, flags) {
218 return this.copyFile(srcPath, destPath, flags);
220 copyFile : function (srcPath, destPath, flags) {
222 flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
223 var dest = Gio.file_new_for_path(String(destPath));
224 var src = Gio.file_new_for_path(String(srcPath));
226 // a bit of a hack for the fact that Gio.File.copy arguments
227 // can be nulled, but not according to the GIR file
228 return src.copy(dest, flags);
234 recursiveListing : function (dir) {
236 function recursiveListingInternal(prefix, listing, dir) {
237 var entries = File.list(dir);
240 for (var index in entries) {
241 next = entries[index];
242 fullPath = File.join(prefix, dir, next);
244 if (File.isDirectory(fullPath)) {
246 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
250 next = File.join(prefix, next);
259 return recursiveListingInternal('', [], dir);