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