yaffs First round of name changes
[yaffs2.git] / direct / python / examples.py
1 from yaffsfs import *
2
3 def yaffs_ls(dname):
4     if dname[-1] != "/": dname = dname + "/"
5     dc = yaffs_opendir(dname)
6     if dc != 0 :
7         sep = yaffs_readdir(dc)
8         while bool(sep):
9             se = sep.contents
10             fullname = dname + se.d_name
11             #print fullname, " ", se.d_ino," ",ord(se.d_type)
12             st = yaffs_stat_struct()
13             result = yaffs_stat(fullname,byref(st))
14             perms = st.st_mode & 0777
15             isFile = True if st.st_mode & 0x8000 else False
16             isDir  = True if st.st_mode & 0x4000 else False
17
18             if isFile :
19                 print "File ",se.d_ino, hex(perms), st.st_size, fullname
20             elif isDir :
21                 print "Dir  ",se.d_ino, hex(perms), fullname
22                 yaffs_ls(fullname)
23             else :
24                 print "Other (",hex(st.st_mode),") ",se.d_ino, hex(perms), fullname
25
26             sep = yaffs_readdir(dc)
27         yaffs_closedir(dc)
28         return 0
29     else:
30         print "Could not open directory"
31         return -1
32
33 def yaffs_mkfile(fname,fsize):
34     fd = yaffs_open(fname,66, 0666)
35     if fd >= 0:
36         b = create_string_buffer("",1024)
37         totalwrite=0
38         while fsize > 0:
39             thiswrite = 1024 if fsize > 1024 else fsize
40             result = yaffs_write(fd,b,thiswrite)
41             totalwrite += result
42             fsize -= result
43             if result != thiswrite:
44                 fsize= 0
45
46         return totalwrite
47     else :
48         return -1
49
50 def yaffs_link_test(dir):
51     fnamea = dir + '/aaa'
52     fnameb = dir + '/bbb'
53     yaffs_unlink(fnamea)
54     fd = yaffs_open(fnamea,66,0666)
55     yaffs_link(fnamea,fnameb)
56     yaffs_ls(dir)
57     yaffs_unlink(fnamea)
58     yaffs_ls(dir)
59     yaffs_unlink(fnameb)
60
61 def yaffs_o_excl_test(dir):
62     fname = dir + '/aaa'
63     yaffs_unlink(fname)
64     fd = yaffs_open(fname, 66, 0666)
65     yaffs_close(fd)
66     print "Created ", fname, " result ", fd
67     fdx = yaffs_open(fname, 0301, 0666)
68     print "Attempt to create with O_EXCL existing file returned ", fdx
69     yaffs_unlink(fname)
70     fdx = yaffs_open(fname, 0301, 0666)
71     print "Attempt to create with O_EXCL non-existing file returned ", fdx
72
73     
74 root = "/yaffs2"
75
76 yaffs_start_up()
77 yaffs_mount(root)
78
79 yaffs_mkdir(root+"/dd",0666)
80
81 h = yaffs_open(root+"/dd/111",66,0666)
82 yaffs_close(h)
83
84 yaffs_ls(root)
85