Partial Fix #5560 - Gitlive - branching wip
[gitlive] / File.js
1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
3 Gio = imports.gi.Gio;
4
5
6
7 /**
8 * @namespace File
9
10 * Library to wrap GLib and Gio basic File related methods
11
12 * usage:
13
14 * File = import.File.File;
15
16 * var contents = File.read("/tmp/test.txt");
17 *
18
19
20 */
21 var File = {
22
23     SEPARATOR : '/',
24
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..
27        
28     rtrim : function (s,toTrim) {
29         if (s.substr(s.length - toTrim.length) == toTrim) {
30             return s.slice(0, s.length - toTrim.length);
31         }
32    
33         return s;
34     },
35    trim : function (s,toTrim) {
36         var out = s.ltrim(toTrim);
37         out = out.rtrim(toTrim);
38         return out;
39     },
40     
41     ltrim : function (s, toTrim) {
42         if (s.substr(0, toTrim.length) == toTrim) {
43             return s.slice(toTrim.length);
44         }
45         
46         return s;
47     },
48     
49     join : function () {
50         var out = "";
51         for (var i = 0; i < arguments.length; i++) {
52             if (i == 0) {
53               out += this.rtrim(arguments[i], File.SEPARATOR);
54             }
55             else if (i == arguments.length - 1) {
56               out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
57             }
58             else {
59               out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
60             }
61         }
62         return out;
63     },
64
65     read : function (path) {
66         var out = {};
67         GLib.file_get_contents(path, out, null, null);
68         
69         return out['value'];
70     },
71
72     isFile : function (path) {
73       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
74     },
75     exists : function (path) {
76       return GLib.file_test(path, GLib.FileTest.EXISTS);
77     },
78     isDirectory : function (path) {
79       return GLib.file_test(path, GLib.FileTest.IS_DIR);
80     },
81
82     list : function (path) {
83         var listing = [];
84
85         var f = Gio.file_new_for_path(String(path));
86         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
87
88         var next_file = null;
89
90         while ((next_file = file_enum.next_file(null)) != null) {
91           listing.push(next_file.get_display_name());
92         }
93
94         file_enum.close(null);
95
96         listing.sort();
97          return listing;
98     },
99
100     mtime : function (path) {
101         var f = Gio.file_new_for_path(String(path));
102         var mtime = new GLib.TimeVal();
103
104         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
105         info.get_modification_time(mtime);
106
107         return new Date(mtime.tv_sec * 1000);
108     },
109
110     /**
111      * resolve the real path
112      * @arg path {String} Path to resolve
113      * @returns {String} the resolved path path.
114      * 
115      */
116     realpath :  function (path) {
117         return imports.os.realpath(path);
118     },
119     canonical : function (path) { 
120         var f = Gio.file_new_for_path(String(path));
121         var can = f.resolve_relative_path('');
122         return can.get_path();
123     },
124     /**
125      * write a string to a file
126      * @arg path {String} File to write to alwasy overwrites.
127      * @arg string {String} Contents of file.
128      * 
129      */
130     write : function (path, string) {
131         var d = new Date();
132         var f = Gio.file_new_for_path(String(path));
133         var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
134         data_out.put_string(string, null);
135         data_out.close(null);
136         print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
137         
138     },
139     /**
140      * append
141      * @arg path {String} File to write to
142      * @arg string {String} string to append to file.
143      * 
144      */
145     append : function (path, string) {
146         var f = Gio.file_new_for_path(String(path));
147         var data_out = new Gio.DataOutputStream({
148                 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
149         });
150         data_out.put_string(string, null);
151         data_out.close(null);
152     },
153     /**
154      * remove 
155      * Delete a file.
156      * @arg path {String} File to remove
157      * 
158      * 
159      */
160     remove : function (path)
161     {
162         var f = Gio.file_new_for_path(String(path));
163         return f['delete']();
164     },
165     // copy files recursively from fromDir, silently ignore them if they already exist in toDir
166     silentRecursiveCopy : function (fromDir, toDir) {
167         var filesToCopy = File.recursiveListing(fromDir);
168         var srcPath, destPath, src, dest;
169
170         for (var index in filesToCopy) {
171           srcPath = File.join(String(fromDir), filesToCopy[index]);
172           destPath = File.join(String(toDir), filesToCopy[index]);
173
174           if (File.isFile(srcPath) && !File.isFile(destPath)) {
175             File.copyFile(srcPath, destPath);
176           }
177           else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
178             File.mkdir(destPath);
179           }
180
181         }
182     },
183     /**
184      * Make a symbolic link
185      * @arg  new_link {String} The new link
186      * @arg  target    {String} Where it links to.
187      */
188     link : function (new_link, target) {
189         var dest = Gio.file_new_for_path(String(new_link));
190         return dest.make_symbolic_link(target, null);
191     },
192     /**
193      * Make a directory
194      * FIXME - needs perms setting..
195      * 
196      * @arg  directory  {String} Directory to make
197      * @arg  recursive {Boolean}
198      */
199
200     mkdir : function (destPath, rec) {
201         //print("mkdir: " + destPath);
202         rec = rec || false;
203         if (rec && !File.isDirectory(GLib.path_get_dirname(destPath))) {
204             File.mkdir(GLib.path_get_dirname(destPath),true);
205         }
206     
207         var dest = Gio.file_new_for_path(String(destPath));
208         return dest.make_directory(null);
209     },
210
211     /**
212      * Copy a file or (directory maybe?)
213      * @arg  srcPath {String} source file
214      * @arg  destPath {String} destination file
215      * @arg  flags {Gio.FileCopyFlags} to overwrite etc...  Gio.FileCopyFlags.OVERWRITE
216      */
217     copy : function (srcPath, destPath, flags) {
218         return this.copyFile(srcPath, destPath, flags);
219     },
220     copyFile : function (srcPath, destPath, flags) {
221         
222         flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
223         var dest = Gio.file_new_for_path(String(destPath));
224         var src = Gio.file_new_for_path(String(srcPath));
225
226         // a bit of a hack for the fact that Gio.File.copy arguments
227         // can be nulled, but not according to the GIR file
228         return src.copy(dest, flags);
229     },
230     
231     
232     
233
234     recursiveListing : function (dir) {
235
236         function recursiveListingInternal(prefix, listing, dir) {
237           var entries = File.list(dir);
238           var next, fullPath;
239
240           for (var index in entries) {
241             next = entries[index];
242             fullPath = File.join(prefix, dir, next);
243
244             if (File.isDirectory(fullPath)) {
245               listing.push(next);
246               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
247             }
248             else {
249               if (prefix) {
250                 next = File.join(prefix, next);
251               }
252               listing.push(next);
253             }
254           }
255
256           return listing;
257         }
258
259         return recursiveListingInternal('', [], dir);
260     }
261
262 };