mirror of
https://github.com/pellepl/spiffs.git
synced 2025-05-20 08:46:42 +08:00
Fix pointer-width bug with ctypes.
Avoid leaking memory (at least, if unmount is called). Block-level test routines to see if read/write/erase are implemented properly.
This commit is contained in:
@ -11,15 +11,22 @@ void *my_spiffs_mount(int phys_size,
|
|||||||
s32_t (read_cb)(u32_t addr, u32_t size, u8_t *dst),
|
s32_t (read_cb)(u32_t addr, u32_t size, u8_t *dst),
|
||||||
s32_t (write_cb)(u32_t addr, u32_t size, u8_t *src),
|
s32_t (write_cb)(u32_t addr, u32_t size, u8_t *src),
|
||||||
s32_t (erase_cb)(u32_t addr, u32_t size)
|
s32_t (erase_cb)(u32_t addr, u32_t size)
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
|
|
||||||
spiffs *fs = malloc(sizeof(spiffs));
|
#define WORK_BUF_SIZE (log_page_size*2)
|
||||||
|
|
||||||
uint8_t *spiffs_work_buf = malloc(log_page_size*2);
|
|
||||||
#define SPIFFS_FDS_SIZE (32*4)
|
#define SPIFFS_FDS_SIZE (32*4)
|
||||||
uint8_t *spiffs_fds = malloc(SPIFFS_FDS_SIZE);
|
#define SPIFFS_CACHE_BUF_SIZE (phys_size) //(log_page_size+32)*128)
|
||||||
#define SPIFFS_CACHE_BUF_SIZE ((log_page_size+32)*4)
|
|
||||||
uint8_t *spiffs_cache_buf = malloc(SPIFFS_CACHE_BUF_SIZE);
|
struct alloced {
|
||||||
|
spiffs fs;
|
||||||
|
uint8_t spiffs_work_buf[WORK_BUF_SIZE];
|
||||||
|
uint8_t spiffs_fds[SPIFFS_FDS_SIZE];
|
||||||
|
uint8_t spiffs_cache_buf[SPIFFS_CACHE_BUF_SIZE];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct alloced *d = malloc(sizeof(struct alloced));
|
||||||
|
spiffs *pfs = &d->fs;
|
||||||
|
|
||||||
spiffs_config cfg;
|
spiffs_config cfg;
|
||||||
cfg.phys_size = phys_size; // use all spi flash
|
cfg.phys_size = phys_size; // use all spi flash
|
||||||
@ -32,17 +39,27 @@ void *my_spiffs_mount(int phys_size,
|
|||||||
cfg.hal_write_f = write_cb;
|
cfg.hal_write_f = write_cb;
|
||||||
cfg.hal_erase_f = erase_cb;
|
cfg.hal_erase_f = erase_cb;
|
||||||
|
|
||||||
int res = SPIFFS_mount(fs,
|
int res = SPIFFS_mount(pfs,
|
||||||
&cfg,
|
&cfg,
|
||||||
spiffs_work_buf,
|
d->spiffs_work_buf,
|
||||||
spiffs_fds,
|
d->spiffs_fds,
|
||||||
SPIFFS_FDS_SIZE,
|
SPIFFS_FDS_SIZE,
|
||||||
spiffs_cache_buf,
|
d->spiffs_cache_buf,
|
||||||
SPIFFS_CACHE_BUF_SIZE,
|
SPIFFS_CACHE_BUF_SIZE,
|
||||||
0);
|
0);
|
||||||
//printf("mount res: %i\n", res);
|
|
||||||
|
|
||||||
return res?NULL:(void *)fs;
|
return res?NULL:(void *)pfs;
|
||||||
|
}
|
||||||
|
|
||||||
|
int my_spiffs_umount(spiffs *fs)
|
||||||
|
{
|
||||||
|
SPIFFS_clearerr(fs);
|
||||||
|
SPIFFS_unmount(fs);
|
||||||
|
|
||||||
|
int ret = SPIFFS_errno(fs);
|
||||||
|
free(fs);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int my_dir(spiffs *fs,
|
int my_dir(spiffs *fs,
|
||||||
|
68
py/spiffs.py
68
py/spiffs.py
@ -119,15 +119,23 @@ class Spiffs(object):
|
|||||||
self._wcb = RWCallback(self._on_write)
|
self._wcb = RWCallback(self._on_write)
|
||||||
self._ecb = EraseCallback(self._on_erase)
|
self._ecb = EraseCallback(self._on_erase)
|
||||||
|
|
||||||
self.fs = spiffs_lib.my_spiffs_mount(self.phys_size,
|
spiffs_lib.my_spiffs_mount.restype = ctypes.c_void_p
|
||||||
self.phys_addr,
|
|
||||||
self.phys_erase_block,
|
|
||||||
self.log_page_size,
|
|
||||||
self.log_block_size,
|
|
||||||
self._rcb,
|
|
||||||
self._wcb,
|
|
||||||
self._ecb)
|
|
||||||
|
|
||||||
|
fs = spiffs_lib.my_spiffs_mount(self.phys_size,
|
||||||
|
self.phys_addr,
|
||||||
|
self.phys_erase_block,
|
||||||
|
self.log_page_size,
|
||||||
|
self.log_block_size,
|
||||||
|
self._rcb,
|
||||||
|
self._wcb,
|
||||||
|
self._ecb)
|
||||||
|
|
||||||
|
self.fs = ctypes.c_void_p(fs)
|
||||||
|
|
||||||
|
def unmount(self):
|
||||||
|
res = spiffs_lib.my_spiffs_umount(self.fs)
|
||||||
|
if res<0: raise SpiffsException(res)
|
||||||
|
self.fs = None
|
||||||
|
|
||||||
def _error_print_traceback(f):
|
def _error_print_traceback(f):
|
||||||
def ret(self, *args):
|
def ret(self, *args):
|
||||||
@ -334,6 +342,48 @@ class SpiffsFileBack(Spiffs):
|
|||||||
self.back_fd.seek(addr)
|
self.back_fd.seek(addr)
|
||||||
self.back_fd.write('\xff'*size)
|
self.back_fd.write('\xff'*size)
|
||||||
|
|
||||||
|
def _destructive_tests(spiffs_mount):
|
||||||
|
"""Tests that the callbacks are implemented OK.
|
||||||
|
These tests will damage the filesystem."""
|
||||||
|
|
||||||
|
print "destructive test"
|
||||||
|
s = spiffs_mount
|
||||||
|
|
||||||
|
addr = s.phys_erase_block
|
||||||
|
size = s.phys_erase_block
|
||||||
|
old_data = s.on_read(addr, size)
|
||||||
|
s.on_erase(addr, size)
|
||||||
|
erased = s.on_read(addr, size)
|
||||||
|
assert erased == '\xff'*size
|
||||||
|
print "erased ok"
|
||||||
|
|
||||||
|
data = ''.join(chr(x & 0xff) for x in range(size))
|
||||||
|
s.on_write(addr,size,data)
|
||||||
|
readback = s.on_read(addr,size)
|
||||||
|
assert readback==data
|
||||||
|
print "wrote ok"
|
||||||
|
|
||||||
|
data2 = '\0'*10
|
||||||
|
s.on_write(addr+10,len(data2),data2)
|
||||||
|
readback = s.on_read(addr,size)
|
||||||
|
assert readback[20:]==data[20:]
|
||||||
|
assert readback[:10]==data[:10]
|
||||||
|
print "part write ok"
|
||||||
|
assert readback[10:20]==data2
|
||||||
|
print "part write ok"
|
||||||
|
|
||||||
|
s.on_write(addr+size, size, data)
|
||||||
|
s.on_erase(addr,size)
|
||||||
|
readback = s.on_read(addr+size,size)
|
||||||
|
assert readback==data
|
||||||
|
print "erase bounded ok"
|
||||||
|
|
||||||
|
s.on_write(addr,size,data)
|
||||||
|
data2 = ''.join(chr(0xff^ord(c)) for c in data)
|
||||||
|
s.on_write(addr,size,data2)
|
||||||
|
readback = s.on_read(addr,size)
|
||||||
|
assert readback=='\0'*size
|
||||||
|
print "AND ok"
|
||||||
|
|
||||||
def _tests(spiffs_mount):
|
def _tests(spiffs_mount):
|
||||||
s = spiffs_mount
|
s = spiffs_mount
|
||||||
@ -365,7 +415,9 @@ if __name__=="__main__":
|
|||||||
with file('/tmp/back.bin','r+wb') as bf:
|
with file('/tmp/back.bin','r+wb') as bf:
|
||||||
s = SpiffsFileBack(bf)
|
s = SpiffsFileBack(bf)
|
||||||
_tests(s)
|
_tests(s)
|
||||||
|
_destructive_tests(s)
|
||||||
|
|
||||||
loc=['\xff']*4*1024*1024
|
loc=['\xff']*4*1024*1024
|
||||||
s = SpiffsCharsBack(loc)
|
s = SpiffsCharsBack(loc)
|
||||||
_tests(s)
|
_tests(s)
|
||||||
|
_destructive_tests(s)
|
||||||
|
Reference in New Issue
Block a user