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");
27 imports['String.js'].load(String);
30 for (var i = 0; i < arguments.length; i++) {
32 out += arguments[i].rtrim(File.SEPARATOR);
34 else if (i == arguments.length - 1) {
35 out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
38 out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
44 read : function (path) {
46 GLib.file_get_contents(path, out, null, null);
50 isFile : function (path) {
51 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
53 exists : function (path) {
54 return GLib.file_test(path, GLib.FileTest.EXISTS);
56 isDirectory : function (path) {
57 return GLib.file_test(path, GLib.FileTest.IS_DIR);
60 list : function (path) {
63 var f = Gio.file_new_for_path(String(path));
64 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
68 while ((next_file = file_enum.next_file(null)) != null) {
69 listing.push(next_file.get_display_name());
72 file_enum.close(null);
79 mtime : function (path) {
80 var f = Gio.file_new_for_path(String(path));
81 var mtime = new GLib.TimeVal();
83 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
84 info.get_modification_time(mtime);
86 return new Date(mtime.tv_sec * 1000);
90 * resolve the real path
91 * @arg path {String} Path to resolve
92 * @returns {String} the resolved path path.
95 realpath : function (path) {
96 return this.canonical(path);
98 canonical : function (path) {
99 var f = Gio.file_new_for_path(String(path));
100 var can = f.resolve_relative_path('');
101 return can.get_path();
104 * write a string to a file
105 * @arg path {String} File to write to alwasy overwrites.
106 * @arg string {String} Contents of file.
109 write : function (path, string) {
111 var f = Gio.file_new_for_path(String(path));
112 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
113 data_out.put_string(string, null);
114 data_out.close(null);
115 print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
120 * @arg path {String} File to write to
121 * @arg string {String} string to append to file.
124 append : function (path, string) {
125 var f = Gio.file_new_for_path(String(path));
126 var data_out = new Gio.DataOutputStream({
127 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
129 data_out.put_string(string, null);
130 data_out.close(null);
135 * @arg path {String} File to remove
139 remove : function (path)
141 var f = Gio.file_new_for_path(String(path));
142 return f['delete']();
144 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
145 silentRecursiveCopy : function (fromDir, toDir) {
146 var filesToCopy = File.recursiveListing(fromDir);
147 var srcPath, destPath, src, dest;
149 for (var index in filesToCopy) {
150 srcPath = File.join(String(fromDir), filesToCopy[index]);
151 destPath = File.join(String(toDir), filesToCopy[index]);
153 if (File.isFile(srcPath) && !File.isFile(destPath)) {
154 File.copyFile(srcPath, destPath);
156 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
157 File.mkdir(destPath);
163 * Make a symbolic link
164 * @arg new_link {String} The new link
165 * @arg target {String} Where it links to.
167 link : function (new_link, target) {
168 var dest = Gio.file_new_for_path(String(new_link));
169 return dest.make_symbolic_link(target, null);
173 * FIXME - needs perms setting..
175 * @arg directory {String} Directory to make
178 mkdir : function (destPath) {
179 var dest = Gio.file_new_for_path(String(destPath));
180 return dest.make_directory(null);
184 * Copy a file or (directory maybe?)
185 * @arg srcPath {String} source file
186 * @arg destPath {String} destination file
187 * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
189 copy : function (srcPath, destPath, flags) {
190 return this.copyFile(srcPath, destPath, flags);
192 copyFile : function (srcPath, destPath, flags) {
194 flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
195 var dest = Gio.file_new_for_path(String(destPath));
196 var src = Gio.file_new_for_path(String(srcPath));
198 // a bit of a hack for the fact that Gio.File.copy arguments
199 // can be nulled, but not according to the GIR file
200 return src.copy(dest, flags);
206 recursiveListing : function (dir) {
208 function recursiveListingInternal(prefix, listing, dir) {
209 var entries = File.list(dir);
212 for (var index in entries) {
213 next = entries[index];
214 fullPath = File.join(prefix, dir, next);
216 if (File.isDirectory(fullPath)) {
218 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
222 next = File.join(prefix, next);
231 return recursiveListingInternal('', [], dir);