yaffs Merge branch 'namechange'
[yaffs2.git] / direct / python / yaffs_browser.py
1 #!/usr/bin/python 
2 import Tkinter as tk
3 from yaffsfs import *
4 #import examples
5 import ctypes
6
7 yaffs_StartUp()
8 yaffs_mount("/yaffs2/")
9 root_window =tk.Tk()
10 root_window.title("YAFFS Browser")
11 mount_list_text_variable=tk.StringVar()
12
13 mount_list_text_variable.set("/yaffs2/")
14 current_directory_dict={}
15 open_windows_list=[]
16
17 class editor():
18     yaffs_handle=0
19     file_editor_root =0
20     save_button=[]
21     file_contents=0
22     file_path=0
23     isLink=0
24     def save_file(self):
25         if  self.isLink==True:
26             target_path=self.file_editor_text.get("1.0", tk.END) ##"1.0" is the index of the first line of text
27             target_path=target_path[0:len(target_path)-1]
28             new_path=self.file_path
29             print "creating a symlink \n target:##", target_path, "##"
30             target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
31             if target_file_exists>=0:
32                 ##file exists,create symlink
33                 print "target file exist, creating symlink"
34                 yaffs_unlink(new_path)
35                 output=yaffs_symlink(target_path, new_path)
36                 print "yaffs symlink output=", output
37                 self.file_editor_root.destroy()
38             else :
39                 ##file does not exist
40                 print "target file does not exist, cannot create symlink"
41         else :
42             #global current_directory_dict
43             print "saving the file"
44             print self.file_editor_text.get("1.0", tk.END) ##"1.0" is the index of the first line of text
45             yaffs_lseek(self.yaffs_handle, 0, 0)
46             data_to_be_written=self.file_editor_text.get("1.0", tk.END)
47             print "data to be saved", data_to_be_written
48             length_of_file=len(data_to_be_written)
49             print "length of data to be written",length_of_file 
50             output=yaffs_write(self.yaffs_handle,data_to_be_written , length_of_file)
51             print "output", output
52             yaffs_ftruncate(self.yaffs_handle, length_of_file)
53             yaffs_close(self.yaffs_handle)
54             self.yaffs_handle = yaffs_open(self.file_path,66,0666)
55         load_dir()
56         
57     def __init__(self, isLink=0):
58         global current_directory_dict
59         x=name_list_box.curselection()
60         self.id=int(x[0])
61         self.file_editor_root =tk.Toplevel()
62         self.save_button=tk.Button(self.file_editor_root, text="save", command=self.save_file)
63         self.save_button.pack()
64
65         self.file_path=current_directory_dict[self.id]["path"]
66         print "file path", self.file_path
67         self.file_editor_root.title(current_directory_dict[self.id]["path"])
68         self.file_editor_text=tk.Text(self.file_editor_root)
69         self.yaffs_handle = yaffs_open(current_directory_dict[self.id]["path"],66,0666)
70         length_of_file=yaffs_lseek(self.yaffs_handle, 0, 2) ##seeks to the end of the file
71         yaffs_lseek(self.yaffs_handle, 0, 0)## returns the handle to the front of th file
72         print "length of file to be opened:", length_of_file
73         if isLink==True:
74             print "opening symlink"
75             self.file_contents=ctypes.create_string_buffer(1000)
76             yaffs_readlink(self.file_path,self.file_contents,1000)
77             self.isLink=True
78         else:
79             print"opening file"
80             self.file_contents=ctypes.create_string_buffer(length_of_file)
81             yaffs_read(self.yaffs_handle,self.file_contents,length_of_file)
82             print "file contents", self.file_contents.raw
83         self.file_editor_text.insert(tk.END, self.file_contents.raw)
84         self.file_editor_text.pack()
85         ##self.file_editor_text.bind("<Control-s>", self.save_file)
86         ##doesn't work because it can't pass "self"
87
88 def load_dir():
89     global current_directory_dict
90     print "loading a new directory*******************************************************************"
91     name_list_box.delete(0, tk.END)
92     current_directory_dict=yaffs_ls(mount_list_text_variable.get())
93     print "new directory", current_directory_dict
94     ##copy directory into file box
95     for x in range(0,len(current_directory_dict)):
96         name_list_box.insert(x,(current_directory_dict[x]["inodes"]+"  "+ current_directory_dict[x]["type"]+"  "+ current_directory_dict[x]["size"]+"  "+ current_directory_dict[x]["path"]))
97     name_list_box.grid(column=0, row=1)
98     return current_directory_dict
99
100 def load_file(link=0):
101     global open_windows_list
102     open_windows_list.append(editor(link))
103
104 def load_command(self=0):
105     global current_directory_dict
106     print "you loaded a file/dir/link"
107     x=name_list_box.curselection()
108     x=int(x[0])
109     print "cursor selection", x
110     print "dict", current_directory_dict
111
112     print "file inode is:", current_directory_dict[x]["inodes"]
113     
114     
115     print "file path is:", current_directory_dict[x]["path"]
116     if current_directory_dict[x]["type"]=="dir":
117         ##open dir
118         print "open directory"
119         mount_list_text_variable.set(current_directory_dict[x]["path"])
120         print mount_list_text_variable.get()
121         print "old directory dict", current_directory_dict
122         #current_directory_dict=load_dir()
123         load_dir()
124
125         print "new directory dict passed back"
126
127     elif current_directory_dict[x]["type"]=="file" :
128         ##open file
129         print "open file"
130         load_file()
131     elif current_directory_dict[x]["type"]=="link": 
132         print "loading a symlink"
133         load_file(1)
134     ##mount_list_text_variable.set(mount_list_text_variable.get()+str(list[0][0]))
135     
136 def back_a_directory(self=0):
137     x=len(mount_list_text_variable.get())
138     string=mount_list_text_variable.get()
139     slashes_id=[]
140     #print "length of path", x 
141     #print "string been sorted:",  string
142     for i in range(0, x):
143         if string[i]=='/':
144             slashes_id.append(i)
145             #print "slash found at:", i
146         
147     print slashes_id
148     ##slashes_id.sort() not needed because the list is already in acending order
149     ##print "sorted",slashes_id
150
151     string=string[0: slashes_id[len(slashes_id)-2]+1]
152     print string
153     mount_list_text_variable.set(string)
154     load_dir()
155
156
157     
158     
159     
160  
161 def yaffs_ls(dname):
162     ls_dict=[]
163
164     if dname[-1] != "/": dname = dname + "/"
165     dc = yaffs_opendir(dname)
166     if dc != 0 :
167         sep = yaffs_readdir(dc)
168         while bool(sep):
169             
170             se = sep.contents
171             fullname = dname + se.d_name
172             st = yaffs_stat_struct()
173             result = yaffs_lstat(fullname,byref(st))
174             perms = st.st_mode & 0777
175             ftype = st.st_mode & yaffs_S_IFMT
176             isFile = True if ftype == yaffs_S_IFREG else False
177             isDir  = True if ftype == yaffs_S_IFDIR else False
178             isSymlink= True if ftype == yaffs_S_IFLNK else False
179
180             if isFile :
181                 ls_dict.append ({"type" :"file",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname})
182                 print "file st.st_mode:", st.st_mode
183
184             elif isDir :
185                 print "dir st.st_mode:", st.st_mode
186
187                 ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":str( hex(perms)),"size":"0",   "path": fullname+"/"})
188             elif isSymlink:
189                 print "symlink st.st_mode:", st.st_mode
190
191                 ls_dict.append ({"type" :"link",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname})
192
193             else :
194                 print "unknown st.st_mode:", st.st_mode
195                 ls_dict.append({ "type":"Other", "inodes":str(se.d_ino),  "permissions":str( hex(perms)), "size":"0",   "path": fullname})
196             sep = yaffs_readdir(dc)
197         yaffs_closedir(dc)
198         return ls_dict
199     else:
200         print "Could not open directory"
201         return -1
202
203
204 ##toolbar 
205 toolbar_frame=tk.Frame(root_window)
206 button_open=tk.Button(toolbar_frame, command=load_command, text="load")
207 button_open.grid(column=0, row=0)
208 button_back=tk.Button(toolbar_frame, command=back_a_directory, text="back")
209 button_back.grid(column=1, row=0)
210 toolbar_frame.grid(row=0, column=0,  columnspan=3)
211
212 def delete_selected(selected_dir=0):
213     if selected_dir==0:
214         print"using current_directory_dict"
215         global current_directory_dict
216         x=name_list_box.curselection()
217         x=int(x[0])
218         print current_directory_dict[x]["type"]
219         if current_directory_dict[x]["type"]=="file":
220             path=current_directory_dict[x]["path"]
221             path =path
222             output=yaffs_unlink(path)
223             print "unlinking output:", output
224         elif current_directory_dict[x]["type"]=="dir":
225             path=current_directory_dict[x]["path"]
226             inside_dir=yaffs_ls(path)
227             print "files and folder inside dir", inside_dir
228             print "len of dir", len(inside_dir)
229             if inside_dir!=[]: ##if the dir is not empty
230                 ## remove stuff in dir
231                 for i in range(0,len(inside_dir)):
232                     print "calling self*****"
233                     delete_selected(inside_dir[i])
234                 
235             path =path[0:len(path)-1] ##this is to remove the "/" off the end of the of the file 
236             print "removing:", path
237             output=yaffs_rmdir(path)
238             print "rmdir output:", output
239     else :
240         print "using passed dir"
241         print "dir passed", selected_dir
242         current_directory_dict =selected_dir
243
244         print "after copying", current_directory_dict
245         print current_directory_dict["type"]
246         if current_directory_dict["type"]=="file":
247             path=current_directory_dict["path"]
248             path =path
249             output=yaffs_unlink(path)
250             print "unlinking output:", output
251         elif current_directory_dict["type"]=="dir":
252             path=current_directory_dict["path"]
253             inside_dir=yaffs_ls(path)
254             print "files and folder inside dir", inside_dir
255             print "len of dir", len(inside_dir)
256             if inside_dir!=[]: ##if the dir is not empty
257                 ## remove stuff in dir
258                 for i in range(0,len(inside_dir)):
259                     print "calling self*****"
260                     delete_selected(inside_dir[i])
261                 
262             path =path[0:len(path)-1] ##this is to remove the "/" off the end of the of the file 
263             print "removing:", path
264             output=yaffs_rmdir(path)
265             print "rmdir output:", output
266             
267     
268     
269     load_dir()
270
271
272
273 class new_file():
274     path_entry_box=0
275     new_file_window=0
276     def open_the_file(self):
277         global mount_list_text_variable
278         print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
279         yaffs_handle=yaffs_open(self.path_entry_box.get(),66,0666)
280         yaffs_close(yaffs_handle)
281         self.new_file_window.destroy()
282         load_dir()
283
284     def cancel(self):
285         ##del self
286         self.new_file_window.destroy()
287     def __init__(self):
288         global mount_list_text_variable
289         self.new_file_window =tk.Toplevel(takefocus=True)
290         path_frame=tk.Frame(self.new_file_window)
291         path_label=tk.Label(path_frame, text="file path")
292         path_label.pack(side=tk.LEFT)
293         text=tk.StringVar()
294         text.set(mount_list_text_variable.get())
295         print "############################",mount_list_text_variable.get()
296         self.path_entry_box= tk.Entry(path_frame, textvariable=text)
297         self.path_entry_box.pack(side=tk.RIGHT)
298         path_frame.pack()
299         button_frame=tk.Frame(self.new_file_window)
300         create_button=tk.Button(button_frame, text="Create", command=self.open_the_file)
301         create_button.pack(side=tk.LEFT)
302         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
303         cancel_button.pack(side=tk.RIGHT)
304         button_frame.pack()
305
306 class new_folder():
307     path_entry_box=0
308     new_folder_window=0
309     def create_the_folder(self):
310         global mount_list_text_variable
311         print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
312         yaffs_mkdir(self.path_entry_box.get(),0666)
313         self.new_folder_window.destroy()
314
315         load_dir()
316
317     def cancel(self):
318         ##del self
319         self.new_folder_window.destroy()
320     def __init__(self):
321         global mount_list_text_variable
322         self.new_folder_window =tk.Toplevel(takefocus=True)
323         path_frame=tk.Frame(self.new_folder_window)
324         path_label=tk.Label(path_frame, text="directory path")
325         path_label.pack(side=tk.LEFT)
326         text=tk.StringVar()
327         text.set(mount_list_text_variable.get())
328         print "############################",mount_list_text_variable.get()
329         self.path_entry_box= tk.Entry(path_frame, textvariable=text)
330         self.path_entry_box.pack(side=tk.RIGHT)
331         path_frame.pack()
332         button_frame=tk.Frame(self.new_folder_window)
333         create_button=tk.Button(button_frame, text="Create", command=self.create_the_folder)
334         create_button.pack(side=tk.LEFT)
335         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
336         cancel_button.pack(side=tk.RIGHT)
337         button_frame.pack()
338
339
340 class new_symlink():
341     path_entry_box=0
342     target_text=0
343     new_text=0
344     new_file_window=0
345     def create_the_symlink(self):
346         global mount_list_text_variable
347         ##check the symlink's target is a file.
348         target_path=self.target_text.get()
349         new_path=self.new_text.get()
350         print "creating a symlink \n target:", target_path
351         target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
352         if target_file_exists>=0:
353             ##file exists,create symlink
354             print "target file exist, creating symlink"
355             yaffs_symlink(target_path, new_path)
356             self.new_file_window.destroy()
357         else :
358             ##file does not exist
359             print "target file does not exist, cannot create symlink"
360         load_dir()
361
362     def cancel(self):
363         ##del self
364         self.new_file_window.destroy()
365     def __init__(self):
366         global mount_list_text_variable
367         self.new_file_window =tk.Toplevel(takefocus=True)
368         target_frame=tk.Frame(self.new_file_window)
369         target_label=tk.Label(target_frame, text="target")
370         target_label.pack(side=tk.LEFT)
371         self.target_text=tk.StringVar()
372         self.target_text.set(mount_list_text_variable.get())
373         #print "############################",mount_list_text_variable.get()
374         self.target_entry_box= tk.Entry(target_frame, textvariable=self.target_text)
375         self.target_entry_box.pack(side=tk.RIGHT)
376         target_frame.pack()
377         
378         new_frame=tk.Frame(self.new_file_window)
379         new_label=tk.Label(new_frame, text="file path")
380         new_label.pack(side=tk.LEFT)
381         self.new_text=tk.StringVar()
382         self.new_text.set(mount_list_text_variable.get())
383         #print "############################",mount_list_text_variable.get()
384         self.new_entry_box= tk.Entry(new_frame, textvariable=self.new_text)
385         self.new_entry_box.pack(side=tk.RIGHT)
386         new_frame.pack()
387         
388         button_frame=tk.Frame(self.new_file_window)
389         create_button=tk.Button(button_frame, text="Create", command=self.create_the_symlink)
390         create_button.pack(side=tk.LEFT)
391         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
392         cancel_button.pack(side=tk.RIGHT)
393         button_frame.pack()
394
395
396
397 class new_hardlink():
398     path_entry_box=0
399     target_text=0
400     new_text=0
401     new_file_window=0
402     def create_the_hardlink(self):
403         global mount_list_text_variable
404         ##check the symlink's target is a file.
405         target_path=self.target_text.get()
406         new_path=self.new_text.get()
407         print "creating a hardlink \n target:", target_path
408         target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
409         if target_file_exists>=0:
410             ##file exists,create symlink
411             print "target file exist, creating hardlink"
412             yaffs_link(target_path, new_path)
413             self.new_file_window.destroy()
414         else :
415             ##file does not exist
416             print "target file does not exist, cannot create hardlink"
417         load_dir()
418
419     def cancel(self):
420         ##del self
421         self.new_file_window.destroy()
422     def __init__(self):
423         global mount_list_text_variable
424         self.new_file_window =tk.Toplevel(takefocus=True)
425         target_frame=tk.Frame(self.new_file_window)
426         target_label=tk.Label(target_frame, text="target")
427         target_label.pack(side=tk.LEFT)
428         self.target_text=tk.StringVar()
429         self.target_text.set(mount_list_text_variable.get())
430         #print "############################",mount_list_text_variable.get()
431         self.target_entry_box= tk.Entry(target_frame, textvariable=self.target_text)
432         self.target_entry_box.pack(side=tk.RIGHT)
433         target_frame.pack()
434         
435         new_frame=tk.Frame(self.new_file_window)
436         new_label=tk.Label(new_frame, text="file path")
437         new_label.pack(side=tk.LEFT)
438         self.new_text=tk.StringVar()
439         self.new_text.set(mount_list_text_variable.get())
440         #print "############################",mount_list_text_variable.get()
441         self.new_entry_box= tk.Entry(new_frame, textvariable=self.new_text)
442         self.new_entry_box.pack(side=tk.RIGHT)
443         new_frame.pack()
444         
445         button_frame=tk.Frame(self.new_file_window)
446         create_button=tk.Button(button_frame, text="Create", command=self.create_the_hardlink)
447         create_button.pack(side=tk.LEFT)
448         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
449         cancel_button.pack(side=tk.RIGHT)
450         button_frame.pack()
451
452
453 ##mount list entry box init
454 mount_list_frame=tk.Frame(root_window)
455 mount_list_label=tk.Label(mount_list_frame, text="mount list")
456 mount_list_label.pack(side=tk.RIGHT)
457
458 mount_list_entry_box= tk.Entry(mount_list_frame,textvariable=mount_list_text_variable)
459 mount_list_entry_box.pack(side=tk.RIGHT)
460 mount_list_frame.grid(row=1, column=0, columnspan=2)
461
462
463 list_frame=tk.Frame(root_window)
464 name_list_box=tk.Listbox(list_frame,exportselection=0, height=30, width=50)
465 load_dir()
466 list_frame.grid()
467
468 name_list_box.bind("<Double-Button-1>", load_command)
469
470 browser_menu_bar=tk.Menu(root_window)
471 browser_file_menu=tk.Menu(browser_menu_bar)
472
473 browser_file_menu.add_command(label="Reload", command=load_dir)
474 #browser_file_menu.add_command(label="Open")
475 #browser_file_menu.add_command(label="Save")
476 browser_menu_bar.add_cascade(label="File", menu=browser_file_menu)
477 root_window.config(menu=browser_menu_bar)
478
479
480 browser_edit_menu=tk.Menu(browser_menu_bar)
481
482 browser_edit_menu.add_command(label="New File", command=new_file)
483 browser_edit_menu.add_command(label="New Folder", command=new_folder)
484 browser_edit_menu.add_command(label="New Symlink", command=new_symlink)
485 browser_edit_menu.add_command(label="New Hardlink", command=new_hardlink)
486
487
488 browser_edit_menu.add_command(label="delete selected", command=delete_selected)
489 browser_menu_bar.add_cascade(label="Edit", menu=browser_edit_menu)
490
491
492
493
494
495
496 root_window.mainloop()
497
498 print"unmounting yaffs:", yaffs_unmount("yaffs2/")
499
500