yaffs some more updates to yaffs_importer.py
[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(fill=tk.BOTH)
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 and False ==True : ##this alows the symlink to be edited and is no longer used. to renable it delete "and False ==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(fill=tk.BOTH)
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"]+"  "+current_directory_dict[x]["extra_data"]))
97     name_list_box.grid(column=0, row=1)
98     return current_directory_dict
99     
100 def remount_yaffs():
101     ##this isn't working. somethihg need to be changed in the config of the simulator to release the handle of the emfile
102     print "remounting yaffs"
103     print"unmounting yaffs:", yaffs_unmount("yaffs2/")
104     print "mounting yaffs", yaffs_mount("/yaffs2/")
105     load_dir()
106     
107 def load_file(link=0):
108     global open_windows_list
109     open_windows_list.append(editor(link))
110
111 def load_command(self=0):
112     global current_directory_dictls
113     
114     print "you loaded a file/dir/link"
115     x=name_list_box.curselection()
116     x=int(x[0])
117     print "cursor selection", x
118     print "dict", current_directory_dict
119
120     print "file inode is:", current_directory_dict[x]["inodes"]
121     
122     
123     print "file path is:", current_directory_dict[x]["path"]
124     if current_directory_dict[x]["type"]=="dir":
125         ##open dir
126         print "open directory"
127         mount_list_text_variable.set(current_directory_dict[x]["path"])
128         print mount_list_text_variable.get()
129         print "old directory dict", current_directory_dict
130         #current_directory_dict=load_dir()
131         load_dir()
132
133         print "new directory dict passed back"
134
135     elif current_directory_dict[x]["type"]=="file" :
136         ##open file
137         print "open file"
138         load_file()
139     elif current_directory_dict[x]["type"]=="link": 
140         print "loading a symlink"
141         load_file(1)
142     ##mount_list_text_variable.set(mount_list_text_variable.get()+str(list[0][0]))
143     
144 def back_a_directory(self=0):
145     x=len(mount_list_text_variable.get())
146     string=mount_list_text_variable.get()
147     slashes_id=[]
148     #print "length of path", x 
149     #print "string been sorted:",  string
150     for i in range(0, x):
151         if string[i]=='/':
152             slashes_id.append(i)
153             #print "slash found at:", i
154         
155     print slashes_id
156     ##slashes_id.sort() not needed because the list is already in acending order
157     ##print "sorted",slashes_id
158
159     string=string[0: slashes_id[len(slashes_id)-2]+1]
160     print string
161     mount_list_text_variable.set(string)
162     load_dir()
163
164
165     
166     
167     
168  
169 def yaffs_ls(dname):
170     ls_dict=[]
171
172     if dname[-1] != "/": dname = dname + "/"
173     dc = yaffs_opendir(dname)
174     if dc != 0 :
175         sep = yaffs_readdir(dc)
176         while bool(sep):
177             
178             se = sep.contents
179             fullname = dname + se.d_name
180             st = yaffs_stat_struct()
181             result = yaffs_lstat(fullname,byref(st))
182             perms = st.st_mode & 0777
183             ftype = st.st_mode & yaffs_S_IFMT
184             isFile = True if ftype == yaffs_S_IFREG else False
185             isDir  = True if ftype == yaffs_S_IFDIR else False
186             isSymlink= True if ftype == yaffs_S_IFLNK else False
187
188             if isFile :
189                 ls_dict.append ({"type" :"file",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname,"extra_data":""})
190                 print "file st.st_mode:", st.st_mode
191
192             elif isDir :
193                 print "dir st.st_mode:", st.st_mode
194
195                 ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":str( hex(perms)),"size":"0",   "path": fullname+"/", "extra_data":""})
196             elif isSymlink:
197                 print "symlink st.st_mode:", st.st_mode
198                 file_contents=ctypes.create_string_buffer(30)
199                 yaffs_readlink(fullname,file_contents,30)
200                 string=repr(file_contents.value)
201                 print "len of str", len(string)
202 #                string.lstrip()
203
204                 print "string", string, "###"
205
206                 ls_dict.append ({"type" :"link",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname, "extra_data":"> "+string})
207
208             else :
209                 print "unknown st.st_mode:", st.st_mode
210                 ls_dict.append({ "type":"Other", "inodes":str(se.d_ino),  "permissions":str( hex(perms)), "size":"0",   "path": fullname,"extra_data":""})
211             sep = yaffs_readdir(dc)
212         yaffs_closedir(dc)
213         return ls_dict
214     else:
215         print "Could not open directory"
216         return -1
217
218
219 ##toolbar 
220 toolbar_frame=tk.Frame(root_window)
221 button_open=tk.Button(toolbar_frame, command=load_command, text="load")
222 button_open.grid(column=0, row=0)
223 button_back=tk.Button(toolbar_frame, command=back_a_directory, text="back")
224 button_back.grid(column=1, row=0)
225 toolbar_frame.grid(row=0, column=0,  columnspan=3)
226
227 def delete_selected(selected_dir=0):
228     if selected_dir==0:
229         print"using current_directory_dict"
230         global current_directory_dict
231         x=name_list_box.curselection()
232         x=int(x[0])
233         print current_directory_dict[x]["type"]
234         if current_directory_dict[x]["type"]=="file":
235             path=current_directory_dict[x]["path"]
236             path =path
237             output=yaffs_unlink(path)
238             print "unlinking output:", output
239         elif current_directory_dict[x]["type"]=="dir":
240             path=current_directory_dict[x]["path"]
241             inside_dir=yaffs_ls(path)
242             print "files and folder inside dir", inside_dir
243             print "len of dir", len(inside_dir)
244             if inside_dir!=[]: ##if the dir is not empty
245                 ## remove stuff in dir
246                 for i in range(0,len(inside_dir)):
247                     print "calling self*****"
248                     delete_selected(inside_dir[i])
249                 
250             path =path[0:len(path)-1] ##this is to remove the "/" off the end of the of the file 
251             print "removing:", path
252             output=yaffs_rmdir(path)
253             print "rmdir output:", output
254     else :
255         print "using passed dir"
256         print "dir passed", selected_dir
257         current_directory_dict =selected_dir
258
259         print "after copying", current_directory_dict
260         print current_directory_dict["type"]
261         if current_directory_dict["type"]=="file":
262             path=current_directory_dict["path"]
263             path =path
264             output=yaffs_unlink(path)
265             print "unlinking output:", output
266         elif current_directory_dict["type"]=="dir":
267             path=current_directory_dict["path"]
268             inside_dir=yaffs_ls(path)
269             print "files and folder inside dir", inside_dir
270             print "len of dir", len(inside_dir)
271             if inside_dir!=[]: ##if the dir is not empty
272                 ## remove stuff in dir
273                 for i in range(0,len(inside_dir)):
274                     print "calling self*****"
275                     delete_selected(inside_dir[i])
276                 
277             path =path[0:len(path)-1] ##this is to remove the "/" off the end of the of the file 
278             print "removing:", path
279             output=yaffs_rmdir(path)
280             print "rmdir output:", output
281             
282     load_dir()
283
284
285
286 class new_file():
287     path_entry_box=0
288     new_file_window=0
289     def open_the_file(self):
290         global mount_list_text_variable
291         print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
292         yaffs_handle=yaffs_open(self.path_entry_box.get(),66,0666)
293         yaffs_close(yaffs_handle)
294         self.new_file_window.destroy()
295         load_dir()
296
297     def cancel(self):
298         ##del self
299         self.new_file_window.destroy()
300     def __init__(self):
301         global mount_list_text_variable
302         self.new_file_window =tk.Toplevel(takefocus=True)
303         path_frame=tk.Frame(self.new_file_window)
304         path_label=tk.Label(path_frame, text="file path")
305         path_label.pack(side=tk.LEFT)
306         text=tk.StringVar()
307         text.set(mount_list_text_variable.get())
308         print "############################",mount_list_text_variable.get()
309         self.path_entry_box= tk.Entry(path_frame, textvariable=text)
310         self.path_entry_box.pack(side=tk.RIGHT)
311         path_frame.pack()
312         button_frame=tk.Frame(self.new_file_window)
313         create_button=tk.Button(button_frame, text="Create", command=self.open_the_file)
314         create_button.pack(side=tk.LEFT)
315         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
316         cancel_button.pack(side=tk.RIGHT)
317         button_frame.pack()
318
319 class new_folder():
320     path_entry_box=0
321     new_folder_window=0
322     def create_the_folder(self):
323         global mount_list_text_variable
324         print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
325         yaffs_mkdir(self.path_entry_box.get(),0666)
326         self.new_folder_window.destroy()
327
328         load_dir()
329
330     def cancel(self):
331         ##del self
332         self.new_folder_window.destroy()
333     def __init__(self):
334         global mount_list_text_variable
335         self.new_folder_window =tk.Toplevel(takefocus=True)
336         path_frame=tk.Frame(self.new_folder_window)
337         path_label=tk.Label(path_frame, text="directory path")
338         path_label.pack(side=tk.LEFT)
339         text=tk.StringVar()
340         text.set(mount_list_text_variable.get())
341         print "############################",mount_list_text_variable.get()
342         self.path_entry_box= tk.Entry(path_frame, textvariable=text)
343         self.path_entry_box.pack(side=tk.RIGHT)
344         path_frame.pack()
345         button_frame=tk.Frame(self.new_folder_window)
346         create_button=tk.Button(button_frame, text="Create", command=self.create_the_folder)
347         create_button.pack(side=tk.LEFT)
348         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
349         cancel_button.pack(side=tk.RIGHT)
350         button_frame.pack()
351
352
353 class new_symlink():
354     path_entry_box=0
355     target_text=0
356     new_text=0
357     new_file_window=0
358     def create_the_symlink(self):
359         global mount_list_text_variable
360         ##check the symlink's target is a file.
361         target_path=self.target_text.get()
362         new_path=self.new_text.get()
363         print "creating a symlink \n target:", target_path
364         target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
365         if target_file_exists>=0:
366             ##file exists,create symlink
367             print "target file exist, creating symlink"
368             yaffs_symlink(target_path, new_path)
369             self.new_file_window.destroy()
370         else :
371             ##file does not exist
372             print "target file does not exist, cannot create symlink"
373         load_dir()
374
375     def cancel(self):
376         ##del self
377         self.new_file_window.destroy()
378     def __init__(self):
379         global mount_list_text_variable
380         self.new_file_window =tk.Toplevel(takefocus=True)
381         target_frame=tk.Frame(self.new_file_window)
382         target_label=tk.Label(target_frame, text="target")
383         target_label.pack(side=tk.LEFT)
384         self.target_text=tk.StringVar()
385         self.target_text.set(mount_list_text_variable.get())
386         #print "############################",mount_list_text_variable.get()
387         self.target_entry_box= tk.Entry(target_frame, textvariable=self.target_text)
388         self.target_entry_box.pack(side=tk.RIGHT)
389         target_frame.pack()
390         
391         new_frame=tk.Frame(self.new_file_window)
392         new_label=tk.Label(new_frame, text="file path")
393         new_label.pack(side=tk.LEFT)
394         self.new_text=tk.StringVar()
395         self.new_text.set(mount_list_text_variable.get())
396         #print "############################",mount_list_text_variable.get()
397         self.new_entry_box= tk.Entry(new_frame, textvariable=self.new_text)
398         self.new_entry_box.pack(side=tk.RIGHT)
399         new_frame.pack()
400         
401         button_frame=tk.Frame(self.new_file_window)
402         create_button=tk.Button(button_frame, text="Create", command=self.create_the_symlink)
403         create_button.pack(side=tk.LEFT)
404         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
405         cancel_button.pack(side=tk.RIGHT)
406         button_frame.pack()
407
408
409
410 class new_hardlink():
411     path_entry_box=0
412     target_text=0
413     new_text=0
414     new_file_window=0
415     def create_the_hardlink(self):
416         global mount_list_text_variable
417         ##check the symlink's target is a file.
418         target_path=self.target_text.get()
419         new_path=self.new_text.get()
420         print "creating a hardlink \n target:", target_path
421         target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
422         if target_file_exists>=0:
423             ##file exists,create symlink
424             print "target file exist, creating hardlink"
425             yaffs_link(target_path, new_path)
426             self.new_file_window.destroy()
427         else :
428             ##file does not exist
429             print "target file does not exist, cannot create hardlink"
430         load_dir()
431
432     def cancel(self):
433         ##del self
434         self.new_file_window.destroy()
435     def __init__(self):
436         global mount_list_text_variable
437         self.new_file_window =tk.Toplevel(takefocus=True)
438         target_frame=tk.Frame(self.new_file_window)
439         target_label=tk.Label(target_frame, text="target")
440         target_label.pack(side=tk.LEFT)
441         self.target_text=tk.StringVar()
442         self.target_text.set(mount_list_text_variable.get())
443         #print "############################",mount_list_text_variable.get()
444         self.target_entry_box= tk.Entry(target_frame, textvariable=self.target_text)
445         self.target_entry_box.pack(side=tk.RIGHT)
446         target_frame.pack()
447         
448         new_frame=tk.Frame(self.new_file_window)
449         new_label=tk.Label(new_frame, text="file path")
450         new_label.pack(side=tk.LEFT)
451         self.new_text=tk.StringVar()
452         self.new_text.set(mount_list_text_variable.get())
453         #print "############################",mount_list_text_variable.get()
454         self.new_entry_box= tk.Entry(new_frame, textvariable=self.new_text)
455         self.new_entry_box.pack(side=tk.RIGHT)
456         new_frame.pack()
457         
458         button_frame=tk.Frame(self.new_file_window)
459         create_button=tk.Button(button_frame, text="Create", command=self.create_the_hardlink)
460         create_button.pack(side=tk.LEFT)
461         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
462         cancel_button.pack(side=tk.RIGHT)
463         button_frame.pack()
464
465
466 ##mount list entry box init
467 mount_list_frame=tk.Frame(root_window)
468 mount_list_label=tk.Label(mount_list_frame, text="mount list")
469 mount_list_label.pack(side=tk.RIGHT)
470
471 mount_list_entry_box= tk.Entry(mount_list_frame,textvariable=mount_list_text_variable)
472 mount_list_entry_box.pack(side=tk.RIGHT)
473 mount_list_frame.grid(row=1, column=0, columnspan=2)
474
475
476 list_frame=tk.Frame(root_window)
477 name_list_box=tk.Listbox(list_frame,exportselection=0, height=30, width=80)
478 load_dir()
479 list_frame.grid(sticky=tk.W+tk.E+tk.N+tk.S)
480
481 name_list_box.bind("<Double-Button-1>", load_command)
482
483 browser_menu_bar=tk.Menu(root_window)
484 browser_file_menu=tk.Menu(browser_menu_bar)
485
486 browser_file_menu.add_command(label="Reload", command=load_dir)
487 browser_file_menu.add_command(label="Remount yaffs", command=remount_yaffs)
488
489 #browser_file_menu.add_command(label="Open")
490 #browser_file_menu.add_command(label="Save")
491 browser_menu_bar.add_cascade(label="File", menu=browser_file_menu)
492 root_window.config(menu=browser_menu_bar)
493
494
495 browser_edit_menu=tk.Menu(browser_menu_bar)
496
497 browser_edit_menu.add_command(label="New File", command=new_file)
498 browser_edit_menu.add_command(label="New Folder", command=new_folder)
499 browser_edit_menu.add_command(label="New Symlink", command=new_symlink)
500 browser_edit_menu.add_command(label="New Hardlink", command=new_hardlink)
501
502
503 browser_edit_menu.add_command(label="delete selected", command=delete_selected)
504 browser_menu_bar.add_cascade(label="Edit", menu=browser_edit_menu)
505
506
507
508
509
510
511 root_window.mainloop()
512
513 print"unmounting yaffs:", yaffs_unmount("yaffs2/")
514
515