3 ## YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
5 # Copyright (C) 2002-2018 Aleph One Ltd.
7 ## Created by Timothy Manning <timothy@yaffs.net>
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.
18 from operator import itemgetter
21 yaffs_mount("/yaffs2/")
23 root_window.title("YAFFS Browser")
24 mount_list_text_variable=tk.StringVar()
26 mount_list_text_variable.set("/yaffs2/")
27 current_directory_dict={}
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()
53 print "target file does not exist, cannot create symlink"
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)
70 def __init__(self, isLink=0):
71 global current_directory_dict
72 x=name_list_box.curselection()
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)
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)
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"
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
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'
117 permissions_string ='-'
118 if current_directory_dict[x]["permissions"] & yaffs_S_IWRITE:
119 permissions_string+='w'
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
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/")
133 def load_file(link=0):
134 global open_windows_list
135 open_windows_list.append(editor(link))
137 def load_command(self=0):
138 global current_directory_dictls
140 print "you loaded a file/dir/link"
141 x=name_list_box.curselection()
143 print "cursor selection", x
144 print "dict", current_directory_dict
146 print "file inode is:", current_directory_dict[x]["inodes"]
149 print "file path is:", current_directory_dict[x]["path"]
150 if current_directory_dict[x]["type"]=="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()
159 print "new directory dict passed back"
161 elif current_directory_dict[x]["type"]=="file" :
165 elif current_directory_dict[x]["type"]=="link":
166 print "loading a symlink"
168 ##mount_list_text_variable.set(mount_list_text_variable.get()+str(list[0][0]))
170 def back_a_directory(self=0):
171 x=len(mount_list_text_variable.get())
172 string=mount_list_text_variable.get()
174 #print "length of path", x
175 #print "string been sorted:", string
176 for i in range(0, x):
179 #print "slash found at:", i
182 ##slashes_id.sort() not needed because the list is already in acending order
183 ##print "sorted",slashes_id
185 string=string[0: slashes_id[len(slashes_id)-2]+1]
187 mount_list_text_variable.set(string)
198 if dname[-1] != "/": dname = dname + "/"
199 dc = yaffs_opendir(dname)
201 sep = yaffs_readdir(dc)
205 fullname = dname + se.d_name
206 st = yaffs_stat_struct()
207 result = yaffs_lstat(fullname,byref(st))
208 #perms = st.st_mode & 0777
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
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
220 print "dir st.st_mode:", st.st_mode
222 ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":perms,"size":"0", "path": fullname+"/", "extra_data":""})
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)
231 print "string", string, "###"
233 ls_dict.append ({"type" :"link", "inodes" : str(se.d_ino), "permissions" : perms, "size": str(st.st_size), "path": fullname, "extra_data":"> "+string})
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)
240 return sorted(ls_dict,key=itemgetter("path"))
242 print "Could not open directory"
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)
254 def delete_selected(selected_dir=0):
256 print"using current_directory_dict"
257 global current_directory_dict
258 x=name_list_box.curselection()
260 print current_directory_dict[x]["type"]
261 if current_directory_dict[x]["type"]=="file":
262 path=current_directory_dict[x]["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])
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
282 print "using passed dir"
283 print "dir passed", selected_dir
284 current_directory_dict =selected_dir
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"]
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])
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
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()
326 self.new_file_window.destroy()
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)
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)
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)
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()
359 self.new_folder_window.destroy()
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)
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)
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)
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()
398 ##file does not exist
399 print "target file does not exist, cannot create symlink"
404 self.new_file_window.destroy()
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)
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)
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)
437 class new_hardlink():
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()
455 ##file does not exist
456 print "target file does not exist, cannot create hardlink"
461 self.new_file_window.destroy()
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)
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)
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)
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)
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)
503 list_frame=tk.Frame(root_window)
504 name_list_box=tk.Listbox(list_frame,exportselection=0, height=30, width=80)
506 list_frame.grid(sticky=tk.W+tk.E+tk.N+tk.S)
508 name_list_box.bind("<Double-Button-1>", load_command)
510 browser_menu_bar=tk.Menu(root_window)
511 browser_file_menu=tk.Menu(browser_menu_bar)
513 browser_file_menu.add_command(label="Reload", command=load_dir)
514 browser_file_menu.add_command(label="Remount yaffs", command=remount_yaffs)
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)
522 browser_edit_menu=tk.Menu(browser_menu_bar)
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)
530 browser_edit_menu.add_command(label="delete selected", command=delete_selected)
531 browser_menu_bar.add_cascade(label="Edit", menu=browser_edit_menu)
538 root_window.mainloop()
540 print"unmounting yaffs:", yaffs_unmount("yaffs2/")