1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
5 imports['String.js'].load(String);
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 for (var i = 0; i < arguments.length; i++) {
29 out += arguments[i].rtrim(File.SEPARATOR);
31 else if (i == arguments.length - 1) {
32 out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
35 out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
41 read : function (path) {
43 GLib.file_get_contents(path, out, null, null);
47 isFile : function (path) {
48 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
50 exists : function (path) {
51 return GLib.file_test(path, GLib.FileTest.EXISTS);
53 isDirectory : function (path) {
54 return GLib.file_test(path, GLib.FileTest.IS_DIR);
57 list : function (path) {
60 var f = Gio.file_new_for_path(String(path));
61 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
65 while ((next_file = file_enum.next_file(null)) != null) {
66 listing.push(next_file.get_display_name());
69 file_enum.close(null);
76 mtime : function (path) {
77 var f = Gio.file_new_for_path(String(path));
78 var mtime = new GLib.TimeVal();
80 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
81 info.get_modification_time(mtime);
83 return new Date(mtime.tv_sec * 1000);
86 canonical : function (path) {
87 var f = Gio.file_new_for_path(String(path));
88 var can = f.resolve_relative_path('');
89 return can.get_path();
94 * @arg path {String} File to write to
95 * @arg string {String} Contents of file.
98 write : function (path, string) {
99 var f = Gio.file_new_for_path(String(path));
100 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
101 data_out.put_string(string, null);
102 data_out.close(null);
106 * @arg path {String} File to write to
107 * @arg string {String} string to append to file.
110 append : function (path, string) {
111 var f = Gio.file_new_for_path(String(path));
112 var data_out = new Gio.DataOutputStream({
113 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
115 data_out.put_string(string, null);
116 data_out.close(null);
121 * @arg path {String} File to remove
125 remove : function (path)
127 var f = Gio.file_new_for_path(String(path));
128 return f['delete']();
131 * silentRecursiveCopy
132 * copy files recursively from fromDir, silently ignore them if they already exist in toDir
133 * unless you select overwrite..
134 * @arg {String} src source path
135 * @arg {String} dest destination path
136 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to
137 * otherwise they will not be copied
140 silentRecursiveCopy : function (fromDir, toDir, opts) {
142 var filesToCopy = File.recursiveListing(fromDir);
143 var srcPath, destPath, src, dest;
144 if (typeof(opts) =='undefined') {
145 opts = Gio.FileCopyFlags.NONE;
148 for (var index in filesToCopy) {
149 srcPath = File.join(String(fromDir), filesToCopy[index]);
150 destPath = File.join(String(toDir), filesToCopy[index]);
152 if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
153 File.mkdir(destPath);
156 // source is not file..?!?!?
157 if (!File.isFile(srcPath)) {
160 if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) {
161 // do not overwrite.. - if file exists and we are not flaged to overwrite.
165 File.copyFile(srcPath, destPath, opts);
173 * @arg {String} dstPath directory to make
175 mkdir : function (destPath) {
176 var dest = Gio.file_new_for_path(String(destPath));
177 return dest.make_directory(null, null);
181 * @arg {String} src source path
182 * @arg {String} dest destination path
183 * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite..
186 copyFile : function (srcPath, destPath, opts) {
187 if (typeof(opts) =='undefined') {
188 opts = Gio.FileCopyFlags.NONE;
190 var dest = Gio.file_new_for_path(String(destPath));
191 var src = Gio.file_new_for_path(String(srcPath));
193 // a bit of a hack for the fact that Gio.File.copy arguments
194 // can be nulled, but not according to the GIR file
195 return src.copy(dest, opts);
198 recursiveListing : function (dir) {
200 function recursiveListingInternal(prefix, listing, dir) {
201 var entries = File.list(dir);
204 for (var index in entries) {
205 next = entries[index];
206 fullPath = File.join(prefix, dir, next);
208 if (File.isDirectory(fullPath)) {
210 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
214 next = File.join(prefix, next);
223 return recursiveListingInternal('', [], dir);