fixed the coding style in components/dfs

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2530 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
dzzxzz@gmail.com 2012-12-26 01:12:13 +00:00
parent ef5c47e684
commit 69521e2c8f
11 changed files with 1540 additions and 1485 deletions

View File

@ -1,7 +1,7 @@
/* /*
* File : dfs.h * File : dfs.h
* This file is part of Device File System in RT-Thread RTOS * 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 * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at

View File

@ -1,7 +1,7 @@
/* /*
* File : dfs_def.h * File : dfs_def.h
* This file is part of Device File System in RT-Thread RTOS * 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 * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -23,278 +23,278 @@
#define __D_FS__ #define __D_FS__
#endif #endif
#define DEVICE_GETGEOME 0 #define DEVICE_GETGEOME 0
#define DEVICE_GETINFO 1 #define DEVICE_GETINFO 1
#define DEVICE_FORMAT 2 #define DEVICE_FORMAT 2
#define DEVICE_CLEAN_SECTOR 3 #define DEVICE_CLEAN_SECTOR 3
/* File flags */ /* File flags */
#define DFS_F_OPEN 0x01000000 #define DFS_F_OPEN 0x01000000
#define DFS_F_DIRECTORY 0x02000000 #define DFS_F_DIRECTORY 0x02000000
#define DFS_F_EOF 0x04000000 #define DFS_F_EOF 0x04000000
#define DFS_F_ERR 0x08000000 #define DFS_F_ERR 0x08000000
#ifndef DFS_PATH_MAX #ifndef DFS_PATH_MAX
#define DFS_PATH_MAX 256 #define DFS_PATH_MAX 256
#endif #endif
#ifndef SECTOR_SIZE #ifndef SECTOR_SIZE
#define SECTOR_SIZE 512 #define SECTOR_SIZE 512
#endif #endif
#ifndef DFS_FILESYSTEM_TYPES_MAX #ifndef DFS_FILESYSTEM_TYPES_MAX
#define DFS_FILESYSTEM_TYPES_MAX 4 #define DFS_FILESYSTEM_TYPES_MAX 4
#endif #endif
#define DFS_DEBUG_INFO 0x01 #define DFS_DEBUG_INFO 0x01
#define DFS_DEBUG_WARNING 0x02 #define DFS_DEBUG_WARNING 0x02
#define DFS_DEBUG_ERROR 0x04 #define DFS_DEBUG_ERROR 0x04
#define DFS_DEBUG_LEVEL (DFS_DEBUG_INFO | DFS_DEBUG_WARNING | DFS_DEBUG_ERROR) #define DFS_DEBUG_LEVEL (DFS_DEBUG_INFO | DFS_DEBUG_WARNING | DFS_DEBUG_ERROR)
/* #define DFS_DEBUG */ /* #define DFS_DEBUG */
#ifdef DFS_DEBUG #ifdef DFS_DEBUG
#define dfs_log(level, x) do { if (level & DFS_DEBUG_LEVEL) \ #define dfs_log(level, x) do { if (level & DFS_DEBUG_LEVEL) \
{rt_kprintf("DFS %s, %d:", __FUNCTION__, __LINE__); rt_kprintf x; \ {rt_kprintf("DFS %s, %d:", __FUNCTION__, __LINE__); rt_kprintf x; \
rt_kprintf ("\n");}}while (0) rt_kprintf ("\n");}}while (0)
#else #else
#define dfs_log(level, x) #define dfs_log(level, x)
#endif #endif
#if defined(RT_USING_NEWLIB) #if defined(RT_USING_NEWLIB)
#include <string.h> #include <string.h>
#include <sys/stat.h> /* used for struct stat */ #include <sys/stat.h> /* used for struct stat */
#include <sys/statfs.h> /* used for struct statfs */ #include <sys/statfs.h> /* used for struct statfs */
#include <sys/errno.h> /* used for error number */ #include <sys/errno.h> /* used for error number */
#include <sys/fcntl.h> /* used for operation flags */ #include <sys/fcntl.h> /* used for operation flags */
#include <sys/unistd.h> /* used for SEEK_SET/CUR/END */ #include <sys/unistd.h> /* used for SEEK_SET/CUR/END */
#include <dirent.h> /* used for struct dirent */ #include <dirent.h> /* used for struct dirent */
/* Device error codes */ /* Device error codes */
#define DFS_STATUS_OK 0 /* no error */ #define DFS_STATUS_OK 0 /* no error */
#define DFS_STATUS_ENOENT ENOENT /* No such file or directory */ #define DFS_STATUS_ENOENT ENOENT /* No such file or directory */
#define DFS_STATUS_EIO EIO /* I/O error */ #define DFS_STATUS_EIO EIO /* I/O error */
#define DFS_STATUS_ENXIO ENXIO /* No such device or address */ #define DFS_STATUS_ENXIO ENXIO /* No such device or address */
#define DFS_STATUS_EBADF EBADF /* Bad file number */ #define DFS_STATUS_EBADF EBADF /* Bad file number */
#define DFS_STATUS_EAGAIN EAGAIN /* Try again */ #define DFS_STATUS_EAGAIN EAGAIN /* Try again */
#define DFS_STATUS_ENOMEM ENOMEM /* no memory */ #define DFS_STATUS_ENOMEM ENOMEM /* no memory */
#define DFS_STATUS_EBUSY EBUSY /* Device or resource busy */ #define DFS_STATUS_EBUSY EBUSY /* Device or resource busy */
#define DFS_STATUS_EEXIST EEXIST /* File exists */ #define DFS_STATUS_EEXIST EEXIST /* File exists */
#define DFS_STATUS_EXDEV EXDEV /* Cross-device link */ #define DFS_STATUS_EXDEV EXDEV /* Cross-device link */
#define DFS_STATUS_ENODEV ENODEV /* No such device */ #define DFS_STATUS_ENODEV ENODEV /* No such device */
#define DFS_STATUS_ENOTDIR ENOTDIR /* Not a directory */ #define DFS_STATUS_ENOTDIR ENOTDIR /* Not a directory */
#define DFS_STATUS_EISDIR EISDIR /* Is a directory */ #define DFS_STATUS_EISDIR EISDIR /* Is a directory */
#define DFS_STATUS_EINVAL EINVAL /* Invalid argument */ #define DFS_STATUS_EINVAL EINVAL /* Invalid argument */
#define DFS_STATUS_ENOSPC ENOSPC /* No space left on device */ #define DFS_STATUS_ENOSPC ENOSPC /* No space left on device */
#define DFS_STATUS_EROFS EROFS /* Read-only file system */ #define DFS_STATUS_EROFS EROFS /* Read-only file system */
#define DFS_STATUS_ENOSYS ENOSYS /* Function not implemented */ #define DFS_STATUS_ENOSYS ENOSYS /* Function not implemented */
#define DFS_STATUS_ENOTEMPTY ENOTEMPTY /* Directory not empty */ #define DFS_STATUS_ENOTEMPTY ENOTEMPTY /* Directory not empty */
/* Operation flags */ /* Operation flags */
#define DFS_O_RDONLY O_RDONLY #define DFS_O_RDONLY O_RDONLY
#define DFS_O_WRONLY O_WRONLY #define DFS_O_WRONLY O_WRONLY
#define DFS_O_RDWR O_RDWR #define DFS_O_RDWR O_RDWR
#define DFS_O_ACCMODE O_ACCMODE #define DFS_O_ACCMODE O_ACCMODE
#define DFS_O_CREAT O_CREAT #define DFS_O_CREAT O_CREAT
#define DFS_O_EXCL O_EXCL #define DFS_O_EXCL O_EXCL
#define DFS_O_TRUNC O_TRUNC #define DFS_O_TRUNC O_TRUNC
#define DFS_O_APPEND O_APPEND #define DFS_O_APPEND O_APPEND
#define DFS_O_DIRECTORY O_DIRECTORY #define DFS_O_DIRECTORY O_DIRECTORY
/* Seek flags */ /* Seek flags */
#define DFS_SEEK_SET SEEK_SET #define DFS_SEEK_SET SEEK_SET
#define DFS_SEEK_CUR SEEK_CUR #define DFS_SEEK_CUR SEEK_CUR
#define DFS_SEEK_END SEEK_END #define DFS_SEEK_END SEEK_END
/* Stat codes */ /* Stat codes */
#define DFS_S_IFMT S_IFMT #define DFS_S_IFMT S_IFMT
#define DFS_S_IFSOCK S_IFSOCK #define DFS_S_IFSOCK S_IFSOCK
#define DFS_S_IFLNK S_IFLNK #define DFS_S_IFLNK S_IFLNK
#define DFS_S_IFREG S_IFREG #define DFS_S_IFREG S_IFREG
#define DFS_S_IFBLK S_IFBLK #define DFS_S_IFBLK S_IFBLK
#define DFS_S_IFDIR S_IFDIR #define DFS_S_IFDIR S_IFDIR
#define DFS_S_IFCHR S_IFCHR #define DFS_S_IFCHR S_IFCHR
#define DFS_S_IFIFO S_IFIFO #define DFS_S_IFIFO S_IFIFO
#define DFS_S_ISUID S_ISUID #define DFS_S_ISUID S_ISUID
#define DFS_S_ISGID S_ISGID #define DFS_S_ISGID S_ISGID
#define DFS_S_ISVTX S_ISVTX #define DFS_S_ISVTX S_ISVTX
#define DFS_S_ISLNK(m) S_ISLNK(m) #define DFS_S_ISLNK(m) S_ISLNK(m)
#define DFS_S_ISREG(m) S_ISREG(m) #define DFS_S_ISREG(m) S_ISREG(m)
#define DFS_S_ISDIR(m) S_ISDIR(m) #define DFS_S_ISDIR(m) S_ISDIR(m)
#define DFS_S_ISCHR(m) S_ISCHR(m) #define DFS_S_ISCHR(m) S_ISCHR(m)
#define DFS_S_ISBLK(m) S_ISBLK(m) #define DFS_S_ISBLK(m) S_ISBLK(m)
#define DFS_S_ISFIFO(m) S_ISFIFO(m) #define DFS_S_ISFIFO(m) S_ISFIFO(m)
#define DFS_S_ISSOCK(m) S_ISSOCK(m) #define DFS_S_ISSOCK(m) S_ISSOCK(m)
#define DFS_S_IRWXU S_IRWXU #define DFS_S_IRWXU S_IRWXU
#define DFS_S_IRUSR S_IRUSR #define DFS_S_IRUSR S_IRUSR
#define DFS_S_IWUSR S_IWUSR #define DFS_S_IWUSR S_IWUSR
#define DFS_S_IXUSR S_IXUSR #define DFS_S_IXUSR S_IXUSR
#define DFS_S_IRWXG S_IRWXG #define DFS_S_IRWXG S_IRWXG
#define DFS_S_IRGRP S_IRGRP #define DFS_S_IRGRP S_IRGRP
#define DFS_S_IWGRP S_IWGRP #define DFS_S_IWGRP S_IWGRP
#define DFS_S_IXGRP S_IXGRP #define DFS_S_IXGRP S_IXGRP
#define DFS_S_IRWXO S_IRWXO #define DFS_S_IRWXO S_IRWXO
#define DFS_S_IROTH S_IROTH #define DFS_S_IROTH S_IROTH
#define DFS_S_IWOTH S_IWOTH #define DFS_S_IWOTH S_IWOTH
#define DFS_S_IXOTH S_IXOTH #define DFS_S_IXOTH S_IXOTH
/* Dirent types */ /* Dirent types */
#define DFS_DT_UNKNOWN DT_UNKNOWN #define DFS_DT_UNKNOWN DT_UNKNOWN
#define DFS_DT_REG DT_REG #define DFS_DT_REG DT_REG
#define DFS_DT_DIR DT_DIR #define DFS_DT_DIR DT_DIR
#else #else
#ifdef RT_USING_MINILIBC #ifdef RT_USING_MINILIBC
#include <string.h> #include <string.h>
#else #else
typedef long off_t; typedef long off_t;
typedef int mode_t; typedef int mode_t;
#endif #endif
/* Device error codes */ /* Device error codes */
#define DFS_STATUS_OK 0 /* no error */ #define DFS_STATUS_OK 0 /* no error */
#define DFS_STATUS_ENOENT 2 /* No such file or directory */ #define DFS_STATUS_ENOENT 2 /* No such file or directory */
#define DFS_STATUS_EIO 5 /* I/O error */ #define DFS_STATUS_EIO 5 /* I/O error */
#define DFS_STATUS_ENXIO 6 /* No such device or address */ #define DFS_STATUS_ENXIO 6 /* No such device or address */
#define DFS_STATUS_EBADF 9 /* Bad file number */ #define DFS_STATUS_EBADF 9 /* Bad file number */
#define DFS_STATUS_EAGAIN 11 /* Try again */ #define DFS_STATUS_EAGAIN 11 /* Try again */
#define DFS_STATUS_ENOMEM 12 /* no memory */ #define DFS_STATUS_ENOMEM 12 /* no memory */
#define DFS_STATUS_EBUSY 16 /* Device or resource busy */ #define DFS_STATUS_EBUSY 16 /* Device or resource busy */
#define DFS_STATUS_EEXIST 17 /* File exists */ #define DFS_STATUS_EEXIST 17 /* File exists */
#define DFS_STATUS_EXDEV 18 /* Cross-device link */ #define DFS_STATUS_EXDEV 18 /* Cross-device link */
#define DFS_STATUS_ENODEV 19 /* No such device */ #define DFS_STATUS_ENODEV 19 /* No such device */
#define DFS_STATUS_ENOTDIR 20 /* Not a directory */ #define DFS_STATUS_ENOTDIR 20 /* Not a directory */
#define DFS_STATUS_EISDIR 21 /* Is a directory */ #define DFS_STATUS_EISDIR 21 /* Is a directory */
#define DFS_STATUS_EINVAL 22 /* Invalid argument */ #define DFS_STATUS_EINVAL 22 /* Invalid argument */
#define DFS_STATUS_ENOSPC 28 /* No space left on device */ #define DFS_STATUS_ENOSPC 28 /* No space left on device */
#define DFS_STATUS_EROFS 30 /* Read-only file system */ #define DFS_STATUS_EROFS 30 /* Read-only file system */
#define DFS_STATUS_ENOSYS 38 /* Function not implemented */ #define DFS_STATUS_ENOSYS 38 /* Function not implemented */
#define DFS_STATUS_ENOTEMPTY 39 /* Directory not empty */ #define DFS_STATUS_ENOTEMPTY 39 /* Directory not empty */
/* Operation flags */ /* Operation flags */
#define DFS_O_RDONLY 0x0000000 #define DFS_O_RDONLY 0x0000000
#define DFS_O_WRONLY 0x0000001 #define DFS_O_WRONLY 0x0000001
#define DFS_O_RDWR 0x0000002 #define DFS_O_RDWR 0x0000002
#define DFS_O_ACCMODE 0x0000003 #define DFS_O_ACCMODE 0x0000003
#define DFS_O_CREAT 0x0000100 #define DFS_O_CREAT 0x0000100
#define DFS_O_EXCL 0x0000200 #define DFS_O_EXCL 0x0000200
#define DFS_O_TRUNC 0x0001000 #define DFS_O_TRUNC 0x0001000
#define DFS_O_APPEND 0x0002000 #define DFS_O_APPEND 0x0002000
#define DFS_O_BINARY 0x0008000 #define DFS_O_BINARY 0x0008000
#define DFS_O_DIRECTORY 0x0200000 #define DFS_O_DIRECTORY 0x0200000
/* File flags */ /* File flags */
#define DFS_F_OPEN 0x01000000 #define DFS_F_OPEN 0x01000000
#define DFS_F_DIRECTORY 0x02000000 #define DFS_F_DIRECTORY 0x02000000
#define DFS_F_EOF 0x04000000 #define DFS_F_EOF 0x04000000
#define DFS_F_ERR 0x08000000 #define DFS_F_ERR 0x08000000
/* Seek flags */ /* Seek flags */
#ifdef __CC_ARM #ifdef __CC_ARM
#include <stdio.h> #include <stdio.h>
#define DFS_SEEK_SET SEEK_SET #define DFS_SEEK_SET SEEK_SET
#define DFS_SEEK_CUR SEEK_CUR #define DFS_SEEK_CUR SEEK_CUR
#define DFS_SEEK_END SEEK_END #define DFS_SEEK_END SEEK_END
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
#include <stdio.h> #include <stdio.h>
#define DFS_SEEK_SET SEEK_SET #define DFS_SEEK_SET SEEK_SET
#define DFS_SEEK_CUR SEEK_CUR #define DFS_SEEK_CUR SEEK_CUR
#define DFS_SEEK_END SEEK_END #define DFS_SEEK_END SEEK_END
#else #else
#define DFS_SEEK_SET 0 #define DFS_SEEK_SET 0
#define DFS_SEEK_CUR 1 #define DFS_SEEK_CUR 1
#define DFS_SEEK_END 2 #define DFS_SEEK_END 2
#endif #endif
/* Stat codes */ /* Stat codes */
#define DFS_S_IFMT 00170000 #define DFS_S_IFMT 00170000
#define DFS_S_IFSOCK 0140000 #define DFS_S_IFSOCK 0140000
#define DFS_S_IFLNK 0120000 #define DFS_S_IFLNK 0120000
#define DFS_S_IFREG 0100000 #define DFS_S_IFREG 0100000
#define DFS_S_IFBLK 0060000 #define DFS_S_IFBLK 0060000
#define DFS_S_IFDIR 0040000 #define DFS_S_IFDIR 0040000
#define DFS_S_IFCHR 0020000 #define DFS_S_IFCHR 0020000
#define DFS_S_IFIFO 0010000 #define DFS_S_IFIFO 0010000
#define DFS_S_ISUID 0004000 #define DFS_S_ISUID 0004000
#define DFS_S_ISGID 0002000 #define DFS_S_ISGID 0002000
#define DFS_S_ISVTX 0001000 #define DFS_S_ISVTX 0001000
#define DFS_S_ISLNK(m) (((m) & DFS_S_IFMT) == DFS_S_IFLNK) #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_ISREG(m) (((m) & DFS_S_IFMT) == DFS_S_IFREG)
#define DFS_S_ISDIR(m) (((m) & DFS_S_IFMT) == DFS_S_IFDIR) #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_ISCHR(m) (((m) & DFS_S_IFMT) == DFS_S_IFCHR)
#define DFS_S_ISBLK(m) (((m) & DFS_S_IFMT) == DFS_S_IFBLK) #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_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO)
#define DFS_S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK) #define DFS_S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK)
#define DFS_S_IRWXU 00700 #define DFS_S_IRWXU 00700
#define DFS_S_IRUSR 00400 #define DFS_S_IRUSR 00400
#define DFS_S_IWUSR 00200 #define DFS_S_IWUSR 00200
#define DFS_S_IXUSR 00100 #define DFS_S_IXUSR 00100
#define DFS_S_IRWXG 00070 #define DFS_S_IRWXG 00070
#define DFS_S_IRGRP 00040 #define DFS_S_IRGRP 00040
#define DFS_S_IWGRP 00020 #define DFS_S_IWGRP 00020
#define DFS_S_IXGRP 00010 #define DFS_S_IXGRP 00010
#define DFS_S_IRWXO 00007 #define DFS_S_IRWXO 00007
#define DFS_S_IROTH 00004 #define DFS_S_IROTH 00004
#define DFS_S_IWOTH 00002 #define DFS_S_IWOTH 00002
#define DFS_S_IXOTH 00001 #define DFS_S_IXOTH 00001
struct stat struct stat
{ {
rt_device_t st_dev; rt_device_t st_dev;
rt_uint16_t st_mode; rt_uint16_t st_mode;
rt_uint32_t st_size; rt_uint32_t st_size;
rt_time_t st_mtime; rt_time_t st_mtime;
rt_uint32_t st_blksize; rt_uint32_t st_blksize;
}; };
struct statfs struct statfs
{ {
rt_size_t f_bsize; /* block size */ rt_size_t f_bsize; /* block size */
rt_size_t f_blocks; /* total data blocks in file system */ 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_bfree; /* free blocks in file system */
}; };
/* File types */ /* File types */
#define FT_REGULAR 0 /* regular file */ #define FT_REGULAR 0 /* regular file */
#define FT_SOCKET 1 /* socket file */ #define FT_SOCKET 1 /* socket file */
#define FT_DIRECTORY 2 /* directory */ #define FT_DIRECTORY 2 /* directory */
#define FT_USER 3 /* user defined */ #define FT_USER 3 /* user defined */
/* Dirent types */ /* Dirent types */
#define DFS_DT_UNKNOWN 0x00 #define DFS_DT_UNKNOWN 0x00
#define DFS_DT_REG 0x01 #define DFS_DT_REG 0x01
#define DFS_DT_DIR 0x02 #define DFS_DT_DIR 0x02
struct dirent struct dirent
{ {
rt_uint8_t d_type; /* The type of the file */ 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_uint8_t d_namlen; /* The length of the not including the terminating null file name */
rt_uint16_t d_reclen; /* length of this record */ rt_uint16_t d_reclen; /* length of this record */
char d_name[DFS_PATH_MAX]; /* The null-terminated file name */ char d_name[DFS_PATH_MAX]; /* The null-terminated file name */
}; };
#endif #endif
/* file descriptor */ /* file descriptor */
struct dfs_fd struct dfs_fd
{ {
char *path; /* Name (below mount point) */ char *path; /* Name (below mount point) */
int type; /* Type (regular or socket) */ int type; /* Type (regular or socket) */
int ref_count; /* Descriptor reference count */ 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_uint32_t flags; /* Descriptor flags */
rt_size_t size; /* Size in bytes */ rt_size_t size; /* Size in bytes */
rt_off_t pos; /* Current file position */ rt_off_t pos; /* Current file position */
void *data; /* Specific file system data */ void *data; /* Specific file system data */
}; };
#endif #endif

View File

@ -1,7 +1,7 @@
/* /*
* File : dfs_elm.h * File : dfs_elm.h
* This file is part of Device File System in RT-Thread RTOS * 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 * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at

View File

@ -1,7 +1,7 @@
/* /*
* File : dfs_file.c * File : dfs_file.h
* This file is part of Device File System in RT-Thread RTOS * 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 * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -12,8 +12,8 @@
* 2005-01-26 Bernard The first version. * 2005-01-26 Bernard The first version.
*/ */
#ifndef __DFS_RAW_H__ #ifndef __DFS_FILE_H__
#define __DFS_RAW_H__ #define __DFS_FILE_H__
#include <dfs_def.h> #include <dfs_def.h>
#include <dfs.h> #include <dfs.h>

View File

@ -1,7 +1,7 @@
/* /*
* File : dfs_fs.h * File : dfs_fs.h
* This file is part of Device File System in RT-Thread RTOS * 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 * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -17,8 +17,8 @@
#include <dfs_def.h> #include <dfs_def.h>
#define DFS_FS_FLAG_DEFAULT 0x00 /* default flag */ #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_FULLPATH 0x01 /* set full path to underlaying file system */
/* Pre-declaration */ /* Pre-declaration */
struct dfs_filesystem; struct dfs_filesystem;
@ -27,58 +27,62 @@ struct dfs_fd;
/* File system operations struct */ /* File system operations struct */
struct dfs_filesystem_operation struct dfs_filesystem_operation
{ {
char *name; char *name;
rt_uint32_t flags; /* flags for file system operations */ rt_uint32_t flags; /* flags for file system operations */
/* mount and unmount file system */ /* mount and unmount file system */
int (*mount) (struct dfs_filesystem *fs, unsigned long rwflag, const void *data); int (*mount) (struct dfs_filesystem *fs, unsigned long rwflag, const void *data);
int (*unmount) (struct dfs_filesystem *fs); int (*unmount) (struct dfs_filesystem *fs);
/* make a file system */ /* make a file system */
int (*mkfs) (const char *device_name); int (*mkfs) (const char *device_name);
int (*statfs) (struct dfs_filesystem *fs, struct statfs *buf); int (*statfs) (struct dfs_filesystem *fs, struct statfs *buf);
int (*open) (struct dfs_fd *fd); int (*open) (struct dfs_fd *fd);
int (*close) (struct dfs_fd *fd); int (*close) (struct dfs_fd *fd);
int (*ioctl) (struct dfs_fd *fd, int cmd, void *args); int (*ioctl) (struct dfs_fd *fd, int cmd, void *args);
int (*read) (struct dfs_fd *fd, void *buf, rt_size_t count); 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 (*write) (struct dfs_fd *fd, const void *buf, rt_size_t count);
int (*flush) (struct dfs_fd *fd); int (*flush) (struct dfs_fd *fd);
int (*lseek) (struct dfs_fd *fd, rt_off_t offset); int (*lseek) (struct dfs_fd *fd, rt_off_t offset);
int (*getdents) (struct dfs_fd *fd, struct dirent *dirp, rt_uint32_t count); int (*getdents) (struct dfs_fd *fd, struct dirent *dirp, rt_uint32_t count);
int (*unlink) (struct dfs_filesystem *fs, const char *pathname); int (*unlink) (struct dfs_filesystem *fs, const char *pathname);
int (*stat) (struct dfs_filesystem *fs, const char *filename, struct stat *buf); int (*stat) (struct dfs_filesystem *fs, const char *filename, struct stat *buf);
int (*rename) (struct dfs_filesystem *fs, const char *oldpath, const char *newpath); int (*rename) (struct dfs_filesystem *fs, const char *oldpath, const char *newpath);
}; };
/* Mounted file system */ /* Mounted file system */
struct dfs_filesystem struct dfs_filesystem
{ {
rt_device_t dev_id; /* Attached device */ rt_device_t dev_id; /* Attached device */
char *path; /* File system mount point */ char *path; /* File system mount point */
const struct dfs_filesystem_operation *ops; /* Operations for file system type */ 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 */ /* file system partition table */
struct dfs_partition struct dfs_partition
{ {
rt_uint8_t type; /* file system type */ rt_uint8_t type; /* file system type */
rt_off_t offset; /* partition start offset */ rt_off_t offset; /* partition start offset */
rt_size_t size; /* partition size */ rt_size_t size; /* partition size */
rt_sem_t lock; rt_sem_t lock;
}; };
int dfs_register(const struct dfs_filesystem_operation *ops); int dfs_register(const struct dfs_filesystem_operation *ops);
struct dfs_filesystem *dfs_filesystem_lookup(const char *path); 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); 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, int dfs_mount(const char *device_name,
const char *filesystemtype, rt_uint32_t rwflag, const const char *path,
void *data); const char *filesystemtype,
rt_uint32_t rwflag,
const void *data);
int dfs_unmount(const char *specialfile); int dfs_unmount(const char *specialfile);
/* extern variable */ /* extern variable */

View File

@ -1,7 +1,7 @@
/* /*
* File : dfs_init.h * File : dfs_init.h
* This file is part of Device File System in RT-Thread RTOS * 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 * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at

View File

@ -1,7 +1,7 @@
/* /*
* File : dfs_def.h * File : dfs_posix.h
* This file is part of Device File System in RT-Thread RTOS * 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 * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -21,68 +21,68 @@
#include <dfs_def.h> #include <dfs_def.h>
#ifndef RT_USING_NEWLIB #ifndef RT_USING_NEWLIB
#define O_RDONLY DFS_O_RDONLY #define O_RDONLY DFS_O_RDONLY
#define O_WRONLY DFS_O_WRONLY #define O_WRONLY DFS_O_WRONLY
#define O_RDWR DFS_O_RDWR #define O_RDWR DFS_O_RDWR
#define O_ACCMODE DFS_O_ACCMODE #define O_ACCMODE DFS_O_ACCMODE
#define O_CREAT DFS_O_CREAT #define O_CREAT DFS_O_CREAT
#define O_EXCL DFS_O_EXCL #define O_EXCL DFS_O_EXCL
#define O_TRUNC DFS_O_TRUNC #define O_TRUNC DFS_O_TRUNC
#define O_APPEND DFS_O_APPEND #define O_APPEND DFS_O_APPEND
#define O_BINARY DFS_O_BINARY #define O_BINARY DFS_O_BINARY
#define O_DIRECTORY DFS_O_DIRECTORY #define O_DIRECTORY DFS_O_DIRECTORY
#define S_IFMT DFS_S_IFMT #define S_IFMT DFS_S_IFMT
#define S_IFSOCK DFS_S_IFSOCK #define S_IFSOCK DFS_S_IFSOCK
#define S_IFLNK DFS_S_IFLNK #define S_IFLNK DFS_S_IFLNK
#define S_IFREG DFS_S_IFREG #define S_IFREG DFS_S_IFREG
#define S_IFBLK DFS_S_IFBLK #define S_IFBLK DFS_S_IFBLK
#define S_IFDIR DFS_S_IFDIR #define S_IFDIR DFS_S_IFDIR
#define S_IFCHR DFS_S_IFCHR #define S_IFCHR DFS_S_IFCHR
#define S_IFIFO DFS_S_IFIFO #define S_IFIFO DFS_S_IFIFO
#define S_ISUID DFS_S_ISUID #define S_ISUID DFS_S_ISUID
#define S_ISGID DFS_S_ISGID #define S_ISGID DFS_S_ISGID
#define S_ISVTX DFS_S_ISVTX #define S_ISVTX DFS_S_ISVTX
#define S_ISLNK(m) (((m) & DFS_S_IFMT) == DFS_S_IFLNK) #define S_ISLNK(m) (((m) & DFS_S_IFMT) == DFS_S_IFLNK)
#define S_ISREG(m) (((m) & DFS_S_IFMT) == DFS_S_IFREG) #define S_ISREG(m) (((m) & DFS_S_IFMT) == DFS_S_IFREG)
#define S_ISDIR(m) (((m) & DFS_S_IFMT) == DFS_S_IFDIR) #define S_ISDIR(m) (((m) & DFS_S_IFMT) == DFS_S_IFDIR)
#define S_ISCHR(m) (((m) & DFS_S_IFMT) == DFS_S_IFCHR) #define S_ISCHR(m) (((m) & DFS_S_IFMT) == DFS_S_IFCHR)
#define S_ISBLK(m) (((m) & DFS_S_IFMT) == DFS_S_IFBLK) #define S_ISBLK(m) (((m) & DFS_S_IFMT) == DFS_S_IFBLK)
#define S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO) #define S_ISFIFO(m) (((m) & DFS_S_IFMT) == DFS_S_IFIFO)
#define S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK) #define S_ISSOCK(m) (((m) & DFS_S_IFMT) == DFS_S_IFSOCK)
#define S_IRWXU DFS_S_IRWXU #define S_IRWXU DFS_S_IRWXU
#define S_IRUSR DFS_S_IRUSR #define S_IRUSR DFS_S_IRUSR
#define S_IWUSR DFS_S_IWUSR #define S_IWUSR DFS_S_IWUSR
#define S_IXUSR DFS_S_IXUSR #define S_IXUSR DFS_S_IXUSR
#define S_IRWXG DFS_S_IRWXG #define S_IRWXG DFS_S_IRWXG
#define S_IRGRP DFS_S_IRGRP #define S_IRGRP DFS_S_IRGRP
#define S_IWGRP DFS_S_IWGRP #define S_IWGRP DFS_S_IWGRP
#define S_IXGRP DFS_S_IXGRP #define S_IXGRP DFS_S_IXGRP
#define S_IRWXO DFS_S_IRWXO #define S_IRWXO DFS_S_IRWXO
#define S_IROTH DFS_S_IROTH #define S_IROTH DFS_S_IROTH
#define S_IWOTH DFS_S_IWOTH #define S_IWOTH DFS_S_IWOTH
#define S_IXOTH DFS_S_IXOTH #define S_IXOTH DFS_S_IXOTH
#if defined(__CC_ARM) #if defined(__CC_ARM)
#include <stdio.h> #include <stdio.h>
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
#include <stdio.h> #include <stdio.h>
#else #else
#define SEEK_SET DFS_SEEK_SET #define SEEK_SET DFS_SEEK_SET
#define SEEK_CUR DFS_SEEK_CUR #define SEEK_CUR DFS_SEEK_CUR
#define SEEK_END DFS_SEEK_END #define SEEK_END DFS_SEEK_END
#endif #endif
typedef struct typedef struct
{ {
int fd; /* directory file */ int fd; /* directory file */
char buf[512]; char buf[512];
int num; int num;
int cur; int cur;
} DIR; } DIR;
/* directory api*/ /* directory api*/

View File

@ -16,7 +16,7 @@
#include <dfs_fs.h> #include <dfs_fs.h>
#include <dfs_file.h> #include <dfs_file.h>
#define NO_WORKING_DIR "system does not support working dir\n" #define NO_WORKING_DIR "system does not support working dir\n"
/* Global variables */ /* Global variables */
const struct dfs_filesystem_operation *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; 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) void dfs_init(void)
{ {
/* clear filesystem operations table */ /* clear filesystem operations table */
rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table)); rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table));
/* clear filesystem table */ /* clear filesystem table */
rt_memset(filesystem_table, 0, sizeof(filesystem_table)); rt_memset(filesystem_table, 0, sizeof(filesystem_table));
/* clean fd table */ /* clean fd table */
rt_memset(fd_table, 0, sizeof(fd_table)); rt_memset(fd_table, 0, sizeof(fd_table));
/* create device filesystem lock */ /* create device filesystem lock */
rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO); rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
#ifdef DFS_USING_WORKDIR #ifdef DFS_USING_WORKDIR
/* set current working directory */ /* set current working directory */
rt_memset(working_directory, 0, sizeof(working_directory)); rt_memset(working_directory, 0, sizeof(working_directory));
working_directory[0] = '/'; working_directory[0] = '/';
#endif #endif
} }
@ -70,13 +70,13 @@ void dfs_init(void)
*/ */
void dfs_lock(void) void dfs_lock(void)
{ {
rt_err_t result; rt_err_t result;
result = rt_mutex_take(&fslock, RT_WAITING_FOREVER); result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
if (result != RT_EOK) if (result != RT_EOK)
{ {
RT_ASSERT(0); RT_ASSERT(0);
} }
} }
/** /**
@ -86,7 +86,7 @@ void dfs_lock(void)
*/ */
void dfs_unlock(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) int fd_new(void)
{ {
struct dfs_fd *d; struct dfs_fd *d;
int idx; int idx;
/* lock filesystem */ /* lock filesystem */
dfs_lock(); dfs_lock();
/* find an empty fd entry */ /* find an empty fd entry */
#ifdef DFS_USING_STDIO #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 #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 #endif
/* can't find an empty fd entry */ /* can't find an empty fd entry */
#ifdef DFS_USING_STDIO #ifdef DFS_USING_STDIO
if (idx == DFS_FD_MAX + 3) if (idx == DFS_FD_MAX + 3)
#else #else
if (idx == DFS_FD_MAX) if (idx == DFS_FD_MAX)
#endif #endif
{ {
idx = -1; idx = -1;
goto __result; goto __result;
} }
d = &(fd_table[idx]); d = &(fd_table[idx]);
d->ref_count = 1; d->ref_count = 1;
__result: __result:
dfs_unlock(); dfs_unlock();
return idx; return idx;
} }
/** /**
@ -140,24 +140,24 @@ __result:
*/ */
struct dfs_fd *fd_get(int fd) struct dfs_fd *fd_get(int fd)
{ {
struct dfs_fd *d; struct dfs_fd *d;
#ifdef DFS_USING_STDIO #ifdef DFS_USING_STDIO
if (fd < 3 || fd >= DFS_FD_MAX + 3) if (fd < 3 || fd >= DFS_FD_MAX + 3)
return RT_NULL; return RT_NULL;
#else #else
if (fd < 0 || fd >= DFS_FD_MAX) if (fd < 0 || fd >= DFS_FD_MAX)
return RT_NULL; return RT_NULL;
#endif #endif
dfs_lock(); dfs_lock();
d = &fd_table[fd]; d = &fd_table[fd];
/* increase the reference count */ /* increase the reference count */
d->ref_count ++; d->ref_count ++;
dfs_unlock(); dfs_unlock();
return d; return d;
} }
/** /**
@ -167,17 +167,17 @@ struct dfs_fd *fd_get(int fd)
*/ */
void fd_put(struct dfs_fd *fd) void fd_put(struct dfs_fd *fd)
{ {
RT_ASSERT(fd != RT_NULL); RT_ASSERT(fd != RT_NULL);
dfs_lock(); dfs_lock();
fd->ref_count --; fd->ref_count --;
/* clear this fd entry */ /* clear this fd entry */
if (fd->ref_count == 0) if (fd->ref_count == 0)
{ {
rt_memset(fd, 0, sizeof(struct dfs_fd)); rt_memset(fd, 0, sizeof(struct dfs_fd));
} }
dfs_unlock(); dfs_unlock();
}; };
/** /**
@ -191,52 +191,52 @@ void fd_put(struct dfs_fd *fd)
*/ */
int fd_is_open(const char *pathname) int fd_is_open(const char *pathname)
{ {
char *fullpath; char *fullpath;
unsigned int index; unsigned int index;
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
struct dfs_fd *fd; struct dfs_fd *fd;
fullpath = dfs_normalize_path(RT_NULL, pathname); fullpath = dfs_normalize_path(RT_NULL, pathname);
if (fullpath != RT_NULL) if (fullpath != RT_NULL)
{ {
char *mountpath; char *mountpath;
fs = dfs_filesystem_lookup(fullpath); fs = dfs_filesystem_lookup(fullpath);
if (fs == RT_NULL) if (fs == RT_NULL)
{ {
/* can't find mounted file system */ /* can't find mounted file system */
rt_free(fullpath); rt_free(fullpath);
return -1; return -1;
} }
/* get file path name under mounted file system */ /* get file path name under mounted file system */
if (fs->path[0] == '/' && fs->path[1] == '\0') if (fs->path[0] == '/' && fs->path[1] == '\0')
mountpath = fullpath; mountpath = fullpath;
else else
mountpath = fullpath + strlen(fs->path); mountpath = fullpath + strlen(fs->path);
dfs_lock(); dfs_lock();
for (index = 0; index < DFS_FD_MAX; index++) for (index = 0; index < DFS_FD_MAX; index++)
{ {
fd = &(fd_table[index]); fd = &(fd_table[index]);
if (fd->fs == RT_NULL) if (fd->fs == RT_NULL)
continue; continue;
if (fd->fs == fs && strcmp(fd->path, mountpath) == 0) if (fd->fs == fs && strcmp(fd->path, mountpath) == 0)
{ {
/* found file in file descriptor table */ /* found file in file descriptor table */
rt_free(fullpath); rt_free(fullpath);
dfs_unlock(); dfs_unlock();
return 0; return 0;
} }
} }
dfs_unlock(); dfs_unlock();
rt_free(fullpath); rt_free(fullpath);
} }
return -1; 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 *dfs_subdir(const char *directory, const char *filename)
{ {
const char *dir; const char *dir;
if (strlen(directory) == strlen(filename)) /* it's a same path */ if (strlen(directory) == strlen(filename)) /* it's a same path */
return RT_NULL; return RT_NULL;
dir = filename + strlen(directory); dir = filename + strlen(directory);
if ((*dir != '/') && (dir != filename)) if ((*dir != '/') && (dir != filename))
{ {
dir --; 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 directory the parent path
* @param filename the file name * @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 *dfs_normalize_path(const char *directory, const char *filename)
{ {
char *fullpath; char *fullpath;
char *dst0, *dst, *src; char *dst0, *dst, *src;
/* check parameters */ /* check parameters */
RT_ASSERT(filename != RT_NULL); RT_ASSERT(filename != RT_NULL);
#ifdef DFS_USING_WORKDIR #ifdef DFS_USING_WORKDIR
if (directory == RT_NULL) /* shall use working directory */ if (directory == RT_NULL) /* shall use working directory */
directory = &working_directory[0]; directory = &working_directory[0];
#else #else
if ((directory == RT_NULL) && (filename[0] != '/')) if ((directory == RT_NULL) && (filename[0] != '/'))
{ {
rt_kprintf(NO_WORKING_DIR); rt_kprintf(NO_WORKING_DIR);
return RT_NULL; return RT_NULL;
} }
#endif #endif
if (filename[0] != '/') /* it's a absolute path, use it directly */ if (filename[0] != '/') /* it's a absolute path, use it directly */
{ {
fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2); fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);
/* join path and file name */ /* join path and file name */
rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
"%s/%s", directory, filename); "%s/%s", directory, filename);
} }
else else
{ {
fullpath = rt_strdup(filename); /* copy string */ fullpath = rt_strdup(filename); /* copy string */
} }
src = fullpath; src = fullpath;
dst = fullpath; dst = fullpath;
dst0 = dst; dst0 = dst;
while (1) while (1)
{ {
char c = *src; char c = *src;
if (c == '.') if (c == '.')
{ {
if (!src[1]) src ++; /* '.' and ends */ if (!src[1]) src ++; /* '.' and ends */
else if (src[1] == '/') else if (src[1] == '/')
{ {
/* './' case */ /* './' case */
src += 2; src += 2;
while ((*src == '/') && (*src != '\0')) while ((*src == '/') && (*src != '\0'))
src ++; src ++;
continue; continue;
} }
else if (src[1] == '.') else if (src[1] == '.')
{ {
if (!src[2]) if (!src[2])
{ {
/* '..' and ends case */ /* '..' and ends case */
src += 2; src += 2;
goto up_one; goto up_one;
} }
else if (src[2] == '/') else if (src[2] == '/')
{ {
/* '../' case */ /* '../' case */
src += 3; src += 3;
while ((*src == '/') && (*src != '\0')) while ((*src == '/') && (*src != '\0'))
src ++; src ++;
goto up_one; goto up_one;
} }
} }
} }
/* copy up the next '/' and erase all '/' */ /* copy up the next '/' and erase all '/' */
while ((c = *src++) != '\0' && c != '/') while ((c = *src++) != '\0' && c != '/')
*dst ++ = c; *dst ++ = c;
if (c == '/') if (c == '/')
{ {
*dst ++ = '/'; *dst ++ = '/';
while (c == '/') while (c == '/')
c = *src++; c = *src++;
src --; src --;
} }
else if (!c) else if (!c)
break; break;
continue; continue;
up_one: up_one:
dst --; dst --;
if (dst < dst0) if (dst < dst0)
{ {
rt_free(fullpath); rt_free(fullpath);
return RT_NULL; return RT_NULL;
} }
while (dst0 < dst && dst[-1] != '/') while (dst0 < dst && dst[-1] != '/')
dst --; dst --;
} }
*dst = '\0'; *dst = '\0';
/* remove '/' in the end of path if exist */ /* remove '/' in the end of path if exist */
dst --; dst --;
if ((dst != fullpath) && (*dst == '/')) if ((dst != fullpath) && (*dst == '/'))
*dst = '\0'; *dst = '\0';
return fullpath; return fullpath;
} }
/*@}*/ /*@}*/

View File

@ -19,6 +19,7 @@
/** /**
* @addtogroup FileApi * @addtogroup FileApi
*/ */
/*@{*/ /*@{*/
/** /**
@ -32,84 +33,85 @@
*/ */
int dfs_file_open(struct dfs_fd *fd, const char *path, int flags) int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
{ {
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
char *fullpath; char *fullpath;
int result; int result;
/* parameter check */ /* parameter check */
if (fd == RT_NULL) if (fd == RT_NULL)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
/* make sure we have an absolute path */ /* make sure we have an absolute path */
fullpath = dfs_normalize_path(RT_NULL, path); fullpath = dfs_normalize_path(RT_NULL, path);
if (fullpath == RT_NULL) if (fullpath == RT_NULL)
{ {
return -1; return -1;
} }
dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath)); dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath));
/* find filesystem */ /* find filesystem */
fs = dfs_filesystem_lookup(fullpath); fs = dfs_filesystem_lookup(fullpath);
if (fs == RT_NULL) if (fs == RT_NULL)
{ {
rt_free(fullpath); /* release path */ rt_free(fullpath); /* release path */
return -DFS_STATUS_ENOENT;
}
dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name)); return -DFS_STATUS_ENOENT;
fd->fs = fs; }
/* initialize the fd item */ dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name));
fd->type = FT_REGULAR; fd->fs = fs;
fd->flags = flags;
fd->size = 0;
fd->pos = 0;
if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) /* initialize the fd item */
{ fd->type = FT_REGULAR;
if (dfs_subdir(fs->path, fullpath) == RT_NULL) fd->flags = flags;
fd->path = rt_strdup("/"); fd->size = 0;
else fd->pos = 0;
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 (!(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;
}
if ((result = fs->ops->open(fd)) < 0) /* specific file system open routine */
{ if (fs->ops->open == RT_NULL)
/* clear fd */ {
rt_free(fd->path); /* clear fd */
rt_memset(fd, 0, sizeof(*fd)); rt_free(fd->path);
rt_memset(fd, 0, sizeof(*fd));
dfs_log(DFS_DEBUG_INFO, ("open failed")); return -DFS_STATUS_ENOSYS;
}
return result; if ((result = fs->ops->open(fd)) < 0)
} {
/* clear fd */
rt_free(fd->path);
rt_memset(fd, 0, sizeof(*fd));
fd->flags |= DFS_F_OPEN; dfs_log(DFS_DEBUG_INFO, ("open failed"));
if (flags & DFS_O_DIRECTORY)
{
fd->type = FT_DIRECTORY;
fd->flags |= DFS_F_DIRECTORY;
}
dfs_log(DFS_DEBUG_INFO, ("open successful")); return result;
return 0; }
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 dfs_file_close(struct dfs_fd *fd)
{ {
int result = 0; int result = 0;
if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) if (fd != RT_NULL && fd->fs->ops->close != RT_NULL)
result = fd->fs->ops->close(fd); result = fd->fs->ops->close(fd);
/* close fd error, return */ /* close fd error, return */
if (result < 0) if (result < 0)
return result; return result;
rt_free(fd->path); rt_free(fd->path);
rt_memset(fd, 0, sizeof(struct dfs_fd)); 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) 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) if (fd == RT_NULL || fd->type != FT_REGULAR)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
fs = fd->fs; fs = fd->fs;
if (fs->ops->ioctl != RT_NULL) if (fs->ops->ioctl != RT_NULL)
return fs->ops->ioctl(fd, cmd, args); 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 fd the file descriptor.
* @param buf the buffer to save the read data. * @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) int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len)
{ {
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
int result = 0; int result = 0;
if (fd == RT_NULL) if (fd == RT_NULL)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
fs = (struct dfs_filesystem *)fd->fs; fs = (struct dfs_filesystem *)fd->fs;
if (fs->ops->read == RT_NULL) if (fs->ops->read == RT_NULL)
return -DFS_STATUS_ENOSYS; return -DFS_STATUS_ENOSYS;
if ((result = fs->ops->read(fd, buf, len)) < 0) if ((result = fs->ops->read(fd, buf, len)) < 0)
fd->flags |= DFS_F_EOF; 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) 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 */ /* parameter check */
if (fd == RT_NULL || fd->type != FT_DIRECTORY) if (fd == RT_NULL || fd->type != FT_DIRECTORY)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
fs = (struct dfs_filesystem *)fd->fs; fs = (struct dfs_filesystem *)fd->fs;
if (fs->ops->getdents != RT_NULL) if (fs->ops->getdents != RT_NULL)
return fs->ops->getdents(fd, dirp, nbytes); 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 dfs_file_unlink(const char *path)
{ {
int result; int result;
char *fullpath; char *fullpath;
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
result = DFS_STATUS_OK; result = DFS_STATUS_OK;
/* Make sure we have an absolute path */ /* Make sure we have an absolute path */
fullpath = dfs_normalize_path(RT_NULL, path); fullpath = dfs_normalize_path(RT_NULL, path);
if (fullpath == RT_NULL) if (fullpath == RT_NULL)
{ {
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
} }
/* get filesystem */ /* get filesystem */
if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
{ {
result = -DFS_STATUS_ENOENT; result = -DFS_STATUS_ENOENT;
goto __exit; goto __exit;
} }
/* Check whether file is already open */ /* Check whether file is already open */
if (fd_is_open(fullpath) == 0) if (fd_is_open(fullpath) == 0)
{ {
result = -DFS_STATUS_EBUSY; result = -DFS_STATUS_EBUSY;
goto __exit; goto __exit;
} }
if (fs->ops->unlink != RT_NULL) if (fs->ops->unlink != RT_NULL)
{ {
if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH)) if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
{ {
if (dfs_subdir(fs->path, fullpath) == RT_NULL) if (dfs_subdir(fs->path, fullpath) == RT_NULL)
result = fs->ops->unlink(fs, "/"); result = fs->ops->unlink(fs, "/");
else else
result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath)); result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
} }
else else
result = fs->ops->unlink(fs, fullpath); result = fs->ops->unlink(fs, fullpath);
} }
else result = -DFS_STATUS_ENOSYS; else result = -DFS_STATUS_ENOSYS;
__exit: __exit:
rt_free(fullpath); rt_free(fullpath);
return result; return result;
} }
/** /**
@ -276,16 +279,16 @@ __exit:
*/ */
int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len) 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) if (fd == RT_NULL)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
fs = fd->fs; fs = fd->fs;
if (fs->ops->write == RT_NULL) if (fs->ops->write == RT_NULL)
return -DFS_STATUS_ENOSYS; 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) int dfs_file_flush(struct dfs_fd *fd)
{ {
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
if (fd == RT_NULL) if (fd == RT_NULL)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
fs = fd->fs; fs = fd->fs;
if (fs->ops->flush == RT_NULL) if (fs->ops->flush == RT_NULL)
return -DFS_STATUS_ENOSYS; 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 dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset)
{ {
int result; int result;
struct dfs_filesystem *fs = fd->fs; struct dfs_filesystem *fs = fd->fs;
if (fd == RT_NULL) if (fd == RT_NULL)
return -DFS_STATUS_EINVAL; return -DFS_STATUS_EINVAL;
if (fs->ops->lseek == RT_NULL) if (fs->ops->lseek == RT_NULL)
return -DFS_STATUS_ENOSYS; return -DFS_STATUS_ENOSYS;
result = fs->ops->lseek(fd, offset); result = fs->ops->lseek(fd, offset);
/* update current position */ /* update current position */
if (result >= 0) if (result >= 0)
fd->pos = result; 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 dfs_file_stat(const char *path, struct stat *buf)
{ {
int result; int result;
char *fullpath; char *fullpath;
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
fullpath = dfs_normalize_path(RT_NULL, path); fullpath = dfs_normalize_path(RT_NULL, path);
if (fullpath == RT_NULL) if (fullpath == RT_NULL)
{ {
return -1; return -1;
} }
if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL) if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
{ {
dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath)); dfs_log(DFS_DEBUG_ERROR,
rt_free(fullpath); ("can't find mounted filesystem on this path:%s", fullpath));
return -DFS_STATUS_ENOENT; rt_free(fullpath);
}
if ((fullpath[0] == '/' && fullpath[1] == '\0') || return -DFS_STATUS_ENOENT;
(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 | if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; (dfs_subdir(fs->path, fullpath) == RT_NULL))
buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; {
/* it's the root directory */
buf->st_dev = 0;
buf->st_size = 0; buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
buf->st_mtime = 0; DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
buf->st_blksize = 512; buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
/* release full path */ buf->st_size = 0;
rt_free(fullpath); buf->st_mtime = 0;
buf->st_blksize = 512;
return DFS_STATUS_OK; /* release full path */
} rt_free(fullpath);
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 */ return DFS_STATUS_OK;
if (fs->ops->flags & DFS_FS_FLAG_FULLPATH) }
result = fs->ops->stat(fs, fullpath, buf); else
else {
result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf); if (fs->ops->stat == RT_NULL)
{
} rt_free(fullpath);
dfs_log(DFS_DEBUG_ERROR,
("the filesystem didn't implement this function"));
rt_free(fullpath); return -DFS_STATUS_ENOSYS;
}
return result; /* 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 dfs_file_rename(const char *oldpath, const char *newpath)
{ {
int result; int result;
struct dfs_filesystem *oldfs, *newfs; struct dfs_filesystem *oldfs, *newfs;
char *oldfullpath, *newfullpath; char *oldfullpath, *newfullpath;
result = DFS_STATUS_OK; result = DFS_STATUS_OK;
newfullpath = RT_NULL; newfullpath = RT_NULL;
oldfullpath = RT_NULL; oldfullpath = RT_NULL;
oldfullpath = dfs_normalize_path(RT_NULL, oldpath); oldfullpath = dfs_normalize_path(RT_NULL, oldpath);
if (oldfullpath == RT_NULL) if (oldfullpath == RT_NULL)
{ {
result = -DFS_STATUS_ENOENT; result = -DFS_STATUS_ENOENT;
goto __exit; goto __exit;
} }
newfullpath = dfs_normalize_path(RT_NULL, newpath); newfullpath = dfs_normalize_path(RT_NULL, newpath);
if (newfullpath == RT_NULL) if (newfullpath == RT_NULL)
{ {
result = -DFS_STATUS_ENOENT; result = -DFS_STATUS_ENOENT;
goto __exit; goto __exit;
} }
oldfs = dfs_filesystem_lookup(oldfullpath); oldfs = dfs_filesystem_lookup(oldfullpath);
newfs = dfs_filesystem_lookup(newfullpath); newfs = dfs_filesystem_lookup(newfullpath);
if (oldfs == newfs) if (oldfs == newfs)
{ {
if (oldfs->ops->rename == RT_NULL) if (oldfs->ops->rename == RT_NULL)
{ {
result = -DFS_STATUS_ENOSYS; result = -DFS_STATUS_ENOSYS;
} }
else else
{ {
if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH) if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH)
result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath); result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
else else
/* use sub directory to rename in file system */ /* use sub directory to rename in file system */
result = oldfs->ops->rename(oldfs, dfs_subdir(oldfs->path, oldfullpath), result = oldfs->ops->rename(oldfs,
dfs_subdir(newfs->path, newfullpath)); dfs_subdir(oldfs->path, oldfullpath),
} dfs_subdir(newfs->path, newfullpath));
} }
else }
{ else
result = -DFS_STATUS_EXDEV; {
} result = -DFS_STATUS_EXDEV;
}
__exit: __exit:
rt_free(oldfullpath); rt_free(oldfullpath);
rt_free(newfullpath); rt_free(newfullpath);
/* not at same file system, return EXDEV */ /* not at same file system, return EXDEV */
return result; return result;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
@ -475,147 +482,152 @@ static struct dfs_fd fd;
static struct dirent dirent; static struct dirent dirent;
void ls(const char *pathname) void ls(const char *pathname)
{ {
struct stat stat; struct stat stat;
int length; int length;
char *fullpath, *path; char *fullpath, *path;
fullpath = RT_NULL; fullpath = RT_NULL;
if (pathname == RT_NULL) if (pathname == RT_NULL)
{ {
#ifdef DFS_USING_WORKDIR #ifdef DFS_USING_WORKDIR
/* open current working directory */ /* open current working directory */
path = rt_strdup(working_directory); path = rt_strdup(working_directory);
#else #else
path = rt_strdup("/"); path = rt_strdup("/");
#endif #endif
if (path == RT_NULL) return ; /* out of memory */ if (path == RT_NULL)
} return ; /* out of memory */
else }
{ else
path = (char *)pathname; {
} path = (char *)pathname;
}
/* list directory */ /* list directory */
if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0) if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0)
{ {
rt_kprintf("Directory %s:\n", path); rt_kprintf("Directory %s:\n", path);
do do
{ {
rt_memset(&dirent, 0, sizeof(struct dirent)); rt_memset(&dirent, 0, sizeof(struct dirent));
length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent)); length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
if (length > 0) if (length > 0)
{ {
rt_memset(&stat, 0, sizeof(struct stat)); rt_memset(&stat, 0, sizeof(struct stat));
/* build full path for each file */ /* build full path for each file */
fullpath = dfs_normalize_path(path, dirent.d_name); fullpath = dfs_normalize_path(path, dirent.d_name);
if (fullpath == RT_NULL) if (fullpath == RT_NULL)
break; break;
if (dfs_file_stat(fullpath, &stat) == 0) if (dfs_file_stat(fullpath, &stat) == 0)
{ {
rt_kprintf("%-20s", dirent.d_name); rt_kprintf("%-20s", dirent.d_name);
if ( DFS_S_ISDIR(stat.st_mode)) if ( DFS_S_ISDIR(stat.st_mode))
{ {
rt_kprintf("%-25s\n", "<DIR>"); rt_kprintf("%-25s\n", "<DIR>");
} }
else else
{ {
rt_kprintf("%-25lu\n", stat.st_size); rt_kprintf("%-25lu\n", stat.st_size);
} }
} }
else else
rt_kprintf("BAD file: %s\n", dirent.d_name); rt_kprintf("BAD file: %s\n", dirent.d_name);
rt_free(fullpath); rt_free(fullpath);
} }
}while(length > 0); }while(length > 0);
dfs_file_close(&fd); dfs_file_close(&fd);
} }
else else
{ {
rt_kprintf("No such directory\n"); rt_kprintf("No such directory\n");
} }
if (pathname == RT_NULL) if (pathname == RT_NULL)
rt_free(path); rt_free(path);
} }
FINSH_FUNCTION_EXPORT(ls, list directory contents) FINSH_FUNCTION_EXPORT(ls, list directory contents)
void rm(const char *filename) void rm(const char *filename)
{ {
if (dfs_file_unlink(filename) < 0) if (dfs_file_unlink(filename) < 0)
{ {
rt_kprintf("Delete %s failed\n", filename); rt_kprintf("Delete %s failed\n", filename);
} }
} }
FINSH_FUNCTION_EXPORT(rm, remove files or directories) FINSH_FUNCTION_EXPORT(rm, remove files or directories)
void cat(const char* filename) void cat(const char* filename)
{ {
rt_uint32_t length; rt_uint32_t length;
char buffer[81]; char buffer[81];
if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0) if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0)
{ {
rt_kprintf("Open %s failed\n", filename); rt_kprintf("Open %s failed\n", filename);
return;
}
do return;
{ }
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); 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) FINSH_FUNCTION_EXPORT(cat, print file)
#define BUF_SZ 4096 #define BUF_SZ 4096
void copy(const char *src, const char *dst) void copy(const char *src, const char *dst)
{ {
struct dfs_fd src_fd; struct dfs_fd src_fd;
rt_uint8_t *block_ptr; rt_uint8_t *block_ptr;
rt_uint32_t read_bytes; rt_uint32_t read_bytes;
block_ptr = rt_malloc(BUF_SZ); block_ptr = rt_malloc(BUF_SZ);
if (block_ptr == RT_NULL) if (block_ptr == RT_NULL)
{ {
rt_kprintf("out of memory\n"); rt_kprintf("out of memory\n");
return;
}
if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0) return;
{ }
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); if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0)
return; {
} rt_free(block_ptr);
rt_kprintf("Read %s failed\n", src);
do return;
{ }
read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ); if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0)
if (read_bytes > 0) {
{ rt_free(block_ptr);
dfs_file_write(&fd, block_ptr, read_bytes); dfs_file_close(&src_fd);
}
} while (read_bytes > 0);
dfs_file_close(&src_fd); rt_kprintf("Write %s failed\n", dst);
dfs_file_close(&fd);
rt_free(block_ptr); 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) FINSH_FUNCTION_EXPORT(copy, copy source file to destination file)

View File

@ -31,41 +31,43 @@
*/ */
int dfs_register(const struct dfs_filesystem_operation *ops) int dfs_register(const struct dfs_filesystem_operation *ops)
{ {
int index, result; int index, result;
result = 0; result = 0;
/* lock filesystem */ /* lock filesystem */
dfs_lock(); dfs_lock();
/* check if this filesystem was already registered */ /* check if this filesystem was already registered */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
{ {
if (filesystem_operation_table[index] != RT_NULL && if (filesystem_operation_table[index] != RT_NULL &&
strcmp(filesystem_operation_table[index]->name, ops->name) == 0) strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
{ {
result = -1; result = -1;
goto err; goto err;
} }
} }
/* find out an empty filesystem type entry */ /* find out an empty filesystem type entry */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL; for (index = 0;
index++) ; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL;
index ++)
;
/* filesystem type table full */ /* filesystem type table full */
if (index == DFS_FILESYSTEM_TYPES_MAX) if (index == DFS_FILESYSTEM_TYPES_MAX)
{ {
result = -1; result = -1;
goto err; goto err;
} }
/* save the filesystem's operations */ /* save the filesystem's operations */
filesystem_operation_table[index] = ops; filesystem_operation_table[index] = ops;
err: err:
dfs_unlock(); dfs_unlock();
return result; return result;
} }
/** /**
@ -78,42 +80,42 @@ err:
*/ */
struct dfs_filesystem *dfs_filesystem_lookup(const char *path) struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
{ {
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
rt_uint32_t index, fspath, prefixlen; rt_uint32_t index, fspath, prefixlen;
fs = RT_NULL; fs = RT_NULL;
prefixlen = 0; prefixlen = 0;
/* lock filesystem */ /* lock filesystem */
dfs_lock(); dfs_lock();
/* lookup it in the filesystem table */ /* lookup it in the filesystem table */
for (index = 0; index < DFS_FILESYSTEMS_MAX; index++) for (index = 0; index < DFS_FILESYSTEMS_MAX; index++)
{ {
if (filesystem_table[index].path == RT_NULL) if (filesystem_table[index].path == RT_NULL)
continue; continue;
else else
{ {
fspath = strlen(filesystem_table[index].path); fspath = strlen(filesystem_table[index].path);
if (fspath < prefixlen) if (fspath < prefixlen)
continue; continue;
} }
if ((filesystem_table[index].ops != RT_NULL) && if ((filesystem_table[index].ops != RT_NULL) &&
(strncmp(filesystem_table[index].path, path, fspath) == 0)) (strncmp(filesystem_table[index].path, path, fspath) == 0))
{ {
/* check next path separator */ /* check next path separator */
if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/')) if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/'))
continue; continue;
fs = &filesystem_table[index]; fs = &filesystem_table[index];
prefixlen = fspath; prefixlen = fspath;
} }
} }
dfs_unlock(); dfs_unlock();
return fs; 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. * @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_ADDRESS 0x1be /* device partition offset in Boot Sector */
#define DPT_ITEM_SIZE 16 /* partition item size */ #define DPT_ITEM_SIZE 16 /* partition item size */
rt_uint8_t *dpt; rt_uint8_t *dpt;
rt_uint8_t type; rt_uint8_t type;
rt_err_t result; rt_err_t result;
RT_ASSERT(part != RT_NULL); RT_ASSERT(part != RT_NULL);
RT_ASSERT(buf != RT_NULL); RT_ASSERT(buf != RT_NULL);
result = RT_EOK; result = RT_EOK;
dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE; dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
if ((*dpt != 0x80) && (*dpt != 0x00)) if ((*dpt != 0x80) && (*dpt != 0x00))
{ {
/* which is not a partition table */ /* which is not a partition table */
result = -RT_ERROR; result = -RT_ERROR;
return result;
}
/* get partition type */ return result;
type = *(dpt+4); }
if (type != 0) /* get partition type */
{ type = *(dpt+4);
/* set partition type */
part->type = type;
/* get partition offset and size */ if (type != 0)
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; /* set partition type */
part->type = type;
rt_kprintf("found part[%d], begin: %d, size: ", pindex, part->offset*512); /* get partition offset and size */
if ((part->size>>11) > 0) /* MB */ 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;
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; 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. * @return 0 on successful or -1 on failed.
*/ */
int dfs_mount(const char *device_name, const char *path, int dfs_mount(const char *device_name,
const char *filesystemtype, unsigned long rwflag, const const char *path,
void *data) const char *filesystemtype,
unsigned long rwflag,
const void *data)
{ {
const struct dfs_filesystem_operation *ops; const struct dfs_filesystem_operation *ops;
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
char *fullpath=RT_NULL; char *fullpath=RT_NULL;
rt_device_t dev_id; rt_device_t dev_id;
int index; int index;
/* open specific device */ /* open specific device */
if (device_name != RT_NULL) if (device_name != RT_NULL)
{ {
dev_id = rt_device_find(device_name); dev_id = rt_device_find(device_name);
if (dev_id == RT_NULL) if (dev_id == RT_NULL)
{ {
/* no this device */ /* no this device */
rt_set_errno(-DFS_STATUS_ENODEV); rt_set_errno(-DFS_STATUS_ENODEV);
return -1;
}
}
else
{
/* which is a non-device filesystem mount */
dev_id = RT_NULL;
}
/* find out specific filesystem */ return -1;
dfs_lock(); }
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) }
{ else
if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0) {
break; /* which is a non-device filesystem mount */
} dev_id = RT_NULL;
}
/* can't find filesystem */ /* find out specific filesystem */
if (index == DFS_FILESYSTEM_TYPES_MAX) dfs_lock();
{ for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
rt_set_errno(-DFS_STATUS_ENODEV); {
dfs_unlock(); if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)
return -1; break;
} }
ops = filesystem_operation_table[index];
dfs_unlock();
/* make full path for special file */ /* can't find filesystem */
fullpath = dfs_normalize_path(RT_NULL, path); if (index == DFS_FILESYSTEM_TYPES_MAX)
if (fullpath == RT_NULL) /* not an abstract path */ {
{ rt_set_errno(-DFS_STATUS_ENODEV);
rt_set_errno(-DFS_STATUS_ENOTDIR); dfs_unlock();
return -1;
}
/* Check if the path exists or not, raw APIs call, fixme */ return -1;
if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0)) }
{ ops = filesystem_operation_table[index];
struct dfs_fd fd; dfs_unlock();
if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0) /* make full path for special file */
{ fullpath = dfs_normalize_path(RT_NULL, path);
rt_free(fullpath); if (fullpath == RT_NULL) /* not an abstract path */
rt_set_errno(-DFS_STATUS_ENOTDIR); {
return -1; rt_set_errno(-DFS_STATUS_ENOTDIR);
}
dfs_file_close(&fd);
}
/* check whether the file system mounted or not */ return -1;
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 */ /* Check if the path exists or not, raw APIs call, fixme */
for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL; if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0))
index++) ; {
if (index == DFS_FILESYSTEMS_MAX) /* can't find en empty filesystem table entry */ struct dfs_fd fd;
{
rt_set_errno(-DFS_STATUS_ENOSPC);
goto err1;
}
/* register file system */ if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0)
fs = &(filesystem_table[index]); {
fs->path = fullpath; rt_free(fullpath);
fs->ops = ops; rt_set_errno(-DFS_STATUS_ENOTDIR);
fs->dev_id = dev_id;
/* release filesystem_table lock */
dfs_unlock();
/* open device, but do not check the status of device */ return -1;
if (dev_id != RT_NULL) }
rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR); dfs_file_close(&fd);
}
if (ops->mount == RT_NULL) /* there is no mount implementation */ /* check whether the file system mounted or not */
{ dfs_lock();
if (dev_id != RT_NULL) for (index = 0; index < DFS_FILESYSTEMS_MAX; index ++)
rt_device_close(dev_id); {
dfs_lock(); if (filesystem_table[index].ops != RT_NULL &&
/* clear filesystem table entry */ strcmp(filesystem_table[index].path, path) == 0)
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); {
dfs_unlock(); rt_set_errno(-DFS_STATUS_EINVAL);
goto err1;
}
}
rt_free(fullpath); /* find out an empty filesystem table entry */
rt_set_errno(-DFS_STATUS_ENOSYS); for (index = 0;
return -1; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL;
} index ++)
/* 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 */ /* can't find en empty filesystem table entry */
dfs_lock(); if (index == DFS_FILESYSTEMS_MAX)
/* clear filesystem table entry */ {
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); rt_set_errno(-DFS_STATUS_ENOSPC);
dfs_unlock(); goto err1;
}
rt_free(fullpath); /* register file system */
return -1; fs = &(filesystem_table[index]);
} fs->path = fullpath;
fs->ops = ops;
fs->dev_id = dev_id;
/* release filesystem_table lock */
dfs_unlock();
return 0; /* 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: err1:
dfs_unlock(); dfs_unlock();
if (fullpath != RT_NULL) if (fullpath != RT_NULL)
rt_free(fullpath); rt_free(fullpath);
return -1; return -1;
} }
/** /**
@ -347,45 +369,48 @@ err1:
*/ */
int dfs_unmount(const char *specialfile) int dfs_unmount(const char *specialfile)
{ {
char *fullpath; char *fullpath;
struct dfs_filesystem *fs = RT_NULL; struct dfs_filesystem *fs = RT_NULL;
fullpath = dfs_normalize_path(RT_NULL, specialfile); fullpath = dfs_normalize_path(RT_NULL, specialfile);
if (fullpath == RT_NULL) if (fullpath == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1;
}
/* lock filesystem */ return -1;
dfs_lock(); }
fs = dfs_filesystem_lookup(fullpath); /* lock filesystem */
if (fs == RT_NULL || fs->ops->unmount == RT_NULL || fs->ops->unmount(fs) < 0) dfs_lock();
{
goto err1;
}
/* close device, but do not check the status of device */ fs = dfs_filesystem_lookup(fullpath);
if (fs->dev_id != RT_NULL) if (fs == RT_NULL ||
rt_device_close(fs->dev_id); fs->ops->unmount == RT_NULL ||
fs->ops->unmount(fs) < 0)
{
goto err1;
}
if (fs->path != RT_NULL) /* close device, but do not check the status of device */
rt_free(fs->path); if (fs->dev_id != RT_NULL)
rt_device_close(fs->dev_id);
/* clear this filesystem table entry */ if (fs->path != RT_NULL)
rt_memset(fs, 0, sizeof(struct dfs_filesystem)); rt_free(fs->path);
dfs_unlock(); /* clear this filesystem table entry */
rt_free(fullpath); rt_memset(fs, 0, sizeof(struct dfs_filesystem));
return 0; dfs_unlock();
rt_free(fullpath);
return 0;
err1: err1:
dfs_unlock(); dfs_unlock();
rt_free(fullpath); rt_free(fullpath);
return -1; return -1;
} }
/** /**
@ -398,30 +423,30 @@ err1:
*/ */
int dfs_mkfs(const char *fs_name, const char *device_name) int dfs_mkfs(const char *fs_name, const char *device_name)
{ {
int index; int index;
/* lock file system */ /* lock file system */
dfs_lock(); dfs_lock();
/* find the file system operations */ /* find the file system operations */
for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++) for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index ++)
{ {
if (filesystem_operation_table[index] != RT_NULL && if (filesystem_operation_table[index] != RT_NULL &&
strcmp(filesystem_operation_table[index]->name, fs_name) == 0) strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
{ {
/* find file system operation */ /* find file system operation */
const struct dfs_filesystem_operation* ops = filesystem_operation_table[index]; const struct dfs_filesystem_operation *ops = filesystem_operation_table[index];
dfs_unlock(); dfs_unlock();
if (ops->mkfs != RT_NULL) if (ops->mkfs != RT_NULL)
return ops->mkfs(device_name); return ops->mkfs(device_name);
break; break;
} }
} }
dfs_unlock(); dfs_unlock();
rt_kprintf("Can not find the file system which named as %s.\n", fs_name); rt_kprintf("Can not find the file system which named as %s.\n", fs_name);
return -1; 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) int dfs_statfs(const char *path, struct statfs *buffer)
{ {
struct dfs_filesystem *fs; struct dfs_filesystem *fs;
fs = dfs_filesystem_lookup(path); fs = dfs_filesystem_lookup(path);
if (fs != RT_NULL) if (fs != RT_NULL)
{ {
if (fs->ops->statfs != RT_NULL) if (fs->ops->statfs != RT_NULL)
return fs->ops->statfs(fs, buffer); return fs->ops->statfs(fs, buffer);
} }
return -1; return -1;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
void mkfs(const char *fs_name, const char *device_name) 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); FINSH_FUNCTION_EXPORT(mkfs, make a file system);
void df(const char *path) void df(const char *path)
{ {
int result; int result;
struct statfs buffer; struct statfs buffer;
if (path == RT_NULL) if (path == RT_NULL)
result = dfs_statfs("/", &buffer); result = dfs_statfs("/", &buffer);
else else
result = dfs_statfs(path, &buffer); result = dfs_statfs(path, &buffer);
if (result == 0) if (result == 0)
{ {
rt_kprintf("disk free: %d block[%d bytes per block]\n", buffer.f_bfree, buffer.f_bsize); rt_kprintf("disk free: %d block[%d bytes per block]\n",
} buffer.f_bfree, buffer.f_bsize);
}
} }
FINSH_FUNCTION_EXPORT(df, get disk free); FINSH_FUNCTION_EXPORT(df, get disk free);
#endif #endif

View File

@ -18,11 +18,12 @@
/** /**
* @addtogroup FsPosixApi * @addtogroup FsPosixApi
*/ */
/*@{*/ /*@{*/
/** /**
* this function is a POSIX compliant version, which will open a file and return * this function is a POSIX compliant version, which will open a file and
* a file descriptor. * return a file descriptor.
* *
* @param file the path name of file. * @param file the path name of file.
* @param flags the file open flags. * @param flags the file open flags.
@ -32,34 +33,35 @@
*/ */
int open(const char *file, int flags, int mode) int open(const char *file, int flags, int mode)
{ {
int fd, result; int fd, result;
struct dfs_fd *d; struct dfs_fd *d;
/* allocate a fd */ /* allocate a fd */
fd = fd_new(); fd = fd_new();
if (fd < 0) if (fd < 0)
{ {
rt_set_errno(-DFS_STATUS_ENOMEM); rt_set_errno(-DFS_STATUS_ENOMEM);
return -1;
}
d = fd_get(fd);
result = dfs_file_open(d, file, flags); return -1;
if (result < 0) }
{ d = fd_get(fd);
/* release the ref-count of fd */
fd_put(d);
fd_put(d);
rt_set_errno(result);
return -1; 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);
/* release the ref-count of fd */ return -1;
fd_put(d); }
return fd; /* release the ref-count of fd */
fd_put(d);
return fd;
} }
RTM_EXPORT(open); RTM_EXPORT(open);
@ -73,35 +75,36 @@ RTM_EXPORT(open);
*/ */
int close(int fd) int close(int fd)
{ {
int result; int result;
struct dfs_fd *d; struct dfs_fd *d;
d = fd_get(fd); d = fd_get(fd);
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1;
}
result = dfs_file_close(d); return -1;
fd_put(d); }
if (result < 0) result = dfs_file_close(d);
{ fd_put(d);
rt_set_errno(result);
return -1; if (result < 0)
} {
rt_set_errno(result);
fd_put(d); return -1;
}
return 0; fd_put(d);
return 0;
} }
RTM_EXPORT(close); RTM_EXPORT(close);
/** /**
* this function is a POSIX compliant version, which will read specified data buffer * this function is a POSIX compliant version, which will read specified data
* length for an open file descriptor. * buffer length for an open file descriptor.
* *
* @param fd the file descriptor. * @param fd the file descriptor.
* @param buf the buffer to save the read data. * @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 read(int fd, void *buf, size_t len)
{ {
int result; int result;
struct dfs_fd *d; struct dfs_fd *d;
/* get the fd */ /* get the fd */
d = fd_get(fd); d = fd_get(fd);
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1;
}
result = dfs_file_read(d, buf, len); return -1;
if (result < 0) }
{
fd_put(d);
rt_set_errno(result);
return -1; result = dfs_file_read(d, buf, len);
} if (result < 0)
{
fd_put(d);
rt_set_errno(result);
/* release the ref-count of fd */ return -1;
fd_put(d); }
return result; /* release the ref-count of fd */
fd_put(d);
return result;
} }
RTM_EXPORT(read); RTM_EXPORT(read);
/** /**
* this function is a POSIX compliant version, which will write specified data buffer * this function is a POSIX compliant version, which will write specified data
* length for an open file descriptor. * buffer length for an open file descriptor.
* *
* @param fd the file descriptor * @param fd the file descriptor
* @param buf the data buffer to be written. * @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 write(int fd, const void *buf, size_t len)
{ {
int result; int result;
struct dfs_fd *d; struct dfs_fd *d;
/* get the fd */ /* get the fd */
d = fd_get(fd); d = fd_get(fd);
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1; return -1;
} }
result = dfs_file_write(d, buf, len); result = dfs_file_write(d, buf, len);
if (result < 0) if (result < 0)
{ {
fd_put(d); fd_put(d);
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
/* release the ref-count of fd */ /* release the ref-count of fd */
fd_put(d); fd_put(d);
return result; return result;
} }
RTM_EXPORT(write); RTM_EXPORT(write);
/** /**
* this function is a POSIX compliant version, which will seek the offset for an * this function is a POSIX compliant version, which will seek the offset for
* open file descriptor. * an open file descriptor.
* *
* @param fd the file descriptor. * @param fd the file descriptor.
* @param offset the offset to be seeked. * @param offset the offset to be seeked.
@ -190,59 +194,61 @@ RTM_EXPORT(write);
*/ */
off_t lseek(int fd, off_t offset, int whence) off_t lseek(int fd, off_t offset, int whence)
{ {
int result; int result;
struct dfs_fd *d; struct dfs_fd *d;
d = fd_get(fd); d = fd_get(fd);
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1;
}
switch (whence) return -1;
{ }
case DFS_SEEK_SET:
break;
case DFS_SEEK_CUR: switch (whence)
offset += d->pos; {
break; case DFS_SEEK_SET:
break;
case DFS_SEEK_END: case DFS_SEEK_CUR:
offset += d->size; offset += d->pos;
break; break;
default: case DFS_SEEK_END:
rt_set_errno(-DFS_STATUS_EINVAL); offset += d->size;
return -1; break;
}
if (offset < 0) default:
{ rt_set_errno(-DFS_STATUS_EINVAL);
rt_set_errno(-DFS_STATUS_EINVAL);
return -1; return -1;
} }
result = dfs_file_lseek(d, offset);
if (result < 0)
{
fd_put(d);
rt_set_errno(result);
return -1; if (offset < 0)
} {
rt_set_errno(-DFS_STATUS_EINVAL);
/* release the ref-count of fd */ return -1;
fd_put(d); }
result = dfs_file_lseek(d, offset);
if (result < 0)
{
fd_put(d);
rt_set_errno(result);
return offset; return -1;
}
/* release the ref-count of fd */
fd_put(d);
return offset;
} }
RTM_EXPORT(lseek); RTM_EXPORT(lseek);
/** /**
* this function is a POSIX compliant version, which will rename old file name to * this function is a POSIX compliant version, which will rename old file name
* new file name. * to new file name.
* *
* @param old the old file name. * @param old the old file name.
* @param new the new file name. * @param new the new file name.
@ -253,17 +259,17 @@ RTM_EXPORT(lseek);
*/ */
int rename(const char *old, const char *new) int rename(const char *old, const char *new)
{ {
int result; int result;
result = dfs_file_rename(old, new); result = dfs_file_rename(old, new);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
return 0; return 0;
} }
RTM_EXPORT(rename); RTM_EXPORT(rename);
@ -277,17 +283,17 @@ RTM_EXPORT(rename);
*/ */
int unlink(const char *pathname) int unlink(const char *pathname)
{ {
int result; int result;
result = dfs_file_unlink(pathname); result = dfs_file_unlink(pathname);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
return 0; return 0;
} }
RTM_EXPORT(unlink); RTM_EXPORT(unlink);
@ -301,17 +307,17 @@ RTM_EXPORT(unlink);
*/ */
int stat(const char *file, struct stat *buf) int stat(const char *file, struct stat *buf)
{ {
int result; int result;
result = dfs_file_stat(file, buf); result = dfs_file_stat(file, buf);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
return result; return result;
} }
RTM_EXPORT(stat); RTM_EXPORT(stat);
@ -323,35 +329,35 @@ RTM_EXPORT(stat);
*/ */
int fstat(int fildes, struct stat *buf) int fstat(int fildes, struct stat *buf)
{ {
struct dfs_fd *d; struct dfs_fd *d;
/* get the fd */ /* get the fd */
d = fd_get(fildes); d = fd_get(fildes);
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1; return -1;
} }
/* it's the root directory */ /* it's the root directory */
buf->st_dev = 0; buf->st_dev = 0;
buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
if (d->type == FT_DIRECTORY) if (d->type == FT_DIRECTORY)
{ {
buf->st_mode &= ~DFS_S_IFREG; 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_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
} }
buf->st_size = d->size; buf->st_size = d->size;
buf->st_mtime = 0; buf->st_mtime = 0;
buf->st_blksize = 512; buf->st_blksize = 512;
fd_put(d); fd_put(d);
return DFS_STATUS_OK; return DFS_STATUS_OK;
} }
RTM_EXPORT(fstat); RTM_EXPORT(fstat);
@ -366,17 +372,17 @@ RTM_EXPORT(fstat);
*/ */
int statfs(const char *path, struct statfs *buf) int statfs(const char *path, struct statfs *buf)
{ {
int result; int result;
result = dfs_statfs(path, buf); result = dfs_statfs(path, buf);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
return result; return result;
} }
RTM_EXPORT(statfs); RTM_EXPORT(statfs);
@ -390,34 +396,34 @@ RTM_EXPORT(statfs);
*/ */
int mkdir(const char *path, mode_t mode) int mkdir(const char *path, mode_t mode)
{ {
int fd; int fd;
struct dfs_fd *d; struct dfs_fd *d;
int result; int result;
fd = fd_new(); fd = fd_new();
if (fd == -1) if (fd == -1)
{ {
rt_set_errno(-DFS_STATUS_ENOMEM); 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) if (result < 0)
{ {
fd_put(d); fd_put(d);
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
dfs_file_close(d); dfs_file_close(d);
fd_put(d); fd_put(d);
return 0; return 0;
} }
RTM_EXPORT(mkdir); RTM_EXPORT(mkdir);
@ -435,17 +441,17 @@ FINSH_FUNCTION_EXPORT(mkdir, create a directory);
*/ */
int rmdir(const char *pathname) int rmdir(const char *pathname)
{ {
int result; int result;
result = dfs_file_unlink(pathname); result = dfs_file_unlink(pathname);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
return 0; return 0;
} }
RTM_EXPORT(rmdir); RTM_EXPORT(rmdir);
@ -458,48 +464,48 @@ RTM_EXPORT(rmdir);
*/ */
DIR *opendir(const char *name) DIR *opendir(const char *name)
{ {
struct dfs_fd *d; struct dfs_fd *d;
int fd, result; int fd, result;
DIR *t; DIR *t;
t = RT_NULL; t = RT_NULL;
/* allocate a fd */ /* allocate a fd */
fd = fd_new(); fd = fd_new();
if (fd == -1) if (fd == -1)
{ {
rt_set_errno(-DFS_STATUS_ENOMEM); rt_set_errno(-DFS_STATUS_ENOMEM);
return RT_NULL; return RT_NULL;
} }
d = fd_get(fd); d = fd_get(fd);
result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY); result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
if (result >= 0) if (result >= 0)
{ {
/* open successfully */ /* open successfully */
t = (DIR *) rt_malloc(sizeof(DIR)); t = (DIR *) rt_malloc(sizeof(DIR));
if (t == RT_NULL) if (t == RT_NULL)
{ {
dfs_file_close(d); dfs_file_close(d);
fd_put(d); fd_put(d);
} }
else else
{ {
rt_memset(t, 0, sizeof(DIR)); rt_memset(t, 0, sizeof(DIR));
t->fd = fd; t->fd = fd;
} }
fd_put(d); fd_put(d);
return t; return t;
} }
/* open failed */ /* open failed */
fd_put(d); fd_put(d);
fd_put(d); fd_put(d);
rt_set_errno(result); rt_set_errno(result);
return RT_NULL; return RT_NULL;
} }
RTM_EXPORT(opendir); RTM_EXPORT(opendir);
@ -514,36 +520,39 @@ RTM_EXPORT(opendir);
*/ */
struct dirent *readdir(DIR *d) struct dirent *readdir(DIR *d)
{ {
int result; int result;
struct dfs_fd *fd; struct dfs_fd *fd;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return RT_NULL; return RT_NULL;
} }
if (!d->num || (d->cur += ((struct dirent*)(d->buf + d->cur))->d_reclen) >= d->num) 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); /* get a new entry */
if (result <= 0) result = dfs_file_getdents(fd,
{ (struct dirent*)d->buf,
fd_put(fd); sizeof(d->buf) - 1);
rt_set_errno(result); if (result <= 0)
{
fd_put(fd);
rt_set_errno(result);
return RT_NULL; return RT_NULL;
} }
d->num = result; d->num = result;
d->cur = 0; /* current entry index */ d->cur = 0; /* current entry index */
} }
fd_put(fd); fd_put(fd);
return (struct dirent *)(d->buf+d->cur); return (struct dirent *)(d->buf+d->cur);
} }
RTM_EXPORT(readdir); RTM_EXPORT(readdir);
@ -557,21 +566,21 @@ RTM_EXPORT(readdir);
*/ */
long telldir(DIR *d) long telldir(DIR *d)
{ {
struct dfs_fd *fd; struct dfs_fd *fd;
long result; long result;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return 0; return 0;
} }
result = fd->pos - d->num + d->cur; result = fd->pos - d->num + d->cur;
fd_put(fd); fd_put(fd);
return result; return result;
} }
RTM_EXPORT(telldir); RTM_EXPORT(telldir);
@ -584,44 +593,45 @@ RTM_EXPORT(telldir);
*/ */
void seekdir(DIR *d, off_t offset) void seekdir(DIR *d, off_t offset)
{ {
struct dfs_fd *fd; struct dfs_fd *fd;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return ; return ;
} }
/* seek to the offset position of directory */ /* seek to the offset position of directory */
if (dfs_file_lseek(fd, offset) >= 0) if (dfs_file_lseek(fd, offset) >= 0)
d->num = d->cur = 0; d->num = d->cur = 0;
fd_put(fd); fd_put(fd);
} }
RTM_EXPORT(seekdir); 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. * @param d the directory stream.
*/ */
void rewinddir(DIR *d) void rewinddir(DIR *d)
{ {
struct dfs_fd *fd; struct dfs_fd *fd;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return ; return ;
} }
/* seek to the beginning of directory */ /* seek to the beginning of directory */
if (dfs_file_lseek(fd, 0) >= 0) if (dfs_file_lseek(fd, 0) >= 0)
d->num = d->cur = 0; d->num = d->cur = 0;
fd_put(fd); fd_put(fd);
} }
RTM_EXPORT(rewinddir); RTM_EXPORT(rewinddir);
@ -635,37 +645,38 @@ RTM_EXPORT(rewinddir);
*/ */
int closedir(DIR *d) int closedir(DIR *d)
{ {
int result; int result;
struct dfs_fd *fd; struct dfs_fd *fd;
fd = fd_get(d->fd); fd = fd_get(d->fd);
if (fd == RT_NULL) if (fd == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_EBADF); rt_set_errno(-DFS_STATUS_EBADF);
return -1; return -1;
} }
result = dfs_file_close(fd); result = dfs_file_close(fd);
fd_put(fd); fd_put(fd);
fd_put(fd); fd_put(fd);
rt_free(d); rt_free(d);
if (result < 0) if (result < 0)
{ {
rt_set_errno(result); rt_set_errno(result);
return -1; return -1;
} }
else else
return 0; return 0;
} }
RTM_EXPORT(closedir); RTM_EXPORT(closedir);
#ifdef DFS_USING_WORKDIR #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. * @param path the path name to be changed to.
* *
@ -673,54 +684,55 @@ RTM_EXPORT(closedir);
*/ */
int chdir(const char *path) int chdir(const char *path)
{ {
char *fullpath; char *fullpath;
DIR *d; DIR *d;
if (path == RT_NULL) if (path == RT_NULL)
{ {
dfs_lock(); dfs_lock();
rt_kprintf("%s\n", working_directory); rt_kprintf("%s\n", working_directory);
dfs_unlock(); dfs_unlock();
return 0; return 0;
} }
if (rt_strlen(path) > DFS_PATH_MAX) if (rt_strlen(path) > DFS_PATH_MAX)
{ {
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; return -1;
} }
fullpath = dfs_normalize_path(NULL, path); fullpath = dfs_normalize_path(NULL, path);
if (fullpath == RT_NULL) if (fullpath == RT_NULL)
{ {
rt_set_errno(-DFS_STATUS_ENOTDIR); rt_set_errno(-DFS_STATUS_ENOTDIR);
return -1; /* build path failed */ return -1; /* build path failed */
} }
dfs_lock(); dfs_lock();
d = opendir(fullpath); d = opendir(fullpath);
if (d == RT_NULL) if (d == RT_NULL)
{ {
rt_free(fullpath); rt_free(fullpath);
/* this is a not exist directory */ /* this is a not exist directory */
dfs_unlock(); dfs_unlock();
return -1; return -1;
} }
/* close directory stream */ /* close directory stream */
closedir(d); closedir(d);
/* copy full path to working directory */ /* copy full path to working directory */
strncpy(working_directory, fullpath, DFS_PATH_MAX); strncpy(working_directory, fullpath, DFS_PATH_MAX);
rt_free(fullpath); /* release normalize directory path name */ /* release normalize directory path name */
rt_free(fullpath);
dfs_unlock(); dfs_unlock();
return 0; return 0;
} }
RTM_EXPORT(chdir); RTM_EXPORT(chdir);
@ -741,14 +753,14 @@ FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory);
char *getcwd(char *buf, size_t size) char *getcwd(char *buf, size_t size)
{ {
#ifdef DFS_USING_WORKDIR #ifdef DFS_USING_WORKDIR
rt_enter_critical(); rt_enter_critical();
rt_strncpy(buf, working_directory, size); rt_strncpy(buf, working_directory, size);
rt_exit_critical(); rt_exit_critical();
#else #else
rt_kprintf("WARNING: not support working directory\n"); rt_kprintf("WARNING: not support working directory\n");
#endif #endif
return buf; return buf;
} }
RTM_EXPORT(getcwd); RTM_EXPORT(getcwd);