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