From 69521e2c8fa17515a625f96fae138df2c1ca8ba3 Mon Sep 17 00:00:00 2001 From: "dzzxzz@gmail.com" Date: Wed, 26 Dec 2012 01:12:13 +0000 Subject: [PATCH] fixed the coding style in components/dfs git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2530 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/dfs/include/dfs.h | 2 +- components/dfs/include/dfs_def.h | 400 +++++++------- components/dfs/include/dfs_elm.h | 2 +- components/dfs/include/dfs_file.h | 8 +- components/dfs/include/dfs_fs.h | 82 +-- components/dfs/include/dfs_init.h | 2 +- components/dfs/include/dfs_posix.h | 96 ++-- components/dfs/src/dfs.c | 423 +++++++-------- components/dfs/src/dfs_file.c | 826 +++++++++++++++-------------- components/dfs/src/dfs_fs.c | 696 ++++++++++++------------ components/dfs/src/dfs_posix.c | 800 ++++++++++++++-------------- 11 files changed, 1696 insertions(+), 1641 deletions(-) diff --git a/components/dfs/include/dfs.h b/components/dfs/include/dfs.h index 193c8b1678..cc8248699c 100644 --- a/components/dfs/include/dfs.h +++ b/components/dfs/include/dfs.h @@ -1,7 +1,7 @@ /* * File : dfs.h * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2011, RT-Thread Development Team + * COPYRIGHT (C) 2004-2012, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at diff --git a/components/dfs/include/dfs_def.h b/components/dfs/include/dfs_def.h index d467792f2f..c90a9709aa 100644 --- a/components/dfs/include/dfs_def.h +++ b/components/dfs/include/dfs_def.h @@ -1,7 +1,7 @@ /* * File : dfs_def.h * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2011, RT-Thread Development Team + * COPYRIGHT (C) 2004-2012, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -23,278 +23,278 @@ #define __D_FS__ #endif -#define DEVICE_GETGEOME 0 -#define DEVICE_GETINFO 1 -#define DEVICE_FORMAT 2 -#define DEVICE_CLEAN_SECTOR 3 +#define DEVICE_GETGEOME 0 +#define DEVICE_GETINFO 1 +#define DEVICE_FORMAT 2 +#define DEVICE_CLEAN_SECTOR 3 /* File flags */ -#define DFS_F_OPEN 0x01000000 -#define DFS_F_DIRECTORY 0x02000000 -#define DFS_F_EOF 0x04000000 -#define DFS_F_ERR 0x08000000 +#define DFS_F_OPEN 0x01000000 +#define DFS_F_DIRECTORY 0x02000000 +#define DFS_F_EOF 0x04000000 +#define DFS_F_ERR 0x08000000 #ifndef DFS_PATH_MAX -#define DFS_PATH_MAX 256 +#define DFS_PATH_MAX 256 #endif #ifndef SECTOR_SIZE -#define SECTOR_SIZE 512 +#define SECTOR_SIZE 512 #endif #ifndef DFS_FILESYSTEM_TYPES_MAX -#define DFS_FILESYSTEM_TYPES_MAX 4 +#define DFS_FILESYSTEM_TYPES_MAX 4 #endif -#define DFS_DEBUG_INFO 0x01 -#define DFS_DEBUG_WARNING 0x02 -#define DFS_DEBUG_ERROR 0x04 -#define DFS_DEBUG_LEVEL (DFS_DEBUG_INFO | DFS_DEBUG_WARNING | DFS_DEBUG_ERROR) +#define DFS_DEBUG_INFO 0x01 +#define DFS_DEBUG_WARNING 0x02 +#define DFS_DEBUG_ERROR 0x04 +#define DFS_DEBUG_LEVEL (DFS_DEBUG_INFO | DFS_DEBUG_WARNING | DFS_DEBUG_ERROR) /* #define DFS_DEBUG */ #ifdef DFS_DEBUG -#define dfs_log(level, x) do { if (level & DFS_DEBUG_LEVEL) \ - {rt_kprintf("DFS %s, %d:", __FUNCTION__, __LINE__); rt_kprintf x; \ - rt_kprintf ("\n");}}while (0) +#define dfs_log(level, x) do { if (level & DFS_DEBUG_LEVEL) \ + {rt_kprintf("DFS %s, %d:", __FUNCTION__, __LINE__); rt_kprintf x; \ + rt_kprintf ("\n");}}while (0) #else #define dfs_log(level, x) #endif #if defined(RT_USING_NEWLIB) #include -#include /* used for struct stat */ -#include /* used for struct statfs */ -#include /* used for error number */ -#include /* used for operation flags */ -#include /* used for SEEK_SET/CUR/END */ -#include /* used for struct dirent */ +#include /* used for struct stat */ +#include /* used for struct statfs */ +#include /* used for error number */ +#include /* used for operation flags */ +#include /* used for SEEK_SET/CUR/END */ +#include /* used for struct dirent */ /* Device error codes */ -#define DFS_STATUS_OK 0 /* no error */ -#define DFS_STATUS_ENOENT ENOENT /* No such file or directory */ -#define DFS_STATUS_EIO EIO /* I/O error */ -#define DFS_STATUS_ENXIO ENXIO /* No such device or address */ -#define DFS_STATUS_EBADF EBADF /* Bad file number */ -#define DFS_STATUS_EAGAIN EAGAIN /* Try again */ -#define DFS_STATUS_ENOMEM ENOMEM /* no memory */ -#define DFS_STATUS_EBUSY EBUSY /* Device or resource busy */ -#define DFS_STATUS_EEXIST EEXIST /* File exists */ -#define DFS_STATUS_EXDEV EXDEV /* Cross-device link */ -#define DFS_STATUS_ENODEV ENODEV /* No such device */ -#define DFS_STATUS_ENOTDIR ENOTDIR /* Not a directory */ -#define DFS_STATUS_EISDIR EISDIR /* Is a directory */ -#define DFS_STATUS_EINVAL EINVAL /* Invalid argument */ -#define DFS_STATUS_ENOSPC ENOSPC /* No space left on device */ -#define DFS_STATUS_EROFS EROFS /* Read-only file system */ -#define DFS_STATUS_ENOSYS ENOSYS /* Function not implemented */ -#define DFS_STATUS_ENOTEMPTY ENOTEMPTY /* Directory not empty */ +#define DFS_STATUS_OK 0 /* no error */ +#define DFS_STATUS_ENOENT ENOENT /* No such file or directory */ +#define DFS_STATUS_EIO EIO /* I/O error */ +#define DFS_STATUS_ENXIO ENXIO /* No such device or address */ +#define DFS_STATUS_EBADF EBADF /* Bad file number */ +#define DFS_STATUS_EAGAIN EAGAIN /* Try again */ +#define DFS_STATUS_ENOMEM ENOMEM /* no memory */ +#define DFS_STATUS_EBUSY EBUSY /* Device or resource busy */ +#define DFS_STATUS_EEXIST EEXIST /* File exists */ +#define DFS_STATUS_EXDEV EXDEV /* Cross-device link */ +#define DFS_STATUS_ENODEV ENODEV /* No such device */ +#define DFS_STATUS_ENOTDIR ENOTDIR /* Not a directory */ +#define DFS_STATUS_EISDIR EISDIR /* Is a directory */ +#define DFS_STATUS_EINVAL EINVAL /* Invalid argument */ +#define DFS_STATUS_ENOSPC ENOSPC /* No space left on device */ +#define DFS_STATUS_EROFS EROFS /* Read-only file system */ +#define DFS_STATUS_ENOSYS ENOSYS /* Function not implemented */ +#define DFS_STATUS_ENOTEMPTY ENOTEMPTY /* Directory not empty */ /* Operation flags */ -#define DFS_O_RDONLY O_RDONLY -#define DFS_O_WRONLY O_WRONLY -#define DFS_O_RDWR O_RDWR -#define DFS_O_ACCMODE O_ACCMODE -#define DFS_O_CREAT O_CREAT -#define DFS_O_EXCL O_EXCL -#define DFS_O_TRUNC O_TRUNC -#define DFS_O_APPEND O_APPEND -#define DFS_O_DIRECTORY O_DIRECTORY +#define DFS_O_RDONLY O_RDONLY +#define DFS_O_WRONLY O_WRONLY +#define DFS_O_RDWR O_RDWR +#define DFS_O_ACCMODE O_ACCMODE +#define DFS_O_CREAT O_CREAT +#define DFS_O_EXCL O_EXCL +#define DFS_O_TRUNC O_TRUNC +#define DFS_O_APPEND O_APPEND +#define DFS_O_DIRECTORY O_DIRECTORY /* Seek flags */ -#define DFS_SEEK_SET SEEK_SET -#define DFS_SEEK_CUR SEEK_CUR -#define DFS_SEEK_END SEEK_END +#define DFS_SEEK_SET SEEK_SET +#define DFS_SEEK_CUR SEEK_CUR +#define DFS_SEEK_END SEEK_END /* Stat codes */ -#define DFS_S_IFMT S_IFMT -#define DFS_S_IFSOCK S_IFSOCK -#define DFS_S_IFLNK S_IFLNK -#define DFS_S_IFREG S_IFREG -#define DFS_S_IFBLK S_IFBLK -#define DFS_S_IFDIR S_IFDIR -#define DFS_S_IFCHR S_IFCHR -#define DFS_S_IFIFO S_IFIFO -#define DFS_S_ISUID S_ISUID -#define DFS_S_ISGID S_ISGID -#define DFS_S_ISVTX S_ISVTX - -#define DFS_S_ISLNK(m) S_ISLNK(m) -#define DFS_S_ISREG(m) S_ISREG(m) -#define DFS_S_ISDIR(m) S_ISDIR(m) -#define DFS_S_ISCHR(m) S_ISCHR(m) -#define DFS_S_ISBLK(m) S_ISBLK(m) -#define DFS_S_ISFIFO(m) S_ISFIFO(m) -#define DFS_S_ISSOCK(m) S_ISSOCK(m) - -#define DFS_S_IRWXU S_IRWXU -#define DFS_S_IRUSR S_IRUSR -#define DFS_S_IWUSR S_IWUSR -#define DFS_S_IXUSR S_IXUSR - -#define DFS_S_IRWXG S_IRWXG -#define DFS_S_IRGRP S_IRGRP -#define DFS_S_IWGRP S_IWGRP -#define DFS_S_IXGRP S_IXGRP - -#define DFS_S_IRWXO S_IRWXO -#define DFS_S_IROTH S_IROTH -#define DFS_S_IWOTH S_IWOTH -#define DFS_S_IXOTH S_IXOTH +#define DFS_S_IFMT S_IFMT +#define DFS_S_IFSOCK S_IFSOCK +#define DFS_S_IFLNK S_IFLNK +#define DFS_S_IFREG S_IFREG +#define DFS_S_IFBLK S_IFBLK +#define DFS_S_IFDIR S_IFDIR +#define DFS_S_IFCHR S_IFCHR +#define DFS_S_IFIFO S_IFIFO +#define DFS_S_ISUID S_ISUID +#define DFS_S_ISGID S_ISGID +#define DFS_S_ISVTX S_ISVTX + +#define DFS_S_ISLNK(m) S_ISLNK(m) +#define DFS_S_ISREG(m) S_ISREG(m) +#define DFS_S_ISDIR(m) S_ISDIR(m) +#define DFS_S_ISCHR(m) S_ISCHR(m) +#define DFS_S_ISBLK(m) S_ISBLK(m) +#define DFS_S_ISFIFO(m) S_ISFIFO(m) +#define DFS_S_ISSOCK(m) S_ISSOCK(m) + +#define DFS_S_IRWXU S_IRWXU +#define DFS_S_IRUSR S_IRUSR +#define DFS_S_IWUSR S_IWUSR +#define DFS_S_IXUSR S_IXUSR + +#define DFS_S_IRWXG S_IRWXG +#define DFS_S_IRGRP S_IRGRP +#define DFS_S_IWGRP S_IWGRP +#define DFS_S_IXGRP S_IXGRP + +#define DFS_S_IRWXO S_IRWXO +#define DFS_S_IROTH S_IROTH +#define DFS_S_IWOTH S_IWOTH +#define DFS_S_IXOTH S_IXOTH /* Dirent types */ -#define DFS_DT_UNKNOWN DT_UNKNOWN -#define DFS_DT_REG DT_REG -#define DFS_DT_DIR DT_DIR +#define DFS_DT_UNKNOWN DT_UNKNOWN +#define DFS_DT_REG DT_REG +#define DFS_DT_DIR DT_DIR #else - #ifdef RT_USING_MINILIBC - #include - #else - typedef long off_t; - typedef int mode_t; - #endif + #ifdef RT_USING_MINILIBC + #include + #else + typedef long off_t; + typedef int mode_t; + #endif /* Device error codes */ -#define DFS_STATUS_OK 0 /* no error */ -#define DFS_STATUS_ENOENT 2 /* No such file or directory */ -#define DFS_STATUS_EIO 5 /* I/O error */ -#define DFS_STATUS_ENXIO 6 /* No such device or address */ -#define DFS_STATUS_EBADF 9 /* Bad file number */ -#define DFS_STATUS_EAGAIN 11 /* Try again */ -#define DFS_STATUS_ENOMEM 12 /* no memory */ -#define DFS_STATUS_EBUSY 16 /* Device or resource busy */ -#define DFS_STATUS_EEXIST 17 /* File exists */ -#define DFS_STATUS_EXDEV 18 /* Cross-device link */ -#define DFS_STATUS_ENODEV 19 /* No such device */ -#define DFS_STATUS_ENOTDIR 20 /* Not a directory */ -#define DFS_STATUS_EISDIR 21 /* Is a directory */ -#define DFS_STATUS_EINVAL 22 /* Invalid argument */ -#define DFS_STATUS_ENOSPC 28 /* No space left on device */ -#define DFS_STATUS_EROFS 30 /* Read-only file system */ -#define DFS_STATUS_ENOSYS 38 /* Function not implemented */ -#define DFS_STATUS_ENOTEMPTY 39 /* Directory not empty */ +#define DFS_STATUS_OK 0 /* no error */ +#define DFS_STATUS_ENOENT 2 /* No such file or directory */ +#define DFS_STATUS_EIO 5 /* I/O error */ +#define DFS_STATUS_ENXIO 6 /* No such device or address */ +#define DFS_STATUS_EBADF 9 /* Bad file number */ +#define DFS_STATUS_EAGAIN 11 /* Try again */ +#define DFS_STATUS_ENOMEM 12 /* no memory */ +#define DFS_STATUS_EBUSY 16 /* Device or resource busy */ +#define DFS_STATUS_EEXIST 17 /* File exists */ +#define DFS_STATUS_EXDEV 18 /* Cross-device link */ +#define DFS_STATUS_ENODEV 19 /* No such device */ +#define DFS_STATUS_ENOTDIR 20 /* Not a directory */ +#define DFS_STATUS_EISDIR 21 /* Is a directory */ +#define DFS_STATUS_EINVAL 22 /* Invalid argument */ +#define DFS_STATUS_ENOSPC 28 /* No space left on device */ +#define DFS_STATUS_EROFS 30 /* Read-only file system */ +#define DFS_STATUS_ENOSYS 38 /* Function not implemented */ +#define DFS_STATUS_ENOTEMPTY 39 /* Directory not empty */ /* Operation flags */ -#define DFS_O_RDONLY 0x0000000 -#define DFS_O_WRONLY 0x0000001 -#define DFS_O_RDWR 0x0000002 -#define DFS_O_ACCMODE 0x0000003 -#define DFS_O_CREAT 0x0000100 -#define DFS_O_EXCL 0x0000200 -#define DFS_O_TRUNC 0x0001000 -#define DFS_O_APPEND 0x0002000 -#define DFS_O_BINARY 0x0008000 -#define DFS_O_DIRECTORY 0x0200000 +#define DFS_O_RDONLY 0x0000000 +#define DFS_O_WRONLY 0x0000001 +#define DFS_O_RDWR 0x0000002 +#define DFS_O_ACCMODE 0x0000003 +#define DFS_O_CREAT 0x0000100 +#define DFS_O_EXCL 0x0000200 +#define DFS_O_TRUNC 0x0001000 +#define DFS_O_APPEND 0x0002000 +#define DFS_O_BINARY 0x0008000 +#define DFS_O_DIRECTORY 0x0200000 /* File flags */ -#define DFS_F_OPEN 0x01000000 -#define DFS_F_DIRECTORY 0x02000000 -#define DFS_F_EOF 0x04000000 -#define DFS_F_ERR 0x08000000 +#define DFS_F_OPEN 0x01000000 +#define DFS_F_DIRECTORY 0x02000000 +#define DFS_F_EOF 0x04000000 +#define DFS_F_ERR 0x08000000 /* Seek flags */ #ifdef __CC_ARM #include -#define DFS_SEEK_SET SEEK_SET -#define DFS_SEEK_CUR SEEK_CUR -#define DFS_SEEK_END SEEK_END +#define DFS_SEEK_SET SEEK_SET +#define DFS_SEEK_CUR SEEK_CUR +#define DFS_SEEK_END SEEK_END #elif defined(_MSC_VER) #include -#define DFS_SEEK_SET SEEK_SET -#define DFS_SEEK_CUR SEEK_CUR -#define DFS_SEEK_END SEEK_END +#define DFS_SEEK_SET SEEK_SET +#define DFS_SEEK_CUR SEEK_CUR +#define DFS_SEEK_END SEEK_END #else -#define DFS_SEEK_SET 0 -#define DFS_SEEK_CUR 1 -#define DFS_SEEK_END 2 +#define DFS_SEEK_SET 0 +#define DFS_SEEK_CUR 1 +#define DFS_SEEK_END 2 #endif /* Stat codes */ -#define DFS_S_IFMT 00170000 -#define DFS_S_IFSOCK 0140000 -#define DFS_S_IFLNK 0120000 -#define DFS_S_IFREG 0100000 -#define DFS_S_IFBLK 0060000 -#define DFS_S_IFDIR 0040000 -#define DFS_S_IFCHR 0020000 -#define DFS_S_IFIFO 0010000 -#define DFS_S_ISUID 0004000 -#define DFS_S_ISGID 0002000 -#define DFS_S_ISVTX 0001000 - -#define DFS_S_ISLNK(m) (((m) & DFS_S_IFMT) == DFS_S_IFLNK) -#define DFS_S_ISREG(m) (((m) & DFS_S_IFMT) == DFS_S_IFREG) -#define DFS_S_ISDIR(m) (((m) & DFS_S_IFMT) == DFS_S_IFDIR) -#define DFS_S_ISCHR(m) (((m) & DFS_S_IFMT) == DFS_S_IFCHR) -#define DFS_S_ISBLK(m) (((m) & DFS_S_IFMT) == DFS_S_IFBLK) -#define DFS_S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO) -#define DFS_S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK) - -#define DFS_S_IRWXU 00700 -#define DFS_S_IRUSR 00400 -#define DFS_S_IWUSR 00200 -#define DFS_S_IXUSR 00100 - -#define DFS_S_IRWXG 00070 -#define DFS_S_IRGRP 00040 -#define DFS_S_IWGRP 00020 -#define DFS_S_IXGRP 00010 - -#define DFS_S_IRWXO 00007 -#define DFS_S_IROTH 00004 -#define DFS_S_IWOTH 00002 -#define DFS_S_IXOTH 00001 +#define DFS_S_IFMT 00170000 +#define DFS_S_IFSOCK 0140000 +#define DFS_S_IFLNK 0120000 +#define DFS_S_IFREG 0100000 +#define DFS_S_IFBLK 0060000 +#define DFS_S_IFDIR 0040000 +#define DFS_S_IFCHR 0020000 +#define DFS_S_IFIFO 0010000 +#define DFS_S_ISUID 0004000 +#define DFS_S_ISGID 0002000 +#define DFS_S_ISVTX 0001000 + +#define DFS_S_ISLNK(m) (((m) & DFS_S_IFMT) == DFS_S_IFLNK) +#define DFS_S_ISREG(m) (((m) & DFS_S_IFMT) == DFS_S_IFREG) +#define DFS_S_ISDIR(m) (((m) & DFS_S_IFMT) == DFS_S_IFDIR) +#define DFS_S_ISCHR(m) (((m) & DFS_S_IFMT) == DFS_S_IFCHR) +#define DFS_S_ISBLK(m) (((m) & DFS_S_IFMT) == DFS_S_IFBLK) +#define DFS_S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO) +#define DFS_S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK) + +#define DFS_S_IRWXU 00700 +#define DFS_S_IRUSR 00400 +#define DFS_S_IWUSR 00200 +#define DFS_S_IXUSR 00100 + +#define DFS_S_IRWXG 00070 +#define DFS_S_IRGRP 00040 +#define DFS_S_IWGRP 00020 +#define DFS_S_IXGRP 00010 + +#define DFS_S_IRWXO 00007 +#define DFS_S_IROTH 00004 +#define DFS_S_IWOTH 00002 +#define DFS_S_IXOTH 00001 struct stat { - rt_device_t st_dev; - rt_uint16_t st_mode; - rt_uint32_t st_size; - rt_time_t st_mtime; - rt_uint32_t st_blksize; + rt_device_t st_dev; + rt_uint16_t st_mode; + rt_uint32_t st_size; + rt_time_t st_mtime; + rt_uint32_t st_blksize; }; struct statfs { - rt_size_t f_bsize; /* block size */ - rt_size_t f_blocks; /* total data blocks in file system */ - rt_size_t f_bfree; /* free blocks in file system */ + rt_size_t f_bsize; /* block size */ + rt_size_t f_blocks; /* total data blocks in file system */ + rt_size_t f_bfree; /* free blocks in file system */ }; /* File types */ -#define FT_REGULAR 0 /* regular file */ -#define FT_SOCKET 1 /* socket file */ -#define FT_DIRECTORY 2 /* directory */ -#define FT_USER 3 /* user defined */ +#define FT_REGULAR 0 /* regular file */ +#define FT_SOCKET 1 /* socket file */ +#define FT_DIRECTORY 2 /* directory */ +#define FT_USER 3 /* user defined */ /* Dirent types */ -#define DFS_DT_UNKNOWN 0x00 -#define DFS_DT_REG 0x01 -#define DFS_DT_DIR 0x02 +#define DFS_DT_UNKNOWN 0x00 +#define DFS_DT_REG 0x01 +#define DFS_DT_DIR 0x02 struct dirent { - rt_uint8_t d_type; /* The type of the file */ - rt_uint8_t d_namlen; /* The length of the not including the terminating null file name */ - rt_uint16_t d_reclen; /* length of this record */ - char d_name[DFS_PATH_MAX]; /* The null-terminated file name */ + rt_uint8_t d_type; /* The type of the file */ + rt_uint8_t d_namlen; /* The length of the not including the terminating null file name */ + rt_uint16_t d_reclen; /* length of this record */ + char d_name[DFS_PATH_MAX]; /* The null-terminated file name */ }; #endif /* file descriptor */ struct dfs_fd { - char *path; /* Name (below mount point) */ - int type; /* Type (regular or socket) */ - int ref_count; /* Descriptor reference count */ + char *path; /* Name (below mount point) */ + int type; /* Type (regular or socket) */ + int ref_count; /* Descriptor reference count */ - struct dfs_filesystem *fs; /* Resident file system */ + struct dfs_filesystem *fs; /* Resident file system */ - rt_uint32_t flags; /* Descriptor flags */ - rt_size_t size; /* Size in bytes */ - rt_off_t pos; /* Current file position */ + rt_uint32_t flags; /* Descriptor flags */ + rt_size_t size; /* Size in bytes */ + rt_off_t pos; /* Current file position */ - void *data; /* Specific file system data */ + void *data; /* Specific file system data */ }; #endif diff --git a/components/dfs/include/dfs_elm.h b/components/dfs/include/dfs_elm.h index 656ba07711..18401ef8ee 100644 --- a/components/dfs/include/dfs_elm.h +++ b/components/dfs/include/dfs_elm.h @@ -1,7 +1,7 @@ /* * File : dfs_elm.h * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2008-2011, RT-Thread Development Team + * COPYRIGHT (C) 2008-2012, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at diff --git a/components/dfs/include/dfs_file.h b/components/dfs/include/dfs_file.h index ba5f348605..a3fba3dada 100644 --- a/components/dfs/include/dfs_file.h +++ b/components/dfs/include/dfs_file.h @@ -1,7 +1,7 @@ /* - * File : dfs_file.c + * File : dfs_file.h * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2011, RT-Thread Development Team + * COPYRIGHT (C) 2004-2012, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -12,8 +12,8 @@ * 2005-01-26 Bernard The first version. */ -#ifndef __DFS_RAW_H__ -#define __DFS_RAW_H__ +#ifndef __DFS_FILE_H__ +#define __DFS_FILE_H__ #include #include diff --git a/components/dfs/include/dfs_fs.h b/components/dfs/include/dfs_fs.h index 90a5b50d69..8d77a252e0 100644 --- a/components/dfs/include/dfs_fs.h +++ b/components/dfs/include/dfs_fs.h @@ -1,7 +1,7 @@ /* * File : dfs_fs.h * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2011, RT-Thread Development Team + * COPYRIGHT (C) 2004-2012, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -17,8 +17,8 @@ #include -#define DFS_FS_FLAG_DEFAULT 0x00 /* default flag */ -#define DFS_FS_FLAG_FULLPATH 0x01 /* set full path to underlaying file system */ +#define DFS_FS_FLAG_DEFAULT 0x00 /* default flag */ +#define DFS_FS_FLAG_FULLPATH 0x01 /* set full path to underlaying file system */ /* Pre-declaration */ struct dfs_filesystem; @@ -27,58 +27,62 @@ struct dfs_fd; /* File system operations struct */ struct dfs_filesystem_operation { - char *name; - rt_uint32_t flags; /* flags for file system operations */ - - /* mount and unmount file system */ - int (*mount) (struct dfs_filesystem *fs, unsigned long rwflag, const void *data); - int (*unmount) (struct dfs_filesystem *fs); - - /* make a file system */ - int (*mkfs) (const char *device_name); - int (*statfs) (struct dfs_filesystem *fs, struct statfs *buf); - - int (*open) (struct dfs_fd *fd); - int (*close) (struct dfs_fd *fd); - int (*ioctl) (struct dfs_fd *fd, int cmd, void *args); - int (*read) (struct dfs_fd *fd, void *buf, rt_size_t count); - int (*write) (struct dfs_fd *fd, const void *buf, rt_size_t count); - int (*flush) (struct dfs_fd *fd); - int (*lseek) (struct dfs_fd *fd, rt_off_t offset); - int (*getdents) (struct dfs_fd *fd, struct dirent *dirp, rt_uint32_t count); - - int (*unlink) (struct dfs_filesystem *fs, const char *pathname); - int (*stat) (struct dfs_filesystem *fs, const char *filename, struct stat *buf); - int (*rename) (struct dfs_filesystem *fs, const char *oldpath, const char *newpath); + char *name; + rt_uint32_t flags; /* flags for file system operations */ + + /* mount and unmount file system */ + int (*mount) (struct dfs_filesystem *fs, unsigned long rwflag, const void *data); + int (*unmount) (struct dfs_filesystem *fs); + + /* make a file system */ + int (*mkfs) (const char *device_name); + int (*statfs) (struct dfs_filesystem *fs, struct statfs *buf); + + int (*open) (struct dfs_fd *fd); + int (*close) (struct dfs_fd *fd); + int (*ioctl) (struct dfs_fd *fd, int cmd, void *args); + int (*read) (struct dfs_fd *fd, void *buf, rt_size_t count); + int (*write) (struct dfs_fd *fd, const void *buf, rt_size_t count); + int (*flush) (struct dfs_fd *fd); + int (*lseek) (struct dfs_fd *fd, rt_off_t offset); + int (*getdents) (struct dfs_fd *fd, struct dirent *dirp, rt_uint32_t count); + + int (*unlink) (struct dfs_filesystem *fs, const char *pathname); + int (*stat) (struct dfs_filesystem *fs, const char *filename, struct stat *buf); + int (*rename) (struct dfs_filesystem *fs, const char *oldpath, const char *newpath); }; /* Mounted file system */ struct dfs_filesystem { - rt_device_t dev_id; /* Attached device */ + rt_device_t dev_id; /* Attached device */ - char *path; /* File system mount point */ - const struct dfs_filesystem_operation *ops; /* Operations for file system type */ + char *path; /* File system mount point */ + const struct dfs_filesystem_operation *ops; /* Operations for file system type */ - void *data; /* Specific file system data */ + void *data; /* Specific file system data */ }; /* file system partition table */ struct dfs_partition { - rt_uint8_t type; /* file system type */ - rt_off_t offset; /* partition start offset */ - rt_size_t size; /* partition size */ - rt_sem_t lock; + rt_uint8_t type; /* file system type */ + rt_off_t offset; /* partition start offset */ + rt_size_t size; /* partition size */ + rt_sem_t lock; }; int dfs_register(const struct dfs_filesystem_operation *ops); struct dfs_filesystem *dfs_filesystem_lookup(const char *path); -rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, rt_uint8_t *buf, rt_uint32_t pindex); - -int dfs_mount(const char *device_name, const char *path, - const char *filesystemtype, rt_uint32_t rwflag, const - void *data); +rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, + rt_uint8_t *buf, + rt_uint32_t pindex); + +int dfs_mount(const char *device_name, + const char *path, + const char *filesystemtype, + rt_uint32_t rwflag, + const void *data); int dfs_unmount(const char *specialfile); /* extern variable */ diff --git a/components/dfs/include/dfs_init.h b/components/dfs/include/dfs_init.h index 176d78981c..4c927a8429 100644 --- a/components/dfs/include/dfs_init.h +++ b/components/dfs/include/dfs_init.h @@ -1,7 +1,7 @@ /* * File : dfs_init.h * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2011, RT-Thread Development Team + * COPYRIGHT (C) 2004-2012, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at diff --git a/components/dfs/include/dfs_posix.h b/components/dfs/include/dfs_posix.h index 503099cff6..c8d15fcf9e 100644 --- a/components/dfs/include/dfs_posix.h +++ b/components/dfs/include/dfs_posix.h @@ -1,7 +1,7 @@ /* - * File : dfs_def.h + * File : dfs_posix.h * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2004-2011, RT-Thread Development Team + * COPYRIGHT (C) 2004-2012, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -21,68 +21,68 @@ #include #ifndef RT_USING_NEWLIB -#define O_RDONLY DFS_O_RDONLY -#define O_WRONLY DFS_O_WRONLY -#define O_RDWR DFS_O_RDWR -#define O_ACCMODE DFS_O_ACCMODE -#define O_CREAT DFS_O_CREAT -#define O_EXCL DFS_O_EXCL -#define O_TRUNC DFS_O_TRUNC -#define O_APPEND DFS_O_APPEND -#define O_BINARY DFS_O_BINARY +#define O_RDONLY DFS_O_RDONLY +#define O_WRONLY DFS_O_WRONLY +#define O_RDWR DFS_O_RDWR +#define O_ACCMODE DFS_O_ACCMODE +#define O_CREAT DFS_O_CREAT +#define O_EXCL DFS_O_EXCL +#define O_TRUNC DFS_O_TRUNC +#define O_APPEND DFS_O_APPEND +#define O_BINARY DFS_O_BINARY #define O_DIRECTORY DFS_O_DIRECTORY -#define S_IFMT DFS_S_IFMT -#define S_IFSOCK DFS_S_IFSOCK -#define S_IFLNK DFS_S_IFLNK -#define S_IFREG DFS_S_IFREG -#define S_IFBLK DFS_S_IFBLK -#define S_IFDIR DFS_S_IFDIR -#define S_IFCHR DFS_S_IFCHR -#define S_IFIFO DFS_S_IFIFO -#define S_ISUID DFS_S_ISUID -#define S_ISGID DFS_S_ISGID -#define S_ISVTX DFS_S_ISVTX +#define S_IFMT DFS_S_IFMT +#define S_IFSOCK DFS_S_IFSOCK +#define S_IFLNK DFS_S_IFLNK +#define S_IFREG DFS_S_IFREG +#define S_IFBLK DFS_S_IFBLK +#define S_IFDIR DFS_S_IFDIR +#define S_IFCHR DFS_S_IFCHR +#define S_IFIFO DFS_S_IFIFO +#define S_ISUID DFS_S_ISUID +#define S_ISGID DFS_S_ISGID +#define S_ISVTX DFS_S_ISVTX -#define S_ISLNK(m) (((m) & DFS_S_IFMT) == DFS_S_IFLNK) -#define S_ISREG(m) (((m) & DFS_S_IFMT) == DFS_S_IFREG) -#define S_ISDIR(m) (((m) & DFS_S_IFMT) == DFS_S_IFDIR) -#define S_ISCHR(m) (((m) & DFS_S_IFMT) == DFS_S_IFCHR) -#define S_ISBLK(m) (((m) & DFS_S_IFMT) == DFS_S_IFBLK) -#define S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO) -#define S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK) +#define S_ISLNK(m) (((m) & DFS_S_IFMT) == DFS_S_IFLNK) +#define S_ISREG(m) (((m) & DFS_S_IFMT) == DFS_S_IFREG) +#define S_ISDIR(m) (((m) & DFS_S_IFMT) == DFS_S_IFDIR) +#define S_ISCHR(m) (((m) & DFS_S_IFMT) == DFS_S_IFCHR) +#define S_ISBLK(m) (((m) & DFS_S_IFMT) == DFS_S_IFBLK) +#define S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO) +#define S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK) -#define S_IRWXU DFS_S_IRWXU -#define S_IRUSR DFS_S_IRUSR -#define S_IWUSR DFS_S_IWUSR -#define S_IXUSR DFS_S_IXUSR +#define S_IRWXU DFS_S_IRWXU +#define S_IRUSR DFS_S_IRUSR +#define S_IWUSR DFS_S_IWUSR +#define S_IXUSR DFS_S_IXUSR -#define S_IRWXG DFS_S_IRWXG -#define S_IRGRP DFS_S_IRGRP -#define S_IWGRP DFS_S_IWGRP -#define S_IXGRP DFS_S_IXGRP +#define S_IRWXG DFS_S_IRWXG +#define S_IRGRP DFS_S_IRGRP +#define S_IWGRP DFS_S_IWGRP +#define S_IXGRP DFS_S_IXGRP -#define S_IRWXO DFS_S_IRWXO -#define S_IROTH DFS_S_IROTH -#define S_IWOTH DFS_S_IWOTH -#define S_IXOTH DFS_S_IXOTH +#define S_IRWXO DFS_S_IRWXO +#define S_IROTH DFS_S_IROTH +#define S_IWOTH DFS_S_IWOTH +#define S_IXOTH DFS_S_IXOTH #if defined(__CC_ARM) #include #elif defined(_MSC_VER) #include #else -#define SEEK_SET DFS_SEEK_SET -#define SEEK_CUR DFS_SEEK_CUR -#define SEEK_END DFS_SEEK_END +#define SEEK_SET DFS_SEEK_SET +#define SEEK_CUR DFS_SEEK_CUR +#define SEEK_END DFS_SEEK_END #endif typedef struct { - int fd; /* directory file */ - char buf[512]; - int num; - int cur; + int fd; /* directory file */ + char buf[512]; + int num; + int cur; } DIR; /* directory api*/ diff --git a/components/dfs/src/dfs.c b/components/dfs/src/dfs.c index 05a7615f2e..86ef809fbf 100644 --- a/components/dfs/src/dfs.c +++ b/components/dfs/src/dfs.c @@ -16,7 +16,7 @@ #include #include -#define NO_WORKING_DIR "system does not support working dir\n" +#define NO_WORKING_DIR "system does not support working dir\n" /* Global variables */ const struct dfs_filesystem_operation *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; @@ -46,20 +46,20 @@ struct dfs_fd fd_table[DFS_FD_MAX]; */ void dfs_init(void) { - /* clear filesystem operations table */ - rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table)); - /* clear filesystem table */ - rt_memset(filesystem_table, 0, sizeof(filesystem_table)); - /* clean fd table */ - rt_memset(fd_table, 0, sizeof(fd_table)); + /* clear filesystem operations table */ + rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table)); + /* clear filesystem table */ + rt_memset(filesystem_table, 0, sizeof(filesystem_table)); + /* clean fd table */ + rt_memset(fd_table, 0, sizeof(fd_table)); - /* create device filesystem lock */ - rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO); + /* create device filesystem lock */ + rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO); #ifdef DFS_USING_WORKDIR - /* set current working directory */ - rt_memset(working_directory, 0, sizeof(working_directory)); - working_directory[0] = '/'; + /* set current working directory */ + rt_memset(working_directory, 0, sizeof(working_directory)); + working_directory[0] = '/'; #endif } @@ -70,13 +70,13 @@ void dfs_init(void) */ void dfs_lock(void) { - rt_err_t result; + rt_err_t result; - result = rt_mutex_take(&fslock, RT_WAITING_FOREVER); - if (result != RT_EOK) - { - RT_ASSERT(0); - } + result = rt_mutex_take(&fslock, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + RT_ASSERT(0); + } } /** @@ -86,7 +86,7 @@ void dfs_lock(void) */ void dfs_unlock(void) { - rt_mutex_release(&fslock); + rt_mutex_release(&fslock); } /** @@ -97,36 +97,36 @@ void dfs_unlock(void) */ int fd_new(void) { - struct dfs_fd *d; - int idx; + struct dfs_fd *d; + int idx; - /* lock filesystem */ - dfs_lock(); + /* lock filesystem */ + dfs_lock(); - /* find an empty fd entry */ + /* find an empty fd entry */ #ifdef DFS_USING_STDIO - for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++); + for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++); #else - for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++); + for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++); #endif - /* can't find an empty fd entry */ + /* can't find an empty fd entry */ #ifdef DFS_USING_STDIO - if (idx == DFS_FD_MAX + 3) + if (idx == DFS_FD_MAX + 3) #else - if (idx == DFS_FD_MAX) + if (idx == DFS_FD_MAX) #endif - { - idx = -1; - goto __result; - } + { + idx = -1; + goto __result; + } - d = &(fd_table[idx]); - d->ref_count = 1; + d = &(fd_table[idx]); + d->ref_count = 1; __result: - dfs_unlock(); - return idx; + dfs_unlock(); + return idx; } /** @@ -140,24 +140,24 @@ __result: */ struct dfs_fd *fd_get(int fd) { - struct dfs_fd *d; + struct dfs_fd *d; #ifdef DFS_USING_STDIO - if (fd < 3 || fd >= DFS_FD_MAX + 3) - return RT_NULL; + if (fd < 3 || fd >= DFS_FD_MAX + 3) + return RT_NULL; #else - if (fd < 0 || fd >= DFS_FD_MAX) - return RT_NULL; + if (fd < 0 || fd >= DFS_FD_MAX) + return RT_NULL; #endif - dfs_lock(); - d = &fd_table[fd]; + dfs_lock(); + d = &fd_table[fd]; - /* increase the reference count */ - d->ref_count ++; - dfs_unlock(); + /* increase the reference count */ + d->ref_count ++; + dfs_unlock(); - return d; + return d; } /** @@ -167,17 +167,17 @@ struct dfs_fd *fd_get(int fd) */ void fd_put(struct dfs_fd *fd) { - RT_ASSERT(fd != RT_NULL); + RT_ASSERT(fd != RT_NULL); - dfs_lock(); - fd->ref_count --; + dfs_lock(); + fd->ref_count --; - /* clear this fd entry */ - if (fd->ref_count == 0) - { - rt_memset(fd, 0, sizeof(struct dfs_fd)); - } - dfs_unlock(); + /* clear this fd entry */ + if (fd->ref_count == 0) + { + rt_memset(fd, 0, sizeof(struct dfs_fd)); + } + dfs_unlock(); }; /** @@ -191,52 +191,52 @@ void fd_put(struct dfs_fd *fd) */ int fd_is_open(const char *pathname) { - char *fullpath; - unsigned int index; - struct dfs_filesystem *fs; - struct dfs_fd *fd; - - fullpath = dfs_normalize_path(RT_NULL, pathname); - if (fullpath != RT_NULL) - { - char *mountpath; - fs = dfs_filesystem_lookup(fullpath); - if (fs == RT_NULL) - { - /* can't find mounted file system */ - rt_free(fullpath); - - return -1; - } - - /* get file path name under mounted file system */ - if (fs->path[0] == '/' && fs->path[1] == '\0') - mountpath = fullpath; - else - mountpath = fullpath + strlen(fs->path); - - dfs_lock(); - for (index = 0; index < DFS_FD_MAX; index++) - { - fd = &(fd_table[index]); - if (fd->fs == RT_NULL) - continue; - - if (fd->fs == fs && strcmp(fd->path, mountpath) == 0) - { - /* found file in file descriptor table */ - rt_free(fullpath); - dfs_unlock(); - - return 0; - } - } - dfs_unlock(); - - rt_free(fullpath); - } - - return -1; + char *fullpath; + unsigned int index; + struct dfs_filesystem *fs; + struct dfs_fd *fd; + + fullpath = dfs_normalize_path(RT_NULL, pathname); + if (fullpath != RT_NULL) + { + char *mountpath; + fs = dfs_filesystem_lookup(fullpath); + if (fs == RT_NULL) + { + /* can't find mounted file system */ + rt_free(fullpath); + + return -1; + } + + /* get file path name under mounted file system */ + if (fs->path[0] == '/' && fs->path[1] == '\0') + mountpath = fullpath; + else + mountpath = fullpath + strlen(fs->path); + + dfs_lock(); + for (index = 0; index < DFS_FD_MAX; index++) + { + fd = &(fd_table[index]); + if (fd->fs == RT_NULL) + continue; + + if (fd->fs == fs && strcmp(fd->path, mountpath) == 0) + { + /* found file in file descriptor table */ + rt_free(fullpath); + dfs_unlock(); + + return 0; + } + } + dfs_unlock(); + + rt_free(fullpath); + } + + return -1; } /** @@ -249,22 +249,23 @@ int fd_is_open(const char *pathname) */ const char *dfs_subdir(const char *directory, const char *filename) { - const char *dir; + const char *dir; - if (strlen(directory) == strlen(filename)) /* it's a same path */ - return RT_NULL; + if (strlen(directory) == strlen(filename)) /* it's a same path */ + return RT_NULL; - dir = filename + strlen(directory); - if ((*dir != '/') && (dir != filename)) - { - dir --; - } + dir = filename + strlen(directory); + if ((*dir != '/') && (dir != filename)) + { + dir --; + } - return dir; + return dir; } /** - * this function will normalize a path according to specified parent directory and file name. + * this function will normalize a path according to specified parent directory + * and file name. * * @param directory the parent path * @param filename the file name @@ -273,113 +274,113 @@ const char *dfs_subdir(const char *directory, const char *filename) */ char *dfs_normalize_path(const char *directory, const char *filename) { - char *fullpath; - char *dst0, *dst, *src; + char *fullpath; + char *dst0, *dst, *src; - /* check parameters */ - RT_ASSERT(filename != RT_NULL); + /* check parameters */ + RT_ASSERT(filename != RT_NULL); #ifdef DFS_USING_WORKDIR - if (directory == RT_NULL) /* shall use working directory */ - directory = &working_directory[0]; + if (directory == RT_NULL) /* shall use working directory */ + directory = &working_directory[0]; #else - if ((directory == RT_NULL) && (filename[0] != '/')) - { - rt_kprintf(NO_WORKING_DIR); + if ((directory == RT_NULL) && (filename[0] != '/')) + { + rt_kprintf(NO_WORKING_DIR); - return RT_NULL; - } + return RT_NULL; + } #endif - if (filename[0] != '/') /* it's a absolute path, use it directly */ - { - fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2); - - /* join path and file name */ - rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, - "%s/%s", directory, filename); - } - else - { - fullpath = rt_strdup(filename); /* copy string */ - } - - src = fullpath; - dst = fullpath; - - dst0 = dst; - while (1) - { - char c = *src; - - if (c == '.') - { - if (!src[1]) src ++; /* '.' and ends */ - else if (src[1] == '/') - { - /* './' case */ - src += 2; - - while ((*src == '/') && (*src != '\0')) - src ++; - continue; - } - else if (src[1] == '.') - { - if (!src[2]) - { - /* '..' and ends case */ - src += 2; - goto up_one; - } - else if (src[2] == '/') - { - /* '../' case */ - src += 3; - - while ((*src == '/') && (*src != '\0')) - src ++; - goto up_one; - } - } - } - - /* copy up the next '/' and erase all '/' */ - while ((c = *src++) != '\0' && c != '/') - *dst ++ = c; - - if (c == '/') - { - *dst ++ = '/'; - while (c == '/') - c = *src++; - - src --; - } - else if (!c) - break; - - continue; + if (filename[0] != '/') /* it's a absolute path, use it directly */ + { + fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2); + + /* join path and file name */ + rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, + "%s/%s", directory, filename); + } + else + { + fullpath = rt_strdup(filename); /* copy string */ + } + + src = fullpath; + dst = fullpath; + + dst0 = dst; + while (1) + { + char c = *src; + + if (c == '.') + { + if (!src[1]) src ++; /* '.' and ends */ + else if (src[1] == '/') + { + /* './' case */ + src += 2; + + while ((*src == '/') && (*src != '\0')) + src ++; + continue; + } + else if (src[1] == '.') + { + if (!src[2]) + { + /* '..' and ends case */ + src += 2; + goto up_one; + } + else if (src[2] == '/') + { + /* '../' case */ + src += 3; + + while ((*src == '/') && (*src != '\0')) + src ++; + goto up_one; + } + } + } + + /* copy up the next '/' and erase all '/' */ + while ((c = *src++) != '\0' && c != '/') + *dst ++ = c; + + if (c == '/') + { + *dst ++ = '/'; + while (c == '/') + c = *src++; + + src --; + } + else if (!c) + break; + + continue; up_one: - dst --; - if (dst < dst0) - { - rt_free(fullpath); - return RT_NULL; - } - while (dst0 < dst && dst[-1] != '/') - dst --; - } - - *dst = '\0'; - - /* remove '/' in the end of path if exist */ - dst --; - if ((dst != fullpath) && (*dst == '/')) - *dst = '\0'; - - return fullpath; + dst --; + if (dst < dst0) + { + rt_free(fullpath); + return RT_NULL; + } + while (dst0 < dst && dst[-1] != '/') + dst --; + } + + *dst = '\0'; + + /* remove '/' in the end of path if exist */ + dst --; + if ((dst != fullpath) && (*dst == '/')) + *dst = '\0'; + + return fullpath; } /*@}*/ diff --git a/components/dfs/src/dfs_file.c b/components/dfs/src/dfs_file.c index b18bdfaadd..0e757a2735 100644 --- a/components/dfs/src/dfs_file.c +++ b/components/dfs/src/dfs_file.c @@ -19,6 +19,7 @@ /** * @addtogroup FileApi */ + /*@{*/ /** @@ -32,84 +33,85 @@ */ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) { - struct dfs_filesystem *fs; - char *fullpath; - int result; - - /* parameter check */ - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; - - /* make sure we have an absolute path */ - fullpath = dfs_normalize_path(RT_NULL, path); - if (fullpath == RT_NULL) - { - return -1; - } - - dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath)); - - /* find filesystem */ - fs = dfs_filesystem_lookup(fullpath); - if (fs == RT_NULL) - { - rt_free(fullpath); /* release path */ - return -DFS_STATUS_ENOENT; - } - - dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name)); - fd->fs = fs; - - /* initialize the fd item */ - fd->type = FT_REGULAR; - fd->flags = flags; - fd->size = 0; - fd->pos = 0; - - if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) - { - if (dfs_subdir(fs->path, fullpath) == RT_NULL) - fd->path = rt_strdup("/"); - else - fd->path = rt_strdup(dfs_subdir(fs->path, fullpath)); - rt_free(fullpath); - dfs_log(DFS_DEBUG_INFO, ("Actual file path: %s\n", fd->path)); - } - else - { - fd->path = fullpath; - } - - /* specific file system open routine */ - if (fs->ops->open == RT_NULL) - { - /* clear fd */ - rt_free(fd->path); - rt_memset(fd, 0, sizeof(*fd)); - - return -DFS_STATUS_ENOSYS; - } - - if ((result = fs->ops->open(fd)) < 0) - { - /* clear fd */ - rt_free(fd->path); - rt_memset(fd, 0, sizeof(*fd)); - - dfs_log(DFS_DEBUG_INFO, ("open failed")); - - return result; - } - - fd->flags |= DFS_F_OPEN; - if (flags & DFS_O_DIRECTORY) - { - fd->type = FT_DIRECTORY; - fd->flags |= DFS_F_DIRECTORY; - } - - dfs_log(DFS_DEBUG_INFO, ("open successful")); - return 0; + struct dfs_filesystem *fs; + char *fullpath; + int result; + + /* parameter check */ + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; + + /* make sure we have an absolute path */ + fullpath = dfs_normalize_path(RT_NULL, path); + if (fullpath == RT_NULL) + { + return -1; + } + + dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath)); + + /* find filesystem */ + fs = dfs_filesystem_lookup(fullpath); + if (fs == RT_NULL) + { + rt_free(fullpath); /* release path */ + + return -DFS_STATUS_ENOENT; + } + + dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name)); + fd->fs = fs; + + /* initialize the fd item */ + fd->type = FT_REGULAR; + fd->flags = flags; + fd->size = 0; + fd->pos = 0; + + if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) + { + if (dfs_subdir(fs->path, fullpath) == RT_NULL) + fd->path = rt_strdup("/"); + else + fd->path = rt_strdup(dfs_subdir(fs->path, fullpath)); + rt_free(fullpath); + dfs_log(DFS_DEBUG_INFO, ("Actual file path: %s\n", fd->path)); + } + else + { + fd->path = fullpath; + } + + /* specific file system open routine */ + if (fs->ops->open == RT_NULL) + { + /* clear fd */ + rt_free(fd->path); + rt_memset(fd, 0, sizeof(*fd)); + + return -DFS_STATUS_ENOSYS; + } + + if ((result = fs->ops->open(fd)) < 0) + { + /* clear fd */ + rt_free(fd->path); + rt_memset(fd, 0, sizeof(*fd)); + + dfs_log(DFS_DEBUG_INFO, ("open failed")); + + return result; + } + + fd->flags |= DFS_F_OPEN; + if (flags & DFS_O_DIRECTORY) + { + fd->type = FT_DIRECTORY; + fd->flags |= DFS_F_DIRECTORY; + } + + dfs_log(DFS_DEBUG_INFO, ("open successful")); + return 0; } /** @@ -121,19 +123,19 @@ int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) */ int dfs_file_close(struct dfs_fd *fd) { - int result = 0; + int result = 0; - if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) - result = fd->fs->ops->close(fd); + if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) + result = fd->fs->ops->close(fd); - /* close fd error, return */ - if (result < 0) - return result; + /* close fd error, return */ + if (result < 0) + return result; - rt_free(fd->path); - rt_memset(fd, 0, sizeof(struct dfs_fd)); + rt_free(fd->path); + rt_memset(fd, 0, sizeof(struct dfs_fd)); - return result; + return result; } /** @@ -147,20 +149,21 @@ int dfs_file_close(struct dfs_fd *fd) */ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - if (fd == RT_NULL || fd->type != FT_REGULAR) - return -DFS_STATUS_EINVAL; + if (fd == RT_NULL || fd->type != FT_REGULAR) + return -DFS_STATUS_EINVAL; - fs = fd->fs; - if (fs->ops->ioctl != RT_NULL) - return fs->ops->ioctl(fd, cmd, args); + fs = fd->fs; + if (fs->ops->ioctl != RT_NULL) + return fs->ops->ioctl(fd, cmd, args); - return -DFS_STATUS_ENOSYS; + return -DFS_STATUS_ENOSYS; } /** - * this function will read specified length data from a file descriptor to a buffer. + * this function will read specified length data from a file descriptor to a + * buffer. * * @param fd the file descriptor. * @param buf the buffer to save the read data. @@ -170,20 +173,20 @@ int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args) */ int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len) { - struct dfs_filesystem *fs; - int result = 0; + struct dfs_filesystem *fs; + int result = 0; - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; - fs = (struct dfs_filesystem *)fd->fs; - if (fs->ops->read == RT_NULL) - return -DFS_STATUS_ENOSYS; + fs = (struct dfs_filesystem *)fd->fs; + if (fs->ops->read == RT_NULL) + return -DFS_STATUS_ENOSYS; - if ((result = fs->ops->read(fd, buf, len)) < 0) - fd->flags |= DFS_F_EOF; + if ((result = fs->ops->read(fd, buf, len)) < 0) + fd->flags |= DFS_F_EOF; - return result; + return result; } /** @@ -197,17 +200,17 @@ int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len) */ int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - /* parameter check */ - if (fd == RT_NULL || fd->type != FT_DIRECTORY) - return -DFS_STATUS_EINVAL; + /* parameter check */ + if (fd == RT_NULL || fd->type != FT_DIRECTORY) + return -DFS_STATUS_EINVAL; - fs = (struct dfs_filesystem *)fd->fs; - if (fs->ops->getdents != RT_NULL) - return fs->ops->getdents(fd, dirp, nbytes); + fs = (struct dfs_filesystem *)fd->fs; + if (fs->ops->getdents != RT_NULL) + return fs->ops->getdents(fd, dirp, nbytes); - return -DFS_STATUS_ENOSYS; + return -DFS_STATUS_ENOSYS; } /** @@ -219,50 +222,50 @@ int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes) */ int dfs_file_unlink(const char *path) { - int result; - char *fullpath; - struct dfs_filesystem *fs; - - result = DFS_STATUS_OK; - - /* Make sure we have an absolute path */ - fullpath = dfs_normalize_path(RT_NULL, path); - if (fullpath == RT_NULL) - { - return -DFS_STATUS_EINVAL; - } - - /* get filesystem */ - if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) - { - result = -DFS_STATUS_ENOENT; - goto __exit; - } - - /* Check whether file is already open */ - if (fd_is_open(fullpath) == 0) - { - result = -DFS_STATUS_EBUSY; - goto __exit; - } - - if (fs->ops->unlink != RT_NULL) - { - if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) - { - if (dfs_subdir(fs->path, fullpath) == RT_NULL) - result = fs->ops->unlink(fs, "/"); - else - result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath)); - } - else - result = fs->ops->unlink(fs, fullpath); - } - else result = -DFS_STATUS_ENOSYS; + int result; + char *fullpath; + struct dfs_filesystem *fs; + + result = DFS_STATUS_OK; + + /* Make sure we have an absolute path */ + fullpath = dfs_normalize_path(RT_NULL, path); + if (fullpath == RT_NULL) + { + return -DFS_STATUS_EINVAL; + } + + /* get filesystem */ + if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) + { + result = -DFS_STATUS_ENOENT; + goto __exit; + } + + /* Check whether file is already open */ + if (fd_is_open(fullpath) == 0) + { + result = -DFS_STATUS_EBUSY; + goto __exit; + } + + if (fs->ops->unlink != RT_NULL) + { + if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) + { + if (dfs_subdir(fs->path, fullpath) == RT_NULL) + result = fs->ops->unlink(fs, "/"); + else + result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath)); + } + else + result = fs->ops->unlink(fs, fullpath); + } + else result = -DFS_STATUS_ENOSYS; __exit: - rt_free(fullpath); - return result; + rt_free(fullpath); + return result; } /** @@ -276,16 +279,16 @@ __exit: */ int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; - fs = fd->fs; - if (fs->ops->write == RT_NULL) - return -DFS_STATUS_ENOSYS; + fs = fd->fs; + if (fs->ops->write == RT_NULL) + return -DFS_STATUS_ENOSYS; - return fs->ops->write(fd, buf, len); + return fs->ops->write(fd, buf, len); } /** @@ -297,16 +300,16 @@ int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len) */ int dfs_file_flush(struct dfs_fd *fd) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; - fs = fd->fs; - if (fs->ops->flush == RT_NULL) - return -DFS_STATUS_ENOSYS; + fs = fd->fs; + if (fs->ops->flush == RT_NULL) + return -DFS_STATUS_ENOSYS; - return fs->ops->flush(fd); + return fs->ops->flush(fd); } /** @@ -319,21 +322,21 @@ int dfs_file_flush(struct dfs_fd *fd) */ int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset) { - int result; - struct dfs_filesystem *fs = fd->fs; + int result; + struct dfs_filesystem *fs = fd->fs; - if (fd == RT_NULL) - return -DFS_STATUS_EINVAL; - if (fs->ops->lseek == RT_NULL) - return -DFS_STATUS_ENOSYS; + if (fd == RT_NULL) + return -DFS_STATUS_EINVAL; + if (fs->ops->lseek == RT_NULL) + return -DFS_STATUS_ENOSYS; - result = fs->ops->lseek(fd, offset); + result = fs->ops->lseek(fd, offset); - /* update current position */ - if (result >= 0) - fd->pos = result; + /* update current position */ + if (result >= 0) + fd->pos = result; - return result; + return result; } /** @@ -346,62 +349,65 @@ int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset) */ int dfs_file_stat(const char *path, struct stat *buf) { - int result; - char *fullpath; - struct dfs_filesystem *fs; - - fullpath = dfs_normalize_path(RT_NULL, path); - if (fullpath == RT_NULL) - { - return -1; - } - - if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) - { - dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath)); - rt_free(fullpath); - return -DFS_STATUS_ENOENT; - } - - if ((fullpath[0] == '/' && fullpath[1] == '\0') || - (dfs_subdir(fs->path, fullpath) == RT_NULL)) - { - /* it's the root directory */ - buf->st_dev = 0; - - buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | - DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; - buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; - - buf->st_size = 0; - buf->st_mtime = 0; - buf->st_blksize = 512; - - /* release full path */ - rt_free(fullpath); - - return DFS_STATUS_OK; - } - else - { - if (fs->ops->stat == RT_NULL) - { - rt_free(fullpath); - dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function")); - return -DFS_STATUS_ENOSYS; - } - - /* get the real file path and get file stat */ - if (fs->ops->flags & DFS_FS_FLAG_FULLPATH) - result = fs->ops->stat(fs, fullpath, buf); - else - result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf); - - } - - rt_free(fullpath); - - return result; + int result; + char *fullpath; + struct dfs_filesystem *fs; + + fullpath = dfs_normalize_path(RT_NULL, path); + if (fullpath == RT_NULL) + { + return -1; + } + + if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) + { + dfs_log(DFS_DEBUG_ERROR, + ("can't find mounted filesystem on this path:%s", fullpath)); + rt_free(fullpath); + + return -DFS_STATUS_ENOENT; + } + + if ((fullpath[0] == '/' && fullpath[1] == '\0') || + (dfs_subdir(fs->path, fullpath) == RT_NULL)) + { + /* it's the root directory */ + buf->st_dev = 0; + + buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | + DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; + buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; + + buf->st_size = 0; + buf->st_mtime = 0; + buf->st_blksize = 512; + + /* release full path */ + rt_free(fullpath); + + return DFS_STATUS_OK; + } + else + { + if (fs->ops->stat == RT_NULL) + { + rt_free(fullpath); + dfs_log(DFS_DEBUG_ERROR, + ("the filesystem didn't implement this function")); + + return -DFS_STATUS_ENOSYS; + } + + /* get the real file path and get file stat */ + if (fs->ops->flags & DFS_FS_FLAG_FULLPATH) + result = fs->ops->stat(fs, fullpath, buf); + else + result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf); + } + + rt_free(fullpath); + + return result; } /** @@ -414,58 +420,59 @@ int dfs_file_stat(const char *path, struct stat *buf) */ int dfs_file_rename(const char *oldpath, const char *newpath) { - int result; - struct dfs_filesystem *oldfs, *newfs; - char *oldfullpath, *newfullpath; - - result = DFS_STATUS_OK; - newfullpath = RT_NULL; - oldfullpath = RT_NULL; - - oldfullpath = dfs_normalize_path(RT_NULL, oldpath); - if (oldfullpath == RT_NULL) - { - result = -DFS_STATUS_ENOENT; - goto __exit; - } - - newfullpath = dfs_normalize_path(RT_NULL, newpath); - if (newfullpath == RT_NULL) - { - result = -DFS_STATUS_ENOENT; - goto __exit; - } - - oldfs = dfs_filesystem_lookup(oldfullpath); - newfs = dfs_filesystem_lookup(newfullpath); - - if (oldfs == newfs) - { - if (oldfs->ops->rename == RT_NULL) - { - result = -DFS_STATUS_ENOSYS; - } - else - { - if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH) - result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath); - else - /* use sub directory to rename in file system */ - result = oldfs->ops->rename(oldfs, dfs_subdir(oldfs->path, oldfullpath), - dfs_subdir(newfs->path, newfullpath)); - } - } - else - { - result = -DFS_STATUS_EXDEV; - } + int result; + struct dfs_filesystem *oldfs, *newfs; + char *oldfullpath, *newfullpath; + + result = DFS_STATUS_OK; + newfullpath = RT_NULL; + oldfullpath = RT_NULL; + + oldfullpath = dfs_normalize_path(RT_NULL, oldpath); + if (oldfullpath == RT_NULL) + { + result = -DFS_STATUS_ENOENT; + goto __exit; + } + + newfullpath = dfs_normalize_path(RT_NULL, newpath); + if (newfullpath == RT_NULL) + { + result = -DFS_STATUS_ENOENT; + goto __exit; + } + + oldfs = dfs_filesystem_lookup(oldfullpath); + newfs = dfs_filesystem_lookup(newfullpath); + + if (oldfs == newfs) + { + if (oldfs->ops->rename == RT_NULL) + { + result = -DFS_STATUS_ENOSYS; + } + else + { + if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH) + result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath); + else + /* use sub directory to rename in file system */ + result = oldfs->ops->rename(oldfs, + dfs_subdir(oldfs->path, oldfullpath), + dfs_subdir(newfs->path, newfullpath)); + } + } + else + { + result = -DFS_STATUS_EXDEV; + } __exit: - rt_free(oldfullpath); - rt_free(newfullpath); + rt_free(oldfullpath); + rt_free(newfullpath); - /* not at same file system, return EXDEV */ - return result; + /* not at same file system, return EXDEV */ + return result; } #ifdef RT_USING_FINSH @@ -475,147 +482,152 @@ static struct dfs_fd fd; static struct dirent dirent; void ls(const char *pathname) { - struct stat stat; - int length; - char *fullpath, *path; + struct stat stat; + int length; + char *fullpath, *path; - fullpath = RT_NULL; - if (pathname == RT_NULL) - { + fullpath = RT_NULL; + if (pathname == RT_NULL) + { #ifdef DFS_USING_WORKDIR - /* open current working directory */ - path = rt_strdup(working_directory); + /* open current working directory */ + path = rt_strdup(working_directory); #else - path = rt_strdup("/"); + path = rt_strdup("/"); #endif - if (path == RT_NULL) return ; /* out of memory */ - } - else - { - path = (char *)pathname; - } - - /* list directory */ - if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0) - { - rt_kprintf("Directory %s:\n", path); - do - { - rt_memset(&dirent, 0, sizeof(struct dirent)); - length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent)); - if (length > 0) - { - rt_memset(&stat, 0, sizeof(struct stat)); - - /* build full path for each file */ - fullpath = dfs_normalize_path(path, dirent.d_name); - if (fullpath == RT_NULL) - break; - - if (dfs_file_stat(fullpath, &stat) == 0) - { - rt_kprintf("%-20s", dirent.d_name); - if ( DFS_S_ISDIR(stat.st_mode)) - { - rt_kprintf("%-25s\n", ""); - } - else - { - rt_kprintf("%-25lu\n", stat.st_size); - } - } - else - rt_kprintf("BAD file: %s\n", dirent.d_name); - rt_free(fullpath); - } - }while(length > 0); - - dfs_file_close(&fd); - } - else - { - rt_kprintf("No such directory\n"); - } - if (pathname == RT_NULL) - rt_free(path); + if (path == RT_NULL) + return ; /* out of memory */ + } + else + { + path = (char *)pathname; + } + + /* list directory */ + if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0) + { + rt_kprintf("Directory %s:\n", path); + do + { + rt_memset(&dirent, 0, sizeof(struct dirent)); + length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent)); + if (length > 0) + { + rt_memset(&stat, 0, sizeof(struct stat)); + + /* build full path for each file */ + fullpath = dfs_normalize_path(path, dirent.d_name); + if (fullpath == RT_NULL) + break; + + if (dfs_file_stat(fullpath, &stat) == 0) + { + rt_kprintf("%-20s", dirent.d_name); + if ( DFS_S_ISDIR(stat.st_mode)) + { + rt_kprintf("%-25s\n", ""); + } + else + { + rt_kprintf("%-25lu\n", stat.st_size); + } + } + else + rt_kprintf("BAD file: %s\n", dirent.d_name); + rt_free(fullpath); + } + }while(length > 0); + + dfs_file_close(&fd); + } + else + { + rt_kprintf("No such directory\n"); + } + if (pathname == RT_NULL) + rt_free(path); } FINSH_FUNCTION_EXPORT(ls, list directory contents) void rm(const char *filename) { - if (dfs_file_unlink(filename) < 0) - { - rt_kprintf("Delete %s failed\n", filename); - } + if (dfs_file_unlink(filename) < 0) + { + rt_kprintf("Delete %s failed\n", filename); + } } FINSH_FUNCTION_EXPORT(rm, remove files or directories) void cat(const char* filename) { - rt_uint32_t length; - char buffer[81]; - - if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0) - { - rt_kprintf("Open %s failed\n", filename); - return; - } - - do - { - rt_memset(buffer, 0, sizeof(buffer)); - length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 ); - if (length > 0) - { - rt_kprintf("%s", buffer); - } - }while (length > 0); - - dfs_file_close(&fd); + rt_uint32_t length; + char buffer[81]; + + if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0) + { + rt_kprintf("Open %s failed\n", filename); + + return; + } + + do + { + rt_memset(buffer, 0, sizeof(buffer)); + length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 ); + if (length > 0) + { + rt_kprintf("%s", buffer); + } + }while (length > 0); + + dfs_file_close(&fd); } FINSH_FUNCTION_EXPORT(cat, print file) -#define BUF_SZ 4096 +#define BUF_SZ 4096 void copy(const char *src, const char *dst) { - struct dfs_fd src_fd; - rt_uint8_t *block_ptr; - rt_uint32_t read_bytes; - - block_ptr = rt_malloc(BUF_SZ); - if (block_ptr == RT_NULL) - { - rt_kprintf("out of memory\n"); - return; - } - - if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0) - { - rt_free(block_ptr); - rt_kprintf("Read %s failed\n", src); - return; - } - if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0) - { - rt_free(block_ptr); - dfs_file_close(&src_fd); - - rt_kprintf("Write %s failed\n", dst); - return; - } - - do - { - read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ); - if (read_bytes > 0) - { - dfs_file_write(&fd, block_ptr, read_bytes); - } - } while (read_bytes > 0); - - dfs_file_close(&src_fd); - dfs_file_close(&fd); - rt_free(block_ptr); + struct dfs_fd src_fd; + rt_uint8_t *block_ptr; + rt_uint32_t read_bytes; + + block_ptr = rt_malloc(BUF_SZ); + if (block_ptr == RT_NULL) + { + rt_kprintf("out of memory\n"); + + return; + } + + if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0) + { + rt_free(block_ptr); + rt_kprintf("Read %s failed\n", src); + + return; + } + if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0) + { + rt_free(block_ptr); + dfs_file_close(&src_fd); + + rt_kprintf("Write %s failed\n", dst); + + return; + } + + do + { + read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ); + if (read_bytes > 0) + { + dfs_file_write(&fd, block_ptr, read_bytes); + } + } while (read_bytes > 0); + + dfs_file_close(&src_fd); + dfs_file_close(&fd); + rt_free(block_ptr); } FINSH_FUNCTION_EXPORT(copy, copy source file to destination file) diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index f747617e5f..78c40013a8 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -31,41 +31,43 @@ */ int dfs_register(const struct dfs_filesystem_operation *ops) { - int index, result; - - result = 0; - - /* lock filesystem */ - dfs_lock(); - - /* check if this filesystem was already registered */ - for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) - { - if (filesystem_operation_table[index] != RT_NULL && - strcmp(filesystem_operation_table[index]->name, ops->name) == 0) - { - result = -1; - goto err; - } - } - - /* find out an empty filesystem type entry */ - for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL; - index++) ; - - /* filesystem type table full */ - if (index == DFS_FILESYSTEM_TYPES_MAX) - { - result = -1; - goto err; - } - - /* save the filesystem's operations */ - filesystem_operation_table[index] = ops; + int index, result; + + result = 0; + + /* lock filesystem */ + dfs_lock(); + + /* check if this filesystem was already registered */ + for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) + { + if (filesystem_operation_table[index] != RT_NULL && + strcmp(filesystem_operation_table[index]->name, ops->name) == 0) + { + result = -1; + goto err; + } + } + + /* find out an empty filesystem type entry */ + for (index = 0; + index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL; + index ++) + ; + + /* filesystem type table full */ + if (index == DFS_FILESYSTEM_TYPES_MAX) + { + result = -1; + goto err; + } + + /* save the filesystem's operations */ + filesystem_operation_table[index] = ops; err: - dfs_unlock(); - return result; + dfs_unlock(); + return result; } /** @@ -78,42 +80,42 @@ err: */ struct dfs_filesystem *dfs_filesystem_lookup(const char *path) { - struct dfs_filesystem *fs; - rt_uint32_t index, fspath, prefixlen; - - fs = RT_NULL; - prefixlen = 0; - - /* lock filesystem */ - dfs_lock(); - - /* lookup it in the filesystem table */ - for (index = 0; index < DFS_FILESYSTEMS_MAX; index++) - { - if (filesystem_table[index].path == RT_NULL) - continue; - else - { - fspath = strlen(filesystem_table[index].path); - if (fspath < prefixlen) - continue; - } - - if ((filesystem_table[index].ops != RT_NULL) && - (strncmp(filesystem_table[index].path, path, fspath) == 0)) - { - /* check next path separator */ - if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/')) - continue; - - fs = &filesystem_table[index]; - prefixlen = fspath; - } - } - - dfs_unlock(); - - return fs; + struct dfs_filesystem *fs; + rt_uint32_t index, fspath, prefixlen; + + fs = RT_NULL; + prefixlen = 0; + + /* lock filesystem */ + dfs_lock(); + + /* lookup it in the filesystem table */ + for (index = 0; index < DFS_FILESYSTEMS_MAX; index++) + { + if (filesystem_table[index].path == RT_NULL) + continue; + else + { + fspath = strlen(filesystem_table[index].path); + if (fspath < prefixlen) + continue; + } + + if ((filesystem_table[index].ops != RT_NULL) && + (strncmp(filesystem_table[index].path, path, fspath) == 0)) + { + /* check next path separator */ + if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/')) + continue; + + fs = &filesystem_table[index]; + prefixlen = fspath; + } + } + + dfs_unlock(); + + return fs; } /** @@ -125,66 +127,73 @@ struct dfs_filesystem *dfs_filesystem_lookup(const char *path) * * @return RT_EOK on successful or -RT_ERROR on failed. */ -rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, rt_uint8_t *buf, rt_uint32_t pindex) +rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, + rt_uint8_t *buf, + rt_uint32_t pindex) { -#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */ -#define DPT_ITEM_SIZE 16 /* partition item size */ - - rt_uint8_t *dpt; - rt_uint8_t type; - rt_err_t result; - - RT_ASSERT(part != RT_NULL); - RT_ASSERT(buf != RT_NULL); - - result = RT_EOK; - - dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE; - - if ((*dpt != 0x80) && (*dpt != 0x00)) - { - /* which is not a partition table */ - result = -RT_ERROR; - return result; - } - - /* get partition type */ - type = *(dpt+4); - - if (type != 0) - { - /* set partition type */ - part->type = type; - - /* get partition offset and size */ - 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) /* MB */ - { - unsigned int part_size; - part_size = part->size >> 11;/* MB */ - if ((part_size>>10) > 0) /* GB */ - { - rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */ - } - else - { - rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");/* MB */ - } - } - else - { - rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */ - } - } - else - { - result = -RT_ERROR; - } - - return result; +#define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */ +#define DPT_ITEM_SIZE 16 /* partition item size */ + + rt_uint8_t *dpt; + rt_uint8_t type; + rt_err_t result; + + RT_ASSERT(part != RT_NULL); + RT_ASSERT(buf != RT_NULL); + + result = RT_EOK; + + dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE; + + if ((*dpt != 0x80) && (*dpt != 0x00)) + { + /* which is not a partition table */ + result = -RT_ERROR; + + return result; + } + + /* get partition type */ + type = *(dpt+4); + + if (type != 0) + { + /* set partition type */ + part->type = type; + + /* get partition offset and size */ + 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) /* MB */ + { + unsigned int part_size; + part_size = part->size >> 11;/* MB */ + if ((part_size>>10) > 0) /* GB */ + { + /* GB */ + rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n"); + } + else + { + /* MB */ + rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n"); + } + } + else + { + /* KB */ + rt_kprintf("%d%s",part->size>>1,"KB\r\n"); + } + } + else + { + result = -RT_ERROR; + } + + return result; } /** @@ -198,144 +207,157 @@ rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, rt_uint8_t *bu * * @return 0 on successful or -1 on failed. */ -int dfs_mount(const char *device_name, const char *path, - const char *filesystemtype, unsigned long rwflag, const - void *data) +int dfs_mount(const char *device_name, + const char *path, + const char *filesystemtype, + unsigned long rwflag, + const void *data) { - const struct dfs_filesystem_operation *ops; - struct dfs_filesystem *fs; - char *fullpath=RT_NULL; - rt_device_t dev_id; - int index; - - /* open specific device */ - if (device_name != RT_NULL) - { - dev_id = rt_device_find(device_name); - if (dev_id == RT_NULL) - { - /* no this device */ - rt_set_errno(-DFS_STATUS_ENODEV); - return -1; - } - } - else - { - /* which is a non-device filesystem mount */ - dev_id = RT_NULL; - } - - /* find out specific filesystem */ - dfs_lock(); - for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) - { - if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0) - break; - } - - /* can't find filesystem */ - if (index == DFS_FILESYSTEM_TYPES_MAX) - { - rt_set_errno(-DFS_STATUS_ENODEV); - dfs_unlock(); - return -1; - } - ops = filesystem_operation_table[index]; - dfs_unlock(); - - /* make full path for special file */ - fullpath = dfs_normalize_path(RT_NULL, path); - if (fullpath == RT_NULL) /* not an abstract path */ - { - rt_set_errno(-DFS_STATUS_ENOTDIR); - return -1; - } - - /* Check if the path exists or not, raw APIs call, fixme */ - if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0)) - { - struct dfs_fd fd; - - if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0) - { - rt_free(fullpath); - rt_set_errno(-DFS_STATUS_ENOTDIR); - return -1; - } - dfs_file_close(&fd); - } - - /* check whether the file system mounted or not */ - dfs_lock(); - for (index =0; index < DFS_FILESYSTEMS_MAX; index++) - { - if (filesystem_table[index].ops != RT_NULL && - strcmp(filesystem_table[index].path, path) == 0) - { - rt_set_errno(-DFS_STATUS_EINVAL); - goto err1; - } - } - - /* find out an empty filesystem table entry */ - for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL; - index++) ; - if (index == DFS_FILESYSTEMS_MAX) /* can't find en empty filesystem table entry */ - { - rt_set_errno(-DFS_STATUS_ENOSPC); - goto err1; - } - - /* register file system */ - fs = &(filesystem_table[index]); - fs->path = fullpath; - fs->ops = ops; - fs->dev_id = dev_id; - /* release filesystem_table lock */ - dfs_unlock(); - - /* open device, but do not check the status of device */ - if (dev_id != RT_NULL) - rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR); - - if (ops->mount == RT_NULL) /* there is no mount implementation */ - { - if (dev_id != RT_NULL) - rt_device_close(dev_id); - dfs_lock(); - /* clear filesystem table entry */ - rt_memset(fs, 0, sizeof(struct dfs_filesystem)); - dfs_unlock(); - - rt_free(fullpath); - rt_set_errno(-DFS_STATUS_ENOSYS); - return -1; - } - /* call mount of this filesystem */ - else if (ops->mount(fs, rwflag, data) < 0) - { - /* close device */ - if (dev_id != RT_NULL) - rt_device_close(fs->dev_id); - - /* mount failed */ - dfs_lock(); - /* clear filesystem table entry */ - rt_memset(fs, 0, sizeof(struct dfs_filesystem)); - dfs_unlock(); - - rt_free(fullpath); - return -1; - } - - return 0; + const struct dfs_filesystem_operation *ops; + struct dfs_filesystem *fs; + char *fullpath=RT_NULL; + rt_device_t dev_id; + int index; + + /* open specific device */ + if (device_name != RT_NULL) + { + dev_id = rt_device_find(device_name); + if (dev_id == RT_NULL) + { + /* no this device */ + rt_set_errno(-DFS_STATUS_ENODEV); + + return -1; + } + } + else + { + /* which is a non-device filesystem mount */ + dev_id = RT_NULL; + } + + /* find out specific filesystem */ + dfs_lock(); + for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) + { + if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0) + break; + } + + /* can't find filesystem */ + if (index == DFS_FILESYSTEM_TYPES_MAX) + { + rt_set_errno(-DFS_STATUS_ENODEV); + dfs_unlock(); + + return -1; + } + ops = filesystem_operation_table[index]; + dfs_unlock(); + + /* make full path for special file */ + fullpath = dfs_normalize_path(RT_NULL, path); + if (fullpath == RT_NULL) /* not an abstract path */ + { + rt_set_errno(-DFS_STATUS_ENOTDIR); + + return -1; + } + + /* Check if the path exists or not, raw APIs call, fixme */ + if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0)) + { + struct dfs_fd fd; + + if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0) + { + rt_free(fullpath); + rt_set_errno(-DFS_STATUS_ENOTDIR); + + return -1; + } + dfs_file_close(&fd); + } + + /* check whether the file system mounted or not */ + dfs_lock(); + for (index = 0; index < DFS_FILESYSTEMS_MAX; index ++) + { + if (filesystem_table[index].ops != RT_NULL && + strcmp(filesystem_table[index].path, path) == 0) + { + rt_set_errno(-DFS_STATUS_EINVAL); + goto err1; + } + } + + /* find out an empty filesystem table entry */ + for (index = 0; + index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL; + index ++) + ; + + /* can't find en empty filesystem table entry */ + if (index == DFS_FILESYSTEMS_MAX) + { + rt_set_errno(-DFS_STATUS_ENOSPC); + goto err1; + } + + /* register file system */ + fs = &(filesystem_table[index]); + fs->path = fullpath; + fs->ops = ops; + fs->dev_id = dev_id; + /* release filesystem_table lock */ + dfs_unlock(); + + /* open device, but do not check the status of device */ + if (dev_id != RT_NULL) + rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR); + + /* there is no mount implementation */ + if (ops->mount == RT_NULL) + { + if (dev_id != RT_NULL) + rt_device_close(dev_id); + dfs_lock(); + /* clear filesystem table entry */ + rt_memset(fs, 0, sizeof(struct dfs_filesystem)); + dfs_unlock(); + + rt_free(fullpath); + rt_set_errno(-DFS_STATUS_ENOSYS); + + return -1; + } + /* call mount of this filesystem */ + else if (ops->mount(fs, rwflag, data) < 0) + { + /* close device */ + if (dev_id != RT_NULL) + rt_device_close(fs->dev_id); + + /* mount failed */ + dfs_lock(); + /* clear filesystem table entry */ + rt_memset(fs, 0, sizeof(struct dfs_filesystem)); + dfs_unlock(); + + rt_free(fullpath); + + return -1; + } + + return 0; err1: - dfs_unlock(); - if (fullpath != RT_NULL) - rt_free(fullpath); + dfs_unlock(); + if (fullpath != RT_NULL) + rt_free(fullpath); - return -1; + return -1; } /** @@ -347,45 +369,48 @@ err1: */ int dfs_unmount(const char *specialfile) { - char *fullpath; - struct dfs_filesystem *fs = RT_NULL; + char *fullpath; + struct dfs_filesystem *fs = RT_NULL; - fullpath = dfs_normalize_path(RT_NULL, specialfile); - if (fullpath == RT_NULL) - { - rt_set_errno(-DFS_STATUS_ENOTDIR); - return -1; - } + fullpath = dfs_normalize_path(RT_NULL, specialfile); + if (fullpath == RT_NULL) + { + rt_set_errno(-DFS_STATUS_ENOTDIR); - /* lock filesystem */ - dfs_lock(); + return -1; + } - fs = dfs_filesystem_lookup(fullpath); - if (fs == RT_NULL || fs->ops->unmount == RT_NULL || fs->ops->unmount(fs) < 0) - { - goto err1; - } + /* lock filesystem */ + dfs_lock(); - /* close device, but do not check the status of device */ - if (fs->dev_id != RT_NULL) - rt_device_close(fs->dev_id); + fs = dfs_filesystem_lookup(fullpath); + if (fs == RT_NULL || + fs->ops->unmount == RT_NULL || + fs->ops->unmount(fs) < 0) + { + goto err1; + } - if (fs->path != RT_NULL) - rt_free(fs->path); + /* close device, but do not check the status of device */ + if (fs->dev_id != RT_NULL) + rt_device_close(fs->dev_id); - /* clear this filesystem table entry */ - rt_memset(fs, 0, sizeof(struct dfs_filesystem)); + if (fs->path != RT_NULL) + rt_free(fs->path); - dfs_unlock(); - rt_free(fullpath); + /* clear this filesystem table entry */ + rt_memset(fs, 0, sizeof(struct dfs_filesystem)); - return 0; + dfs_unlock(); + rt_free(fullpath); + + return 0; err1: - dfs_unlock(); - rt_free(fullpath); + dfs_unlock(); + rt_free(fullpath); - return -1; + return -1; } /** @@ -398,30 +423,30 @@ err1: */ int dfs_mkfs(const char *fs_name, const char *device_name) { - int index; - - /* lock file system */ - dfs_lock(); - /* find the file system operations */ - for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) - { - if (filesystem_operation_table[index] != RT_NULL && - strcmp(filesystem_operation_table[index]->name, fs_name) == 0) - { - /* find file system operation */ - const struct dfs_filesystem_operation* ops = filesystem_operation_table[index]; - dfs_unlock(); - - if (ops->mkfs != RT_NULL) - return ops->mkfs(device_name); - - break; - } - } - dfs_unlock(); - - rt_kprintf("Can not find the file system which named as %s.\n", fs_name); - return -1; + int index; + + /* lock file system */ + dfs_lock(); + /* find the file system operations */ + for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index ++) + { + if (filesystem_operation_table[index] != RT_NULL && + strcmp(filesystem_operation_table[index]->name, fs_name) == 0) + { + /* find file system operation */ + const struct dfs_filesystem_operation *ops = filesystem_operation_table[index]; + dfs_unlock(); + + if (ops->mkfs != RT_NULL) + return ops->mkfs(device_name); + + break; + } + } + dfs_unlock(); + + rt_kprintf("Can not find the file system which named as %s.\n", fs_name); + return -1; } /** @@ -434,40 +459,41 @@ int dfs_mkfs(const char *fs_name, const char *device_name) */ int dfs_statfs(const char *path, struct statfs *buffer) { - struct dfs_filesystem *fs; + struct dfs_filesystem *fs; - fs = dfs_filesystem_lookup(path); - if (fs != RT_NULL) - { - if (fs->ops->statfs != RT_NULL) - return fs->ops->statfs(fs, buffer); - } + fs = dfs_filesystem_lookup(path); + if (fs != RT_NULL) + { + if (fs->ops->statfs != RT_NULL) + return fs->ops->statfs(fs, buffer); + } - return -1; + return -1; } #ifdef RT_USING_FINSH #include void mkfs(const char *fs_name, const char *device_name) { - dfs_mkfs(fs_name, device_name); + dfs_mkfs(fs_name, device_name); } FINSH_FUNCTION_EXPORT(mkfs, make a file system); void df(const char *path) { - int result; - struct statfs buffer; - - if (path == RT_NULL) - result = dfs_statfs("/", &buffer); - else - result = dfs_statfs(path, &buffer); - - if (result == 0) - { - rt_kprintf("disk free: %d block[%d bytes per block]\n", buffer.f_bfree, buffer.f_bsize); - } + int result; + struct statfs buffer; + + if (path == RT_NULL) + result = dfs_statfs("/", &buffer); + else + result = dfs_statfs(path, &buffer); + + if (result == 0) + { + rt_kprintf("disk free: %d block[%d bytes per block]\n", + buffer.f_bfree, buffer.f_bsize); + } } FINSH_FUNCTION_EXPORT(df, get disk free); #endif diff --git a/components/dfs/src/dfs_posix.c b/components/dfs/src/dfs_posix.c index fb69d484c0..2ccc1541d5 100644 --- a/components/dfs/src/dfs_posix.c +++ b/components/dfs/src/dfs_posix.c @@ -18,11 +18,12 @@ /** * @addtogroup FsPosixApi */ + /*@{*/ /** - * this function is a POSIX compliant version, which will open a file and return - * a file descriptor. + * this function is a POSIX compliant version, which will open a file and + * return a file descriptor. * * @param file the path name of file. * @param flags the file open flags. @@ -32,34 +33,35 @@ */ int open(const char *file, int flags, int mode) { - int fd, result; - struct dfs_fd *d; - - /* allocate a fd */ - fd = fd_new(); - if (fd < 0) - { - rt_set_errno(-DFS_STATUS_ENOMEM); - return -1; - } - d = fd_get(fd); - - result = dfs_file_open(d, file, flags); - if (result < 0) - { - /* release the ref-count of fd */ - fd_put(d); - fd_put(d); - - rt_set_errno(result); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return fd; + int fd, result; + struct dfs_fd *d; + + /* allocate a fd */ + fd = fd_new(); + if (fd < 0) + { + rt_set_errno(-DFS_STATUS_ENOMEM); + + return -1; + } + d = fd_get(fd); + + result = dfs_file_open(d, file, flags); + if (result < 0) + { + /* release the ref-count of fd */ + fd_put(d); + fd_put(d); + + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return fd; } RTM_EXPORT(open); @@ -73,35 +75,36 @@ RTM_EXPORT(open); */ int close(int fd) { - int result; - struct dfs_fd *d; + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); - d = fd_get(fd); - if (d == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); - return -1; - } + return -1; + } - result = dfs_file_close(d); - fd_put(d); + result = dfs_file_close(d); + fd_put(d); - if (result < 0) - { - rt_set_errno(result); + if (result < 0) + { + rt_set_errno(result); - return -1; - } + return -1; + } - fd_put(d); + fd_put(d); - return 0; + return 0; } RTM_EXPORT(close); /** - * this function is a POSIX compliant version, which will read specified data buffer - * length for an open file descriptor. + * this function is a POSIX compliant version, which will read specified data + * buffer length for an open file descriptor. * * @param fd the file descriptor. * @param buf the buffer to save the read data. @@ -111,36 +114,37 @@ RTM_EXPORT(close); */ int read(int fd, void *buf, size_t len) { - int result; - struct dfs_fd *d; - - /* get the fd */ - d = fd_get(fd); - if (d == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); - return -1; - } - - result = dfs_file_read(d, buf, len); - if (result < 0) - { - fd_put(d); - rt_set_errno(result); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return result; + int result; + struct dfs_fd *d; + + /* get the fd */ + d = fd_get(fd); + if (d == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); + + return -1; + } + + result = dfs_file_read(d, buf, len); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return result; } RTM_EXPORT(read); /** - * this function is a POSIX compliant version, which will write specified data buffer - * length for an open file descriptor. + * this function is a POSIX compliant version, which will write specified data + * buffer length for an open file descriptor. * * @param fd the file descriptor * @param buf the data buffer to be written. @@ -150,37 +154,37 @@ RTM_EXPORT(read); */ int write(int fd, const void *buf, size_t len) { - int result; - struct dfs_fd *d; + int result; + struct dfs_fd *d; - /* get the fd */ - d = fd_get(fd); - if (d == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); + /* get the fd */ + d = fd_get(fd); + if (d == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); - return -1; - } + return -1; + } - result = dfs_file_write(d, buf, len); - if (result < 0) - { - fd_put(d); - rt_set_errno(result); + result = dfs_file_write(d, buf, len); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); - return -1; - } + return -1; + } - /* release the ref-count of fd */ - fd_put(d); + /* release the ref-count of fd */ + fd_put(d); - return result; + return result; } RTM_EXPORT(write); /** - * this function is a POSIX compliant version, which will seek the offset for an - * open file descriptor. + * this function is a POSIX compliant version, which will seek the offset for + * an open file descriptor. * * @param fd the file descriptor. * @param offset the offset to be seeked. @@ -190,59 +194,61 @@ RTM_EXPORT(write); */ off_t lseek(int fd, off_t offset, int whence) { - int result; - struct dfs_fd *d; - - d = fd_get(fd); - if (d == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); - return -1; - } - - switch (whence) - { - case DFS_SEEK_SET: - break; - - case DFS_SEEK_CUR: - offset += d->pos; - break; - - case DFS_SEEK_END: - offset += d->size; - break; - - default: - rt_set_errno(-DFS_STATUS_EINVAL); - return -1; - } - - if (offset < 0) - { - rt_set_errno(-DFS_STATUS_EINVAL); - - return -1; - } - result = dfs_file_lseek(d, offset); - if (result < 0) - { - fd_put(d); - rt_set_errno(result); - - return -1; - } - - /* release the ref-count of fd */ - fd_put(d); - - return offset; + int result; + struct dfs_fd *d; + + d = fd_get(fd); + if (d == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); + + return -1; + } + + switch (whence) + { + case DFS_SEEK_SET: + break; + + case DFS_SEEK_CUR: + offset += d->pos; + break; + + case DFS_SEEK_END: + offset += d->size; + break; + + default: + rt_set_errno(-DFS_STATUS_EINVAL); + + return -1; + } + + if (offset < 0) + { + rt_set_errno(-DFS_STATUS_EINVAL); + + return -1; + } + result = dfs_file_lseek(d, offset); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); + + return -1; + } + + /* release the ref-count of fd */ + fd_put(d); + + return offset; } RTM_EXPORT(lseek); /** - * this function is a POSIX compliant version, which will rename old file name to - * new file name. + * this function is a POSIX compliant version, which will rename old file name + * to new file name. * * @param old the old file name. * @param new the new file name. @@ -253,17 +259,17 @@ RTM_EXPORT(lseek); */ int rename(const char *old, const char *new) { - int result; + int result; - result = dfs_file_rename(old, new); - if (result < 0) - { - rt_set_errno(result); + result = dfs_file_rename(old, new); + if (result < 0) + { + rt_set_errno(result); - return -1; - } + return -1; + } - return 0; + return 0; } RTM_EXPORT(rename); @@ -277,17 +283,17 @@ RTM_EXPORT(rename); */ int unlink(const char *pathname) { - int result; + int result; - result = dfs_file_unlink(pathname); - if (result < 0) - { - rt_set_errno(result); + result = dfs_file_unlink(pathname); + if (result < 0) + { + rt_set_errno(result); - return -1; - } + return -1; + } - return 0; + return 0; } RTM_EXPORT(unlink); @@ -301,17 +307,17 @@ RTM_EXPORT(unlink); */ int stat(const char *file, struct stat *buf) { - int result; + int result; - result = dfs_file_stat(file, buf); - if (result < 0) - { - rt_set_errno(result); + result = dfs_file_stat(file, buf); + if (result < 0) + { + rt_set_errno(result); - return -1; - } + return -1; + } - return result; + return result; } RTM_EXPORT(stat); @@ -323,35 +329,35 @@ RTM_EXPORT(stat); */ int fstat(int fildes, struct stat *buf) { - struct dfs_fd *d; + struct dfs_fd *d; - /* get the fd */ - d = fd_get(fildes); - if (d == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); + /* get the fd */ + d = fd_get(fildes); + if (d == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); - return -1; - } + return -1; + } - /* it's the root directory */ - buf->st_dev = 0; + /* it's the root directory */ + buf->st_dev = 0; - buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | - DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; - if (d->type == FT_DIRECTORY) - { - buf->st_mode &= ~DFS_S_IFREG; - buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; - } + buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | + DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; + if (d->type == FT_DIRECTORY) + { + buf->st_mode &= ~DFS_S_IFREG; + buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; + } - buf->st_size = d->size; - buf->st_mtime = 0; - buf->st_blksize = 512; + buf->st_size = d->size; + buf->st_mtime = 0; + buf->st_blksize = 512; - fd_put(d); + fd_put(d); - return DFS_STATUS_OK; + return DFS_STATUS_OK; } RTM_EXPORT(fstat); @@ -366,17 +372,17 @@ RTM_EXPORT(fstat); */ int statfs(const char *path, struct statfs *buf) { - int result; + int result; - result = dfs_statfs(path, buf); - if (result < 0) - { - rt_set_errno(result); + result = dfs_statfs(path, buf); + if (result < 0) + { + rt_set_errno(result); - return -1; - } + return -1; + } - return result; + return result; } RTM_EXPORT(statfs); @@ -390,34 +396,34 @@ RTM_EXPORT(statfs); */ int mkdir(const char *path, mode_t mode) { - int fd; - struct dfs_fd *d; - int result; + int fd; + struct dfs_fd *d; + int result; - fd = fd_new(); - if (fd == -1) - { - rt_set_errno(-DFS_STATUS_ENOMEM); + fd = fd_new(); + if (fd == -1) + { + rt_set_errno(-DFS_STATUS_ENOMEM); - return -1; - } + return -1; + } - d = fd_get(fd); + d = fd_get(fd); - result = dfs_file_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT); + result = dfs_file_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT); - if (result < 0) - { - fd_put(d); - rt_set_errno(result); + if (result < 0) + { + fd_put(d); + rt_set_errno(result); - return -1; - } + return -1; + } - dfs_file_close(d); - fd_put(d); + dfs_file_close(d); + fd_put(d); - return 0; + return 0; } RTM_EXPORT(mkdir); @@ -435,17 +441,17 @@ FINSH_FUNCTION_EXPORT(mkdir, create a directory); */ int rmdir(const char *pathname) { - int result; + int result; - result = dfs_file_unlink(pathname); - if (result < 0) - { - rt_set_errno(result); + result = dfs_file_unlink(pathname); + if (result < 0) + { + rt_set_errno(result); - return -1; - } + return -1; + } - return 0; + return 0; } RTM_EXPORT(rmdir); @@ -458,48 +464,48 @@ RTM_EXPORT(rmdir); */ DIR *opendir(const char *name) { - struct dfs_fd *d; - int fd, result; - DIR *t; - - t = RT_NULL; - - /* allocate a fd */ - fd = fd_new(); - if (fd == -1) - { - rt_set_errno(-DFS_STATUS_ENOMEM); - - return RT_NULL; - } - d = fd_get(fd); - - result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY); - if (result >= 0) - { - /* open successfully */ - t = (DIR *) rt_malloc(sizeof(DIR)); - if (t == RT_NULL) - { - dfs_file_close(d); - fd_put(d); - } - else - { - rt_memset(t, 0, sizeof(DIR)); - t->fd = fd; - } - fd_put(d); - - return t; - } - - /* open failed */ - fd_put(d); - fd_put(d); - rt_set_errno(result); - - return RT_NULL; + struct dfs_fd *d; + int fd, result; + DIR *t; + + t = RT_NULL; + + /* allocate a fd */ + fd = fd_new(); + if (fd == -1) + { + rt_set_errno(-DFS_STATUS_ENOMEM); + + return RT_NULL; + } + d = fd_get(fd); + + result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY); + if (result >= 0) + { + /* open successfully */ + t = (DIR *) rt_malloc(sizeof(DIR)); + if (t == RT_NULL) + { + dfs_file_close(d); + fd_put(d); + } + else + { + rt_memset(t, 0, sizeof(DIR)); + t->fd = fd; + } + fd_put(d); + + return t; + } + + /* open failed */ + fd_put(d); + fd_put(d); + rt_set_errno(result); + + return RT_NULL; } RTM_EXPORT(opendir); @@ -514,36 +520,39 @@ RTM_EXPORT(opendir); */ struct dirent *readdir(DIR *d) { - int result; - struct dfs_fd *fd; - - fd = fd_get(d->fd); - if (fd == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); - - return RT_NULL; - } - - if (!d->num || (d->cur += ((struct dirent*)(d->buf + d->cur))->d_reclen) >= d->num) - { - /* get a new entry */ - result = dfs_file_getdents(fd, (struct dirent*)d->buf, sizeof(d->buf) - 1); - if (result <= 0) - { - fd_put(fd); - rt_set_errno(result); - - return RT_NULL; - } - - d->num = result; - d->cur = 0; /* current entry index */ - } - - fd_put(fd); - - return (struct dirent *)(d->buf+d->cur); + int result; + struct dfs_fd *fd; + + fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); + + return RT_NULL; + } + + if (!d->num || + (d->cur += ((struct dirent *)(d->buf + d->cur))->d_reclen) >= d->num) + { + /* get a new entry */ + result = dfs_file_getdents(fd, + (struct dirent*)d->buf, + sizeof(d->buf) - 1); + if (result <= 0) + { + fd_put(fd); + rt_set_errno(result); + + return RT_NULL; + } + + d->num = result; + d->cur = 0; /* current entry index */ + } + + fd_put(fd); + + return (struct dirent *)(d->buf+d->cur); } RTM_EXPORT(readdir); @@ -557,21 +566,21 @@ RTM_EXPORT(readdir); */ long telldir(DIR *d) { - struct dfs_fd *fd; - long result; + struct dfs_fd *fd; + long result; - fd = fd_get(d->fd); - if (fd == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); + fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); - return 0; - } + return 0; + } - result = fd->pos - d->num + d->cur; - fd_put(fd); + result = fd->pos - d->num + d->cur; + fd_put(fd); - return result; + return result; } RTM_EXPORT(telldir); @@ -584,44 +593,45 @@ RTM_EXPORT(telldir); */ void seekdir(DIR *d, off_t offset) { - struct dfs_fd *fd; + struct dfs_fd *fd; - fd = fd_get(d->fd); - if (fd == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); + fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); - return ; - } + return ; + } - /* seek to the offset position of directory */ - if (dfs_file_lseek(fd, offset) >= 0) - d->num = d->cur = 0; - fd_put(fd); + /* seek to the offset position of directory */ + if (dfs_file_lseek(fd, offset) >= 0) + d->num = d->cur = 0; + fd_put(fd); } RTM_EXPORT(seekdir); /** - * this function is a POSIX compliant version, which will reset directory stream. + * this function is a POSIX compliant version, which will reset directory + * stream. * * @param d the directory stream. */ void rewinddir(DIR *d) { - struct dfs_fd *fd; + struct dfs_fd *fd; - fd = fd_get(d->fd); - if (fd == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); + fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); - return ; - } + return ; + } - /* seek to the beginning of directory */ - if (dfs_file_lseek(fd, 0) >= 0) - d->num = d->cur = 0; - fd_put(fd); + /* seek to the beginning of directory */ + if (dfs_file_lseek(fd, 0) >= 0) + d->num = d->cur = 0; + fd_put(fd); } RTM_EXPORT(rewinddir); @@ -635,37 +645,38 @@ RTM_EXPORT(rewinddir); */ int closedir(DIR *d) { - int result; - struct dfs_fd *fd; + int result; + struct dfs_fd *fd; - fd = fd_get(d->fd); - if (fd == RT_NULL) - { - rt_set_errno(-DFS_STATUS_EBADF); + fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-DFS_STATUS_EBADF); - return -1; - } + return -1; + } - result = dfs_file_close(fd); - fd_put(fd); + result = dfs_file_close(fd); + fd_put(fd); - fd_put(fd); - rt_free(d); + fd_put(fd); + rt_free(d); - if (result < 0) - { - rt_set_errno(result); + if (result < 0) + { + rt_set_errno(result); - return -1; - } - else - return 0; + return -1; + } + else + return 0; } RTM_EXPORT(closedir); #ifdef DFS_USING_WORKDIR /** - * this function is a POSIX compliant version, which will change working directory. + * this function is a POSIX compliant version, which will change working + * directory. * * @param path the path name to be changed to. * @@ -673,54 +684,55 @@ RTM_EXPORT(closedir); */ int chdir(const char *path) { - char *fullpath; - DIR *d; + char *fullpath; + DIR *d; - if (path == RT_NULL) - { - dfs_lock(); - rt_kprintf("%s\n", working_directory); - dfs_unlock(); + if (path == RT_NULL) + { + dfs_lock(); + rt_kprintf("%s\n", working_directory); + dfs_unlock(); - return 0; - } + return 0; + } - if (rt_strlen(path) > DFS_PATH_MAX) - { - rt_set_errno(-DFS_STATUS_ENOTDIR); + if (rt_strlen(path) > DFS_PATH_MAX) + { + rt_set_errno(-DFS_STATUS_ENOTDIR); - return -1; - } + return -1; + } - fullpath = dfs_normalize_path(NULL, path); - if (fullpath == RT_NULL) - { - rt_set_errno(-DFS_STATUS_ENOTDIR); + fullpath = dfs_normalize_path(NULL, path); + if (fullpath == RT_NULL) + { + rt_set_errno(-DFS_STATUS_ENOTDIR); - return -1; /* build path failed */ - } + return -1; /* build path failed */ + } - dfs_lock(); - d = opendir(fullpath); - if (d == RT_NULL) - { - rt_free(fullpath); - /* this is a not exist directory */ - dfs_unlock(); + dfs_lock(); + d = opendir(fullpath); + if (d == RT_NULL) + { + rt_free(fullpath); + /* this is a not exist directory */ + dfs_unlock(); - return -1; - } + return -1; + } - /* close directory stream */ - closedir(d); + /* close directory stream */ + closedir(d); - /* copy full path to working directory */ - strncpy(working_directory, fullpath, DFS_PATH_MAX); - rt_free(fullpath); /* release normalize directory path name */ + /* copy full path to working directory */ + strncpy(working_directory, fullpath, DFS_PATH_MAX); + /* release normalize directory path name */ + rt_free(fullpath); - dfs_unlock(); + dfs_unlock(); - return 0; + return 0; } RTM_EXPORT(chdir); @@ -741,14 +753,14 @@ FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory); char *getcwd(char *buf, size_t size) { #ifdef DFS_USING_WORKDIR - rt_enter_critical(); - rt_strncpy(buf, working_directory, size); - rt_exit_critical(); + rt_enter_critical(); + rt_strncpy(buf, working_directory, size); + rt_exit_critical(); #else - rt_kprintf("WARNING: not support working directory\n"); + rt_kprintf("WARNING: not support working directory\n"); #endif - return buf; + return buf; } RTM_EXPORT(getcwd); -- GitLab