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