diff options
| author | xph <[email protected]> | 2020-04-09 16:35:28 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-04-09 16:35:28 +0800 |
| commit | ca7f01d79b3e0a997c771cec298e4ebd65b75809 (patch) | |
| tree | 3d2c48aae7203b14038ba7b98b500b5343ed7f2a /components/dfs | |
| parent | f763f8e7284b0492c5a65ad0abf94ee1963d7dcc (diff) | |
| parent | baa3145bfe61b3330b6a160523cc5470fa5db3a7 (diff) | |
Merge pull request #1 from RT-Thread/master
sync rtt
Diffstat (limited to 'components/dfs')
38 files changed, 776 insertions, 2196 deletions
diff --git a/components/dfs/Kconfig b/components/dfs/Kconfig index 35c7cf1b02..c93fd02e66 100644 --- a/components/dfs/Kconfig +++ b/components/dfs/Kconfig @@ -113,6 +113,7 @@ if RT_USING_DFS config RT_USING_DFS_RAMFS bool "Enable RAM file system" + select RT_USING_MEMHEAP default n config RT_USING_DFS_UFFS diff --git a/components/dfs/filesystems/devfs/devfs.c b/components/dfs/filesystems/devfs/devfs.c index 3080312874..936b074ce1 100644 --- a/components/dfs/filesystems/devfs/devfs.c +++ b/components/dfs/filesystems/devfs/devfs.c @@ -97,7 +97,7 @@ int dfs_device_fs_close(struct dfs_fd *file) root_dirent = (struct device_dirent *)file->data; RT_ASSERT(root_dirent != RT_NULL); - + /* release dirent */ rt_free(root_dirent); return RT_EOK; @@ -133,7 +133,7 @@ int dfs_device_fs_open(struct dfs_fd *file) struct rt_object_information *information; struct device_dirent *root_dirent; rt_uint32_t count = 0; - + /* lock scheduler */ rt_enter_critical(); @@ -145,8 +145,8 @@ int dfs_device_fs_open(struct dfs_fd *file) count ++; } - root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) + - count * sizeof(rt_device_t)); + root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) + + count * sizeof(rt_device_t)); if (root_dirent != RT_NULL) { root_dirent->devices = (rt_device_t *)(root_dirent + 1); @@ -165,7 +165,7 @@ int dfs_device_fs_open(struct dfs_fd *file) /* set data */ file->data = root_dirent; - + return RT_EOK; } @@ -178,7 +178,7 @@ int dfs_device_fs_open(struct dfs_fd *file) { /* use device fops */ file->fops = device->fops; - file->data = (void*)device; + file->data = (void *)device; /* use fops */ if (file->fops->open) @@ -214,7 +214,7 @@ int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat st->st_dev = 0; st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | - S_IWUSR | S_IWGRP | S_IWOTH; + S_IWUSR | S_IWGRP | S_IWOTH; st->st_mode &= ~S_IFREG; st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; @@ -233,7 +233,7 @@ int dfs_device_fs_stat(struct dfs_filesystem *fs, const char *path, struct stat st->st_dev = 0; st->st_mode = S_IRUSR | S_IRGRP | S_IROTH | - S_IWUSR | S_IWGRP | S_IWOTH; + S_IWUSR | S_IWGRP | S_IWOTH; if (dev_id->type == RT_Device_Class_Char) st->st_mode |= S_IFCHR; @@ -293,7 +293,7 @@ static int dfs_device_fs_poll(struct dfs_fd *fd, struct rt_pollreq *req) return mask; } -static const struct dfs_file_ops _device_fops = +static const struct dfs_file_ops _device_fops = { dfs_device_fs_open, dfs_device_fs_close, @@ -306,7 +306,7 @@ static const struct dfs_file_ops _device_fops = dfs_device_fs_poll, }; -static const struct dfs_filesystem_ops _device_fs = +static const struct dfs_filesystem_ops _device_fs = { "devfs", DFS_FS_FLAG_DEFAULT, diff --git a/components/dfs/filesystems/elmfat/dfs_elm.c b/components/dfs/filesystems/elmfat/dfs_elm.c index 290cf7c4bb..b4c4c627aa 100644 --- a/components/dfs/filesystems/elmfat/dfs_elm.c +++ b/components/dfs/filesystems/elmfat/dfs_elm.c @@ -101,7 +101,7 @@ int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *d FRESULT result; int index; struct rt_device_blk_geometry geometry; - char logic_nbr[2] = {'0',':'}; + char logic_nbr[2] = {'0', ':'}; /* get an empty position */ index = get_disk(RT_NULL); @@ -129,7 +129,7 @@ int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *d } /* mount fatfs, always 0 logic driver */ - result = f_mount(fat, (const TCHAR*)logic_nbr, 1); + result = f_mount(fat, (const TCHAR *)logic_nbr, 1); if (result == FR_OK) { char drive[8]; @@ -139,7 +139,7 @@ int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *d dir = (DIR *)rt_malloc(sizeof(DIR)); if (dir == RT_NULL) { - f_mount(RT_NULL, (const TCHAR*)logic_nbr, 1); + f_mount(RT_NULL, (const TCHAR *)logic_nbr, 1); disk[index] = RT_NULL; rt_free(fat); return -ENOMEM; @@ -157,7 +157,7 @@ int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *d } __err: - f_mount(RT_NULL, (const TCHAR*)logic_nbr, 1); + f_mount(RT_NULL, (const TCHAR *)logic_nbr, 1); disk[index] = RT_NULL; rt_free(fat); return elm_result_to_dfs(result); @@ -168,7 +168,7 @@ int dfs_elm_unmount(struct dfs_filesystem *fs) FATFS *fat; FRESULT result; int index; - char logic_nbr[2] = {'0',':'}; + char logic_nbr[2] = {'0', ':'}; fat = (FATFS *)fs->data; @@ -180,7 +180,7 @@ int dfs_elm_unmount(struct dfs_filesystem *fs) return -ENOENT; logic_nbr[0] = '0' + index; - result = f_mount(RT_NULL, logic_nbr, (BYTE)1); + result = f_mount(RT_NULL, logic_nbr, (BYTE)1); if (result != FR_OK) return elm_result_to_dfs(result); @@ -200,10 +200,11 @@ int dfs_elm_mkfs(rt_device_t dev_id) int flag; FRESULT result; int index; - char logic_nbr[2] = {'0',':'}; - - work = rt_malloc(_MAX_SS); - if(RT_NULL == work) { + char logic_nbr[2] = {'0', ':'}; + + work = (BYTE *)rt_malloc(_MAX_SS); + if (RT_NULL == work) + { return -ENOMEM; } @@ -232,7 +233,7 @@ int dfs_elm_mkfs(rt_device_t dev_id) } else { - fat = rt_malloc(sizeof(FATFS)); + fat = (FATFS *)rt_malloc(sizeof(FATFS)); if (fat == RT_NULL) { rt_free(work); /* release memory */ @@ -265,14 +266,15 @@ int dfs_elm_mkfs(rt_device_t dev_id) /* [IN] Size of the allocation unit */ /* [-] Working buffer */ /* [IN] Size of working buffer */ - result = f_mkfs(logic_nbr, FM_ANY, 0, work, _MAX_SS); - rt_free(work); work = RT_NULL; + result = f_mkfs(logic_nbr, FM_ANY | FM_SFD, 0, work, _MAX_SS); + rt_free(work); + work = RT_NULL; /* check flag status, we need clear the temp driver stored in disk[] */ if (flag == FSM_STATUS_USE_TEMP_DRIVER) { rt_free(fat); - f_mount(RT_NULL, logic_nbr,(BYTE)index); + f_mount(RT_NULL, logic_nbr, (BYTE)index); disk[index] = RT_NULL; /* close device */ rt_device_close(dev_id); @@ -338,7 +340,7 @@ int dfs_elm_open(struct dfs_fd *file) vol = elm_get_vol((FATFS *)fs->data); if (vol < 0) return -ENOENT; - drivers_fn = rt_malloc(256); + drivers_fn = (char *)rt_malloc(256); if (drivers_fn == RT_NULL) return -ENOMEM; @@ -476,6 +478,33 @@ int dfs_elm_close(struct dfs_fd *file) int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args) { + switch (cmd) + { + case RT_FIOFTRUNCATE: + { + FIL *fd; + FSIZE_t fptr, length; + FRESULT result = FR_OK; + fd = (FIL *)(file->data); + RT_ASSERT(fd != RT_NULL); + + /* save file read/write point */ + fptr = fd->fptr; + length = *(off_t*)args; + if (length <= fd->obj.objsize) + { + fd->fptr = length; + result = f_truncate(fd); + } + else + { + result = f_lseek(fd, length); + } + /* restore file read/write point */ + fd->fptr = fptr; + return elm_result_to_dfs(result); + } + } return -ENOSYS; } @@ -646,7 +675,7 @@ int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path) vol = elm_get_vol((FATFS *)fs->data); if (vol < 0) return -ENOENT; - drivers_fn = rt_malloc(256); + drivers_fn = (char *)rt_malloc(256); if (drivers_fn == RT_NULL) return -ENOMEM; @@ -678,7 +707,7 @@ int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *n if (vol < 0) return -ENOENT; - drivers_oldfn = rt_malloc(256); + drivers_oldfn = (char *)rt_malloc(256); if (drivers_oldfn == RT_NULL) return -ENOMEM; drivers_newfn = newpath; @@ -712,7 +741,7 @@ int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) vol = elm_get_vol((FATFS *)fs->data); if (vol < 0) return -ENOENT; - drivers_fn = rt_malloc(256); + drivers_fn = (char *)rt_malloc(256); if (drivers_fn == RT_NULL) return -ENOMEM; @@ -778,7 +807,7 @@ int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) return elm_result_to_dfs(result); } -static const struct dfs_file_ops dfs_elm_fops = +static const struct dfs_file_ops dfs_elm_fops = { dfs_elm_open, dfs_elm_close, @@ -834,7 +863,7 @@ DSTATUS disk_status(BYTE drv) } /* Read Sector(s) */ -DRESULT disk_read (BYTE drv, BYTE* buff, DWORD sector, UINT count) +DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, UINT count) { rt_size_t result; rt_device_t device = disk[drv]; @@ -849,7 +878,7 @@ DRESULT disk_read (BYTE drv, BYTE* buff, DWORD sector, UINT count) } /* Write Sector(s) */ -DRESULT disk_write (BYTE drv, const BYTE* buff, DWORD sector, UINT count) +DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, UINT count) { rt_size_t result; rt_device_t device = disk[drv]; @@ -916,7 +945,7 @@ DWORD get_fattime(void) { DWORD fat_time = 0; -#ifdef RT_USING_LIBC +#ifdef RT_USING_LIBC time_t now; struct tm *p_tm; struct tm tm_now; @@ -933,12 +962,12 @@ DWORD get_fattime(void) /* unlock scheduler. */ rt_exit_critical(); - fat_time = (DWORD)(tm_now.tm_year - 80) << 25 | - (DWORD)(tm_now.tm_mon + 1) << 21 | - (DWORD)tm_now.tm_mday << 16 | - (DWORD)tm_now.tm_hour << 11 | - (DWORD)tm_now.tm_min << 5 | - (DWORD)tm_now.tm_sec / 2 ; + fat_time = (DWORD)(tm_now.tm_year - 80) << 25 | + (DWORD)(tm_now.tm_mon + 1) << 21 | + (DWORD)tm_now.tm_mday << 16 | + (DWORD)tm_now.tm_hour << 11 | + (DWORD)tm_now.tm_min << 5 | + (DWORD)tm_now.tm_sec / 2 ; #endif /* RT_USING_LIBC */ return fat_time; diff --git a/components/dfs/filesystems/elmfat/ff.c b/components/dfs/filesystems/elmfat/ff.c index 4a2f0258b6..19df7450e2 100644 --- a/components/dfs/filesystems/elmfat/ff.c +++ b/components/dfs/filesystems/elmfat/ff.c @@ -569,7 +569,7 @@ static WCHAR LfnBuf[_MAX_LFN+1]; /* LFN enabled with static working buffer */ #define FREE_NAMBUF() ff_memfree(lfn) #else #define DEF_NAMBUF WCHAR *lfn; -#define INIT_NAMBUF(fs) { lfn = ff_memalloc((_MAX_LFN+1)*2); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; } +#define INIT_NAMBUF(fs) { lfn = (WCHAR *)ff_memalloc((_MAX_LFN+1)*2); if (!lfn) LEAVE_FF(fs, FR_NOT_ENOUGH_CORE); (fs)->lfnbuf = lfn; } #define FREE_NAMBUF() ff_memfree(lfn) #endif diff --git a/components/dfs/filesystems/nfs/SConscript b/components/dfs/filesystems/nfs/SConscript index 8f1e6defcd..f830dfc754 100644 --- a/components/dfs/filesystems/nfs/SConscript +++ b/components/dfs/filesystems/nfs/SConscript @@ -6,6 +6,8 @@ cwd = GetCurrentDir() src = Glob('*.c') + Glob('rpc/*.c') CPPPATH = [cwd] +SrcRemove(src, ['rpc/auth_none.c']) + group = DefineGroup('Filesystem', src, depend = ['RT_USING_DFS', 'RT_USING_DFS_NFS'], CPPPATH = CPPPATH) Return('group') diff --git a/components/dfs/filesystems/nfs/dfs_nfs.c b/components/dfs/filesystems/nfs/dfs_nfs.c index ea055b73c1..e60f6f98c0 100644 --- a/components/dfs/filesystems/nfs/dfs_nfs.c +++ b/components/dfs/filesystems/nfs/dfs_nfs.c @@ -6,7 +6,7 @@ * Change Logs: * Date Author Notes */ - + #include <stdio.h> #include <rtthread.h> #include <dfs_fs.h> @@ -65,9 +65,9 @@ typedef struct nfs_dir nfs_dir; nfs_dir *nfs_opendir(nfs_filesystem *nfs, const char *path); -static int nfs_parse_host_export(const char *host_export, +static int nfs_parse_host_export(const char *host_export, char *host, - size_t host_len, + size_t host_len, char *export, size_t export_len) { @@ -99,7 +99,7 @@ static int nfs_parse_host_export(const char *host_export, /* copy export path */ for (index = host_len; index < host_len + export_len; index ++) { - if (host_export[index] == 0) + if (host_export[index] == 0) { export[index - host_len] = '\0'; @@ -133,11 +133,11 @@ static nfs_fh3 *get_handle(nfs_filesystem *nfs, const char *name) char *path; char *init; - init = path = rt_malloc(strlen(name)+1); + init = path = rt_malloc(strlen(name) + 1); if (init == NULL) return NULL; - memcpy(init, name, strlen(name)+1); + memcpy(init, name, strlen(name) + 1); handle = rt_malloc(sizeof(nfs_fh3)); if (handle == NULL) @@ -202,10 +202,10 @@ static nfs_fh3 *get_dir_handle(nfs_filesystem *nfs, const char *name) char *path; char *init; - init = path = rt_malloc(strlen(name)+1); + init = path = rt_malloc(strlen(name) + 1); if (init == NULL) return NULL; - memcpy(init, name, strlen(name)+1); + memcpy(init, name, strlen(name) + 1); handle = rt_malloc(sizeof(nfs_fh3)); if (handle == NULL) @@ -225,7 +225,7 @@ static nfs_fh3 *get_dir_handle(nfs_filesystem *nfs, const char *name) copy_handle(handle, &nfs->current_handle); } - while ((file = strtok_r(NULL, "/", &path)) != NULL && path[0] != 0) + while ((file = strtok_r(NULL, "/", &path)) != NULL && path && path[0] != 0) { LOOKUP3args args; LOOKUP3res res; @@ -274,7 +274,7 @@ static size_t nfs_get_filesize(nfs_filesystem *nfs, nfs_fh3 *handle) memset(&res, '\0', sizeof(res)); - if ((nfsproc3_getattr_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS) || + if ((nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) || res.status != NFS3_OK) { rt_kprintf("GetAttr failed: %d\n", res.status); @@ -285,7 +285,7 @@ static size_t nfs_get_filesize(nfs_filesystem *nfs, nfs_fh3 *handle) info = &res.GETATTR3res_u.resok.obj_attributes; size = info->size; xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res); - + return size; } @@ -319,7 +319,7 @@ rt_bool_t nfs_is_directory(nfs_filesystem *nfs, const char *name) return RT_FALSE; } - info=&res.GETATTR3res_u.resok.obj_attributes; + info = &res.GETATTR3res_u.resok.obj_attributes; if (info->type == NFS3DIR) result = RT_TRUE; @@ -327,7 +327,7 @@ rt_bool_t nfs_is_directory(nfs_filesystem *nfs, const char *name) xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); rt_free(handle); - + return result; } @@ -427,7 +427,7 @@ int nfs_mkdir(nfs_filesystem *nfs, const char *name, mode_t mode) xdr_free((xdrproc_t)xdr_MKDIR3res, (char *)&res); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); rt_free(handle); - + return ret; } @@ -441,13 +441,13 @@ int nfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) memset(nfs, 0, sizeof(nfs_filesystem)); if (nfs_parse_host_export((const char *)data, nfs->host, HOST_LENGTH, - nfs->export, EXPORT_PATH_LENGTH) < 0) + nfs->export, EXPORT_PATH_LENGTH) < 0) { rt_kprintf("host or export path error\n"); goto __return; } - nfs->mount_client=clnt_create((char *)nfs->host, MOUNT_PROGRAM, MOUNT_V3, "udp"); + nfs->mount_client = clnt_create((char *)nfs->host, MOUNT_PROGRAM, MOUNT_V3, "udp"); if (nfs->mount_client == NULL) { rt_kprintf("create mount client failed\n"); @@ -465,7 +465,7 @@ int nfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) rt_kprintf("nfs mount failed\n"); goto __return; } - nfs->nfs_client=clnt_create((char *)nfs->host, NFS_PROGRAM, NFS_V3, "udp"); + nfs->nfs_client = clnt_create((char *)nfs->host, NFS_PROGRAM, NFS_V3, "udp"); if (nfs->nfs_client == NULL) { rt_kprintf("creat nfs client failed\n"); @@ -555,7 +555,7 @@ int nfs_read(struct dfs_fd *file, void *buf, size_t count) { READ3args args; READ3res res; - ssize_t bytes, total=0; + ssize_t bytes, total = 0; nfs_file *fd; nfs_filesystem *nfs; @@ -564,8 +564,8 @@ int nfs_read(struct dfs_fd *file, void *buf, size_t count) RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem*)(file->data)); - nfs = (struct nfs_filesystem *)(dfs_nfs->data); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + nfs = (struct nfs_filesystem *)(dfs_nfs->data); fd = (nfs_file *)(nfs->data); RT_ASSERT(fd != NULL); @@ -577,7 +577,8 @@ int nfs_read(struct dfs_fd *file, void *buf, size_t count) return 0; args.file = fd->handle; - do { + do + { args.offset = fd->offset; args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count; count -= args.count; @@ -612,7 +613,8 @@ int nfs_read(struct dfs_fd *file, void *buf, size_t count) } } xdr_free((xdrproc_t)xdr_READ3res, (char *)&res); - } while(count > 0); + } + while (count > 0); xdr_free((xdrproc_t)xdr_READ3res, (char *)&res); @@ -623,7 +625,7 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count) { WRITE3args args; WRITE3res res; - ssize_t bytes, total=0; + ssize_t bytes, total = 0; nfs_file *fd; nfs_filesystem *nfs; @@ -631,8 +633,8 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count) return -EISDIR; RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem*)(file->data)); - nfs = (struct nfs_filesystem *)(dfs_nfs->data); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + nfs = (struct nfs_filesystem *)(dfs_nfs->data); fd = (nfs_file *)(nfs->data); RT_ASSERT(fd != NULL); @@ -642,11 +644,12 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count) args.file = fd->handle; args.stable = FILE_SYNC; - do { + do + { args.offset = fd->offset; memset(&res, 0, sizeof(res)); - args.data.data_val=(void *)buf; + args.data.data_val = (void *)buf; args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count; args.data.data_len = args.count; count -= args.count; @@ -676,7 +679,8 @@ int nfs_write(struct dfs_fd *file, const void *buf, size_t count) file->size = fd->size; } xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res); - } while (count > 0); + } + while (count > 0); xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res); @@ -692,8 +696,8 @@ int nfs_lseek(struct dfs_fd *file, off_t offset) return -EISDIR; RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem*)(file->data)); - nfs = (struct nfs_filesystem *)(dfs_nfs->data); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + nfs = (struct nfs_filesystem *)(dfs_nfs->data); fd = (nfs_file *)(nfs->data); RT_ASSERT(fd != NULL); @@ -711,8 +715,8 @@ int nfs_close(struct dfs_fd *file) { nfs_filesystem *nfs; RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem*)(file->data)); - nfs = (struct nfs_filesystem *)(dfs_nfs->data); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + nfs = (struct nfs_filesystem *)(dfs_nfs->data); if (file->type == FT_DIRECTORY) { @@ -741,14 +745,14 @@ int nfs_open(struct dfs_fd *file) { nfs_filesystem *nfs; RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem*)(file->data)); - nfs = (struct nfs_filesystem *)(dfs_nfs->data); - + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + nfs = (struct nfs_filesystem *)(dfs_nfs->data); + RT_ASSERT(nfs != NULL); if (file->flags & O_DIRECTORY) { nfs_dir *dir; - + if (file->flags & O_CREAT) { if (nfs_mkdir(nfs, file->path, 0755) < 0) @@ -930,7 +934,7 @@ char *nfs_readdir(nfs_filesystem *nfs, nfs_dir *dir) return NULL; dir->cookie = dir->entry->cookie; - strncpy(name, dir->entry->name, NAME_MAX-1); + strncpy(name, dir->entry->name, NAME_MAX - 1); dir->entry = dir->entry->nextentry; name[NAME_MAX - 1] = '\0'; @@ -1013,7 +1017,7 @@ int nfs_unlink(struct dfs_filesystem *fs, const char *path) xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res); xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle); - rt_free(handle); + rt_free(handle); } return ret; @@ -1083,8 +1087,8 @@ int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) RT_ASSERT(file->data != NULL); - struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem*)(file->data)); - nfs = (struct nfs_filesystem *)(dfs_nfs->data); + struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->data)); + nfs = (struct nfs_filesystem *)(dfs_nfs->data); dir = (nfs_dir *)(nfs->data); RT_ASSERT(dir != NULL); @@ -1102,6 +1106,15 @@ int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) if (name == NULL) break; + if (rt_strcmp(name, ".") == 0) + { + continue; + } + else if (rt_strcmp(name, "..") == 0) + { + continue; + } + d->d_type = DT_REG; d->d_namlen = rt_strlen(name); @@ -1118,18 +1131,18 @@ int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count) static const struct dfs_file_ops nfs_fops = { - nfs_open, - nfs_close, - nfs_ioctl, - nfs_read, - nfs_write, - NULL, /* flush */ - nfs_lseek, - nfs_getdents, - NULL, /* poll */ + nfs_open, + nfs_close, + nfs_ioctl, + nfs_read, + nfs_write, + NULL, /* flush */ + nfs_lseek, + nfs_getdents, + NULL, /* poll */ }; -static const struct dfs_filesystem_ops _nfs = +static const struct dfs_filesystem_ops _nfs = { "nfs", DFS_FS_FLAG_DEFAULT, @@ -1138,7 +1151,7 @@ static const struct dfs_filesystem_ops _nfs = nfs_unmount, NULL, /* mkfs */ NULL, /* statfs */ - nfs_unlink, + nfs_unlink, nfs_stat, nfs_rename, }; diff --git a/components/dfs/filesystems/nfs/mount_xdr.c b/components/dfs/filesystems/nfs/mount_xdr.c index 8e191d3017..04faa3bb7a 100644 --- a/components/dfs/filesystems/nfs/mount_xdr.c +++ b/components/dfs/filesystems/nfs/mount_xdr.c @@ -101,6 +101,8 @@ xdr_mountres3(register XDR *xdrs, mountres3 *objp) if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo)) return (FALSE); break; + default : + return (FALSE); } return (TRUE); } diff --git a/components/dfs/filesystems/nfs/nfs_auth.c b/components/dfs/filesystems/nfs/nfs_auth.c new file mode 100644 index 0000000000..c65a1126a6 --- /dev/null +++ b/components/dfs/filesystems/nfs/nfs_auth.c @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ + +#include <rpc/types.h> +#include <rpc/xdr.h> +#include <rpc/auth.h> + +#define MAX_MARSHEL_SIZE 64 + +struct nfs_credentia +{ + rt_uint32_t stamp; + char *name; + rt_uint32_t uid; + rt_uint32_t gid; + rt_uint32_t *auxi; + rt_uint32_t auxi_count; +}; + +static void authnone_verf(AUTH *); +static bool_t authnone_validate(AUTH *, struct opaque_auth *); +static bool_t authnone_refresh(AUTH *); +static void authnone_destroy(AUTH *); +static bool_t authnone_marshal(AUTH *client, XDR *xdrs); + +static struct nfs_credentia _credentia = { + .stamp = 0, + .name = "rt-thread", + .uid = 0, + .gid = 0, + .auxi = NULL, + .auxi_count = 0, +}; + +struct opaque_auth _null_auth; + +static struct auth_ops ops = +{ + authnone_verf, + authnone_marshal, + authnone_validate, + authnone_refresh, + authnone_destroy +}; + +static struct authnone_private +{ + AUTH no_client; + char marshalled_client[MAX_MARSHEL_SIZE]; + unsigned int mcnt; +} *authnone_private; + +AUTH *authnone_create(void) +{ + register struct authnone_private *ap = authnone_private; + XDR xdr_stream; + register XDR *xdrs; + extern bool_t xdr_opaque_auth(XDR * xdrs, struct opaque_auth * ap); + struct opaque_auth auth; + rt_uint32_t *auth_buf, *auth_base; + int buf_len = 0, str_len = 0; + + if (_credentia.name) + { + str_len = strlen(_credentia.name); + } + if (str_len == 0) + { + _credentia.name = "unknown"; + str_len = strlen(_credentia.name); + } + buf_len = ((str_len) + (sizeof(rt_uint32_t)) - 1) & ~((sizeof(rt_uint32_t)) - 1); + buf_len += sizeof(struct nfs_credentia); + if (_credentia.auxi && _credentia.auxi_count) + { + buf_len += sizeof(rt_uint32_t) * _credentia.auxi_count; + } + auth_buf = auth_base = rt_malloc(buf_len); + if (auth_buf == NULL) + { + return NULL; + } + memset(auth_buf, 0, buf_len); + *auth_buf++ = htonl(rt_tick_get()); + *auth_buf++ = htonl(str_len); + memcpy(auth_buf, _credentia.name, str_len); + auth_buf += (str_len + sizeof(rt_uint32_t) - 1) >> 2; + *auth_buf++ = htonl(_credentia.uid); + *auth_buf++ = htonl(_credentia.gid); + if (_credentia.auxi && _credentia.auxi_count) + { + rt_uint32_t tmp_cnt = 0; + *auth_buf++ = htonl(_credentia.auxi_count); + while (tmp_cnt < _credentia.auxi_count) + { + *auth_buf++ = htonl(_credentia.auxi[tmp_cnt]); + } + } + else + { + *auth_buf++ = htonl(0); + } + + if (ap == 0) + { + ap = (struct authnone_private *) rt_malloc(sizeof(*ap)); + if (ap == 0) + { + rt_free(auth_base); + return NULL; + } + memset(ap, 0, sizeof(*ap)); + authnone_private = ap; + } + + if (!ap->mcnt) + { + memset(&auth, 0, sizeof(auth)); + auth.oa_flavor = 1; + auth.oa_base = (char *)auth_base; + auth.oa_length = (auth_buf - auth_base) * sizeof(rt_uint32_t); + ap->no_client.ah_cred = auth; + ap->no_client.ah_verf = _null_auth; + ap->no_client.ah_ops = &ops; + xdrs = &xdr_stream; + xdrmem_create(xdrs, ap->marshalled_client, + (unsigned int) MAX_MARSHEL_SIZE, XDR_ENCODE); + (void) xdr_opaque_auth(xdrs, &ap->no_client.ah_cred); + (void) xdr_opaque_auth(xdrs, &ap->no_client.ah_verf); + ap->mcnt = XDR_GETPOS(xdrs); + XDR_DESTROY(xdrs); + } + rt_free(auth_base); + return (&ap->no_client); +} + +/*ARGSUSED*/ +static bool_t authnone_marshal(AUTH *client, XDR *xdrs) +{ + register struct authnone_private *ap = authnone_private; + + if (ap == 0) + return (0); + return ((*xdrs->x_ops->x_putbytes)(xdrs, + ap->marshalled_client, ap->mcnt)); +} + +static void authnone_verf(AUTH *x) +{ +} + +static bool_t authnone_validate(AUTH *x, struct opaque_auth *x1) +{ + + return (TRUE); +} + +static bool_t authnone_refresh(AUTH *x) +{ + + return (FALSE); +} + +static void authnone_destroy(AUTH *x) +{ +} diff --git a/components/dfs/filesystems/nfs/nfs_xdr.c b/components/dfs/filesystems/nfs/nfs_xdr.c index 4cd69ba743..33563976c3 100644 --- a/components/dfs/filesystems/nfs/nfs_xdr.c +++ b/components/dfs/filesystems/nfs/nfs_xdr.c @@ -496,6 +496,8 @@ xdr_GETATTR3res(register XDR *xdrs, GETATTR3res *objp) if (!xdr_GETATTR3resok(xdrs, &objp->GETATTR3res_u.resok)) return (FALSE); break; + default : + return (FALSE); } return (TRUE); } diff --git a/components/dfs/filesystems/ramfs/dfs_ramfs.c b/components/dfs/filesystems/ramfs/dfs_ramfs.c index e851c7ef78..aa85081e1e 100644 --- a/components/dfs/filesystems/ramfs/dfs_ramfs.c +++ b/components/dfs/filesystems/ramfs/dfs_ramfs.c @@ -21,14 +21,14 @@ int dfs_ramfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) { - struct dfs_ramfs* ramfs; + struct dfs_ramfs *ramfs; if (data == NULL) return -EIO; ramfs = (struct dfs_ramfs *)data; fs->data = ramfs; - + return RT_EOK; } @@ -48,8 +48,8 @@ int dfs_ramfs_statfs(struct dfs_filesystem *fs, struct statfs *buf) RT_ASSERT(buf != NULL); buf->f_bsize = 512; - buf->f_blocks = ramfs->memheap.pool_size/512; - buf->f_bfree = ramfs->memheap.available_size/512; + buf->f_blocks = ramfs->memheap.pool_size / 512; + buf->f_bfree = ramfs->memheap.available_size / 512; return RT_EOK; } @@ -119,7 +119,7 @@ int dfs_ramfs_write(struct dfs_fd *fd, const void *buf, size_t count) struct ramfs_dirent *dirent; struct dfs_ramfs *ramfs; - dirent = (struct ramfs_dirent*)fd->data; + dirent = (struct ramfs_dirent *)fd->data; RT_ASSERT(dirent != NULL); ramfs = dirent->fs; @@ -260,7 +260,7 @@ int dfs_ramfs_open(struct dfs_fd *file) file->size = dirent->size; if (file->flags & O_APPEND) file->pos = file->size; - else + else file->pos = 0; return 0; @@ -381,7 +381,7 @@ int dfs_ramfs_rename(struct dfs_filesystem *fs, return RT_EOK; } -static const struct dfs_file_ops _ram_fops = +static const struct dfs_file_ops _ram_fops = { dfs_ramfs_open, dfs_ramfs_close, @@ -418,7 +418,7 @@ int dfs_ramfs_init(void) } INIT_COMPONENT_EXPORT(dfs_ramfs_init); -struct dfs_ramfs* dfs_ramfs_create(rt_uint8_t *pool, rt_size_t size) +struct dfs_ramfs *dfs_ramfs_create(rt_uint8_t *pool, rt_size_t size) { struct dfs_ramfs *ramfs; rt_uint8_t *data_ptr; @@ -435,7 +435,7 @@ struct dfs_ramfs* dfs_ramfs_create(rt_uint8_t *pool, rt_size_t size) if (result != RT_EOK) return NULL; /* detach this memheap object from the system */ - rt_object_detach((rt_object_t)&(ramfs->memheap)); + rt_object_detach((rt_object_t) & (ramfs->memheap)); /* initialize ramfs object */ ramfs->magic = RAMFS_MAGIC; diff --git a/components/dfs/filesystems/ramfs/dfs_ramfs.h b/components/dfs/filesystems/ramfs/dfs_ramfs.h index 9419784db9..4975ecb332 100644 --- a/components/dfs/filesystems/ramfs/dfs_ramfs.h +++ b/components/dfs/filesystems/ramfs/dfs_ramfs.h @@ -24,7 +24,7 @@ struct ramfs_dirent struct dfs_ramfs *fs; /* file system ref */ char name[RAMFS_NAME_MAX]; /* dirent name */ - rt_uint8_t* data; + rt_uint8_t *data; rt_size_t size; /* file size */ }; @@ -41,7 +41,7 @@ struct dfs_ramfs }; int dfs_ramfs_init(void); -struct dfs_ramfs* dfs_ramfs_create(rt_uint8_t* pool, rt_size_t size); +struct dfs_ramfs *dfs_ramfs_create(rt_uint8_t *pool, rt_size_t size); #endif diff --git a/components/dfs/filesystems/romfs/dfs_romfs.c b/components/dfs/filesystems/romfs/dfs_romfs.c index a953ac72d5..dcc34e5830 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.c +++ b/components/dfs/filesystems/romfs/dfs_romfs.c @@ -105,7 +105,7 @@ struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const ch if (dirent[index].type == ROMFS_DIRENT_DIR) { /* enter directory */ - dirent = (struct romfs_dirent*)dirent[index].data; + dirent = (struct romfs_dirent *)dirent[index].data; found = 1; break; } @@ -226,7 +226,7 @@ int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) st->st_dev = 0; st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | - S_IWUSR | S_IWGRP | S_IWOTH; + S_IWUSR | S_IWGRP | S_IWOTH; if (dirent->type == ROMFS_DIRENT_DIR) { diff --git a/components/dfs/filesystems/romfs/dfs_romfs.h b/components/dfs/filesystems/romfs/dfs_romfs.h index 74b256c8a6..ba6a3d7dfa 100644 --- a/components/dfs/filesystems/romfs/dfs_romfs.h +++ b/components/dfs/filesystems/romfs/dfs_romfs.h @@ -1,10 +1,11 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2019, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes + * 2019/01/13 Bernard code cleanup */ #ifndef __DFS_ROMFS_H__ @@ -12,16 +13,16 @@ #include <rtthread.h> -#define ROMFS_DIRENT_FILE 0x00 -#define ROMFS_DIRENT_DIR 0x01 +#define ROMFS_DIRENT_FILE 0x00 +#define ROMFS_DIRENT_DIR 0x01 struct romfs_dirent { - rt_uint32_t type; /* dirent type */ + rt_uint32_t type; /* dirent type */ - const char *name; /* dirent name */ - const rt_uint8_t *data; /* file date ptr */ - rt_size_t size; /* file size */ + const char *name; /* dirent name */ + const rt_uint8_t *data; /* file date ptr */ + rt_size_t size; /* file size */ }; int dfs_romfs_init(void); diff --git a/components/dfs/filesystems/romfs/mkromfs.py b/components/dfs/filesystems/romfs/mkromfs.py deleted file mode 100644 index f3c12876d2..0000000000 --- a/components/dfs/filesystems/romfs/mkromfs.py +++ /dev/null @@ -1,252 +0,0 @@ -#!/usr/bin/env python - -import sys -import os - -import struct -from collections import namedtuple -import StringIO - -import argparse -parser = argparse.ArgumentParser() -parser.add_argument('rootdir', type=str, help='the path to rootfs') -parser.add_argument('output', type=argparse.FileType('wb'), nargs='?', help='output file name') -parser.add_argument('--dump', action='store_true', help='dump the fs hierarchy') -parser.add_argument('--binary', action='store_true', help='output binary file') -parser.add_argument('--addr', default='0', help='set the base address of the binary file, default to 0.') - -class File(object): - def __init__(self, name): - self._name = name - self._data = open(name, 'rb').read() - - @property - def name(self): - return self._name - - @property - def c_name(self): - return '_' + self._name.replace('.', '_') - - @property - def bin_name(self): - # Pad to 4 bytes boundary with \0 - pad_len = 4 - bn = self._name + '\0' * (pad_len - len(self._name) % pad_len) - return bn - - def c_data(self, prefix=''): - '''Get the C code represent of the file content.''' - head = 'static const rt_uint8_t %s[] = {\n' % \ - (prefix + self.c_name) - tail = '\n};' - return head + ','.join(('0x%02x' % ord(i) for i in self._data)) + tail - - @property - def entry_size(self): - return len(self._data) - - def bin_data(self, base_addr=0x0): - return bytes(self._data) - - def dump(self, indent=0): - print('%s%s' % (' ' * indent, self._name)) - -class Folder(object): - bin_fmt = struct.Struct('IIII') - bin_item = namedtuple('dirent', 'type, name, data, size') - - def __init__(self, name): - self._name = name - self._children = [] - - @property - def name(self): - return self._name - - @property - def c_name(self): - # add _ to avoid conflict with C key words. - return '_' + self._name - - @property - def bin_name(self): - # Pad to 4 bytes boundary with \0 - pad_len = 4 - bn = self._name + '\0' * (pad_len - len(self._name) % pad_len) - return bn - - def walk(self): - # os.listdir will return unicode list if the argument is unicode. - # TODO: take care of the unicode names - for ent in os.listdir(u'.'): - if os.path.isdir(ent): - cwd = os.getcwdu() - d = Folder(ent) - # depth-first - os.chdir(os.path.join(cwd, ent)) - d.walk() - # restore the cwd - os.chdir(cwd) - self._children.append(d) - else: - self._children.append(File(ent)) - - def sort(self): - def _sort(x, y): - if x.name == y.name: - return 0 - elif x.name > y.name: - return 1 - else: - return -1 - self._children.sort(cmp=_sort) - - # sort recursively - for c in self._children: - if isinstance(c, Folder): - c.sort() - - def dump(self, indent=0): - print('%s%s' % (' ' * indent, self._name)) - for c in self._children: - c.dump(indent + 1) - - def c_data(self, prefix=''): - '''get the C code represent of the folder. - - It is recursive.''' - # make the current dirent - # static is good. Only root dirent is global visible. - dhead = 'static const struct romfs_dirent %s[] = {\n' % (prefix + self.c_name) - dtail = '\n};' - body_fmt = ' {{{type}, "{name}", (rt_uint8_t *){data}, sizeof({data})/sizeof({data}[0])}}' - # prefix of children - cpf = prefix+self.c_name - body_li = [] - payload_li = [] - for c in self._children: - if isinstance(c, File): - tp = 'ROMFS_DIRENT_FILE' - elif isinstance(c, Folder): - tp = 'ROMFS_DIRENT_DIR' - else: - assert False, 'Unkown instance:%s' % str(c) - body_li.append(body_fmt.format(type=tp, - name=c.name, - data=cpf+c.c_name)) - payload_li.append(c.c_data(prefix=cpf)) - - # All the data we need is defined in payload so we should append the - # dirent to it. It also meet the depth-first policy in this code. - payload_li.append(dhead + ',\n'.join(body_li) + dtail) - - return '\n\n'.join(payload_li) - - @property - def entry_size(self): - return len(self._children) - - def bin_data(self, base_addr=0x0): - '''Return StringIO object''' - # The binary layout is different from the C code layout. We put the - # dirent before the payload in this mode. But the idea is still simple: - # Depth-First. - - #{ - # rt_uint32_t type; - # const char *name; - # const rt_uint8_t *data; - # rt_size_t size; - #} - d_li = [] - # payload base - p_base = base_addr + self.bin_fmt.size * self.entry_size - # the length to record how many data is in - v_len = p_base - # payload - p_li = [] - for c in self._children: - if isinstance(c, File): - # ROMFS_DIRENT_FILE - tp = 0 - elif isinstance(c, Folder): - # ROMFS_DIRENT_DIR - tp = 1 - else: - assert False, 'Unkown instance:%s' % str(c) - - name = bytes(c.bin_name) - name_addr = v_len - v_len += len(name) - - data = c.bin_data(base_addr=v_len) - data_addr = v_len - # pad the data to 4 bytes boundary - pad_len = 4 - if len(data) % pad_len != 0: - data += '\0' * (pad_len - len(data) % pad_len) - v_len += len(data) - - d_li.append(self.bin_fmt.pack(*self.bin_item( - type=tp, - name=name_addr, - data=data_addr, - size=c.entry_size))) - - p_li.extend((name, data)) - - return bytes().join(d_li) + bytes().join(p_li) - -def get_c_data(tree): - # Handle the root dirent specially. - root_dirent_fmt = '''/* Generated by mkromfs. Edit with caution. */ -#include <rtthread.h> -#include <dfs_romfs.h> - -{data} - -const struct romfs_dirent {name} = {{ - ROMFS_DIRENT_DIR, "/", (rt_uint8_t *){rootdirent}, sizeof({rootdirent})/sizeof({rootdirent}[0]) -}}; -''' - - return root_dirent_fmt.format(name='romfs_root', - rootdirent=tree.c_name, - data=tree.c_data()) - -def get_bin_data(tree, base_addr): - v_len = base_addr + Folder.bin_fmt.size - name = bytes('/\0\0\0') - name_addr = v_len - v_len += len(name) - data_addr = v_len - # root entry - data = Folder.bin_fmt.pack(*Folder.bin_item(type=1, - name=name_addr, - data=data_addr, - size=tree.entry_size)) - return data + name + tree.bin_data(v_len) - -if __name__ == '__main__': - args = parser.parse_args() - - os.chdir(args.rootdir) - - tree = Folder('romfs_root') - tree.walk() - tree.sort() - - if args.dump: - tree.dump() - - if args.binary: - data = get_bin_data(tree, int(args.addr, 16)) - else: - data = get_c_data(tree) - - output = args.output - if not output: - output = sys.stdout - - output.write(data) diff --git a/components/dfs/filesystems/romfs/romfs.c b/components/dfs/filesystems/romfs/romfs.c index 1ea9f21d8e..cfce09a095 100644 --- a/components/dfs/filesystems/romfs/romfs.c +++ b/components/dfs/filesystems/romfs/romfs.c @@ -12,7 +12,7 @@ const static unsigned char _dummy_dummy_txt[] = { - 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x61,0x20,0x66,0x69,0x6c,0x65,0x21,0x0d,0x0a, + 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x21, 0x0d, 0x0a, }; const static struct romfs_dirent _dummy[] = @@ -22,17 +22,17 @@ const static struct romfs_dirent _dummy[] = const static unsigned char _dummy_txt[] = { - 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x61,0x20,0x66,0x69,0x6c,0x65,0x21,0x0d,0x0a, + 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x21, 0x0d, 0x0a, }; RT_WEAK const struct romfs_dirent _root_dirent[] = { - {ROMFS_DIRENT_DIR, "dummy", (rt_uint8_t *)_dummy, sizeof(_dummy)/sizeof(_dummy[0])}, + {ROMFS_DIRENT_DIR, "dummy", (rt_uint8_t *)_dummy, sizeof(_dummy) / sizeof(_dummy[0])}, {ROMFS_DIRENT_FILE, "dummy.txt", _dummy_txt, sizeof(_dummy_txt)}, }; RT_WEAK const struct romfs_dirent romfs_root = { - ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_root_dirent, sizeof(_root_dirent)/sizeof(_root_dirent[0]) + ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_root_dirent, sizeof(_root_dirent) / sizeof(_root_dirent[0]) }; diff --git a/components/dfs/filesystems/skeleton/skeleton.c b/components/dfs/filesystems/skeleton/skeleton.c index 30f0d4f629..0c20513e82 100644 --- a/components/dfs/filesystems/skeleton/skeleton.c +++ b/components/dfs/filesystems/skeleton/skeleton.c @@ -14,52 +14,52 @@ #include "dfs_skt_fs.h" -int dfs_skt_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* data) +int dfs_skt_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) { return RT_EOK; } -int dfs_skt_unmount(struct dfs_filesystem* fs) +int dfs_skt_unmount(struct dfs_filesystem *fs) { return RT_EOK; } -int dfs_skt_ioctl(struct dfs_fd* file, int cmd, void* args) +int dfs_skt_ioctl(struct dfs_fd *file, int cmd, void *args) { return -RT_EIO; } -int dfs_skt_read(struct dfs_fd* file, void *buf, rt_size_t count) +int dfs_skt_read(struct dfs_fd *file, void *buf, rt_size_t count) { return count; } -int dfs_skt_lseek(struct dfs_fd* file, rt_off_t offset) +int dfs_skt_lseek(struct dfs_fd *file, rt_off_t offset) { return -RT_EIO; } -int dfs_skt_close(struct dfs_fd* file) +int dfs_skt_close(struct dfs_fd *file) { return RT_EOK; } -int dfs_skt_open(struct dfs_fd* file) +int dfs_skt_open(struct dfs_fd *file) { return RT_EOK; } -int dfs_skt_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) +int dfs_skt_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) { return RT_EOK; } -int dfs_skt_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) +int dfs_skt_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count) { return count * sizeof(struct dirent); } -static const struct dfs_file_ops _skt_fops = +static const struct dfs_file_ops _skt_fops = { dfs_skt_open, dfs_skt_close, diff --git a/components/dfs/filesystems/uffs/dfs_uffs.c b/components/dfs/filesystems/uffs/dfs_uffs.c index b875d1eb00..fb8caf9dd7 100644 --- a/components/dfs/filesystems/uffs/dfs_uffs.c +++ b/components/dfs/filesystems/uffs/dfs_uffs.c @@ -33,12 +33,12 @@ struct _nand_dev { - struct rt_mtd_nand_device * dev; + struct rt_mtd_nand_device *dev; struct uffs_StorageAttrSt storage; uffs_Device uffs_dev; uffs_MountTable mount_table; char mount_path[UFFS_MOUNT_PATH_MAX]; - void * data; /* when uffs use static buf, it will save ptr here */ + void *data; /* when uffs use static buf, it will save ptr here */ }; /* make sure the following struct var had been initilased to 0! */ static struct _nand_dev nand_part[UFFS_DEVICE_MAX] = {0}; @@ -109,11 +109,11 @@ static URET _device_release(uffs_Device *dev) } static int init_uffs_fs( - struct _nand_dev * nand_part) + struct _nand_dev *nand_part) { - uffs_MountTable * mtb; - struct rt_mtd_nand_device * nand; - struct uffs_StorageAttrSt * flash_storage; + uffs_MountTable *mtb; + struct rt_mtd_nand_device *nand; + struct uffs_StorageAttrSt *flash_storage; mtb = &nand_part->mount_table; nand = nand_part->dev; @@ -123,7 +123,7 @@ static int init_uffs_fs( uffs_setup_storage(flash_storage, nand); /* register mount table */ - if(mtb->dev) + if (mtb->dev) { /* set memory allocator for uffs */ #if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0 @@ -141,15 +141,15 @@ static int init_uffs_fs( } static int dfs_uffs_mount( - struct dfs_filesystem* fs, + struct dfs_filesystem *fs, unsigned long rwflag, - const void* data) + const void *data) { rt_base_t index; - uffs_MountTable * mount_part; - struct rt_mtd_nand_device * dev; - - RT_ASSERT(rt_strlen(fs->path) < (UFFS_MOUNT_PATH_MAX-1)); + uffs_MountTable *mount_part; + struct rt_mtd_nand_device *dev; + + RT_ASSERT(rt_strlen(fs->path) < (UFFS_MOUNT_PATH_MAX - 1)); dev = RT_MTD_NAND_DEVICE(fs->dev_id); /*1. find a empty entry in partition table */ @@ -184,7 +184,7 @@ static int dfs_uffs_mount( return 0; } -static int dfs_uffs_unmount(struct dfs_filesystem* fs) +static int dfs_uffs_unmount(struct dfs_filesystem *fs) { rt_base_t index; int result; @@ -210,7 +210,7 @@ static int dfs_uffs_mkfs(rt_device_t dev_id) { rt_base_t index; rt_uint32_t block; - struct rt_mtd_nand_device * mtd; + struct rt_mtd_nand_device *mtd; /*1. find the device index */ for (index = 0; index < UFFS_DEVICE_MAX; index++) @@ -226,7 +226,7 @@ static int dfs_uffs_mkfs(rt_device_t dev_id) } /*2. then unmount the partition */ - uffs_Mount(nand_part[index].mount_path); + uffs_UnMount(nand_part[index].mount_path); mtd = nand_part[index].dev; /*3. erase all blocks on the partition */ @@ -234,11 +234,13 @@ static int dfs_uffs_mkfs(rt_device_t dev_id) for (; block <= mtd->block_end; block++) { rt_mtd_nand_erase_block(mtd, block); +#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) if (rt_mtd_nand_check_block(mtd, block) != RT_EOK) { rt_kprintf("found bad block %d\n", block); rt_mtd_nand_mark_badblock(mtd, block); } +#endif } /*4. remount it */ @@ -249,11 +251,11 @@ static int dfs_uffs_mkfs(rt_device_t dev_id) return RT_EOK; } -static int dfs_uffs_statfs(struct dfs_filesystem* fs, - struct statfs *buf) +static int dfs_uffs_statfs(struct dfs_filesystem *fs, + struct statfs *buf) { rt_base_t index; - struct rt_mtd_nand_device * mtd = RT_MTD_NAND_DEVICE(fs->dev_id); + struct rt_mtd_nand_device *mtd = RT_MTD_NAND_DEVICE(fs->dev_id); RT_ASSERT(mtd != RT_NULL); @@ -265,24 +267,24 @@ static int dfs_uffs_statfs(struct dfs_filesystem* fs, } if (index == UFFS_DEVICE_MAX) return -ENOENT; - - buf->f_bsize = mtd->page_size*mtd->pages_per_block; + + buf->f_bsize = mtd->page_size * mtd->pages_per_block; buf->f_blocks = (mtd->block_end - mtd->block_start + 1); - buf->f_bfree = uffs_GetDeviceFree(&nand_part[index].uffs_dev)/buf->f_bsize ; - + buf->f_bfree = uffs_GetDeviceFree(&nand_part[index].uffs_dev) / buf->f_bsize ; + return 0; } -static int dfs_uffs_open(struct dfs_fd* file) +static int dfs_uffs_open(struct dfs_fd *file) { int fd; int oflag, mode; - char * file_path; + char *file_path; oflag = file->flags; if (oflag & O_DIRECTORY) /* operations about dir */ { - uffs_DIR * dir; + uffs_DIR *dir; if (oflag & O_CREAT) /* create a dir*/ { @@ -291,8 +293,8 @@ static int dfs_uffs_open(struct dfs_fd* file) } /* open dir */ file_path = rt_malloc(FILE_PATH_MAX); - if(file_path == RT_NULL) - return -ENOMEM; + if (file_path == RT_NULL) + return -ENOMEM; if (file->path[0] == '/' && !(file->path[1] == 0)) rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->path); @@ -306,7 +308,7 @@ static int dfs_uffs_open(struct dfs_fd* file) if (dir == RT_NULL) { - rt_free(file_path); + rt_free(file_path); return uffs_result_to_dfs(uffs_get_error()); } /* save this pointer,will used by dfs_uffs_getdents*/ @@ -349,7 +351,7 @@ static int dfs_uffs_open(struct dfs_fd* file) return 0; } -static int dfs_uffs_close(struct dfs_fd* file) +static int dfs_uffs_close(struct dfs_fd *file) { int oflag; int fd; @@ -372,12 +374,12 @@ static int dfs_uffs_close(struct dfs_fd* file) return uffs_result_to_dfs(uffs_get_error()); } -static int dfs_uffs_ioctl(struct dfs_fd * file, int cmd, void* args) +static int dfs_uffs_ioctl(struct dfs_fd *file, int cmd, void *args) { return -ENOSYS; } -static int dfs_uffs_read(struct dfs_fd * file, void* buf, size_t len) +static int dfs_uffs_read(struct dfs_fd *file, void *buf, size_t len) { int fd; int char_read; @@ -392,9 +394,9 @@ static int dfs_uffs_read(struct dfs_fd * file, void* buf, size_t len) return char_read; } -static int dfs_uffs_write(struct dfs_fd* file, - const void* buf, - size_t len) +static int dfs_uffs_write(struct dfs_fd *file, + const void *buf, + size_t len) { int fd; int char_write; @@ -410,7 +412,7 @@ static int dfs_uffs_write(struct dfs_fd* file, return char_write; } -static int dfs_uffs_flush(struct dfs_fd* file) +static int dfs_uffs_flush(struct dfs_fd *file) { int fd; int result; @@ -418,7 +420,7 @@ static int dfs_uffs_flush(struct dfs_fd* file) fd = (int)(file->data); result = uffs_flush(fd); - if (result < 0 ) + if (result < 0) return uffs_result_to_dfs(uffs_get_error()); return 0; } @@ -427,18 +429,18 @@ int uffs_seekdir(uffs_DIR *dir, long offset) { int i = 0; - while(i < offset) - { + while (i < offset) + { if (uffs_readdir(dir) == RT_NULL) return -1; i++; - } + } return 0; } -static int dfs_uffs_seek(struct dfs_fd* file, - rt_off_t offset) +static int dfs_uffs_seek(struct dfs_fd *file, + rt_off_t offset) { int result; @@ -446,17 +448,17 @@ static int dfs_uffs_seek(struct dfs_fd* file, if (file->type == FT_DIRECTORY) { uffs_rewinddir((uffs_DIR *)(file->data)); - result = uffs_seekdir((uffs_DIR *)(file->data), offset/sizeof(struct dirent)); + result = uffs_seekdir((uffs_DIR *)(file->data), offset / sizeof(struct dirent)); if (result >= 0) { - file->pos = offset; + file->pos = offset; return offset; } } else if (file->type == FT_REGULAR) { result = uffs_seek((int)(file->data), offset, USEEK_SET); - if (result >= 0) + if (result >= 0) return offset; } @@ -465,19 +467,19 @@ static int dfs_uffs_seek(struct dfs_fd* file, /* return the size of struct dirent*/ static int dfs_uffs_getdents( - struct dfs_fd* file, - struct dirent* dirp, + struct dfs_fd *file, + struct dirent *dirp, uint32_t count) { rt_uint32_t index; - char * file_path; - struct dirent* d; - uffs_DIR* dir; - struct uffs_dirent * uffs_d; - - dir = (uffs_DIR*)(file->data); + char *file_path; + struct dirent *d; + uffs_DIR *dir; + struct uffs_dirent *uffs_d; + + dir = (uffs_DIR *)(file->data); RT_ASSERT(dir != RT_NULL); - + /* round count, count is always 1 */ count = (count / sizeof(struct dirent)) * sizeof(struct dirent); if (count == 0) return -EINVAL; @@ -486,13 +488,13 @@ static int dfs_uffs_getdents( file_path = rt_malloc(FILE_PATH_MAX); if (file_path == RT_NULL) return -ENOMEM; - + index = 0; /* usually, the while loop should only be looped only once! */ while (1) { struct uffs_stat s; - + d = dirp + index; uffs_d = uffs_readdir(dir); @@ -507,8 +509,8 @@ static int dfs_uffs_getdents( else rt_strncpy(file_path, uffs_d->d_name, FILE_PATH_MAX); - uffs_stat(file_path, &s); - switch(s.st_mode & US_IFMT) /* file type mark */ + uffs_stat(file_path, &s); + switch (s.st_mode & US_IFMT) /* file type mark */ { case US_IFREG: /* directory */ d->d_type = DT_REG; @@ -533,10 +535,10 @@ static int dfs_uffs_getdents( if (index * sizeof(struct dirent) >= count) break; } - + /* free file name buf */ rt_free(file_path); - + if (index == 0) return uffs_result_to_dfs(uffs_get_error()); @@ -545,7 +547,7 @@ static int dfs_uffs_getdents( return index * sizeof(struct dirent); } -static int dfs_uffs_unlink(struct dfs_filesystem* fs, const char* path) +static int dfs_uffs_unlink(struct dfs_filesystem *fs, const char *path) { int result; struct uffs_stat s; @@ -556,7 +558,7 @@ static int dfs_uffs_unlink(struct dfs_filesystem* fs, const char* path) return uffs_result_to_dfs(uffs_get_error()); } - switch(s.st_mode & US_IFMT) + switch (s.st_mode & US_IFMT) { case US_IFREG: result = uffs_remove(path); @@ -575,12 +577,12 @@ static int dfs_uffs_unlink(struct dfs_filesystem* fs, const char* path) } static int dfs_uffs_rename( - struct dfs_filesystem* fs, - const char* oldpath, - const char* newpath) + struct dfs_filesystem *fs, + const char *oldpath, + const char *newpath) { int result; - + result = uffs_rename(oldpath, newpath); if (result < 0) return uffs_result_to_dfs(uffs_get_error()); @@ -588,7 +590,7 @@ static int dfs_uffs_rename( return 0; } -static int dfs_uffs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) +static int dfs_uffs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) { int result; struct uffs_stat s; @@ -607,7 +609,7 @@ static int dfs_uffs_stat(struct dfs_filesystem* fs, const char *path, struct sta return 0; } -static const struct dfs_file_ops dfs_uffs_fops = +static const struct dfs_file_ops dfs_uffs_fops = { dfs_uffs_open, dfs_uffs_close, diff --git a/components/dfs/filesystems/uffs/dfs_uffs.h b/components/dfs/filesystems/uffs/dfs_uffs.h index 615d29036d..72f0ee9240 100644 --- a/components/dfs/filesystems/uffs/dfs_uffs.h +++ b/components/dfs/filesystems/uffs/dfs_uffs.h @@ -83,7 +83,7 @@ extern const uffs_FlashOps nand_ops; extern void uffs_setup_storage( struct uffs_StorageAttrSt *attr, - struct rt_mtd_nand_device * nand); + struct rt_mtd_nand_device *nand); extern int dfs_uffs_init(void); #endif /* DFS_UFFS_H_ */ diff --git a/components/dfs/filesystems/uffs/src/emu/uffs_fileem_ecc_hw_auto.c b/components/dfs/filesystems/uffs/src/emu/uffs_fileem_ecc_hw_auto.c index ae86f7f16a..31fbb28c95 100644 --- a/components/dfs/filesystems/uffs/src/emu/uffs_fileem_ecc_hw_auto.c +++ b/components/dfs/filesystems/uffs/src/emu/uffs_fileem_ecc_hw_auto.c @@ -344,13 +344,13 @@ ext: uffs_FlashOps g_femu_ops_ecc_hw_auto = { - femu_hw_auto_InitFlash, // InitFlash() - femu_ReleaseFlash, // ReleaseFlash() - NULL, // ReadPage() + femu_hw_auto_InitFlash, // InitFlash() + femu_ReleaseFlash, // ReleaseFlash() + NULL, // ReadPage() femu_hw_auto_ReadPageWithLayout, // ReadPageWithLayout() - NULL, // WritePage() - femu_hw_auto_WritePageWithLayout, // WirtePageWithLayout() - NULL, // IsBadBlock(), let UFFS take care of it. - NULL, // MarkBadBlock(), let UFFS take care of it. + NULL, // WritePage() + femu_hw_auto_WritePageWithLayout, // WritePageWithLayout() + NULL, // IsBadBlock(), let UFFS take care of it. + NULL, // MarkBadBlock(), let UFFS take care of it. femu_EraseBlock, // EraseBlock() }; diff --git a/components/dfs/filesystems/uffs/src/emu/uffs_fileem_ecc_soft.c b/components/dfs/filesystems/uffs/src/emu/uffs_fileem_ecc_soft.c index f657066145..0a24c1cf3b 100644 --- a/components/dfs/filesystems/uffs/src/emu/uffs_fileem_ecc_soft.c +++ b/components/dfs/filesystems/uffs/src/emu/uffs_fileem_ecc_soft.c @@ -203,10 +203,10 @@ uffs_FlashOps g_femu_ops_ecc_soft = { femu_InitFlash, // InitFlash() femu_ReleaseFlash, // ReleaseFlash() femu_ReadPage, // ReadPage() - NULL, // ReadPageWithLayout + NULL, // ReadPageWithLayout femu_WritePage, // WritePage() - NULL, // WirtePageWithLayout - NULL, // IsBadBlock(), let UFFS take care of it. - NULL, // MarkBadBlock(), let UFFS take care of it. + NULL, // WritePageWithLayout + NULL, // IsBadBlock(), let UFFS take care of it. + NULL, // MarkBadBlock(), let UFFS take care of it. femu_EraseBlock, // EraseBlock() }; diff --git a/components/dfs/filesystems/uffs/src/example/flash-interface-example.c b/components/dfs/filesystems/uffs/src/example/flash-interface-example.c index 9a6e008262..ff26beb281 100644 --- a/components/dfs/filesystems/uffs/src/example/flash-interface-example.c +++ b/components/dfs/filesystems/uffs/src/example/flash-interface-example.c @@ -292,11 +292,11 @@ static uffs_FlashOps g_my_nand_ops = { nand_init_flash, // InitFlash() nand_release_flash, // ReleaseFlash() nand_read_page, // ReadPage() - NULL, // ReadPageWithLayout + NULL, // ReadPageWithLayout nand_write_page, // WritePage() - NULL, // WirtePageWithLayout - NULL, // IsBadBlock(), let UFFS take care of it. - NULL, // MarkBadBlock(), let UFFS take care of it. + NULL, // WritePageWithLayout + NULL, // IsBadBlock(), let UFFS take care of it. + NULL, // MarkBadBlock(), let UFFS take care of it. nand_erase_block, // EraseBlock() }; diff --git a/components/dfs/filesystems/uffs/uffs_nandif.c b/components/dfs/filesystems/uffs/uffs_nandif.c index 47cb88b6aa..2e1a981fba 100644 --- a/components/dfs/filesystems/uffs/uffs_nandif.c +++ b/components/dfs/filesystems/uffs/uffs_nandif.c @@ -135,7 +135,7 @@ const uffs_FlashOps nand_ops = nand_read_page, /* ReadPage() */ NULL, /* ReadPageWithLayout */ nand_write_page, /* WritePage() */ - NULL, /* WirtePageWithLayout */ + NULL, /* WritePageWithLayout */ #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) nand_check_block, nand_mark_badblock, @@ -302,7 +302,7 @@ const uffs_FlashOps nand_ops = NULL, /* ReadPage() */ ReadPageWithLayout, /* ReadPageWithLayout */ NULL, /* WritePage() */ - WritePageWithLayout,/* WirtePageWithLayout */ + WritePageWithLayout,/* WritePageWithLayout */ #if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON) nand_check_block, diff --git a/components/dfs/filesystems/yaffs2/fixwarning.diff b/components/dfs/filesystems/yaffs2/fixwarning.diff deleted file mode 100644 index d0f9304924..0000000000 --- a/components/dfs/filesystems/yaffs2/fixwarning.diff +++ /dev/null @@ -1,398 +0,0 @@ -diff --git a/dfs_yaffs2.c b/dfs_yaffs2.c -index 4b056ae..e63d5d4 100644 ---- a/dfs_yaffs2.c -+++ b/dfs_yaffs2.c -@@ -39,7 +39,6 @@ static int dfs_yaffs_mount(struct dfs_filesystem* fs, - const void* data) - { - unsigned index; -- ynandif_Geometry *g; - - /*1. find a empty entry in partition table */ - for (index = 0; index < NAND_DEVICE_PART_MAX ; index ++) -diff --git a/yaffs/direct/yaffsfs.c b/yaffs/direct/yaffsfs.c -index 17fc502..1f47ea7 100644 ---- a/yaffs/direct/yaffsfs.c -+++ b/yaffs/direct/yaffsfs.c -@@ -1367,7 +1367,7 @@ off_t yaffs_lseek(int handle, off_t offset, int whence) - if(offset >= 0) - pos = offset; - } else if(whence == SEEK_CUR) { -- if( (fd->position + offset) >= 0) -+ if( ((int)fd->position + offset) >= 0) - pos = (fd->position + offset); - } else if(whence == SEEK_END) { - fSize = yaffs_get_obj_length(obj); -diff --git a/yaffs/direct/yportenv.h b/yaffs/direct/yportenv.h -index f693c16..ac0a737 100644 ---- a/yaffs/direct/yportenv.h -+++ b/yaffs/direct/yportenv.h -@@ -45,7 +45,7 @@ - #endif - - #ifndef loff_t -- typedef unsigned long loff_t; -+ typedef long loff_t; - #endif - - #ifndef dev_t -@@ -66,7 +66,7 @@ - #elif defined (__GNUC__) && !defined(__CC_ARM) - - #ifndef loff_t -- typedef unsigned long loff_t; -+ typedef long loff_t; - #endif - - #ifndef dev_t -diff --git a/yaffs/yaffs_guts.c b/yaffs/yaffs_guts.c -index d72aa5b..08db8c8 100644 ---- a/yaffs/yaffs_guts.c -+++ b/yaffs/yaffs_guts.c -@@ -280,7 +280,7 @@ static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk, - - static inline int yaffs_hash_fn(int n) - { -- n = abs(n); -+ n = n > 0 ? n : -n; - return n % YAFFS_NOBJECT_BUCKETS; - } - -@@ -319,9 +319,9 @@ static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk) - int retval = YAFFS_OK; - u8 *data = yaffs_get_temp_buffer(dev); - struct yaffs_ext_tags tags; -- int result; - -- result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags); -+ -+ yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags); - - if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR) - retval = YAFFS_FAIL; -@@ -347,9 +347,9 @@ static int yaffs_verify_chunk_written(struct yaffs_dev *dev, - int retval = YAFFS_OK; - struct yaffs_ext_tags temp_tags; - u8 *buffer = yaffs_get_temp_buffer(dev); -- int result; - -- result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags); -+ -+ yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags); - if (memcmp(buffer, data, dev->data_bytes_per_chunk) || - temp_tags.obj_id != tags->obj_id || - temp_tags.chunk_id != tags->chunk_id || -@@ -1478,7 +1478,7 @@ static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev) - struct yaffs_obj *the_obj; - int usage; - int i; -- int pushout; -+ /* int pushout; */ - - if (dev->param.n_caches < 1) - return NULL; -@@ -1499,7 +1499,7 @@ static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev) - the_obj = dev->cache[0].object; - usage = -1; - cache = NULL; -- pushout = -1; -+ /* pushout = -1; */ - - for (i = 0; i < dev->param.n_caches; i++) { - if (dev->cache[i].object && -@@ -1509,7 +1509,7 @@ static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev) - usage = dev->cache[i].last_use; - the_obj = dev->cache[i].object; - cache = &dev->cache[i]; -- pushout = i; -+ /* pushout = i; */ - } - } - -@@ -3167,7 +3167,7 @@ static void yaffs_check_obj_details_loaded(struct yaffs_obj *in) - struct yaffs_obj_hdr *oh; - struct yaffs_dev *dev; - struct yaffs_ext_tags tags; -- int result; -+ - int alloc_failed = 0; - - if (!in || !in->lazy_loaded || in->hdr_chunk < 1) -@@ -3177,7 +3177,7 @@ static void yaffs_check_obj_details_loaded(struct yaffs_obj *in) - in->lazy_loaded = 0; - buf = yaffs_get_temp_buffer(dev); - -- result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags); -+ yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags); - oh = (struct yaffs_obj_hdr *)buf; - - in->yst_mode = oh->yst_mode; -@@ -3189,6 +3189,7 @@ static void yaffs_check_obj_details_loaded(struct yaffs_obj *in) - yaffs_clone_str(oh->alias); - if (!in->variant.symlink_variant.alias) - alloc_failed = 1; /* Not returned */ -+ alloc_failed = alloc_failed; - } - yaffs_release_temp_buffer(dev, buf); - } -@@ -3274,7 +3275,7 @@ int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force, - struct yaffs_dev *dev = in->my_dev; - int prev_chunk_id; - int ret_val = 0; -- int result = 0; -+ - int new_chunk_id; - struct yaffs_ext_tags new_tags; - struct yaffs_ext_tags old_tags; -@@ -3297,7 +3298,7 @@ int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force, - prev_chunk_id = in->hdr_chunk; - - if (prev_chunk_id > 0) { -- result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id, -+ yaffs_rd_chunk_tags_nand(dev, prev_chunk_id, - buffer, &old_tags); - - yaffs_verify_oh(in, oh, &old_tags, 0); -@@ -3921,7 +3922,7 @@ int yaffs_del_obj(struct yaffs_obj *obj) - list_del_init(&obj->variant.dir_variant.dirty); - } - return yaffs_del_dir(obj); -- break; -+ //break; - case YAFFS_OBJECT_TYPE_SYMLINK: - ret_val = yaffs_del_symlink(obj); - break; -@@ -3993,17 +3994,17 @@ static int yaffs_unlink_worker(struct yaffs_obj *obj) - switch (obj->variant_type) { - case YAFFS_OBJECT_TYPE_FILE: - return yaffs_del_file(obj); -- break; -+ //break; - case YAFFS_OBJECT_TYPE_DIRECTORY: - list_del_init(&obj->variant.dir_variant.dirty); - return yaffs_del_dir(obj); -- break; -+ //break; - case YAFFS_OBJECT_TYPE_SYMLINK: - return yaffs_del_symlink(obj); -- break; -+ //break; - case YAFFS_OBJECT_TYPE_SPECIAL: - return yaffs_generic_obj_del(obj); -- break; -+ //break; - case YAFFS_OBJECT_TYPE_HARDLINK: - case YAFFS_OBJECT_TYPE_UNKNOWN: - default: -@@ -4421,7 +4422,7 @@ int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size) - } else if (obj->short_name[0]) { - strcpy(name, obj->short_name); - } else if (obj->hdr_chunk > 0) { -- int result; -+ - u8 *buffer = yaffs_get_temp_buffer(obj->my_dev); - - struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer; -@@ -4429,7 +4430,7 @@ int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size) - memset(buffer, 0, obj->my_dev->data_bytes_per_chunk); - - if (obj->hdr_chunk > 0) { -- result = yaffs_rd_chunk_tags_nand(obj->my_dev, -+ yaffs_rd_chunk_tags_nand(obj->my_dev, - obj->hdr_chunk, - buffer, NULL); - } -@@ -4490,16 +4491,16 @@ unsigned yaffs_get_obj_type(struct yaffs_obj *obj) - switch (obj->variant_type) { - case YAFFS_OBJECT_TYPE_FILE: - return DT_REG; -- break; -+ //break; - case YAFFS_OBJECT_TYPE_DIRECTORY: - return DT_DIR; -- break; -+ //break; - case YAFFS_OBJECT_TYPE_SYMLINK: - return DT_LNK; -- break; -+ //break; - case YAFFS_OBJECT_TYPE_HARDLINK: - return DT_REG; -- break; -+ //break; - case YAFFS_OBJECT_TYPE_SPECIAL: - if (S_ISFIFO(obj->yst_mode)) - return DT_FIFO; -@@ -4510,10 +4511,10 @@ unsigned yaffs_get_obj_type(struct yaffs_obj *obj) - if (S_ISSOCK(obj->yst_mode)) - return DT_SOCK; - return DT_REG; -- break; -+ //break; - default: - return DT_REG; -- break; -+ //break; - } - } - -@@ -4837,7 +4838,7 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) - dev->n_erased_blocks = 0; - dev->n_free_chunks = 0; - dev->alloc_block = -1; -- dev->alloc_page = -1; -+ dev->alloc_page = (u32)(-1); - dev->n_deleted_files = 0; - dev->n_unlinked_files = 0; - dev->n_bg_deletions = 0; -diff --git a/yaffs/yaffs_packedtags2.c b/yaffs/yaffs_packedtags2.c -index 820bc41..9306623 100644 ---- a/yaffs/yaffs_packedtags2.c -+++ b/yaffs/yaffs_packedtags2.c -@@ -35,7 +35,7 @@ - - /* Also, the top 4 bits of the object Id are set to the object type. */ - #define EXTRA_OBJECT_TYPE_SHIFT (28) --#define EXTRA_OBJECT_TYPE_MASK ((0x0f) << EXTRA_OBJECT_TYPE_SHIFT) -+#define EXTRA_OBJECT_TYPE_MASK ((0x0fUL) << EXTRA_OBJECT_TYPE_SHIFT) - - static void yaffs_dump_packed_tags2_tags_only( - const struct yaffs_packed_tags2_tags_only *ptt) -@@ -129,7 +129,7 @@ void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t, - t->extra_parent_id = ptt->chunk_id & (~(ALL_EXTRA_FLAGS)); - t->extra_is_shrink = ptt->chunk_id & EXTRA_SHRINK_FLAG ? 1 : 0; - t->extra_shadows = ptt->chunk_id & EXTRA_SHADOWS_FLAG ? 1 : 0; -- t->extra_obj_type = ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT; -+ t->extra_obj_type = (enum yaffs_obj_type)(ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT); - t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK; - - if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK) -diff --git a/yaffs/yaffs_verify.c b/yaffs/yaffs_verify.c -index b3e540d..1758125 100644 ---- a/yaffs/yaffs_verify.c -+++ b/yaffs/yaffs_verify.c -@@ -223,7 +223,7 @@ void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh, - void yaffs_verify_file(struct yaffs_obj *obj) - { - int required_depth; -- int actual_depth; -+ //int actual_depth; - u32 last_chunk; - u32 the_chunk; - u32 x; -@@ -252,7 +252,7 @@ void yaffs_verify_file(struct yaffs_obj *obj) - required_depth++; - } - -- actual_depth = obj->variant.file_variant.top_level; -+ //actual_depth = obj->variant.file_variant.top_level; - - /* Check that the chunks in the tnode tree are all correct. - * We do this by scanning through the tnode tree and -diff --git a/yaffs/yaffs_yaffs1.c b/yaffs/yaffs_yaffs1.c -index da6a40f..8e7f4b2 100644 ---- a/yaffs/yaffs_yaffs1.c -+++ b/yaffs/yaffs_yaffs1.c -@@ -23,7 +23,7 @@ int yaffs1_scan(struct yaffs_dev *dev) - { - struct yaffs_ext_tags tags; - int blk; -- int result; -+ //int result; - int chunk; - int c; - int deleted; -@@ -84,7 +84,7 @@ int yaffs1_scan(struct yaffs_dev *dev) - cond_resched(); - - bi = yaffs_get_block_info(dev, blk); -- state = bi->block_state; -+ state = (enum yaffs_block_state)(bi->block_state); - - deleted = 0; - -@@ -95,7 +95,7 @@ int yaffs1_scan(struct yaffs_dev *dev) - /* Read the tags and decide what to do */ - chunk = blk * dev->param.chunks_per_block + c; - -- result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, -+ yaffs_rd_chunk_tags_nand(dev, chunk, NULL, - &tags); - - /* Let's have a good look at this chunk... */ -@@ -181,7 +181,7 @@ int yaffs1_scan(struct yaffs_dev *dev) - yaffs_set_chunk_bit(dev, blk, c); - bi->pages_in_use++; - -- result = yaffs_rd_chunk_tags_nand(dev, chunk, -+ yaffs_rd_chunk_tags_nand(dev, chunk, - chunk_data, - NULL); - -diff --git a/yaffs/yaffs_yaffs2.c b/yaffs/yaffs_yaffs2.c -index 5761e96..69acd20 100644 ---- a/yaffs/yaffs_yaffs2.c -+++ b/yaffs/yaffs_yaffs2.c -@@ -946,7 +946,7 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev, - int is_shrink; - int is_unlinked; - struct yaffs_ext_tags tags; -- int result; -+ //int result; - int alloc_failed = 0; - int chunk = blk * dev->param.chunks_per_block + chunk_in_block; - struct yaffs_file_var *file_var; -@@ -954,12 +954,12 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev, - struct yaffs_symlink_var *sl_var; - - if (summary_available) { -- result = yaffs_summary_fetch(dev, &tags, chunk_in_block); -+ yaffs_summary_fetch(dev, &tags, chunk_in_block); - tags.seq_number = bi->seq_number; - } - - if (!summary_available || tags.obj_id == 0) { -- result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags); -+ yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags); - dev->tags_used++; - } else { - dev->summary_used++; -@@ -1114,7 +1114,7 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev, - * invalid data until needed. - */ - -- result = yaffs_rd_chunk_tags_nand(dev, -+ yaffs_rd_chunk_tags_nand(dev, - chunk, - chunk_data, - NULL); -@@ -1349,7 +1349,7 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev) - int n_to_scan = 0; - enum yaffs_block_state state; - int c; -- int deleted; -+ //int deleted; - LIST_HEAD(hard_list); - struct yaffs_block_info *bi; - u32 seq_number; -@@ -1467,7 +1467,7 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev) - /* get the block to scan in the correct order */ - blk = block_index[block_iter].block; - bi = yaffs_get_block_info(dev, blk); -- deleted = 0; -+ //deleted = 0; - - summary_available = yaffs_summary_read(dev, dev->sum_tags, blk); - -diff --git a/yaffs_osglue.c b/yaffs_osglue.c -index 2c34f99..4f2e5db 100644 ---- a/yaffs_osglue.c -+++ b/yaffs_osglue.c -@@ -58,7 +58,6 @@ void yaffsfs_LockInit(void) - #else - - static rt_mutex_t mutex = RT_NULL; --static rt_sem_t sem = RT_NULL; - void yaffsfs_Lock(void) - { - rt_mutex_take(mutex, RT_WAITING_FOREVER); diff --git a/components/dfs/filesystems/yaffs2/readme.txt b/components/dfs/filesystems/yaffs2/readme.txt deleted file mode 100644 index e6b8d3b976..0000000000 --- a/components/dfs/filesystems/yaffs2/readme.txt +++ /dev/null @@ -1,46 +0,0 @@ -how to use yaffs under rt-thread. - -There are three steps. -1. get yaffs tarball from official repo. - the repo of official repo is here. - http://www.aleph1.co.uk/gitweb?p=yaffs2.git;a=summary - then you should find the 2011-6-28's snapshot, download the tarball - http://www.aleph1.co.uk/gitweb?p=yaffs2.git;a=snapshot;h=2df51cdb98e799c4d10b4cc7dd7e8858aa79e7d8;sf=tgz - - decompress the yaffs.tar.gz to rt-thread\components\dfs\filesystems\yaffs2\yaffs - -2. patch yaffs.diff - open an terminal. - (1) on windows - open cmd command prompt, then use [cd] command to come current path - for example - F:\Project\svn\rt-thread\components\dfs\filesystems\yaffs2> - then type command - patch -p1 < yaffs.diff - - you will get some log information as followings - F:\Project\svn\rt-thread\components\dfs\filesystems\yaffs2>patch -p1 < yaffs.diff - patching file `dfs_yaffs2.c' - patching file `yaffs/direct/yaffs_list.h' - patching file `yaffs/direct/yaffs_nandif.c' - patching file `yaffs/direct/ydirectenv.h' - patching file `yaffs/direct/yportenv.h' - patching file `yaffs_nandcfg.c' - patching file `yaffs_osglue.c' - - now you can delete yaffs.diff - there is another patch file, as the name shows, it is to fix compile - warning, so it is not necessary. If you want, you can use the - following command. - patch -p1 < fixwarning.diff - - (2) on linux - Help yourself. Since you have use linux as your os, I believe in you. - -3.add nand driver and compile - In order to use yaffs, you should provide a nand driver which is needed - by yaffs. There is an example file in rt-thread\components\dfs\filesystems\uffs\nand. - you should modify yaffs_nandcfg.c according to your nand driver. - then you can use scons or IDE like MDK or IAR to compile. - - enjoy ! diff --git a/components/dfs/filesystems/yaffs2/yaffs.diff b/components/dfs/filesystems/yaffs2/yaffs.diff deleted file mode 100644 index e487ac6a6d..0000000000 --- a/components/dfs/filesystems/yaffs2/yaffs.diff +++ /dev/null @@ -1,1014 +0,0 @@ -diff --git a/dfs_yaffs2.c b/dfs_yaffs2.c -new file mode 100644 -index 0000000..4b056ae ---- /dev/null -+++ b/dfs_yaffs2.c -@@ -0,0 +1,405 @@ -+/* -+ * File : rtthread.h -+ * This file is part of RT-Thread RTOS -+ * COPYRIGHT (C) 2006-2011, RT-Thread Development Team -+ * -+ * The license and distribution terms for this file may be -+ * found in the file LICENSE in this distribution or at -+ * http://www.rt-thread.org/license/LICENSE. -+ * -+ * Change Logs: -+ * Date Author Notes -+ * 2011-10-22 prife the first version -+ * 2012-04-14 prife use mtd device interface -+*/ -+#include <rtthread.h> -+#include <rtdevice.h> -+ -+#include "yaffsfs.h" -+#include "yaffs_nandif.h" -+ -+#include <dfs_fs.h> -+#include <dfs_def.h> -+ -+#define YAFFS_FILE_PATH_MAX 256 /* the longest file path */ -+ -+#define NAND_DEVICE_PART_MAX 4 /* the max partitions on a nand deivce*/ -+struct device_part -+{ -+ struct rt_mtd_nand_device *dev; -+ ynandif_Geometry g; -+}; -+static struct device_part nand_part[NAND_DEVICE_PART_MAX] = {0}; -+ -+extern int yaffs_start(const char * mountpath, ynandif_Geometry * g); -+extern void yaffs_config(ynandif_Geometry * g, struct rt_mtd_nand_device * dev); -+ -+static int dfs_yaffs_mount(struct dfs_filesystem* fs, -+ unsigned long rwflag, -+ const void* data) -+{ -+ unsigned index; -+ ynandif_Geometry *g; -+ -+ /*1. find a empty entry in partition table */ -+ for (index = 0; index < NAND_DEVICE_PART_MAX ; index ++) -+ { -+ if (nand_part[index].dev == RT_NULL) -+ break; -+ } -+ if (index == NAND_DEVICE_PART_MAX) -+ return -DFS_STATUS_ENOSPC; -+ -+ /*2. fill nand_part*/ -+ nand_part[index].dev = RT_MTD_NAND_DEVICE(fs->dev_id); -+ yaffs_config(&nand_part[index].g, RT_MTD_NAND_DEVICE(fs->dev_id)); -+ -+ /*3. start up yaffs2 */ -+ yaffs_start(fs->path, &nand_part[index].g); -+ if (yaffs_mount(fs->path) < 0) -+ { -+ return yaffsfs_GetLastError(); -+ } -+ return 0; -+} -+ -+static int dfs_yaffs_unmount(struct dfs_filesystem* fs) -+{ -+ unsigned index; -+ if (yaffs_unmount(fs->path) < 0) -+ return yaffsfs_GetLastError(); -+ -+ /* find device index, then umount it */ -+ for (index = 0; index < NAND_DEVICE_PART_MAX; index++) -+ { -+ if (nand_part[index].dev == RT_MTD_NAND_DEVICE(fs->dev_id)) -+ { -+ nand_part[index].dev = RT_NULL; -+ return DFS_STATUS_OK; -+ } -+ } -+ return -DFS_STATUS_ENOENT; -+} -+ -+static int dfs_yaffs_mkfs(const char* device_name) -+{ -+ /* just erase all blocks on this nand partition */ -+ return -DFS_STATUS_ENOSYS; -+} -+ -+static int dfs_yaffs_statfs(struct dfs_filesystem* fs, -+ struct statfs *buf) -+{ -+ struct rt_mtd_nand_device * mtd = RT_MTD_NAND_DEVICE(fs->dev_id); -+ -+ RT_ASSERT(mtd != RT_NULL); -+ -+ buf->f_bsize = mtd->page_size; -+ buf->f_blocks = (mtd->block_size)/(mtd->page_size)* -+ (mtd->block_start - mtd->block_end + 1); -+ buf->f_bfree = yaffs_freespace(fs->path) / mtd->page_size; -+ -+ return 0; -+} -+ -+static int dfs_yaffs_open(struct dfs_fd* file) -+{ -+ int fd; -+ int oflag; -+ int result; -+ -+ oflag = file->flags; -+ if (oflag & DFS_O_DIRECTORY) /* operations about dir */ -+ { -+ yaffs_DIR * dir; -+ if (oflag & DFS_O_CREAT) /* create a dir*/ -+ { -+ result = yaffs_mkdir(file->path, 0x777); /* the second args not supported by rtt */ -+ if (result < 0) -+ return yaffsfs_GetLastError(); -+ } -+ /* open dir */ -+ dir = yaffs_opendir(file->path); -+ if (dir == RT_NULL) -+ return yaffsfs_GetLastError(); -+ /* save this pointer,will used by dfs_yaffs_getdents*/ -+ file->data = dir; -+ return 0; -+ } -+ /* regular file operations */ -+ fd = yaffs_open(file->path, oflag, S_IREAD | S_IWRITE); -+ if (fd < 0) -+ return yaffsfs_GetLastError(); -+ -+ /* save this pointer, it will be used when calling read()��write(), -+ flush(), lessk(), and will be free when calling close()*/ -+ file->data = (void *)fd; -+ file->pos = yaffs_lseek(fd,0,SEEK_CUR); -+ file->size = yaffs_lseek(fd,0,SEEK_END); -+ yaffs_lseek(fd, file->pos, SEEK_SET); -+ -+ if (oflag & DFS_O_APPEND) -+ { -+ file->pos = file->size; -+ file->size = yaffs_lseek(fd,0,SEEK_END); -+ } -+ return 0; -+} -+ -+static int dfs_yaffs_close(struct dfs_fd* file) -+{ -+ int oflag; -+ int fd; -+ -+ oflag = file->flags; -+ if (oflag & DFS_O_DIRECTORY) /* operations about dir */ -+ { -+ if (yaffs_closedir((yaffs_DIR *)(file->data)) < 0) -+ return yaffsfs_GetLastError(); -+ return 0; -+ } -+ /* regular file operations */ -+ fd = (int)(file->data); -+ -+ if (yaffs_close(fd) == 0) -+ return 0; -+ -+ /* release memory */ -+ return yaffsfs_GetLastError(); -+} -+ -+static int dfs_yaffs_ioctl(struct dfs_fd* file, int cmd, void* args) -+{ -+ return -DFS_STATUS_ENOSYS; -+} -+ -+static int dfs_yaffs_read(struct dfs_fd* file, void* buf, rt_size_t len) -+{ -+ int fd; -+ int char_read; -+ -+ fd = (int)(file->data); -+ char_read = yaffs_read(fd, buf, len); -+ if (char_read < 0) -+ return yaffsfs_GetLastError(); -+ -+ /* update position */ -+ file->pos = yaffs_lseek(fd, 0, SEEK_CUR); -+ return char_read; -+} -+ -+static int dfs_yaffs_write(struct dfs_fd* file, -+ const void* buf, -+ rt_size_t len) -+{ -+ int fd; -+ int char_write; -+ -+ fd = (int)(file->data); -+ -+ char_write = yaffs_write(fd, buf, len); -+ if (char_write < 0) -+ return yaffsfs_GetLastError(); -+ -+ /* update position */ -+ file->pos = yaffs_lseek(fd, 0, SEEK_CUR); -+ return char_write; -+} -+ -+static int dfs_yaffs_flush(struct dfs_fd* file) -+{ -+ int fd; -+ int result; -+ -+ fd = (int)(file->data); -+ -+ result = yaffs_flush(fd); -+ if (result < 0) -+ return yaffsfs_GetLastError(); -+ return 0; -+} -+ -+static int dfs_yaffs_lseek(struct dfs_fd* file, -+ rt_off_t offset) -+{ -+ int fd; -+ int result; -+ -+ fd = (int)(file->data); -+ -+ /* set offset as current offset */ -+ result = yaffs_lseek(fd, offset, SEEK_SET); -+ if (result < 0) -+ return yaffsfs_GetLastError(); -+ return result; -+} -+ -+/* return the size of struct dirent*/ -+static int dfs_yaffs_getdents(struct dfs_fd* file, -+ struct dirent* dirp, -+ rt_uint32_t count) -+{ -+ rt_uint32_t index; -+ char * file_path; -+ struct dirent* d; -+ yaffs_DIR* dir; -+ struct yaffs_dirent * yaffs_d; -+ -+ dir = (yaffs_DIR*)(file->data); -+ RT_ASSERT(dir != RT_NULL); -+ -+ /* make integer count, usually count is 1 */ -+ count = (count / sizeof(struct dirent)) * sizeof(struct dirent); -+ if (count == 0) return -DFS_STATUS_EINVAL; -+ -+ /* allocate file name */ -+ file_path = rt_malloc(YAFFS_FILE_PATH_MAX); -+ if (file_path == RT_NULL) -+ return -DFS_STATUS_ENOMEM; -+ -+ index = 0; -+ /* usually, the while loop should only be looped only once! */ -+ while (1) -+ { -+ struct yaffs_stat s; -+ -+ d = dirp + index; -+ -+ yaffs_d = yaffs_readdir(dir); -+ if (yaffs_d == RT_NULL) -+ { -+ if (yaffsfs_GetLastError() == EBADF) -+ return -DFS_STATUS_EBADF; -+ -+ rt_free(file_path); -+ return -1; /* a general error */ -+ } -+ -+ rt_snprintf(file_path, YAFFS_FILE_PATH_MAX, "%s/%s", file->path, yaffs_d->d_name); -+ -+ yaffs_lstat(file_path, &s); -+ switch(s.st_mode & S_IFMT) -+ { -+ case S_IFREG: d->d_type = DFS_DT_REG; break; -+ -+ case S_IFDIR: d->d_type = DFS_DT_DIR; break; -+ -+ case S_IFLNK: -+ default: d->d_type = DFS_DT_UNKNOWN; break; -+ } -+ -+ /* write the rest feilds of struct dirent* dirp */ -+ d->d_namlen = rt_strlen(yaffs_d->d_name); -+ d->d_reclen = (rt_uint16_t)sizeof(struct dirent); -+ rt_strncpy(d->d_name, yaffs_d->d_name, rt_strlen(yaffs_d->d_name) + 1); -+ -+ index ++; -+ if (index * sizeof(struct dirent) >= count) -+ break; -+ } -+ -+ /* free file name buf */ -+ rt_free(file_path); -+ -+ if (index == 0) -+ return yaffsfs_GetLastError(); -+ -+ return index * sizeof(struct dirent); -+} -+ -+static int dfs_yaffs_unlink(struct dfs_filesystem* fs, const char* path) -+{ -+ int result; -+ struct yaffs_stat s; -+ -+ /* judge file type, dir is to be delete by yaffs_rmdir, others by yaffs_unlink */ -+ if (yaffs_lstat(path, &s) < 0) -+ { -+ return yaffsfs_GetLastError(); -+ } -+ -+ switch(s.st_mode & S_IFMT) -+ { -+ case S_IFREG: -+ result = yaffs_unlink(path); -+ break; -+ case S_IFDIR: -+ result = yaffs_rmdir(path); -+ break; -+ default: -+ /* unknown file type */ -+ return -1; -+ } -+ if (result < 0) -+ return yaffsfs_GetLastError(); -+ return 0; -+} -+ -+static int dfs_yaffs_rename(struct dfs_filesystem* fs, -+ const char* oldpath, -+ const char* newpath) -+{ -+ int result; -+ -+ result = yaffs_rename(oldpath, newpath); -+ -+ if (result < 0) -+ return yaffsfs_GetLastError(); -+ return 0; -+} -+ -+static int dfs_yaffs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) -+{ -+ int result; -+ struct yaffs_stat s; -+ struct rt_mtd_nand_device * mtd; -+ -+ result = yaffs_stat(path, &s); -+ if (result < 0) -+ return yaffsfs_GetLastError(); -+ -+ /* convert to dfs stat structure */ -+ st->st_dev = 0; -+ st->st_mode = s.st_mode; -+ st->st_size = s.st_size; -+ st->st_mtime = s.yst_mtime; -+ -+ mtd = RT_MTD_NAND_DEVICE(fs->dev_id); -+ st->st_blksize = mtd->page_size; -+ -+ return 0; -+} -+ -+static const struct dfs_filesystem_operation dfs_yaffs_ops = -+{ -+ "yaffs2", /* file system type: yaffs2 */ -+#if RTTHREAD_VERSION >= 10100 -+ DFS_FS_FLAG_FULLPATH, -+#else -+#error "yaffs2 can only work with rtthread whose version should >= 1.01\n" -+#endif -+ dfs_yaffs_mount, -+ dfs_yaffs_unmount, -+ dfs_yaffs_mkfs, -+ dfs_yaffs_statfs, -+ -+ dfs_yaffs_open, -+ dfs_yaffs_close, -+ dfs_yaffs_ioctl, -+ dfs_yaffs_read, -+ dfs_yaffs_write, -+ dfs_yaffs_flush, -+ dfs_yaffs_lseek, -+ dfs_yaffs_getdents, -+ dfs_yaffs_unlink, -+ dfs_yaffs_stat, -+ dfs_yaffs_rename, -+}; -+ -+int dfs_yaffs_init(void) -+{ -+ /* register fatfs file system */ -+ dfs_register(&dfs_yaffs_ops); -+ -+ return 0; -+} -diff --git a/yaffs/direct/yaffs_list.h b/yaffs/direct/yaffs_list.h -index f1c5254..17044f5 100644 ---- a/yaffs/direct/yaffs_list.h -+++ b/yaffs/direct/yaffs_list.h -@@ -39,7 +39,7 @@ struct list_head { - - /* Initialise a static list */ - #define LIST_HEAD(name) \ --struct list_head name = { &(name), &(name)} -+static struct list_head name = { &(name), &(name)} - - - -diff --git a/yaffs/direct/yaffs_nandif.c b/yaffs/direct/yaffs_nandif.c -index b93b55a..3fae80e 100644 ---- a/yaffs/direct/yaffs_nandif.c -+++ b/yaffs/direct/yaffs_nandif.c -@@ -18,7 +18,6 @@ - #include "yaffs_nandif.h" - #include "yaffs_packedtags2.h" - --#include "yramsim.h" - - #include "yaffs_trace.h" - #include "yaffsfs.h" -@@ -216,8 +215,8 @@ struct yaffs_dev * - yaffs_add_dev_from_geometry(const YCHAR *name, - const ynandif_Geometry *geometry) - { -- YCHAR *clonedName = malloc(sizeof(YCHAR) * (strnlen(name,YAFFS_MAX_NAME_LENGTH)+1)); -- struct yaffs_dev *dev = malloc(sizeof(struct yaffs_dev)); -+ YCHAR *clonedName = kmalloc(sizeof(YCHAR) * (strnlen(name,YAFFS_MAX_NAME_LENGTH)+1), GFP_NOFS); -+ struct yaffs_dev *dev = kmalloc(sizeof(struct yaffs_dev), GFP_NOFS); - - if(dev && clonedName){ - memset(dev,0,sizeof(struct yaffs_dev)); -@@ -242,16 +241,17 @@ struct yaffs_dev * - dev->param.n_reserved_blocks = 5; - dev->driver_context = (void *)geometry; - -+ /* save rt_mtd_nand_device * dev*/ -+ dev->os_context = (void *)(geometry->privateData); - yaffs_add_device(dev); - - return dev; - } - - if(dev) -- free(dev); -+ kfree(dev); - if(clonedName) -- free(clonedName); -+ kfree(clonedName); - - return NULL; - } -- -diff --git a/yaffs/direct/ydirectenv.h b/yaffs/direct/ydirectenv.h -index 7860b84..5add3ec 100644 ---- a/yaffs/direct/ydirectenv.h -+++ b/yaffs/direct/ydirectenv.h -@@ -22,16 +22,13 @@ - - // Direct interface - --#include "stdlib.h" --#include "stdio.h" -+#include <rtthread.h> - #include "string.h" -+ - #include "yaffs_osglue.h" - #include "yaffs_hweight.h" - --#include "assert.h" --#define BUG() assert(0) --//#define BUG() do { *((int *)0) =1;} while(0) -- -+#define BUG() RT_ASSERT(0) - - #define YCHAR char - #define YUCHAR unsigned char -@@ -47,30 +44,24 @@ void yaffs_qsort(void *aa, size_t n, size_t es, - - #define YAFFS_PATH_DIVIDERS "/" - --#ifdef NO_inline --#define inline --#else --#define inline __inline__ --#endif -- --#define kmalloc(x,flags) yaffsfs_malloc(x) -+#define GFP_NOFS 0 -+#define kmalloc(x, y) yaffsfs_malloc(x) - #define kfree(x) yaffsfs_free(x) - #define vmalloc(x) yaffsfs_malloc(x) --#define vfree(x) yaffsfs_free(x) -+#define vfree(x) yaffsfs_free(x) -+#define printf rt_kprintf - - #define cond_resched() do {} while(0) - - #define yaffs_trace(msk, fmt, ...) do { \ - if(yaffs_trace_mask & (msk)) \ -- printf("yaffs: " fmt "\n", ##__VA_ARGS__); \ -+ rt_kprintf("yaffs: " fmt "\n", ##__VA_ARGS__); \ - } while(0) - - - #define YAFFS_LOSTNFOUND_NAME "lost+found" - #define YAFFS_LOSTNFOUND_PREFIX "obj" - --#include "yaffscfg.h" -- - #define Y_CURRENT_TIME yaffsfs_CurrentTime() - #define Y_TIME_CONVERT(x) x - -@@ -79,8 +70,6 @@ void yaffs_qsort(void *aa, size_t n, size_t es, - - #include "yaffs_list.h" - --#include "yaffsfs.h" -- - #endif - - -diff --git a/yaffs/direct/yportenv.h b/yaffs/direct/yportenv.h -index 939cd3a..f693c16 100644 ---- a/yaffs/direct/yportenv.h -+++ b/yaffs/direct/yportenv.h -@@ -17,36 +17,146 @@ - #ifndef __YPORTENV_H__ - #define __YPORTENV_H__ - -+#include <dfs_posix.h> -+ -+#define CONFIG_YAFFS_DIRECT -+#define CONFIG_YAFFS_PROVIDE_DEFS -+#define CONFIG_YAFFSFS_PROVIDE_VALUES -+ -+#ifdef NO_inline -+ #define inline -+#else -+ #ifdef __CC_ARM -+ #define inline __inline -+ #elif defined (__GNUC__) /* GNU GCC Compiler */ -+ #define inline __inline -+ #else -+ #error "Please set a macro inline with a valid args" -+ #endif -+#endif -+ -+#if defined(__CC_ARM) -+ #ifdef RT_USING_NEWLIB -+ #error "error: when use armcc, please Don't USE NEWLIB!!!" -+ #endif -+ -+ #ifndef off_t -+ typedef long off_t; -+ #endif -+ -+ #ifndef loff_t -+ typedef unsigned long loff_t; -+ #endif -+ -+ #ifndef dev_t -+ typedef long dev_t; -+ #endif -+ -+ #ifndef mode_t -+ typedef int mode_t; -+ #endif -+ -+ /* just like strlen(3), but cap the number of bytes we count */ -+ rt_inline size_t strnlen(const char *s, size_t max) { -+ register const char *p; -+ for(p = s; *p && max--; ++p); -+ return(p - s); -+ } -+ -+#elif defined (__GNUC__) && !defined(__CC_ARM) -+ -+ #ifndef loff_t -+ typedef unsigned long loff_t; -+ #endif -+ -+ #ifndef dev_t -+ typedef long dev_t; -+ #endif -+#endif - - /* Definition of types */ - typedef unsigned char u8; - typedef unsigned short u16; - typedef unsigned u32; -- -- --#ifndef WIN32 --#include <sys/stat.h> --#endif -- -+ -+#ifndef S_ISDIR -+ #ifndef _DEV_T_DEFINED -+ typedef unsigned int _dev_t; /* device code */ -+ #if !__STDC__ -+ /* Non-ANSI name for compatibility */ -+ typedef unsigned int dev_t; -+ #endif -+ -+ #define _DEV_T_DEFINED -+ #endif -+ -+ #define __S_IFMT 0170000 /* These bits determine file type. */ -+ /* File types. */ -+ #define __S_IFDIR 0040000 /* Directory. */ -+ #define __S_IFCHR 0020000 /* Character device. */ -+ #define __S_IFBLK 0060000 /* Block device. */ -+ #define __S_IFREG 0100000 /* Regular file. */ -+ #define __S_IFIFO 0010000 /* FIFO. */ -+ #define __S_IFLNK 0120000 /* Symbolic link. */ -+ #define __S_IFSOCK 0140000 /* Socket. */ -+ -+ /* Protection bits. */ -+ #define __S_ISUID 04000 /* Set user ID on execution. */ -+ #define __S_ISGID 02000 /* Set group ID on execution. */ -+ #define __S_ISVTX 01000 /* Save swapped text after use (sticky). */ -+ #define __S_IREAD 0400 /* Read by owner. */ -+ #define __S_IWRITE 0200 /* Write by owner. */ -+ #define __S_IEXEC 0100 /* Execute by owner. */ -+ #define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask)) -+ -+ #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR) -+ #define S_ISCHR(mode) __S_ISTYPE((mode), __S_IFCHR) -+ #define S_ISBLK(mode) __S_ISTYPE((mode), __S_IFBLK) -+ #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG) -+ #define S_ISSOCK(mode) __S_ISTYPE((mode), __S_IFSOCK) -+ #define S_ISFIFO(mode) __S_ISTYPE((mode), __S_IFSOCK) -+ #define S_IRUSR __S_IREAD // Read by owner. -+ #define S_IWUSR __S_IWRITE // Write by owner. -+ #define S_IXUSR __S_IEXEC // Execute by owner. -+ #define S_IREAD S_IRUSR -+ #define S_IWRITE S_IWUSR -+ #define S_IEXEC S_IXUSR -+ #define S_IFDIR __S_IFDIR -+#endif - - #ifdef CONFIG_YAFFS_PROVIDE_DEFS - /* File types */ - -+#ifndef DT_UNKNOWN -+ #define DT_UNKNOWN 0 -+#endif -+#ifndef DT_FIFO -+ #define DT_FIFO 1 -+#endif - --#define DT_UNKNOWN 0 --#define DT_FIFO 1 --#define DT_CHR 2 --#define DT_DIR 4 --#define DT_BLK 6 --#define DT_REG 8 --#define DT_LNK 10 --#define DT_SOCK 12 --#define DT_WHT 14 -- -+#ifndef DT_CHR -+ #define DT_CHR 2 -+#endif - --#ifndef WIN32 --#include <sys/stat.h> -+#ifndef DT_DIR -+ #define DT_DIR 4 -+#endif -+#ifndef DT_BLK -+ #define DT_BLK 6 -+#endif -+#ifndef DT_REG -+ #define DT_REG 8 - #endif -+#ifndef DT_LNK -+ #define DT_LNK 10 -+#endif -+#ifndef DT_SOCK -+ #define DT_SOCK 12 -+#endif -+#ifndef DT_WHT -+ #define DT_WHT 14 -+#endif -+ - - /* - * Attribute flags. -@@ -75,7 +185,6 @@ struct iattr { - #endif - - -- - #if defined CONFIG_YAFFS_WINCE - - #include "ywinceenv.h" -@@ -267,9 +376,17 @@ struct iattr { - #endif - - #else --#include <errno.h> --#include <sys/stat.h> --#include <fcntl.h> -+//#include <errno.h> -+//#include <sys/stat.h> -+//#include <fcntl.h> -+#ifndef XATTR_CREATE -+#define XATTR_CREATE 1 -+#endif -+ -+#ifndef XATTR_REPLACE -+#define XATTR_REPLACE 2 -+#endif -+ - #endif - - #endif -diff --git a/yaffs_nandcfg.c b/yaffs_nandcfg.c -new file mode 100644 -index 0000000..099fa38 ---- /dev/null -+++ b/yaffs_nandcfg.c -@@ -0,0 +1,149 @@ -+#include <rtthread.h> -+#include <rtdevice.h> -+#include <dfs_fs.h> -+#include <dfs_def.h> -+ -+#include "yaffsfs.h" -+#include "yaffs_nandif.h" -+#include "yaffs_trace.h" -+ -+/* -+ * RT-Thread Device Interface for yaffs -+ */ -+static int nand_init(struct yaffs_dev *dev) -+{ -+ return YAFFS_OK; -+} -+ -+static int nand_deinit(struct yaffs_dev *dev) -+{ -+ return YAFFS_OK; -+} -+ -+/* if block is good, return YAFFS_OK, else return YAFFS_FAIL */ -+static int nand_checkblock(struct yaffs_dev *dev, unsigned block) -+{ -+ rt_err_t res; -+ res = rt_mtd_nand_check_block(RT_MTD_NAND_DEVICE(dev->os_context), block); -+ return res == RT_EOK ? YAFFS_OK : YAFFS_FAIL; -+} -+static int nand_markbadblk(struct yaffs_dev *dev, unsigned block) -+{ -+ rt_err_t res; -+ res = rt_mtd_nand_mark_badblock(RT_MTD_NAND_DEVICE(dev->os_context), block); -+ return res == RT_EOK ? YAFFS_OK : YAFFS_FAIL; -+} -+ -+static int nand_eraseblock(struct yaffs_dev *dev, unsigned block) -+{ -+ int res; -+ res = rt_mtd_nand_erase_block(RT_MTD_NAND_DEVICE(dev->os_context), block); -+ return res == RT_EOK ? YAFFS_OK : YAFFS_FAIL; -+} -+ -+static int nand_readpage( -+ struct yaffs_dev *dev, -+ unsigned page, -+ unsigned char *data, unsigned data_len, /* page data */ -+ unsigned char *spare, unsigned spare_len,/* page spare */ -+ int *ecc_status) -+{ -+ int res; -+ unsigned char spare_buf[64]; -+ -+ res = rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->os_context), -+ page, data, data_len, spare_buf, spare_len + 5); -+ rt_memcpy(spare, spare_buf + 5, spare_len); -+ if (res == 0) -+ *ecc_status = 0; -+ else if (res == -1) -+ *ecc_status = 1; -+ else -+ *ecc_status = -1; -+ -+ return YAFFS_OK; -+} -+ -+static int nand_writepage( -+ struct yaffs_dev *dev, -+ unsigned page, -+ const unsigned char *data, unsigned data_len, /* page data */ -+ const unsigned char *spare, unsigned spare_len) /* page spare */ -+{ -+ int res; -+ unsigned char spare_buf[64]; //not use malloc, this can be faster -+ rt_memset(spare_buf, 0xFF, sizeof(spare_buf)); -+ rt_memcpy(spare_buf+5, spare, spare_len); -+ -+ res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->os_context), -+ page, data, data_len, spare_buf, spare_len + 5); -+ if (res != RT_EOK) -+ goto __error; -+ -+ return YAFFS_OK; -+ -+__error: -+ return YAFFS_FAIL; -+} -+ -+ -+/* -+ * yaffs2 configuration -+ */ -+#define CONFIG_YAFFS_ECC_MODE 1 //1 use ecc, 0 no ecc -+#define CONFIG_YAFFS_INBAND_TAGS 0 //1 use in band tags, 0-no in band tags -+#define CONFIG_YAFFS_USE_YAFFS2 1 //1 yaffs2, 0-yaffs1 -+ -+void yaffs_config(ynandif_Geometry * g, struct rt_mtd_nand_device * dev) -+{ -+ rt_memset(g,0,sizeof(ynandif_Geometry)); -+ -+ g->start_block = dev->block_start; -+ g->end_block = dev->block_end; -+ g->dataSize = dev->page_size; -+ g->spareSize = dev->oob_size; -+ g->pagesPerBlock = dev->block_size / dev->page_size; -+ -+ g->hasECC = CONFIG_YAFFS_ECC_MODE; -+ g->inband_tags = CONFIG_YAFFS_INBAND_TAGS; -+ g->useYaffs2 = CONFIG_YAFFS_USE_YAFFS2; -+ -+ g->privateData = dev;//will be copy to dev->os_context. -+ -+ g->initialise = nand_init; -+ g->deinitialise = nand_deinit; -+ g->readChunk = nand_readpage; -+ g->writeChunk = nand_writepage; -+ g->eraseBlock = nand_eraseblock; -+ g->checkBlockOk = nand_checkblock; -+ g->markBlockBad = nand_markbadblk; -+} -+ -+static struct yaffs_dev *ynand_CreatePart(const YCHAR *name, ynandif_Geometry * g) -+{ -+ return yaffs_add_dev_from_geometry(name, g); -+} -+ -+/* configuration for yaffs's log */ -+unsigned yaffs_trace_mask = -+ -+ YAFFS_TRACE_SCAN | -+ YAFFS_TRACE_GC | -+ YAFFS_TRACE_ERASE | -+ YAFFS_TRACE_ERROR | -+ YAFFS_TRACE_TRACING | -+ YAFFS_TRACE_ALLOCATE | -+ YAFFS_TRACE_BAD_BLOCKS | -+ YAFFS_TRACE_VERIFY | -+ -+ 0; -+ -+int yaffs_start(const char * mount_point, ynandif_Geometry * g) -+{ -+ // Stuff to configure YAFFS -+ // Stuff to initialise anything special (eg lock semaphore). -+ yaffsfs_OSInitialisation(); -+ ynand_CreatePart(mount_point, g); -+ -+ return 0; -+} -diff --git a/yaffs_osglue.c b/yaffs_osglue.c -new file mode 100644 -index 0000000..2c34f99 ---- /dev/null -+++ b/yaffs_osglue.c -@@ -0,0 +1,118 @@ -+/* -+ * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. -+ * -+ * Copyright (C) 2002-2011 Aleph One Ltd. -+ * for Toby Churchill Ltd and Brightstar Engineering -+ * -+ * Created by Charles Manning <[email protected]> -+ * -+ * This program is free software; you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License version 2 as -+ * published by the Free Software Foundation. -+ */ -+ -+ -+#include "yaffscfg.h" -+#include "yaffs_guts.h" -+#include "yaffsfs.h" -+#include "yaffs_trace.h" -+#include <rtthread.h> -+ -+//#include <errno.h> -+ -+ -+static int yaffsfs_lastError; -+ -+void yaffsfs_SetError(int err) -+{ -+ //Do whatever to set error -+ yaffsfs_lastError = err; -+} -+ -+int yaffsfs_GetLastError(void) -+{ -+ return yaffsfs_lastError; -+} -+ -+ -+#ifdef CONFIG_YAFFS_USE_PTHREADS -+#include <pthread.h> -+static pthread_mutex_t mutex1; -+ -+ -+void yaffsfs_Lock(void) -+{ -+ pthread_mutex_lock( &mutex1 ); -+} -+ -+void yaffsfs_Unlock(void) -+{ -+ pthread_mutex_unlock( &mutex1 ); -+} -+ -+void yaffsfs_LockInit(void) -+{ -+ pthread_mutex_init( &mutex1, NULL); -+} -+ -+#else -+ -+static rt_mutex_t mutex = RT_NULL; -+static rt_sem_t sem = RT_NULL; -+void yaffsfs_Lock(void) -+{ -+ rt_mutex_take(mutex, RT_WAITING_FOREVER); -+} -+ -+void yaffsfs_Unlock(void) -+{ -+ rt_mutex_release(mutex); -+} -+ -+void yaffsfs_LockInit(void) -+{ -+ mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO); -+ if (mutex == RT_NULL) -+ { -+ yaffs_trace(YAFFS_TRACE_ERROR, -+ "**>> yaffs error :yaffs_LockInit can't get a mutex!"); -+ } -+ -+} -+#endif -+ -+u32 yaffsfs_CurrentTime(void) -+{ -+ return 0; -+} -+ -+ -+static int yaffs_kill_alloc = 0; -+static size_t total_malloced = 0; -+static size_t malloc_limit = 0 & 6000000; -+ -+void *yaffsfs_malloc(size_t size) -+{ -+ void * this; -+ if(yaffs_kill_alloc) -+ return NULL; -+ if(malloc_limit && malloc_limit <(total_malloced + size) ) -+ return NULL; -+ -+ this = rt_malloc(size); -+ if(this) -+ total_malloced += size; -+ return this; -+} -+ -+void yaffsfs_free(void *ptr) -+{ -+ rt_free(ptr); -+} -+ -+void yaffsfs_OSInitialisation(void) -+{ -+ yaffsfs_LockInit(); -+} -+ -+ diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index f17458bd69..eb32a4b411 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -1,21 +1,7 @@ /* - * File : dfs.h - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -116,7 +102,7 @@ struct dfs_fd *fd_get(int fd); void fd_put(struct dfs_fd *fd); int fd_is_open(const char *pathname); -struct dfs_fdtable* dfs_fdtable_get(void); +struct dfs_fdtable *dfs_fdtable_get(void); #ifdef __cplusplus } diff --git a/components/dfs/include/dfs_file.h b/components/dfs/include/dfs_file.h index 5dc3946899..2dfacd653e 100644 --- a/components/dfs/include/dfs_file.h +++ b/components/dfs/include/dfs_file.h @@ -1,21 +1,7 @@ /* - * File : dfs_file.h - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -58,6 +44,7 @@ struct dfs_fd char *path; /* Name (below mount point) */ int ref_count; /* Descriptor reference count */ + struct dfs_filesystem *fs; const struct dfs_file_ops *fops; uint32_t flags; /* Descriptor flags */ @@ -79,6 +66,10 @@ int dfs_file_lseek(struct dfs_fd *fd, off_t offset); int dfs_file_stat(const char *path, struct stat *buf); int dfs_file_rename(const char *oldpath, const char *newpath); +int dfs_file_ftruncate(struct dfs_fd *fd, off_t length); + +/* 0x5254 is just a magic number to make these relatively unique ("RT") */ +#define RT_FIOFTRUNCATE 0x52540000U #ifdef __cplusplus } diff --git a/components/dfs/include/dfs_fs.h b/components/dfs/include/dfs_fs.h index 38d8766ee1..a35a513044 100644 --- a/components/dfs/include/dfs_fs.h +++ b/components/dfs/include/dfs_fs.h @@ -1,27 +1,13 @@ /* - * File : dfs_fs.h - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2005-02-22 Bernard The first version. */ - + #ifndef __DFS_FS_H__ #define __DFS_FS_H__ @@ -74,7 +60,7 @@ struct dfs_partition uint8_t type; /* file system type */ off_t offset; /* partition start offset */ size_t size; /* partition size */ - rt_sem_t lock; + rt_sem_t lock; }; /* mount table */ @@ -89,11 +75,11 @@ struct dfs_mount_tbl int dfs_register(const struct dfs_filesystem_ops *ops); struct dfs_filesystem *dfs_filesystem_lookup(const char *path); -const char* dfs_filesystem_get_mounted_path(struct rt_device* device); +const char *dfs_filesystem_get_mounted_path(struct rt_device *device); int dfs_filesystem_get_partition(struct dfs_partition *part, - uint8_t *buf, - uint32_t pindex); + uint8_t *buf, + uint32_t pindex); int dfs_mount(const char *device_name, const char *path, @@ -104,6 +90,8 @@ int dfs_unmount(const char *specialfile); int dfs_mkfs(const char *fs_name, const char *device_name); int dfs_statfs(const char *path, struct statfs *buffer); +int dfs_mount_device(rt_device_t dev); +int dfs_unmount_device(rt_device_t dev); #ifdef __cplusplus } diff --git a/components/dfs/include/dfs_poll.h b/components/dfs/include/dfs_poll.h index cfdf74fbe4..d9597a4364 100644 --- a/components/dfs/include/dfs_poll.h +++ b/components/dfs/include/dfs_poll.h @@ -1,3 +1,12 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ + #ifndef DFS_POLL_H__ #define DFS_POLL_H__ @@ -6,6 +15,7 @@ #ifdef RT_USING_POSIX #include <sys/time.h> /* for struct timeval */ +#if !defined(POLLIN) && !defined(POLLOUT) #define POLLIN (0x01) #define POLLRDNORM (0x01) #define POLLRDBAND (0x01) @@ -19,8 +29,6 @@ #define POLLHUP (0x08) #define POLLNVAL (0x10) -#define POLLMASK_DEFAULT (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM) - typedef unsigned int nfds_t; struct pollfd @@ -29,9 +37,10 @@ struct pollfd short events; short revents; }; +#endif /* !defined(POLLIN) && !defined(POLLOUT) */ +#define POLLMASK_DEFAULT (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM) int poll(struct pollfd *fds, nfds_t nfds, int timeout); -#endif - -#endif +#endif /* RT_USING_POSIX */ +#endif /* DFS_POLL_H__ */ diff --git a/components/dfs/include/dfs_posix.h b/components/dfs/include/dfs_posix.h index b751f1ed17..87d5f0771e 100644 --- a/components/dfs/include/dfs_posix.h +++ b/components/dfs/include/dfs_posix.h @@ -1,21 +1,7 @@ /* - * File : dfs_posix.h - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -50,18 +36,20 @@ struct dirent *readdir(DIR *d); long telldir(DIR *d); void seekdir(DIR *d, off_t offset); void rewinddir(DIR *d); -int closedir(DIR* d); +int closedir(DIR *d); /* file api*/ int open(const char *file, int flags, ...); int close(int d); -#ifdef RT_USING_NEWLIB + +#if defined(RT_USING_NEWLIB) && defined(_EXFUN) _READ_WRITE_RETURN_TYPE _EXFUN(read, (int __fd, void *__buf, size_t __nbyte)); _READ_WRITE_RETURN_TYPE _EXFUN(write, (int __fd, const void *__buf, size_t __nbyte)); #else int read(int fd, void *buf, size_t len); int write(int fd, const void *buf, size_t len); #endif + off_t lseek(int fd, off_t offset, int whence); int rename(const char *from, const char *to); int unlink(const char *pathname); @@ -70,6 +58,7 @@ int fstat(int fildes, struct stat *buf); int fsync(int fildes); int fcntl(int fildes, int cmd, ...); int ioctl(int fildes, int cmd, ...); +int ftruncate(int fd, off_t length); /* directory api*/ int rmdir(const char *path); diff --git a/components/dfs/include/dfs_private.h b/components/dfs/include/dfs_private.h index 43be0db00e..fccd260ac0 100644 --- a/components/dfs/include/dfs_private.h +++ b/components/dfs/include/dfs_private.h @@ -1,12 +1,19 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ + #ifndef DFS_PRIVATE_H__ #define DFS_PRIVATE_H__ #include <dfs.h> -// #define DBG_ENABLE -#define DBG_SECTION_NAME "DFS" -#define DBG_COLOR -#define DBG_LEVEL DBG_LOG +#define DBG_TAG "DFS" +#define DBG_LVL DBG_INFO #include <rtdbg.h> #define NO_WORKING_DIR "system does not support working directory\n" diff --git a/components/dfs/include/dfs_select.h b/components/dfs/include/dfs_select.h index 1c51b1df38..3ac5817874 100644 --- a/components/dfs/include/dfs_select.h +++ b/components/dfs/include/dfs_select.h @@ -1,3 +1,12 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ + #ifndef DFS_SELECT_H__ #define DFS_SELECT_H__ diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 5a98883549..b20de13988 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -1,21 +1,7 @@ /* - * File : dfs.c - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -32,6 +18,10 @@ #include <lwp.h> #endif +#if defined(RT_USING_DFS_DEVFS) && defined(RT_USING_POSIX) +#include <libc.h> +#endif + /* Global variables */ const struct dfs_filesystem_ops *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX]; @@ -133,7 +123,7 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) int idx; /* find an empty fd entry */ - for (idx = startfd; idx < fdt->maxfd; idx++) + for (idx = startfd; idx < (int)fdt->maxfd; idx++) { if (fdt->fds[idx] == RT_NULL) break; @@ -149,9 +139,9 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) /* increase the number of FD with 4 step length */ cnt = fdt->maxfd + 4; - cnt = cnt > DFS_FD_MAX? DFS_FD_MAX : cnt; + cnt = cnt > DFS_FD_MAX ? DFS_FD_MAX : cnt; - fds = rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *)); + fds = (struct dfs_fd **)rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *)); if (fds == NULL) goto __exit; /* return fdt->maxfd */ /* clean the new allocated fds */ @@ -165,9 +155,9 @@ static int fd_alloc(struct dfs_fdtable *fdt, int startfd) } /* allocate 'struct dfs_fd' */ - if (idx < fdt->maxfd &&fdt->fds[idx] == RT_NULL) + if (idx < (int)fdt->maxfd && fdt->fds[idx] == RT_NULL) { - fdt->fds[idx] = rt_calloc(1, sizeof(struct dfs_fd)); + fdt->fds[idx] = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd)); if (fdt->fds[idx] == RT_NULL) idx = fdt->maxfd; } @@ -199,7 +189,7 @@ int fd_new(void) if (idx == fdt->maxfd) { idx = -(1 + DFS_FD_OFFSET); - dbg_log(DBG_ERROR, "DFS fd new is failed! Could not found an empty fd entry."); + LOG_E("DFS fd new is failed! Could not found an empty fd entry."); goto __result; } @@ -226,16 +216,21 @@ struct dfs_fd *fd_get(int fd) struct dfs_fd *d; struct dfs_fdtable *fdt; +#if defined(RT_USING_DFS_DEVFS) && defined(RT_USING_POSIX) + if ((0 <= fd) && (fd <= 2)) + fd = libc_stdio_get_console(); +#endif + fdt = dfs_fdtable_get(); fd = fd - DFS_FD_OFFSET; - if (fd < 0 || fd >= fdt->maxfd) + if (fd < 0 || fd >= (int)fdt->maxfd) return NULL; dfs_lock(); d = fdt->fds[fd]; /* check dfs_fd valid or not */ - if (d->magic != DFS_FD_MAGIC) + if ((d == NULL) || (d->magic != DFS_FD_MAGIC)) { dfs_unlock(); return NULL; @@ -268,7 +263,7 @@ void fd_put(struct dfs_fd *fd) struct dfs_fdtable *fdt; fdt = dfs_fdtable_get(); - for (index = 0; index < fdt->maxfd; index ++) + for (index = 0; index < (int)fdt->maxfd; index ++) { if (fdt->fds[index] == fd) { @@ -325,7 +320,7 @@ int fd_is_open(const char *pathname) fd = fdt->fds[index]; if (fd == NULL || fd->fops == NULL || fd->path == NULL) continue; - if (fd->fops == fs->ops->fops && strcmp(fd->path, mountpath) == 0) + if (fd->fs == fs && strcmp(fd->path, mountpath) == 0) { /* found file in file descriptor table */ rt_free(fullpath); @@ -398,14 +393,14 @@ char *dfs_normalize_path(const char *directory, const char *filename) if (filename[0] != '/') /* it's a absolute path, use it directly */ { - fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2); + fullpath = (char *)rt_malloc(strlen(directory) + strlen(filename) + 2); if (fullpath == NULL) return NULL; /* join path and file name */ rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, - "%s/%s", directory, filename); + "%s/%s", directory, filename); } else { @@ -504,7 +499,7 @@ RTM_EXPORT(dfs_normalize_path); /** * This function will get the file descriptor table of current process. */ -struct dfs_fdtable* dfs_fdtable_get(void) +struct dfs_fdtable *dfs_fdtable_get(void) { struct dfs_fdtable *fdt; #ifdef RT_USING_LWP @@ -528,21 +523,21 @@ int list_fd(void) { int index; struct dfs_fdtable *fd_table; - + fd_table = dfs_fdtable_get(); if (!fd_table) return -1; rt_enter_critical(); - + rt_kprintf("fd type ref magic path\n"); rt_kprintf("-- ------ --- ----- ------\n"); - for (index = 0; index < fd_table->maxfd; index ++) + for (index = 0; index < (int)fd_table->maxfd; index ++) { struct dfs_fd *fd = fd_table->fds[index]; if (fd && fd->fops) { - rt_kprintf("%2d ", index); + rt_kprintf("%2d ", index + DFS_FD_OFFSET); if (fd->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir"); else if (fd->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file"); else if (fd->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket"); diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index 76cf419c0a..91d39597f0 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -1,27 +1,14 @@ /* - * File : dfs_file.c - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2005-02-22 Bernard The first version. * 2011-12-08 Bernard Merges rename patch from iamcacy. * 2015-05-27 Bernard Fix the fd clear issue. + * 2019-01-24 Bernard Remove file repeatedly open check. */ #include <dfs.h> @@ -60,15 +47,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) return -ENOMEM; } - dbg_log(DBG_LOG, "open file:%s\n", fullpath); - - /* Check whether file is already open */ - if (fd_is_open(fullpath) == 0) - { - rt_free(fullpath); /* release path */ - - return -EBUSY; - } + LOG_D("open file:%s", fullpath); /* find filesystem */ fs = dfs_filesystem_lookup(fullpath); @@ -79,8 +58,9 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) return -ENOENT; } - dbg_log(DBG_LOG, "open in filesystem:%s\n", fs->ops->name); - fd->fops = fs->ops->fops; /* set file ops */ + LOG_D("open in filesystem:%s", fs->ops->name); + fd->fs = fs; /* set file system */ + fd->fops = fs->ops->fops; /* set file ops */ /* initialize the fd item */ fd->type = FT_REGULAR; @@ -96,7 +76,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) else fd->path = rt_strdup(dfs_subdir(fs->path, fullpath)); rt_free(fullpath); - dbg_log(DBG_LOG, "Actual file path: %s\n", fd->path); + LOG_D("Actual file path: %s", fd->path); } else { @@ -119,7 +99,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) rt_free(fd->path); fd->path = NULL; - dbg_log(DBG_ERROR, "open failed\n"); + LOG_D("%s open failed", fullpath); return result; } @@ -131,7 +111,7 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) fd->flags |= DFS_F_DIRECTORY; } - dbg_log(DBG_INFO, "open successful\n"); + LOG_D("open successful"); return 0; } @@ -185,7 +165,7 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) return fd->flags; /* return flags */ case F_SETFL: { - int flags = (int)args; + int flags = (int)(rt_base_t)args; int mask = O_NONBLOCK | O_APPEND; flags &= mask; @@ -389,8 +369,7 @@ int dfs_file_stat(const char *path, struct stat *buf) if ((fs = dfs_filesystem_lookup(fullpath)) == NULL) { - dbg_log(DBG_ERROR, - "can't find mounted filesystem on this path:%s\n", fullpath); + LOG_E("can't find mounted filesystem on this path:%s", fullpath); rt_free(fullpath); return -ENOENT; @@ -419,8 +398,7 @@ int dfs_file_stat(const char *path, struct stat *buf) if (fs->ops->stat == NULL) { rt_free(fullpath); - dbg_log(DBG_ERROR, - "the filesystem didn't implement this function\n"); + LOG_E("the filesystem didn't implement this function"); return -ENOSYS; } @@ -502,6 +480,35 @@ __exit: return result; } +/** + * this function is will cause the regular file referenced by fd + * to be truncated to a size of precisely length bytes. + * + * @param fd the file descriptor. + * @param length the length to be truncated. + * + * @return the status of truncated. + */ +int dfs_file_ftruncate(struct dfs_fd *fd, off_t length) +{ + int result; + + /* fd is null or not a regular file system fd, or length is invalid */ + if (fd == NULL || fd->type != FT_REGULAR || length < 0) + return -EINVAL; + + if (fd->fops->ioctl == NULL) + return -ENOSYS; + + result = fd->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length); + + /* update current size */ + if (result == 0) + fd->size = length; + + return result; +} + #ifdef RT_USING_FINSH #include <finsh.h> @@ -556,14 +563,15 @@ void ls(const char *pathname) } else { - rt_kprintf("%-25lu\n", stat.st_size); + rt_kprintf("%-25lu\n", (unsigned long)stat.st_size); } } else rt_kprintf("BAD file: %s\n", dirent.d_name); rt_free(fullpath); } - }while(length > 0); + } + while (length > 0); dfs_file_close(&fd); } @@ -585,7 +593,7 @@ void rm(const char *filename) } FINSH_FUNCTION_EXPORT(rm, remove files or directories); -void cat(const char* filename) +void cat(const char *filename) { uint32_t length; char buffer[81]; @@ -600,12 +608,13 @@ void cat(const char* filename) do { memset(buffer, 0, sizeof(buffer)); - length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 ); + length = dfs_file_read(&fd, buffer, sizeof(buffer) - 1); if (length > 0) { rt_kprintf("%s", buffer); } - }while (length > 0); + } + while (length > 0); dfs_file_close(&fd); } @@ -618,7 +627,7 @@ static void copyfile(const char *src, const char *dst) rt_uint8_t *block_ptr; rt_int32_t read_bytes; - block_ptr = rt_malloc(BUF_SZ); + block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ); if (block_ptr == NULL) { rt_kprintf("out of memory\n"); @@ -658,7 +667,8 @@ static void copyfile(const char *src, const char *dst) break; } } - } while (read_bytes > 0); + } + while (read_bytes > 0); dfs_file_close(&src_fd); dfs_file_close(&fd); @@ -666,7 +676,7 @@ static void copyfile(const char *src, const char *dst) } extern int mkdir(const char *path, mode_t mode); -static void copydir(const char * src, const char * dst) +static void copydir(const char *src, const char *dst) { struct dirent dirent; struct stat stat; @@ -685,8 +695,8 @@ static void copydir(const char * src, const char * dst) length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent)); if (length > 0) { - char * src_entry_full = NULL; - char * dst_entry_full = NULL; + char *src_entry_full = NULL; + char *dst_entry_full = NULL; if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0) continue; @@ -723,15 +733,16 @@ static void copydir(const char * src, const char * dst) rt_free(src_entry_full); rt_free(dst_entry_full); } - }while(length > 0); + } + while (length > 0); dfs_file_close(&cpfd); } static const char *_get_path_lastname(const char *path) { - char * ptr; - if ((ptr = strrchr(path, '/')) == NULL) + char *ptr; + if ((ptr = (char *)strrchr(path, '/')) == NULL) return path; /* skip the '/' then return */ @@ -787,7 +798,7 @@ void copy(const char *src, const char *dst) { if (flag & FLAG_DST_IS_DIR) { - char * fdst; + char *fdst; fdst = dfs_normalize_path(dst, _get_path_lastname(src)); if (fdst == NULL) { @@ -806,7 +817,7 @@ void copy(const char *src, const char *dst) { if (flag & FLAG_DST_IS_DIR) { - char * fdst; + char *fdst; fdst = dfs_normalize_path(dst, _get_path_lastname(src)); if (fdst == NULL) { diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index 66740d647e..476e47fc06 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -1,21 +1,7 @@ /* - * File : dfs_fs.c - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -52,7 +38,7 @@ int dfs_register(const struct dfs_filesystem_ops *ops) dfs_lock(); /* check if this filesystem was already registered */ for (iter = &filesystem_operation_table[0]; - iter < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; iter ++) + iter < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; iter ++) { /* find out an empty filesystem type entry */ if (*iter == NULL) @@ -69,7 +55,7 @@ int dfs_register(const struct dfs_filesystem_ops *ops) if (empty == NULL) { rt_set_errno(-ENOSPC); - dbg_log(DBG_ERROR, "There is no space to register this file system (%d).\n", ops->name); + LOG_E("There is no space to register this file system (%s).", ops->name); ret = -1; } else if (ret == RT_EOK) @@ -134,9 +120,9 @@ struct dfs_filesystem *dfs_filesystem_lookup(const char *path) * * @return the mounted path or NULL if none device mounted. */ -const char* dfs_filesystem_get_mounted_path(struct rt_device* device) +const char *dfs_filesystem_get_mounted_path(struct rt_device *device) { - const char* path = NULL; + const char *path = NULL; struct dfs_filesystem *iter; dfs_lock(); @@ -168,8 +154,8 @@ const char* dfs_filesystem_get_mounted_path(struct rt_device* device) * @return RT_EOK on successful or -RT_ERROR on failed. */ int dfs_filesystem_get_partition(struct dfs_partition *part, - uint8_t *buf, - uint32_t pindex) + uint8_t *buf, + uint32_t pindex) { #define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */ #define DPT_ITEM_SIZE 16 /* partition item size */ @@ -187,28 +173,28 @@ int dfs_filesystem_get_partition(struct dfs_partition *part, return -EIO; /* get partition type */ - type = *(dpt+4); + type = *(dpt + 4); if (type == 0) return -EIO; /* set partition information * size is the number of 512-Byte */ part->type = type; - part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24; - part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24; + part->offset = *(dpt + 8) | *(dpt + 9) << 8 | *(dpt + 10) << 16 | *(dpt + 11) << 24; + part->size = *(dpt + 12) | *(dpt + 13) << 8 | *(dpt + 14) << 16 | *(dpt + 15) << 24; rt_kprintf("found part[%d], begin: %d, size: ", - pindex, part->offset*512); - if ((part->size>>11) == 0) - rt_kprintf("%d%s",part->size>>1,"KB\n"); /* KB */ + pindex, part->offset * 512); + if ((part->size >> 11) == 0) + rt_kprintf("%d%s", part->size >> 1, "KB\n"); /* KB */ else { unsigned int part_size; part_size = part->size >> 11; /* MB */ - if ((part_size>>10) == 0) - rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\n"); + if ((part_size >> 10) == 0) + rt_kprintf("%d.%d%s", part_size, (part->size >> 1) & 0x3FF, "MB\n"); else - rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\n"); + rt_kprintf("%d.%d%s", part_size >> 10, part_size & 0x3FF, "GB\n"); } return RT_EOK; @@ -254,7 +240,7 @@ int dfs_mount(const char *device_name, dfs_lock(); for (ops = &filesystem_operation_table[0]; - ops < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; ops++) + ops < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; ops++) if ((*ops != NULL) && (strcmp((*ops)->name, filesystemtype) == 0)) break; @@ -318,7 +304,7 @@ int dfs_mount(const char *device_name, if ((fs == NULL) && (iter == &filesystem_table[DFS_FILESYSTEMS_MAX])) { rt_set_errno(-ENOSPC); - dbg_log(DBG_ERROR, "There is no space to mount this file system (%s).\n", filesystemtype); + LOG_E("There is no space to mount this file system (%s).", filesystemtype); goto err1; } @@ -451,14 +437,14 @@ int dfs_mkfs(const char *fs_name, const char *device_name) if (dev_id == NULL) { rt_set_errno(-ENODEV); - dbg_log(DBG_ERROR, "Device (%s) was not found\n", device_name); + LOG_E("Device (%s) was not found", device_name); return -1; } /* lock file system */ dfs_lock(); /* find the file system operations */ - for (index = 0; index <= DFS_FILESYSTEM_TYPES_MAX; index ++) + for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index ++) { if (filesystem_operation_table[index] != NULL && strcmp(filesystem_operation_table[index]->name, fs_name) == 0) @@ -466,13 +452,13 @@ int dfs_mkfs(const char *fs_name, const char *device_name) } dfs_unlock(); - if (index <= DFS_FILESYSTEM_TYPES_MAX) + if (index < DFS_FILESYSTEM_TYPES_MAX) { /* find file system operation */ const struct dfs_filesystem_ops *ops = filesystem_operation_table[index]; if (ops->mkfs == NULL) { - dbg_log(DBG_ERROR, "The file system (%s) mkfs function was not implement\n", fs_name); + LOG_E("The file system (%s) mkfs function was not implement", fs_name); rt_set_errno(-ENOSYS); return -1; } @@ -480,7 +466,7 @@ int dfs_mkfs(const char *fs_name, const char *device_name) return ops->mkfs(dev_id); } - dbg_log(DBG_ERROR, "File system (%s) was not found.\n", fs_name); + LOG_E("File system (%s) was not found.", fs_name); return -1; } @@ -517,13 +503,13 @@ int dfs_mount_table(void) if (mount_table[index].path == NULL) break; if (dfs_mount(mount_table[index].device_name, - mount_table[index].path, - mount_table[index].filesystemtype, - mount_table[index].rwflag, - mount_table[index].data) != 0) + mount_table[index].path, + mount_table[index].filesystemtype, + mount_table[index].rwflag, + mount_table[index].data) != 0) { - rt_kprintf("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype, - mount_table[index].path); + LOG_E("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype, + mount_table[index].path); return -RT_ERROR; } @@ -532,6 +518,91 @@ int dfs_mount_table(void) return 0; } INIT_ENV_EXPORT(dfs_mount_table); + +int dfs_mount_device(rt_device_t dev) +{ + int index = 0; + + if(dev == RT_NULL) { + rt_kprintf("the device is NULL to be mounted.\n"); + return -RT_ERROR; + } + + while (1) + { + if (mount_table[index].path == NULL) break; + + if(strcmp(mount_table[index].device_name, dev->parent.name) == 0) { + if (dfs_mount(mount_table[index].device_name, + mount_table[index].path, + mount_table[index].filesystemtype, + mount_table[index].rwflag, + mount_table[index].data) != 0) + { + LOG_E("mount fs[%s] device[%s] to %s failed.\n", mount_table[index].filesystemtype, dev->parent.name, + mount_table[index].path); + return -RT_ERROR; + } else { + LOG_D("mount fs[%s] device[%s] to %s ok.\n", mount_table[index].filesystemtype, dev->parent.name, + mount_table[index].path); + return RT_EOK; + } + } + + index ++; + } + + rt_kprintf("can't find device:%s to be mounted.\n", dev->parent.name); + return -RT_ERROR; +} + +int dfs_unmount_device(rt_device_t dev) +{ + struct dfs_filesystem *iter; + struct dfs_filesystem *fs = NULL; + + /* lock filesystem */ + dfs_lock(); + + for (iter = &filesystem_table[0]; + iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++) + { + /* check if the PATH is mounted */ + if ((iter->dev_id->parent.name != NULL) + && (strcmp(iter->dev_id->parent.name, dev->parent.name) == 0)) + { + fs = iter; + break; + } + } + + if (fs == NULL || + fs->ops->unmount == NULL || + fs->ops->unmount(fs) < 0) + { + goto err1; + } + + /* close device, but do not check the status of device */ + if (fs->dev_id != NULL) + rt_device_close(fs->dev_id); + + if (fs->path != NULL) + rt_free(fs->path); + + /* clear this filesystem table entry */ + memset(fs, 0, sizeof(struct dfs_filesystem)); + + dfs_unlock(); + + return 0; + +err1: + dfs_unlock(); + + return -1; +} + #endif #ifdef RT_USING_FINSH @@ -569,7 +640,7 @@ int df(const char *path) } rt_kprintf("disk free: %d.%d %s [ %d block, %d bytes per block ]\n", - (unsigned long)cap, minor, unit_str[unit_index], buffer.f_bfree, buffer.f_bsize); + (unsigned long)cap, minor, unit_str[unit_index], buffer.f_bfree, buffer.f_bsize); return 0; } FINSH_FUNCTION_EXPORT(df, get disk free); diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index 898488a2aa..fc603df437 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -1,21 +1,7 @@ /* - * File : dfs_posix.c - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -124,7 +110,7 @@ RTM_EXPORT(close); * @return the actual read data buffer length. If the returned value is 0, it * may be reach the end of file, please check errno. */ -#ifdef RT_USING_NEWLIB +#if defined(RT_USING_NEWLIB) && defined(_EXFUN) _READ_WRITE_RETURN_TYPE _EXFUN(read, (int fd, void *buf, size_t len)) #else int read(int fd, void *buf, size_t len) @@ -168,7 +154,7 @@ RTM_EXPORT(read); * * @return the actual written data buffer length. */ -#ifdef RT_USING_NEWLIB +#if defined(RT_USING_NEWLIB) && defined(_EXFUN) _READ_WRITE_RETURN_TYPE _EXFUN(write, (int fd, const void *buf, size_t len)) #else int write(int fd, const void *buf, size_t len) @@ -279,11 +265,11 @@ RTM_EXPORT(lseek); * * note: the old and new file name must be belong to a same file system. */ -int rename(const char *old, const char *new) +int rename(const char *old_file, const char *new_file) { int result; - result = dfs_file_rename(old, new); + result = dfs_file_rename(old_file, new_file); if (result < 0) { rt_set_errno(result); @@ -437,11 +423,11 @@ int fcntl(int fildes, int cmd, ...) d = fd_get(fildes); if (d) { - void* arg; + void *arg; va_list ap; va_start(ap, cmd); - arg = va_arg(ap, void*); + arg = va_arg(ap, void *); va_end(ap); ret = dfs_file_ioctl(d, cmd, arg); @@ -473,11 +459,11 @@ RTM_EXPORT(fcntl); */ int ioctl(int fildes, int cmd, ...) { - void* arg; + void *arg; va_list ap; va_start(ap, cmd); - arg = va_arg(ap, void*); + arg = va_arg(ap, void *); va_end(ap); /* we use fcntl for this API. */ @@ -486,6 +472,52 @@ int ioctl(int fildes, int cmd, ...) RTM_EXPORT(ioctl); /** + * + * this function is a POSIX compliant version, which cause the regular file + * referenced by fd to be truncated to a size of precisely length bytes. + * @param fd the file descriptor. + * @param length the length to be truncated. + * + * @return Upon successful completion, ftruncate() shall return 0; + * otherwise, -1 shall be returned and errno set to indicate the error. + */ +int ftruncate(int fd, off_t length) +{ + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == NULL) + { + rt_set_errno(-EBADF); + + return -1; + } + + if (length < 0) + { + fd_put(d); + rt_set_errno(-EINVAL); + + return -1; + } + result = dfs_file_ftruncate(d, length); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return 0; +} +RTM_EXPORT(ftruncate); + +/** * this function is a POSIX compliant version, which will return the * information about a mounted file system. * @@ -659,8 +691,8 @@ struct dirent *readdir(DIR *d) if (d->num) { - struct dirent* dirent_ptr; - dirent_ptr = (struct dirent*)&d->buf[d->cur]; + struct dirent *dirent_ptr; + dirent_ptr = (struct dirent *)&d->buf[d->cur]; d->cur += dirent_ptr->d_reclen; } @@ -668,7 +700,7 @@ struct dirent *readdir(DIR *d) { /* get a new entry */ result = dfs_file_getdents(fd, - (struct dirent*)d->buf, + (struct dirent *)d->buf, sizeof(d->buf) - 1); if (result <= 0) { @@ -684,7 +716,7 @@ struct dirent *readdir(DIR *d) fd_put(fd); - return (struct dirent *)(d->buf+d->cur); + return (struct dirent *)(d->buf + d->cur); } RTM_EXPORT(readdir); @@ -904,9 +936,9 @@ int access(const char *path, int amode) char *getcwd(char *buf, size_t size) { #ifdef DFS_USING_WORKDIR - rt_enter_critical(); + dfs_lock(); strncpy(buf, working_directory, size); - rt_exit_critical(); + dfs_unlock(); #else rt_kprintf(NO_WORKING_DIR); #endif diff --git a/components/dfs/src/poll.c b/components/dfs/src/poll.c index f2f3fc4c76..3f304549f7 100644 --- a/components/dfs/src/poll.c +++ b/components/dfs/src/poll.c @@ -1,21 +1,7 @@ /* - * File : poll.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -33,6 +19,8 @@ #include <dfs_posix.h> #include <dfs_poll.h> +#ifdef RT_USING_POSIX + struct rt_poll_node; struct rt_poll_table @@ -54,7 +42,7 @@ static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key) { struct rt_poll_node *pn; - if (key && !((rt_uint32_t)key & wait->key)) + if (key && !((rt_ubase_t)key & wait->key)) return -1; pn = rt_container_of(wait, struct rt_poll_node, wqn); @@ -68,7 +56,7 @@ static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req) struct rt_poll_table *pt; struct rt_poll_node *node; - node = rt_malloc(sizeof(struct rt_poll_node)); + node = (struct rt_poll_node *)rt_malloc(sizeof(struct rt_poll_node)); if (node == RT_NULL) return; @@ -146,7 +134,7 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req) mask = POLLMASK_DEFAULT; if (f->fops->poll) { - req->_key = pollfd->events | POLLERR| POLLHUP; + req->_key = pollfd->events | POLLERR | POLLHUP; mask = f->fops->poll(f, req); } @@ -228,3 +216,4 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout) return num; } +#endif diff --git a/components/dfs/src/select.c b/components/dfs/src/select.c index cbbd159557..181855bc74 100644 --- a/components/dfs/src/select.c +++ b/components/dfs/src/select.c @@ -1,21 +1,7 @@ /* - * File : select.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -28,16 +14,18 @@ #include <dfs_poll.h> #include <dfs_select.h> +#ifdef RT_USING_POSIX + static void fdszero(fd_set *set, int nfds) { fd_mask *m; int n; - + /* The 'sizeof(fd_set)' of the system space may differ from user space, so the actual size of the 'fd_set' is determined here with the parameter 'nfds' */ - m = (fd_mask*)set; + m = (fd_mask *)set; for (n = 0; n < nfds; n += (sizeof(fd_mask) * 8)) { rt_memset(m, 0, sizeof(fd_mask)); @@ -192,3 +180,4 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc return ret; } +#endif |
