yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev()
[yaffs2.git] / direct / test-framework / python / examples.py
1 ##
2 ## YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 ##
4 # Copyright (C) 2002-2018 Aleph One Ltd.
5 ##
6 ## Created by Charles Manning <charles@aleph1.co.uk>
7 ##
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License version 2 as
10 ## published by the Free Software Foundation.
11 ##
12
13
14 from yaffsfs import *
15
16 def yaffs_ls(dname):
17     if dname[-1] != "/": dname = dname + "/"
18     dc = yaffs_opendir(dname)
19     if dc != 0 :
20         sep = yaffs_readdir(dc)
21         while bool(sep):
22             se = sep.contents
23             fullname = dname + se.d_name
24             #print fullname, " ", se.d_ino," ",ord(se.d_type)
25             st = yaffs_stat_struct()
26             result = yaffs_stat(fullname,byref(st))
27             perms = st.st_mode & 0777
28             isFile = True if st.st_mode & 0x8000 else False
29             isDir  = True if st.st_mode & 0x4000 else False
30
31             if isFile :
32                 print "File ",se.d_ino, hex(perms), st.st_size, fullname, " times ", st.yst_atime, st.yst_ctime, st.yst_mtime
33             elif isDir :
34                 print "Dir  ",se.d_ino, hex(perms), fullname, " times ", st.yst_atime, st.yst_ctime, st.yst_mtime
35                 yaffs_ls(fullname)
36             else :
37                 print "Other (",hex(st.st_mode),") ",se.d_ino, hex(perms), fullname, " times ", st.yst_atime, st.yst_ctime, st.yst_mtime
38
39             sep = yaffs_readdir(dc)
40         yaffs_closedir(dc)
41         return 0
42     else:
43         print "Could not open directory"
44         return -1
45
46 def yaffs_mkfile(fname,fsize):
47     fd = yaffs_open(fname,66, 0666)
48     if fd >= 0:
49         b = create_string_buffer("",1024)
50         totalwrite=0
51         while fsize > 0:
52             thiswrite = 1024 if fsize > 1024 else fsize
53             result = yaffs_write(fd,b,thiswrite)
54             totalwrite += result
55             fsize -= result
56             if result != thiswrite:
57                 fsize= 0
58
59         return totalwrite
60     else :
61         return -1
62
63 def yaffs_link_test(dir):
64     fnamea = dir + '/aaa'
65     fnameb = dir + '/bbb'
66     yaffs_unlink(fnamea)
67     fd = yaffs_open(fnamea,66,0666)
68     yaffs_link(fnamea,fnameb)
69     yaffs_ls(dir)
70     yaffs_unlink(fnamea)
71     yaffs_ls(dir)
72     yaffs_unlink(fnameb)
73
74 def yaffs_o_excl_test(dir):
75     fname = dir + '/aaa'
76     yaffs_unlink(fname)
77     fd = yaffs_open(fname, 66, 0666)
78     yaffs_close(fd)
79     print "Created ", fname, " result ", fd
80     fdx = yaffs_open(fname, 0301, 0666)
81     print "Attempt to create with O_EXCL existing file returned ", fdx
82     yaffs_unlink(fname)
83     fdx = yaffs_open(fname, 0301, 0666)
84     print "Attempt to create with O_EXCL non-existing file returned ", fdx
85
86     
87 root = "/yaffs2"
88
89 yaffs_start_up()
90 yaffs_mount(root)
91
92 yaffs_mkdir(root+"/dd",0666)
93
94 h = yaffs_open(root+"/dd/111",66,0666)
95 yaffs_close(h)
96
97 yaffs_ls(root)