int uffs_rmdir(const char *name) { int err = 0; int ret = 0; struct uffs_stat st; if (uffs_stat(name, &st) < 0) { err = UENOENT; ret = -1; } else if ((st.st_mode & US_IFDIR) == 0) { err = UENOTDIR; ret = -1; } else { uffs_GlobalFsLockLock(); if (uffs_DeleteObject(name, &err) == U_SUCC) { ret = 0; } else { ret = -1; } uffs_GlobalFsLockUnlock(); } uffs_set_error(-err); return ret; }
/** rm <obj> */ static int cmd_rm(int argc, char *argv[]) { const char *name = NULL; int ret = 0; struct uffs_stat st; CHK_ARGC(2, 2); name = argv[1]; ret = uffs_stat(name, &st); if (ret < 0) { MSGLN("Can't stat '%s'", name); return ret; } if (st.st_mode & US_IFDIR) { ret = uffs_rmdir(name); } else { ret = uffs_remove(name); } if (ret == 0) MSGLN("Delete '%s' succ.", name); else MSGLN("Delete '%s' fail!", name); return ret; }
/* * 函数功能: 列出文件清单 * 输入参数: name:分区名称 * 返回参数: 成功:TRUE,失败:rt_false */ int uffs_ls(const char *name) { uffs_DIR *dirp; struct uffs_dirent *ent; struct uffs_stat stat_buf; int count = 0; char buf[MAX_FILENAME_LENGTH+2]; char *sub; if(name == NULL) { return FALSE; } dirp = uffs_opendir(name); //会获得一个uffs_DIR实例 if(dirp == NULL) { rt_kprintf("Can't open '%s' for list\n", name); } else { rt_kprintf("%-16s%-8s%-8s%-8s\n","name","type","size","serial"); rt_kprintf("-----------------------------------------\n"); ent = uffs_readdir(dirp); while(ent) { rt_kprintf("%-16s", ent->d_name); strcpy(buf, name); sub = buf; if(name[strlen(name)-1] != '/') sub = strcat(buf, "/"); sub = strcat(sub, ent->d_name); if(ent->d_type & FILE_ATTR_DIR) { sub = strcat(sub, "/"); rt_kprintf("%-8s", "<DIR>"); rt_kprintf("%-8d", CountFileUnder(sub)); } else { uffs_stat(sub, &stat_buf); rt_kprintf("%-8s", ""); rt_kprintf("%-8d", stat_buf.st_size); } rt_kprintf("%-8d\n", ent->d_ino); count++; ent = uffs_readdir(dirp); } if(dirp != NULL) uffs_closedir(dirp); rt_kprintf("Total: %d objects.\n", count); } return TRUE; }
/** ls [<dir>] */ static int cmd_ls(int argc, char *argv[]) { uffs_DIR *dirp; struct uffs_dirent *ent; struct uffs_stat stat_buf; int count = 0; char buf[MAX_PATH_LENGTH+2]; const char *name = "/"; char *sub; int ret = 0; CHK_ARGC(1, 2); if (argc > 1) name = argv[1]; dirp = uffs_opendir(name); if (dirp == NULL) { MSGLN("Can't open '%s' for list", name); ret = -1; } else { MSG("------name-----------size---------serial-----" TENDSTR); ent = uffs_readdir(dirp); while (ent) { MSG("%9s", ent->d_name); strcpy(buf, name); sub = buf; if (name[strlen(name)-1] != '/') sub = strcat(buf, "/"); sub = strcat(sub, ent->d_name); if (ent->d_type & FILE_ATTR_DIR) { sub = strcat(sub, "/"); MSG("/ \t<%8d>", CountObjectUnder(sub)); } else { uffs_stat(sub, &stat_buf); MSG(" \t %8d ", stat_buf.st_size); } MSG("\t%6d" TENDSTR, ent->d_ino); count++; ent = uffs_readdir(dirp); } uffs_closedir(dirp); MSG("Total: %d objects." TENDSTR, count); } return ret; }
static int dfs_uffs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) { int result; struct uffs_stat s; result = uffs_stat(path, &s); if (result < 0) return uffs_result_to_dfs(uffs_get_error()); /* convert uffs stat to dfs stat structure */ /* FIXME, these field may not be the same */ st->st_dev = 0; st->st_mode = s.st_mode; st->st_size = s.st_size; st->st_mtime = s.st_mtime; return 0; }
int uffs_remove(const char *name) { int err = 0; int ret = 0; struct uffs_stat st; if (uffs_stat(name, &st) < 0) { err = UENOENT; ret = -1; } else if (st.st_mode & US_IFDIR) { err = UEISDIR; ret = -1; } else if (uffs_DeleteObject(name, &err) == U_SUCC) { ret = 0; } else { ret = -1; } uffs_set_error(-err); return ret; }
static int dfs_uffs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st) { int result; struct uffs_stat s; struct rt_mtd_nand_device * mtd; result = uffs_stat(path, &s); if (result < 0) return uffs_result_to_dfs(uffs_get_error()); /* convert uffs stat to dfs stat structure */ /* FIXME, these field may not be the same */ st->st_dev = 0; st->st_mode = s.st_mode; st->st_size = s.st_size; st->st_mtime = s.st_mtime; mtd = RT_MTD_NAND_DEVICE(fs->dev_id); st->st_blksize = mtd->page_size; return 0; }
int dfs_uffs_stat(struct dfs_filesystem* fs, const char* path, struct stat* st) { int ret=U_SUCC; struct uffs_stat stat_buf; ret = uffs_stat(path, &stat_buf); if (ret == U_SUCC) { rt_uint32_t mode=0; st->st_dev = 0; if(stat_buf.st_mode & US_IFREG) mode |= DFS_S_IFREG; if(stat_buf.st_mode & US_IFDIR) mode |= DFS_S_IFDIR; if(stat_buf.st_mode & US_IRWXU) mode |= DFS_S_IRWXU; if(stat_buf.st_mode & US_IRUSR) mode |= DFS_S_IRUSR; if(stat_buf.st_mode & US_IWUSR) mode |= DFS_S_IWUSR; if(stat_buf.st_mode & US_IXUSR) mode |= DFS_S_IXUSR; if(stat_buf.st_mode & US_IRWXG) mode |= DFS_S_IRWXG; if(stat_buf.st_mode & US_IRGRP) mode |= DFS_S_IRGRP; if(stat_buf.st_mode & US_IWGRP) mode |= DFS_S_IWGRP; if(stat_buf.st_mode & US_IXGRP) mode |= DFS_S_IXGRP; if(stat_buf.st_mode & US_IRWXO) mode |= DFS_S_IRWXO; if(stat_buf.st_mode & US_IROTH) mode |= DFS_S_IROTH; if(stat_buf.st_mode & US_IWOTH) mode |= DFS_S_IWOTH; if(stat_buf.st_mode & US_IXOTH) mode |= DFS_S_IXOTH; st->st_mode = mode; st->st_size = stat_buf.st_size; st->st_mtime= stat_buf.st_mtime; st->st_blksize= stat_buf.st_blksize; return U_SUCC; } return U_FAIL; }
int dfs_uffs_write(struct dfs_fd* fd, const void* buf, rt_size_t count) { uffs_Object* fp; u32 byte_write; struct uffs_stat stat_buf; rt_kprintf("count=%d\n",count); if(fd->type == FT_DIRECTORY) { return -DFS_STATUS_EISDIR; } fp = (uffs_Object*)(fd->data); RT_ASSERT(fp != RT_NULL); byte_write = uffs_WriteObject(fp, buf, count); /* update position and file size */ fd->pos = fp->pos; uffs_stat(fp->name, &stat_buf); fd->size = stat_buf.st_size; return byte_write; }
int uffs_lstat(const char *name, struct uffs_stat *buf) { return uffs_stat(name, buf); }
/* return the size of struct dirent*/ static int dfs_uffs_getdents( struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count) { rt_uint32_t index; char * file_path; struct dirent* d; uffs_DIR* dir; struct uffs_dirent * uffs_d; dir = (uffs_DIR*)(file->data); RT_ASSERT(dir != RT_NULL); /* round count, count is always 1 */ count = (count / sizeof(struct dirent)) * sizeof(struct dirent); if (count == 0) return -DFS_STATUS_EINVAL; /* allocate file name */ file_path = rt_malloc(FILE_PATH_MAX); if (file_path == RT_NULL) return -DFS_STATUS_ENOMEM; index = 0; /* usually, the while loop should only be looped only once! */ while (1) { struct uffs_stat s; d = dirp + index; uffs_d = uffs_readdir(dir); if (uffs_d == RT_NULL) { rt_free(file_path); return (uffs_result_to_dfs(uffs_get_error())); } if (file->path[0] == '/' && !(file->path[1] == 0)) rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->path, uffs_d->d_name); else rt_strncpy(file_path, uffs_d->d_name, FILE_PATH_MAX); uffs_stat(file_path, &s); switch(s.st_mode & US_IFMT) /* file type mark */ { case US_IFREG: /* directory */ d->d_type = DFS_DT_REG; break; case US_IFDIR: /* regular file */ d->d_type = DFS_DT_DIR; break; case US_IFLNK: /* symbolic link */ case US_IREAD: /* read permission */ case US_IWRITE:/* write permission */ default: d->d_type = DFS_DT_UNKNOWN; break; } /* write the rest args of struct dirent* dirp */ d->d_namlen = rt_strlen(uffs_d->d_name); d->d_reclen = (rt_uint16_t)sizeof(struct dirent); rt_strncpy(d->d_name, uffs_d->d_name, rt_strlen(uffs_d->d_name) + 1); index ++; if (index * sizeof(struct dirent) >= count) break; } /* free file name buf */ rt_free(file_path); if (index == 0) return uffs_result_to_dfs(uffs_get_error()); file->pos += index * sizeof(struct dirent); return index * sizeof(struct dirent); }
static UBOOL check_entry_exist(const char *name) { struct uffs_stat sb; return uffs_stat(name, &sb) < 0 ? U_FALSE : U_TRUE; }
int dfs_uffs_open(struct dfs_fd* fd) { if (fd->flags & DFS_O_DIRECTORY) { /* directory */ uffs_DIR* dirp; int oflag = UO_DIR; if (fd->flags & DFS_O_CREAT) oflag |= UO_CREATE; if (fd->flags & DFS_O_RDONLY) oflag |= UO_RDONLY; if (fd->flags & DFS_O_WRONLY) oflag |= UO_WRONLY; if (oflag & UO_CREATE) { /* create directory right now */ uffs_Object* fp = uffs_GetObject(); if(fp == NULL) { uffs_set_error(-UEMFILE); return U_FAIL; } if(uffs_OpenObject(fp, fd->path, oflag) != U_SUCC) { return U_FAIL; } /* release object hander */ uffs_PutObject(fp); } /* use directory handler */ dirp = uffs_opendir(fd->path); if(dirp == NULL) { uffs_set_error(-UEMFILE); return U_FAIL; } fd->data = dirp; return U_SUCC; } else {/* file */ uffs_Object *fp; int mode = UO_RDONLY; if (fd->flags & DFS_O_WRONLY) mode |= UO_WRONLY; if ((fd->flags & DFS_O_ACCMODE) & DFS_O_RDWR) mode |= UO_WRONLY; /* Opens the file, if it is existing. If not, a new file is created. */ if (fd->flags & DFS_O_CREAT) mode |= UO_CREATE; /* Creates a new file. If the file is existing, it is truncated and overwritten. */ if (fd->flags & DFS_O_TRUNC) mode |= UO_TRUNC; /* Creates a new file. The function fails if the file is already existing. */ if (fd->flags & DFS_O_EXCL) mode |= UO_EXCL; /* get an object hander */ fp = uffs_GetObject(); if(fp == NULL) { uffs_set_error(-UEMFILE); return U_FAIL; } if(uffs_OpenObject(fp, fd->path, mode) == U_SUCC) { struct uffs_stat stat_buf; uffs_stat(fd->path, &stat_buf); fd->pos = fp->pos; fd->size = stat_buf.st_size; fd->data = fp; if(fd->flags & DFS_O_APPEND) { fd->pos = uffs_SeekObject(fp, 0, USEEK_END); } return U_SUCC; } else { /* open failed, return */ uffs_set_error(-uffs_GetObjectErr(fp)); /* release object hander */ uffs_PutObject(fp); return U_FAIL; } } }