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