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