Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
[yaffs2.git] / direct / python / examples.py
1 ##
2 ## YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 ##
4 ## Copyright (C) 2002-2010 Aleph One Ltd.
5 ##   for Toby Churchill Ltd and Brightstar Engineering
6 ##
7 ## Created by Charles Manning <charles@aleph1.co.uk>
8 ##
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.
12 ##
13
14
15 from yaffsfs import *
16
17 def yaffs_ls(dname):
18     if dname[-1] != "/": dname = dname + "/"
19     dc = yaffs_opendir(dname)
20     if dc != 0 :
21         sep = yaffs_readdir(dc)
22         while bool(sep):
23             se = sep.contents
24             fullname = dname + se.d_name
25             #print fullname, " ", se.d_ino," ",ord(se.d_type)
26             st = yaffs_stat_struct()
27             result = yaffs_stat(fullname,byref(st))
28             perms = st.st_mode & 0777
29             isFile = True if st.st_mode & 0x8000 else False
30             isDir  = True if st.st_mode & 0x4000 else False
31
32             if isFile :
33                 print "File ",se.d_ino, hex(perms), st.st_size, fullname
34             elif isDir :
35                 print "Dir  ",se.d_ino, hex(perms), fullname
36                 yaffs_ls(fullname)
37             else :
38                 print "Other (",hex(st.st_mode),") ",se.d_ino, hex(perms), fullname
39
40             sep = yaffs_readdir(dc)
41         yaffs_closedir(dc)
42         return 0
43     else:
44         print "Could not open directory"
45         return -1
46
47 def yaffs_mkfile(fname,fsize):
48     fd = yaffs_open(fname,66, 0666)
49     if fd >= 0:
50         b = create_string_buffer("",1024)
51         totalwrite=0
52         while fsize > 0:
53             thiswrite = 1024 if fsize > 1024 else fsize
54             result = yaffs_write(fd,b,thiswrite)
55             totalwrite += result
56             fsize -= result
57             if result != thiswrite:
58                 fsize= 0
59
60         return totalwrite
61     else :
62         return -1
63
64 def yaffs_link_test(dir):
65     fnamea = dir + '/aaa'
66     fnameb = dir + '/bbb'
67     yaffs_unlink(fnamea)
68     fd = yaffs_open(fnamea,66,0666)
69     yaffs_link(fnamea,fnameb)
70     yaffs_ls(dir)
71     yaffs_unlink(fnamea)
72     yaffs_ls(dir)
73     yaffs_unlink(fnameb)
74
75 def yaffs_o_excl_test(dir):
76     fname = dir + '/aaa'
77     yaffs_unlink(fname)
78     fd = yaffs_open(fname, 66, 0666)
79     yaffs_close(fd)
80     print "Created ", fname, " result ", fd
81     fdx = yaffs_open(fname, 0301, 0666)
82     print "Attempt to create with O_EXCL existing file returned ", fdx
83     yaffs_unlink(fname)
84     fdx = yaffs_open(fname, 0301, 0666)
85     print "Attempt to create with O_EXCL non-existing file returned ", fdx
86
87     
88 root = "/yaffs2"
89
90 yaffs_start_up()
91 yaffs_mount(root)
92
93 yaffs_mkdir(root+"/dd",0666)
94
95 h = yaffs_open(root+"/dd/111",66,0666)
96 yaffs_close(h)
97
98 yaffs_ls(root)
99