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);
87 * resolve the real path
88 * @arg path {String} Path to resolve
89 * @returns {String} the resolved path path.
92 realpath : function (path) {
93 return this.canonical(path);
95 canonical : function (path) {
96 var f = Gio.file_new_for_path(String(path));
97 var can = f.resolve_relative_path('');
98 return can.get_path();
101 * write a string to a file
102 * @arg path {String} File to write to alwasy overwrites.
103 * @arg string {String} Contents of file.
106 write : function (path, string) {
107 var f = Gio.file_new_for_path(String(path));
108 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
109 data_out.put_string(string, null);
110 data_out.close(null);
114 * @arg path {String} File to write to
115 * @arg string {String} string to append to file.
118 append : function (path, string) {
119 var f = Gio.file_new_for_path(String(path));
120 var data_out = new Gio.DataOutputStream({
121 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
123 data_out.put_string(string, null);
124 data_out.close(null);
129 * @arg path {String} File to remove
133 remove : function (path)
135 var f = Gio.file_new_for_path(String(path));
136 return f['delete']();
138 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
139 silentRecursiveCopy : function (fromDir, toDir) {
140 var filesToCopy = File.recursiveListing(fromDir);
141 var srcPath, destPath, src, dest;
143 for (var index in filesToCopy) {
144 srcPath = File.join(String(fromDir), filesToCopy[index]);
145 destPath = File.join(String(toDir), filesToCopy[index]);
147 if (File.isFile(srcPath) && !File.isFile(destPath)) {
148 File.copyFile(srcPath, destPath);
150 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
151 File.mkdir(destPath);
157 * Make a symbolic link
158 * @arg new_link {String} The new link
159 * @arg target {String} Where it links to.
161 link : function (new_link, target) {
162 var dest = Gio.file_new_for_path(String(new_link));
163 return dest.make_symbolic_link(target, null);
167 * @arg directory {String} Directory to make
170 mkdir : function (destPath) {
171 var dest = Gio.file_new_for_path(String(destPath));
172 return dest.make_directory(null);
176 * Copy a file or (directory maybe?)
177 * @arg srcPath {String} source file
178 * @arg destPath {String} destination file
179 * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
181 copy : function (srcPath, destPath, flags) {
182 return this.copyFile(srcPath, destPath, flags);
184 copyFile : function (srcPath, destPath, flags) {
186 flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
187 var dest = Gio.file_new_for_path(String(destPath));
188 var src = Gio.file_new_for_path(String(srcPath));
190 // a bit of a hack for the fact that Gio.File.copy arguments
191 // can be nulled, but not according to the GIR file
192 return src.copy(dest, flags);
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);