Fix #5725 - disable create branch until ticket selected
[gitlive] / Monitor.vala
1 //<Script type="text/javascript">
2 //var Gio     = imports.gi.Gio;
3 //var GLib    = imports.gi.GLib;
4
5 //var XObject = imports.XObject.XObject;
6 //var File    = imports.File.File;
7
8 /// # valac  --pkg gio-2.0  --pkg posix Monitor.vala -o /tmp/Monitor
9
10  
11 //using Gee; // for array list?
12 /*
13 static int main (string[] args) {
14     // A reference to our file
15     //var file = File.new_for_path ("data.txt");
16     MainLoop loop = new MainLoop ();
17     print("starting");
18     var m = new Monitor();
19     
20     m.add("/home/alan/gitlive");
21     m.start();
22     loop.run ();
23
24     return 0;
25
26 }
27 */
28
29 public class  MonitorNamePathDir {
30     public string action;
31     public string name;
32     public string path;
33     public string dir;
34     
35     public MonitorNamePathDir(string name, string path, string dir)
36     {
37         this.name = name;
38         this.path = path;
39         this.dir = dir;
40         this.action = "?";
41         
42     }
43 }
44
45
46
47 public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
48
49
50 /**
51  * Monitor class - handles monitor managment for a large tree...
52  *
53  *
54  * This 
55  * 
56  * usage : 
57  * x = new Monitor({
58      change : function () {
59          * ....
60          *}
61   }
62  * x.add('/somepath')
63  * x.stop() // stops all scanning.
64  * x.play(); // starts the scanning
65  * 
66  * 
67  * 
68  * 
69  */
70  
71 public abstract class gitMonitorBase : Object
72 {
73         public abstract void monitor(string path, int depth = 0);
74
75  
76 public class Monitor : gitMonitorBase
77 {
78
79
80
81     public Monitor()
82     {
83        
84      
85         this.monitors = new Array<FileMonitor> ();
86         this.top = new Array<string> ();
87         this.paused = false;
88     }
89      
90     public Array<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
91     public Array<string> top; // list of top level directories..
92     public bool paused;
93     /**
94      * add a directory or file to monitor
95      */
96     public void add (string add)
97     {
98         
99         print("Monitor.add: " + add);
100         this.top.append_val(add);
101     }
102     /**
103      * start monitoring
104      */
105     public void start()
106     {
107         for(int i = 0; i < this.top.length ; i++) {
108             this.monitor(this.top.index(i));
109         }
110     }
111     /**
112      * stop / pause monitoring
113      * 
114      */
115     public void stop()
116     {
117         
118         for(int i = 0; i < this.monitors.length ; i++) {
119             this.monitors.index(i).cancel();
120         } 
121         this.monitors = new Array<FileMonitor>(); // clean /destroy/ kill old?
122     }
123     /**
124      * pause monitoring - without changing what's monitored 
125      */
126     public void pause()
127     {
128         this.paused = true;
129     }
130     /**
131      * resume monitoring - without changing what's monitored 
132      */
133     public void resume()
134     {
135         this.paused = false;
136     }
137     /**
138      * monitor a file or directory (privatish)
139      *
140      * initially called with ~/gitlive  null 0 (effectvely)
141      * 
142      * 
143      */
144     public override void monitor(string path, int depth = 0)
145     {
146          
147         //GLib.debug("ADD: (%d): %s\n", depth, path);
148         
149         //depth = typeof(depth) == 'number'  ? depth *1 : 0;
150         depth = depth > 0  ? depth *1 : 0;
151         
152         
153         //fn = fn || function (fm, f, of, event_type, uh) {
154         //    _this.onEvent(fm, f, of, event_type, uh);
155         //}
156                  
157         var f = File.new_for_path(path);
158             //var cancel = new Gio.Cancellable ();
159         if (depth > 0) { 
160   
161             try {
162                  var fm = f.monitor(FileMonitorFlags.SEND_MOVED + FileMonitorFlags.WATCH_MOVES,null);  
163                  //var fm = f.monitor(FileMonitorFlags.WATCH_MOVES,null);
164  
165                  fm.changed.connect( this.onEvent );
166                 this.monitors.append_val(fm);
167
168             } catch (Error e) {
169                   GLib.debug("Error adding monitor: %s\n", e.message);
170                   GLib.debug("Try: \n\nsudo su\necho 512 > /proc/sys/fs/inotify/max_user_instances\n");     
171                 // FIXME -- show error? do nothing..            
172             }
173             // print("ADD path " + depth + ' ' + path);
174         }
175         // iterate children?
176         // - this is not used.
177         //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
178             
179         //    this.initRepo(path);
180         //}
181         FileEnumerator file_enum;
182         var cancellable = new Cancellable ();
183         try {      
184             file_enum = f.enumerate_children(
185                FileAttribute.STANDARD_DISPLAY_NAME + "," +   FileAttribute.STANDARD_TYPE,
186                         FileQueryInfoFlags.NOFOLLOW_SYMLINKS,  // FileQueryInfoFlags.NONE,
187                cancellable);
188         } catch (Error e) {
189             // FIXME - show error..
190             return;
191         }
192         FileInfo next_file;
193         
194         while (cancellable.is_cancelled () == false ) {
195             try {
196                 next_file = file_enum .next_file (cancellable);
197             } catch(Error e) {
198                 print(e.message);
199                 break;
200             }
201
202             if (next_file == null) {
203                 break;
204             }
205
206             //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
207
208             if (next_file.get_file_type() != FileType.DIRECTORY) {
209                 next_file = null;
210                 continue;
211             }
212
213
214             //stdout.printf("Monitor.monitor: got file %s : type :%u\n",
215             //        next_file.get_display_name(), next_file.get_file_type());
216
217
218             if (next_file.get_is_symlink()) {
219                 next_file = null;
220                 continue;
221             }
222             
223             if (next_file.get_display_name()[0] == '.') {
224                 next_file = null;
225                 continue;
226             }
227             var sp = path+"/"+next_file.get_display_name();
228             // skip modules.
229             //print("got a file : " + sp);
230          
231             next_file = null;
232             
233             
234             
235             this.monitor(sp, depth + 1);
236             
237         }
238         try {
239             file_enum.close(null);
240         } catch(Error e) {
241             // ignore?
242         }
243     }
244     
245     
246     
247     public File realpath(File file)
248     {
249         if (file != null) {
250             return file;
251         }
252         
253         if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
254             var rp = Posix.realpath(file.get_path());
255             return File.new_for_path(rp);  
256             
257         }
258         // file does not currently exist..
259         // check parent.
260         
261 // FIX ME - string split?/? 
262         var bn = file.get_basename();
263         var ar =  file.get_path().split("/");
264         ar.resize(ar.length-1);
265         var dirname = string.joinv("/",ar );
266         var rp = Posix.realpath(dirname);
267         return File.new_for_path(rp + "/" + bn);
268         
269     }
270    
271     
272     
273      
274     public void onEvent(File f_orig, File? of_orig, FileMonitorEvent event_type)
275     {
276         if (this.paused) {
277             return;
278         }
279        // print("onEvent\n");
280         var f = this.realpath(f_orig);
281         
282  
283         MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
284                 
285         GLib.debug("onEvent src: %s", f.get_path());
286     
287         try {
288                 
289
290             switch(event_type) {
291                 case FileMonitorEvent.CHANGED:
292                     src.action = "changed";
293                     this.onChanged(src);
294                     return; // ingore thise?? -wait for changes_done_htin?
295                     
296                 case FileMonitorEvent.CHANGES_DONE_HINT:
297                     src.action = "changed";
298                     this.onChangesDoneHint(src);
299                     return;
300                     
301                 case FileMonitorEvent.DELETED:
302                     src.action = "rm";
303                     this.onDeleted(src);
304                     return;
305                     
306                 case FileMonitorEvent.CREATED:
307                     src.action = "created";
308                     this.onCreated(src);
309                     return;
310                 
311                 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
312                     src.action = "attrib";
313                     this.onAttributeChanged(src);
314                     return;
315                 
316                 case FileMonitorEvent.MOVED: // eg. chmod/chatt
317                 case FileMonitorEvent.MOVED_IN: // eg. chmod/chatt
318                 case FileMonitorEvent.MOVED_OUT: // eg. chmod/chatt                                
319                 case FileMonitorEvent.RENAMED: // eg. chmod/chatt                      
320                                         if (of_orig == null) { // move from outside of monitoring?
321                                                 this.onChanged(src);
322                                                 return;
323                                         }
324                                         
325                                         
326                    var of = this.realpath(of_orig);
327                    var   dest = new MonitorNamePathDir(
328                                 of.get_basename(), 
329                                 of.get_path(),  
330                                 Path.get_dirname(of.get_path())
331                     );
332                     
333                                         if (dest == null) {
334                                                 
335                                                 return;
336                                         }
337                     src.action = "moved";
338                     dest.action = "moved";
339                     this.onMoved(src,dest);
340                     return; 
341                     
342  
343                 default:
344                     stdout.printf("event type not handled %u", event_type);
345                     break;
346                 // rest are mount related - not really relivant.. maybe add later..
347             } 
348         } catch(Error e) {
349             print(e.message);
350         }
351         
352     }
353     
354
355     /** override these to do stuff.. */
356     //public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
357     public virtual void  onChanged(MonitorNamePathDir src) { }
358     public virtual void onChangesDoneHint(MonitorNamePathDir src) { }
359     public virtual void onDeleted(MonitorNamePathDir src) { }
360     public virtual void onCreated(MonitorNamePathDir src) { }
361     public virtual void onAttributeChanged(MonitorNamePathDir src) { }
362     public virtual void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }
363           
364     
365 }
366  
367  
368
369
370
371
372