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 (toTrim) {
29 if (this.substr(this.length - toTrim.length) == toTrim) {
30 return this.slice(0, this.length - toTrim.length);
42 for (var i = 0; i < arguments.length; i++) {
44 out += arguments[i].rtrim(File.SEPARATOR);
46 else if (i == arguments.length - 1) {
47 out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
50 out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
56 read : function (path) {
58 GLib.file_get_contents(path, out, null, null);
62 isFile : function (path) {
63 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
65 exists : function (path) {
66 return GLib.file_test(path, GLib.FileTest.EXISTS);
68 isDirectory : function (path) {
69 return GLib.file_test(path, GLib.FileTest.IS_DIR);
72 list : function (path) {
75 var f = Gio.file_new_for_path(String(path));
76 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
80 while ((next_file = file_enum.next_file(null)) != null) {
81 listing.push(next_file.get_display_name());
84 file_enum.close(null);
91 mtime : function (path) {
92 var f = Gio.file_new_for_path(String(path));
93 var mtime = new GLib.TimeVal();
95 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
96 info.get_modification_time(mtime);
98 return new Date(mtime.tv_sec * 1000);
102 * resolve the real path
103 * @arg path {String} Path to resolve
104 * @returns {String} the resolved path path.
107 realpath : function (path) {
108 return this.canonical(path);
110 canonical : function (path) {
111 var f = Gio.file_new_for_path(String(path));
112 var can = f.resolve_relative_path('');
113 return can.get_path();
116 * write a string to a file
117 * @arg path {String} File to write to alwasy overwrites.
118 * @arg string {String} Contents of file.
121 write : function (path, string) {
123 var f = Gio.file_new_for_path(String(path));
124 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
125 data_out.put_string(string, null);
126 data_out.close(null);
127 print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
132 * @arg path {String} File to write to
133 * @arg string {String} string to append to file.
136 append : function (path, string) {
137 var f = Gio.file_new_for_path(String(path));
138 var data_out = new Gio.DataOutputStream({
139 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
141 data_out.put_string(string, null);
142 data_out.close(null);
147 * @arg path {String} File to remove
151 remove : function (path)
153 var f = Gio.file_new_for_path(String(path));
154 return f['delete']();
156 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
157 silentRecursiveCopy : function (fromDir, toDir) {
158 var filesToCopy = File.recursiveListing(fromDir);
159 var srcPath, destPath, src, dest;
161 for (var index in filesToCopy) {
162 srcPath = File.join(String(fromDir), filesToCopy[index]);
163 destPath = File.join(String(toDir), filesToCopy[index]);
165 if (File.isFile(srcPath) && !File.isFile(destPath)) {
166 File.copyFile(srcPath, destPath);
168 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
169 File.mkdir(destPath);
175 * Make a symbolic link
176 * @arg new_link {String} The new link
177 * @arg target {String} Where it links to.
179 link : function (new_link, target) {
180 var dest = Gio.file_new_for_path(String(new_link));
181 return dest.make_symbolic_link(target, null);
185 * FIXME - needs perms setting..
187 * @arg directory {String} Directory to make
190 mkdir : function (destPath) {
191 var dest = Gio.file_new_for_path(String(destPath));
192 return dest.make_directory(null);
196 * Copy a file or (directory maybe?)
197 * @arg srcPath {String} source file
198 * @arg destPath {String} destination file
199 * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
201 copy : function (srcPath, destPath, flags) {
202 return this.copyFile(srcPath, destPath, flags);
204 copyFile : function (srcPath, destPath, flags) {
206 flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
207 var dest = Gio.file_new_for_path(String(destPath));
208 var src = Gio.file_new_for_path(String(srcPath));
210 // a bit of a hack for the fact that Gio.File.copy arguments
211 // can be nulled, but not according to the GIR file
212 return src.copy(dest, flags);
218 recursiveListing : function (dir) {
220 function recursiveListingInternal(prefix, listing, dir) {
221 var entries = File.list(dir);
224 for (var index in entries) {
225 next = entries[index];
226 fullPath = File.join(prefix, dir, next);
228 if (File.isDirectory(fullPath)) {
230 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
234 next = File.join(prefix, next);
243 return recursiveListingInternal('', [], dir);