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");
26 imports['String.js'].load(String);
29 for (var i = 0; i < arguments.length; i++) {
31 out += arguments[i].rtrim(File.SEPARATOR);
33 else if (i == arguments.length - 1) {
34 out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
37 out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
43 read : function (path) {
45 GLib.file_get_contents(path, out, null, null);
49 isFile : function (path) {
50 return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
52 exists : function (path) {
53 return GLib.file_test(path, GLib.FileTest.EXISTS);
55 isDirectory : function (path) {
56 return GLib.file_test(path, GLib.FileTest.IS_DIR);
59 list : function (path) {
62 var f = Gio.file_new_for_path(String(path));
63 var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
67 while ((next_file = file_enum.next_file(null)) != null) {
68 listing.push(next_file.get_display_name());
71 file_enum.close(null);
78 mtime : function (path) {
79 var f = Gio.file_new_for_path(String(path));
80 var mtime = new GLib.TimeVal();
82 var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
83 info.get_modification_time(mtime);
85 return new Date(mtime.tv_sec * 1000);
89 * resolve the real path
90 * @arg path {String} Path to resolve
91 * @returns {String} the resolved path path.
94 realpath : function (path) {
95 return this.canonical(path);
97 canonical : function (path) {
98 var f = Gio.file_new_for_path(String(path));
99 var can = f.resolve_relative_path('');
100 return can.get_path();
103 * write a string to a file
104 * @arg path {String} File to write to alwasy overwrites.
105 * @arg string {String} Contents of file.
108 write : function (path, string) {
109 var f = Gio.file_new_for_path(String(path));
110 var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
111 data_out.put_string(string, null);
112 data_out.close(null);
116 * @arg path {String} File to write to
117 * @arg string {String} string to append to file.
120 append : function (path, string) {
121 var f = Gio.file_new_for_path(String(path));
122 var data_out = new Gio.DataOutputStream({
123 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
125 data_out.put_string(string, null);
126 data_out.close(null);
131 * @arg path {String} File to remove
135 remove : function (path)
137 var f = Gio.file_new_for_path(String(path));
138 return f['delete']();
140 // copy files recursively from fromDir, silently ignore them if they already exist in toDir
141 silentRecursiveCopy : function (fromDir, toDir) {
142 var filesToCopy = File.recursiveListing(fromDir);
143 var srcPath, destPath, src, dest;
145 for (var index in filesToCopy) {
146 srcPath = File.join(String(fromDir), filesToCopy[index]);
147 destPath = File.join(String(toDir), filesToCopy[index]);
149 if (File.isFile(srcPath) && !File.isFile(destPath)) {
150 File.copyFile(srcPath, destPath);
152 else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
153 File.mkdir(destPath);
159 * Make a symbolic link
160 * @arg new_link {String} The new link
161 * @arg target {String} Where it links to.
163 link : function (new_link, target) {
164 var dest = Gio.file_new_for_path(String(new_link));
165 return dest.make_symbolic_link(target, null);
169 * FIXME - needs perms setting..
171 * @arg directory {String} Directory to make
174 mkdir : function (destPath) {
175 var dest = Gio.file_new_for_path(String(destPath));
176 return dest.make_directory(null);
180 * Copy a file or (directory maybe?)
181 * @arg srcPath {String} source file
182 * @arg destPath {String} destination file
183 * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
185 copy : function (srcPath, destPath, flags) {
186 return this.copyFile(srcPath, destPath, flags);
188 copyFile : function (srcPath, destPath, flags) {
190 flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
191 var dest = Gio.file_new_for_path(String(destPath));
192 var src = Gio.file_new_for_path(String(srcPath));
194 // a bit of a hack for the fact that Gio.File.copy arguments
195 // can be nulled, but not according to the GIR file
196 return src.copy(dest, flags);
202 recursiveListing : function (dir) {
204 function recursiveListingInternal(prefix, listing, dir) {
205 var entries = File.list(dir);
208 for (var index in entries) {
209 next = entries[index];
210 fullPath = File.join(prefix, dir, next);
212 if (File.isDirectory(fullPath)) {
214 listing = listing.concat(recursiveListingInternal(next, [], fullPath));
218 next = File.join(prefix, next);
227 return recursiveListingInternal('', [], dir);