|
@@ -1,32 +1,30 @@
|
|
|
/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Project : Device Filesystem
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Copyright 2004, 2005 www.fayfayspace.org.
|
|
|
-| All rights reserved.
|
|
|
-|------------------------------------------------------------------------------
|
|
|
-| File : dfs_posix.c, the interface related implementations of Device FileSystem
|
|
|
-|------------------------------------------------------------------------------
|
|
|
-| Chang Logs:
|
|
|
-| Date Author Notes
|
|
|
-| 2009-05-27 Yi.qiu The first version.
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
-#include <string.h>
|
|
|
-#include <dfs_util.h>
|
|
|
+ * File : dfs_posix.c
|
|
|
+ * This file is part of Device File System in RT-Thread RTOS
|
|
|
+ * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
|
|
|
+ *
|
|
|
+ * The license and distribution terms for this file may be
|
|
|
+ * found in the file LICENSE in this distribution or at
|
|
|
+ * http://www.rt-thread.org/license/LICENSE.
|
|
|
+ *
|
|
|
+ * Change Logs:
|
|
|
+ * Date Author Notes
|
|
|
+ * 2009-05-27 Yi.qiu The first version
|
|
|
+ */
|
|
|
+
|
|
|
+#include <dfs.h>
|
|
|
#include <dfs_posix.h>
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : open
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * 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.
|
|
|
+ * @param mode
|
|
|
+ *
|
|
|
+ * @return the non-negative integer on successful open, others for failed.
|
|
|
+ */
|
|
|
int open(const char *file, int flags, int mode)
|
|
|
{
|
|
|
int fd, result;
|
|
@@ -37,10 +35,12 @@ int open(const char *file, int flags, int mode)
|
|
|
if (fd < 0) return -1;
|
|
|
d = fd_get(fd);
|
|
|
|
|
|
- result = dfile_raw_open(d, file, flags);
|
|
|
+ result = dfs_file_open(d, file, flags);
|
|
|
if (result < 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
|
+
|
|
|
+ /* release the ref-count of fd */
|
|
|
fd_put(d);
|
|
|
fd_put(d);
|
|
|
|
|
@@ -52,17 +52,14 @@ int open(const char *file, int flags, int mode)
|
|
|
return fd;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : close
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will close the open
|
|
|
+ * file descriptor.
|
|
|
+ *
|
|
|
+ * @param fd the file descriptor.
|
|
|
+ *
|
|
|
+ * @return 0 on successful, -1 on failed.
|
|
|
+ */
|
|
|
int close(int fd)
|
|
|
{
|
|
|
int result;
|
|
@@ -75,8 +72,7 @@ int close(int fd)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- result = dfile_raw_close(d);
|
|
|
- fd_put(d);
|
|
|
+ result = dfs_file_close(d);
|
|
|
fd_put(d);
|
|
|
|
|
|
if (result < 0)
|
|
@@ -84,20 +80,21 @@ int close(int fd)
|
|
|
rt_set_errno(result);
|
|
|
return -1;
|
|
|
}
|
|
|
+
|
|
|
+ fd_put(d);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : read
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * 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.
|
|
|
+ * @param len the maximal length of data buffer
|
|
|
+ *
|
|
|
+ * @return the actual read data buffer length
|
|
|
+ */
|
|
|
int read(int fd, char *buf, int len)
|
|
|
{
|
|
|
int result;
|
|
@@ -111,7 +108,7 @@ int read(int fd, char *buf, int len)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- result = dfile_raw_read(d, buf, len);
|
|
|
+ result = dfs_file_read(d, buf, len);
|
|
|
if (result < 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
@@ -125,17 +122,16 @@ int read(int fd, char *buf, int len)
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : write
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will write pecified data buffer
|
|
|
+ * length for an open file descriptor.
|
|
|
+ *
|
|
|
+ * @param fd the file descriptor
|
|
|
+ * @param buf the data buffer to be written.
|
|
|
+ * @param len the data buffer length.
|
|
|
+ *
|
|
|
+ * @return the actual written data buffer length.
|
|
|
+ */
|
|
|
int write(int fd, char *buf, int len)
|
|
|
{
|
|
|
int result;
|
|
@@ -149,7 +145,7 @@ int write(int fd, char *buf, int len)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- result = dfile_raw_write(d, buf, len);
|
|
|
+ result = dfs_file_write(d, buf, len);
|
|
|
if (result < 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
@@ -163,17 +159,16 @@ int write(int fd, char *buf, int len)
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : lseek
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * 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.
|
|
|
+ * @param dir the directory of seek.
|
|
|
+ *
|
|
|
+ * @return the current file position, or -1 on failed.
|
|
|
+ */
|
|
|
int lseek(int fd, int offset, int dir)
|
|
|
{
|
|
|
int result;
|
|
@@ -200,7 +195,7 @@ int lseek(int fd, int offset, int dir)
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- result = dfile_raw_lseek(d, offset);
|
|
|
+ result = dfs_file_lseek(d, offset);
|
|
|
if (result < 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
@@ -213,22 +208,22 @@ int lseek(int fd, int offset, int dir)
|
|
|
return offset;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : rename
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
-int rename(const char* old, const char* new )
|
|
|
+/**
|
|
|
+ * 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.
|
|
|
+ *
|
|
|
+ * @return 0 on successful, -1 on failed.
|
|
|
+ *
|
|
|
+ * note: the old and new file name must be belong to a same file system.
|
|
|
+ */
|
|
|
+int rename(const char* old, const char* new)
|
|
|
{
|
|
|
int result;
|
|
|
|
|
|
- result = dfile_raw_rename(old, new);
|
|
|
+ result = dfs_file_rename(old, new);
|
|
|
if (result < 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
@@ -237,22 +232,19 @@ int rename(const char* old, const char* new )
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : unlink
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will unlink (remove) a
|
|
|
+ * specified path file from file system.
|
|
|
+ *
|
|
|
+ * @param pathname the specified path name to be unlinked.
|
|
|
+ *
|
|
|
+ * @return 0 on successful, -1 on failed.
|
|
|
+ */
|
|
|
int unlink(const char *pathname)
|
|
|
{
|
|
|
int result;
|
|
|
|
|
|
- result = dfile_raw_unlink(pathname);
|
|
|
+ result = dfs_file_unlink(pathname);
|
|
|
if (result < 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
@@ -261,22 +253,19 @@ int unlink(const char *pathname)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : stat
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will get file information.
|
|
|
+ *
|
|
|
+ * @param file the file name
|
|
|
+ * @param buf the the data buffer to save stat description.
|
|
|
+ *
|
|
|
+ * @return 0 on successful, -1 on failed.
|
|
|
+ */
|
|
|
int stat(const char *file, struct dfs_stat *buf)
|
|
|
{
|
|
|
int result;
|
|
|
|
|
|
- result = dfile_raw_stat(file, (struct dfs_stat *)buf);
|
|
|
+ result = dfs_file_stat(file, buf);
|
|
|
if (result < 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
@@ -285,17 +274,37 @@ int stat(const char *file, struct dfs_stat *buf)
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : mkdir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will return the
|
|
|
+ * information about a mounted file system.
|
|
|
+ *
|
|
|
+ * @param path the path which mounted file system.
|
|
|
+ * @param buf the buffer to save the returned information.
|
|
|
+ *
|
|
|
+ * @return 0 on successful, others on failed.
|
|
|
+ */
|
|
|
+int statfs(const char *path, struct dfs_statfs *buf)
|
|
|
+{
|
|
|
+ int result;
|
|
|
+
|
|
|
+ result = dfs_statfs(path, buf);
|
|
|
+ if (result < 0)
|
|
|
+ {
|
|
|
+ rt_set_errno(result);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will make a directory
|
|
|
+ *
|
|
|
+ * @param path the directory path to be made.
|
|
|
+ * @param mode
|
|
|
+ *
|
|
|
+ * @return 0 on successful, others on failed.
|
|
|
+ */
|
|
|
int mkdir (const char *path, rt_uint16_t mode)
|
|
|
{
|
|
|
int fd;
|
|
@@ -305,9 +314,7 @@ int mkdir (const char *path, rt_uint16_t mode)
|
|
|
fd = fd_new();
|
|
|
d = fd_get(fd);
|
|
|
|
|
|
- result = dfile_raw_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT);
|
|
|
- fd_put(d);
|
|
|
- fd_put(d);
|
|
|
+ result = dfs_file_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT);
|
|
|
|
|
|
if (result < 0)
|
|
|
{
|
|
@@ -315,25 +322,23 @@ int mkdir (const char *path, rt_uint16_t mode)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
+ fd_put(d);
|
|
|
+ fd_put(d);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : rmdir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will remove a directory.
|
|
|
+ *
|
|
|
+ * @param pathname the path name to be removed.
|
|
|
+ *
|
|
|
+ * @return 0 on sucessfull, others on failed.
|
|
|
+ */
|
|
|
int rmdir(const char *pathname)
|
|
|
{
|
|
|
int result;
|
|
|
|
|
|
- result = dfile_raw_unlink(pathname);
|
|
|
+ result = dfs_file_unlink(pathname);
|
|
|
if (result < 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
@@ -343,17 +348,13 @@ int rmdir(const char *pathname)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : opendir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will open a directory.
|
|
|
+ *
|
|
|
+ * @param name the path name to be open.
|
|
|
+ *
|
|
|
+ * @return the DIR pointer of directory, NULL on open failed.
|
|
|
+ */
|
|
|
DIR* opendir(const char* name)
|
|
|
{
|
|
|
struct dfs_fd* d;
|
|
@@ -367,14 +368,14 @@ DIR* opendir(const char* name)
|
|
|
if (fd == -1) { rt_kprintf("no fd\n"); return RT_NULL; }
|
|
|
d = fd_get(fd);
|
|
|
|
|
|
- result = dfile_raw_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
|
|
|
+ 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)
|
|
|
{
|
|
|
- dfile_raw_close(d);
|
|
|
+ dfs_file_close(d);
|
|
|
fd_put(d);
|
|
|
}
|
|
|
else
|
|
@@ -394,17 +395,15 @@ DIR* opendir(const char* name)
|
|
|
return RT_NULL;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : readdir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will return a pointer
|
|
|
+ * to a dirent structure representing the next directory entry in the
|
|
|
+ * directory stream.
|
|
|
+ *
|
|
|
+ * @param d the directory stream pointer.
|
|
|
+ *
|
|
|
+ * @return the next directory entry, NULL on the end of directory or failed.
|
|
|
+ */
|
|
|
struct dfs_dirent* readdir(DIR *d)
|
|
|
{
|
|
|
int result;
|
|
@@ -419,7 +418,7 @@ struct dfs_dirent* readdir(DIR *d)
|
|
|
|
|
|
if (!d->num || (d->cur += ((struct dfs_dirent*)(d->buf + d->cur))->d_reclen) >= d->num)
|
|
|
{
|
|
|
- result = dfile_raw_getdents(fd, (struct dfs_dirent*)d->buf, sizeof(d->buf) - 1);
|
|
|
+ result = dfs_file_getdents(fd, (struct dfs_dirent*)d->buf, sizeof(d->buf) - 1);
|
|
|
if (result <= 0)
|
|
|
{
|
|
|
rt_set_errno(result);
|
|
@@ -436,17 +435,14 @@ struct dfs_dirent* readdir(DIR *d)
|
|
|
return (struct dfs_dirent*)(d->buf+d->cur);
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : telldir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will return current
|
|
|
+ * location in directory stream.
|
|
|
+ *
|
|
|
+ * @param d the directory stream pointer.
|
|
|
+ *
|
|
|
+ * @return the current location in directory stream.
|
|
|
+ */
|
|
|
rt_off_t telldir(DIR *d)
|
|
|
{
|
|
|
struct dfs_fd* fd;
|
|
@@ -465,17 +461,13 @@ rt_off_t telldir(DIR *d)
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : seekdir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will set position of
|
|
|
+ * next directory structure in the directory stream.
|
|
|
+ *
|
|
|
+ * @param d the directory stream.
|
|
|
+ * @param offset the offset in directory stream.
|
|
|
+ */
|
|
|
void seekdir(DIR *d, rt_off_t offset)
|
|
|
{
|
|
|
struct dfs_fd* fd;
|
|
@@ -487,21 +479,15 @@ void seekdir(DIR *d, rt_off_t offset)
|
|
|
return ;
|
|
|
}
|
|
|
|
|
|
- if (dfile_raw_lseek(fd, offset) >= 0) d->num = d->cur = 0;
|
|
|
+ if (dfs_file_lseek(fd, offset) >= 0) d->num = d->cur = 0;
|
|
|
fd_put(fd);
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : rewinddir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * 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;
|
|
@@ -513,21 +499,18 @@ void rewinddir(DIR *d)
|
|
|
return ;
|
|
|
}
|
|
|
|
|
|
- if (dfile_raw_lseek(fd, 0) >= 0) d->num = d->cur = 0;
|
|
|
+ if (dfs_file_lseek(fd, 0) >= 0) d->num = d->cur = 0;
|
|
|
fd_put(fd);
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : closedir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will close a directory
|
|
|
+ * stream.
|
|
|
+ *
|
|
|
+ * @param d the directory stream.
|
|
|
+ *
|
|
|
+ * @return 0 on successful, -1 on failed.
|
|
|
+ */
|
|
|
int closedir(DIR* d)
|
|
|
{
|
|
|
int result;
|
|
@@ -540,7 +523,7 @@ int closedir(DIR* d)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- result = dfile_raw_close(fd);
|
|
|
+ result = dfs_file_close(fd);
|
|
|
fd_put(fd);
|
|
|
|
|
|
fd_put(fd);
|
|
@@ -554,60 +537,58 @@ int closedir(DIR* d)
|
|
|
else return 0;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : chdir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+#ifdef DFS_USING_WORKDIR
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will change working directory.
|
|
|
+ *
|
|
|
+ * @param path the path name to be changed to.
|
|
|
+ *
|
|
|
+ * @return 0 on successful, -1 on failed.
|
|
|
+ */
|
|
|
int chdir(const char *path)
|
|
|
{
|
|
|
- char* fullpath, full_path[DFS_PATH_MAX + 1];
|
|
|
+ char* fullpath;
|
|
|
+ DIR* d;
|
|
|
|
|
|
if(path == RT_NULL || rt_strlen(path) > DFS_PATH_MAX)
|
|
|
return -1;
|
|
|
|
|
|
- fullpath = (char*)path;
|
|
|
- if ( fullpath[0] != '/' )
|
|
|
- {
|
|
|
- /* build full path */
|
|
|
- fullpath = full_path;
|
|
|
-#ifdef DFS_USING_WORKDIR
|
|
|
- dfs_lock();
|
|
|
- build_fullpath(working_directory, path, fullpath);
|
|
|
- strcpy(working_directory, fullpath);
|
|
|
- dfs_unlock();
|
|
|
-#endif
|
|
|
- }
|
|
|
- else
|
|
|
+ fullpath = dfs_normalize_path(NULL, path);
|
|
|
+ if (fullpath == RT_NULL)
|
|
|
+ return -1; /* build path failed */
|
|
|
+
|
|
|
+ dfs_lock();
|
|
|
+ d = opendir(fullpath);
|
|
|
+ if (d == RT_NULL)
|
|
|
{
|
|
|
-#ifdef DFS_USING_WORKDIR
|
|
|
- dfs_lock();
|
|
|
- rt_strncpy(working_directory, path, strlen(path) + 1);
|
|
|
- working_directory[strlen(path)] = '\0';
|
|
|
+ rt_free(fullpath);
|
|
|
+ /* this is a not exist directory */
|
|
|
dfs_unlock();
|
|
|
-#endif
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
+ /* 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 */
|
|
|
+
|
|
|
+ dfs_unlock();
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
+#endif
|
|
|
|
|
|
-/*
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Function : chdir
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-| Description :
|
|
|
-|
|
|
|
-| Parameters :
|
|
|
-| Returns :
|
|
|
-|
|
|
|
-+------------------------------------------------------------------------------
|
|
|
-*/
|
|
|
+/**
|
|
|
+ * this function is a POSIX compliant version, which will return current
|
|
|
+ * working directory.
|
|
|
+ *
|
|
|
+ * @param buf the returned current directory.
|
|
|
+ * @size the buffer size.
|
|
|
+ *
|
|
|
+ * @return the returned current directory.
|
|
|
+ */
|
|
|
char* getcwd(char *buf, rt_size_t size)
|
|
|
{
|
|
|
#ifdef DFS_USING_WORKDIR
|