e3f58f46cc2539aefbad976a527d2b2fb5aec9ab
[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_stat(fullname,byref(st))
171             perms = st.st_mode & 0777
172             isFile = True if st.st_mode & 0x8000 else False
173             isDir  = True if st.st_mode & 0x4000 else False
174             isSymlink=False if st.st_mode & 0x120000 else True
175
176             if isFile :
177                 ls_dict.append ({"type" :"file",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname})
178                 print "file st.st_mode:", st.st_mode
179                 print "st.st_mode andded with st.st_mode & 0x8000", st.st_mode & 0x8000
180             elif isDir :
181                 print "dir st.st_mode:", st.st_mode
182                 print "st.st_mode andded with st.st_mode & 0x4000", st.st_mode & 0x4000
183                 ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":str( hex(perms)),"size":"0",   "path": fullname+"/"})
184             elif isSymlink:
185
186                 ls_dict.append ({"type" :"link",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname})
187
188             else :
189                 print "unknown st.st_mode:", st.st_mode
190                 print "st.st_mode andded with st.st_mode & 0x120000", st.st_mode & 0x120000
191                 ls_dict.append({ "type":"Other", "inodes":str(se.d_ino),  "permissions":str( hex(perms)), "size":"0",   "path": fullname})
192             sep = yaffs_readdir(dc)
193         yaffs_closedir(dc)
194         return ls_dict
195     else:
196         print "Could not open directory"
197         return -1
198
199
200 ##toolbar 
201 toolbar_frame=tk.Frame(root_window)
202 button_open=tk.Button(toolbar_frame, command=load_command, text="load")
203 button_open.grid(column=0, row=0)
204 button_back=tk.Button(toolbar_frame, command=back_a_directory, text="back")
205 button_back.grid(column=1, row=0)
206 toolbar_frame.grid(row=0, column=0,  columnspan=3)
207
208 def delete_selected(selected_dir=0):
209     if selected_dir==0:
210         print"using current_directory_dict"
211         global current_directory_dict
212         x=name_list_box.curselection()
213         x=int(x[0])
214         print current_directory_dict[x]["type"]
215         if current_directory_dict[x]["type"]=="file":
216             path=current_directory_dict[x]["path"]
217             path =path
218             output=yaffs_unlink(path)
219             print "unlinking output:", output
220         elif current_directory_dict[x]["type"]=="dir":
221             path=current_directory_dict[x]["path"]
222             inside_dir=yaffs_ls(path)
223             print "files and folder inside dir", inside_dir
224             print "len of dir", len(inside_dir)
225             if inside_dir!=[]: ##if the dir is not empty
226                 ## remove stuff in dir
227                 for i in range(0,len(inside_dir)):
228                     print "calling self*****"
229                     delete_selected(inside_dir[i])
230                 
231             path =path[0:len(path)-1] ##this is to remove the "/" off the end of the of the file 
232             print "removing:", path
233             output=yaffs_rmdir(path)
234             print "rmdir output:", output
235     else :
236         print "using passed dir"
237         print "dir passed", selected_dir
238         current_directory_dict =selected_dir
239
240         print "after copying", current_directory_dict
241         print current_directory_dict["type"]
242         if current_directory_dict["type"]=="file":
243             path=current_directory_dict["path"]
244             path =path
245             output=yaffs_unlink(path)
246             print "unlinking output:", output
247         elif current_directory_dict["type"]=="dir":
248             path=current_directory_dict["path"]
249             inside_dir=yaffs_ls(path)
250             print "files and folder inside dir", inside_dir
251             print "len of dir", len(inside_dir)
252             if inside_dir!=[]: ##if the dir is not empty
253                 ## remove stuff in dir
254                 for i in range(0,len(inside_dir)):
255                     print "calling self*****"
256                     delete_selected(inside_dir[i])
257                 
258             path =path[0:len(path)-1] ##this is to remove the "/" off the end of the of the file 
259             print "removing:", path
260             output=yaffs_rmdir(path)
261             print "rmdir output:", output
262             
263     
264     
265     load_dir()
266
267
268
269 class new_file():
270     path_entry_box=0
271     new_file_window=0
272     def open_the_file(self):
273         global mount_list_text_variable
274         print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
275         yaffs_handle=yaffs_open(self.path_entry_box.get(),66,0666)
276         yaffs_close(yaffs_handle)
277         self.new_file_window.destroy()
278         load_dir()
279
280     def cancel(self):
281         ##del self
282         self.new_file_window.destroy()
283     def __init__(self):
284         global mount_list_text_variable
285         self.new_file_window =tk.Toplevel(takefocus=True)
286         path_frame=tk.Frame(self.new_file_window)
287         path_label=tk.Label(path_frame, text="file path")
288         path_label.pack(side=tk.LEFT)
289         text=tk.StringVar()
290         text.set(mount_list_text_variable.get())
291         print "############################",mount_list_text_variable.get()
292         self.path_entry_box= tk.Entry(path_frame, textvariable=text)
293         self.path_entry_box.pack(side=tk.RIGHT)
294         path_frame.pack()
295         button_frame=tk.Frame(self.new_file_window)
296         create_button=tk.Button(button_frame, text="Create", command=self.open_the_file)
297         create_button.pack(side=tk.LEFT)
298         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
299         cancel_button.pack(side=tk.RIGHT)
300         button_frame.pack()
301
302 class new_folder():
303     path_entry_box=0
304     new_folder_window=0
305     def create_the_folder(self):
306         global mount_list_text_variable
307         print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
308         yaffs_mkdir(self.path_entry_box.get(),0666)
309         self.new_folder_window.destroy()
310
311         load_dir()
312
313     def cancel(self):
314         ##del self
315         self.new_folder_window.destroy()
316     def __init__(self):
317         global mount_list_text_variable
318         self.new_folder_window =tk.Toplevel(takefocus=True)
319         path_frame=tk.Frame(self.new_folder_window)
320         path_label=tk.Label(path_frame, text="directory path")
321         path_label.pack(side=tk.LEFT)
322         text=tk.StringVar()
323         text.set(mount_list_text_variable.get())
324         print "############################",mount_list_text_variable.get()
325         self.path_entry_box= tk.Entry(path_frame, textvariable=text)
326         self.path_entry_box.pack(side=tk.RIGHT)
327         path_frame.pack()
328         button_frame=tk.Frame(self.new_folder_window)
329         create_button=tk.Button(button_frame, text="Create", command=self.create_the_folder)
330         create_button.pack(side=tk.LEFT)
331         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
332         cancel_button.pack(side=tk.RIGHT)
333         button_frame.pack()
334
335
336 class new_symlink():
337     path_entry_box=0
338     target_text=0
339     new_text=0
340     new_file_window=0
341     def create_the_symlink(self):
342         global mount_list_text_variable
343         ##check the symlink's target is a file.
344         target_path=self.target_text.get()
345         new_path=self.new_text.get()
346         print "creating a symlink \n target:", target_path
347         target_file_exists=yaffs_access(target_path, 0) ##yaffs_access will return 0 on success and -1 on failure.
348         if target_file_exists>=0:
349             ##file exists,create symlink
350             print "target file exist, creating symlink"
351             yaffs_symlink(target_path, new_path)
352             self.new_file_window.destroy()
353         else :
354             ##file does not exist
355             print "target file does not exist, cannot create symlink"
356         
357         
358         
359         load_dir()
360
361     def cancel(self):
362         ##del self
363         self.new_file_window.destroy()
364     def __init__(self):
365         global mount_list_text_variable
366         self.new_file_window =tk.Toplevel(takefocus=True)
367         target_frame=tk.Frame(self.new_file_window)
368         target_label=tk.Label(target_frame, text="target")
369         target_label.pack(side=tk.LEFT)
370         self.target_text=tk.StringVar()
371         self.target_text.set(mount_list_text_variable.get())
372         #print "############################",mount_list_text_variable.get()
373         self.target_entry_box= tk.Entry(target_frame, textvariable=self.target_text)
374         self.target_entry_box.pack(side=tk.RIGHT)
375         target_frame.pack()
376         
377         new_frame=tk.Frame(self.new_file_window)
378         new_label=tk.Label(new_frame, text="file path")
379         new_label.pack(side=tk.LEFT)
380         self.new_text=tk.StringVar()
381         self.new_text.set(mount_list_text_variable.get())
382         #print "############################",mount_list_text_variable.get()
383         self.new_entry_box= tk.Entry(new_frame, textvariable=self.new_text)
384         self.new_entry_box.pack(side=tk.RIGHT)
385         new_frame.pack()
386         
387         button_frame=tk.Frame(self.new_file_window)
388         create_button=tk.Button(button_frame, text="Create", command=self.create_the_symlink)
389         create_button.pack(side=tk.LEFT)
390         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
391         cancel_button.pack(side=tk.RIGHT)
392         button_frame.pack()
393
394
395 ##mount list entry box init
396 mount_list_frame=tk.Frame(root_window)
397 mount_list_label=tk.Label(mount_list_frame, text="mount list")
398 mount_list_label.pack(side=tk.RIGHT)
399
400 mount_list_entry_box= tk.Entry(mount_list_frame,textvariable=mount_list_text_variable)
401 mount_list_entry_box.pack(side=tk.RIGHT)
402 mount_list_frame.grid(row=1, column=0, columnspan=2)
403
404
405 list_frame=tk.Frame(root_window)
406 name_list_box=tk.Listbox(list_frame,exportselection=0, height=30, width=50)
407 load_dir()
408 list_frame.grid()
409
410 name_list_box.bind("<Double-Button-1>", load_command)
411
412 browser_menu_bar=tk.Menu(root_window)
413 browser_file_menu=tk.Menu(browser_menu_bar)
414
415 browser_file_menu.add_command(label="Reload", command=load_dir)
416 #browser_file_menu.add_command(label="Open")
417 #browser_file_menu.add_command(label="Save")
418 browser_menu_bar.add_cascade(label="File", menu=browser_file_menu)
419 root_window.config(menu=browser_menu_bar)
420
421
422 browser_edit_menu=tk.Menu(browser_menu_bar)
423
424 browser_edit_menu.add_command(label="New File", command=new_file)
425 browser_edit_menu.add_command(label="New Folder", command=new_folder)
426 browser_edit_menu.add_command(label="New Symlink", command=new_symlink)
427
428 browser_edit_menu.add_command(label="delete selected", command=delete_selected)
429 browser_menu_bar.add_cascade(label="Edit", menu=browser_edit_menu)
430
431
432
433
434
435
436 root_window.mainloop()
437
438 print"unmounting yaffs:", yaffs_unmount("yaffs2/")
439
440