int uffs_open(const char *name, int oflag, ...) { uffs_Object *obj; int ret = 0; uffs_GlobalFsLockLock(); obj = uffs_GetObject(); if (obj == NULL) { uffs_set_error(-UEMFILE); ret = -1; } else { if (uffs_OpenObject(obj, name, oflag) == U_FAIL) { uffs_set_error(-uffs_GetObjectErr(obj)); uffs_PutObject(obj); ret = -1; } else { ret = OBJ2FD(obj); } } uffs_GlobalFsLockUnlock(); return ret; }
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; }
uffs_DIR * uffs_opendir(const char *path) { int err = 0; uffs_DIR *ret = NULL; uffs_DIR *dirp = GetDirEntry(); if (dirp) { dirp->obj = uffs_GetObject(); if (dirp->obj) { if (uffs_OpenObject(dirp->obj, path, UO_RDONLY | UO_DIR) == U_SUCC) { if (uffs_FindObjectOpen(&dirp->f, dirp->obj) == U_SUCC) { ret = dirp; goto ext; } else { uffs_CloseObject(dirp->obj); } } else { err = uffs_GetObjectErr(dirp->obj); } uffs_PutObject(dirp->obj); dirp->obj = NULL; } else { err = UEMFILE; } PutDirEntry(dirp); } else { err = UEMFILE; } ext: uffs_set_error(-err); return ret; }
int uffs_stat(const char *name, struct uffs_stat *buf) { uffs_Object *obj; int ret = 0; int err = 0; URET result; obj = uffs_GetObject(); if (obj) { if (*name && name[strlen(name) - 1] == '/') { result = uffs_OpenObject(obj, name, UO_RDONLY | UO_DIR); } else { if ((result = uffs_OpenObject(obj, name, UO_RDONLY)) != U_SUCC) // try file result = uffs_OpenObject(obj, name, UO_RDONLY | UO_DIR); // then try dir } if (result == U_SUCC) { ret = do_stat(obj, buf); uffs_CloseObject(obj); } else { err = uffs_GetObjectErr(obj); ret = -1; } uffs_PutObject(obj); } else { err = UENOMEM; ret = -1; } uffs_set_error(-err); return ret; }
int uffs_mkdir(const char *name, ...) { uffs_Object *obj; int ret = 0; int err = 0; uffs_GlobalFsLockLock(); obj = uffs_GetObject(); if (obj) { if (uffs_CreateObject(obj, name, UO_CREATE|UO_DIR) != U_SUCC) { err = obj->err; ret = -1; } else { uffs_CloseObject(obj); ret = 0; } uffs_PutObject(obj); } else { err = UEMFILE; ret = -1; } uffs_set_error(-err); uffs_GlobalFsLockUnlock(); return ret; }
static int do_stat(uffs_Object *obj, struct uffs_stat *buf) { uffs_ObjectInfo info; int ret = 0; int err = 0; if (uffs_GetObjectInfo(obj, &info, &err) == U_FAIL) { ret = -1; } else { buf->st_dev = obj->dev->dev_num; buf->st_ino = info.serial; buf->st_nlink = 0; buf->st_uid = 0; buf->st_gid = 0; buf->st_rdev = 0; buf->st_size = info.len; buf->st_blksize = obj->dev->com.pg_data_size; buf->st_blocks = 0; buf->st_atime = info.info.last_modify; buf->st_mtime = info.info.last_modify; buf->st_ctime = info.info.create_time; buf->st_mode = (info.info.attr & FILE_ATTR_DIR ? US_IFDIR : US_IFREG); if (info.info.attr & FILE_ATTR_WRITE) buf->st_mode |= US_IRWXU; } uffs_set_error(-err); return ret; }
/* * 函数功能: 创建一个文件 * 输入参数: 文件名称 * 返回参数: */ int uffs_mkfile(const char *name) { uffs_Object *fp; int ret = 0; int err = 0; fp = uffs_GetObject(); if(fp != NULL) { if(uffs_CreateObject(fp, name, UO_CREATE) != U_SUCC) { err = fp->err; ret = -1; uffs_Perror(UFFS_ERR_NORMAL, "Create %s fail, err: %d", name, uffs_get_error()); } else { uffs_Perror(UFFS_ERR_NORMAL, "Create %s succ.", name); uffs_CloseObject(fp); ret = 0; } uffs_PutObject(fp); } else { err = UEMFILE; ret = -1; } uffs_set_error(-err); return ret; }
//Delete a File or Directory int dfs_uffs_unlink(struct dfs_filesystem* fs, const char* path) { int ret; int err = 0; ret = uffs_DeleteObject(path, &err); uffs_set_error(-err); return ret; }
int uffs_rename(const char *old_name, const char *new_name) { int err = 0; int ret = 0; ret = (uffs_RenameObject(old_name, new_name, &err) == U_SUCC) ? 0 : -1; uffs_set_error(-err); return ret; }
int dfs_uffs_rename(struct dfs_filesystem* fs, const char* oldpath, const char* newpath) { int ret; int err = 0; ret = uffs_RenameObject(oldpath, newpath, &err); uffs_set_error(-err); return ret; }
int uffs_flush(int fd) { int ret; uffs_Object *obj = FD2OBJ(fd); CHK_OBJ(obj, -1); uffs_ClearObjectErr(obj); ret = (uffs_FlushObject(obj) == U_SUCC) ? 0 : -1; uffs_set_error(-uffs_GetObjectErr(obj)); return ret; }
int uffs_eof(int fd) { int ret; uffs_Object *obj = FD2OBJ(fd); CHK_OBJ(obj, -1); uffs_ClearObjectErr(obj); ret = uffs_EndOfFile(obj); uffs_set_error(-uffs_GetObjectErr(obj)); return ret; }
long uffs_tell(int fd) { long ret; uffs_Object *obj = FD2OBJ(fd); CHK_OBJ(obj, -1); uffs_ClearObjectErr(obj); ret = (long) uffs_GetCurOffset(obj); uffs_set_error(-uffs_GetObjectErr(obj)); return ret; }
long uffs_seek(int fd, long offset, int origin) { int ret; uffs_Object *obj = FD2OBJ(fd); CHK_OBJ(obj, -1); uffs_ClearObjectErr(obj); ret = uffs_SeekObject(obj, offset, origin); uffs_set_error(-uffs_GetObjectErr(obj)); return ret; }
int uffs_write(int fd, void *data, int len) { int ret; uffs_Object *obj = FD2OBJ(fd); CHK_OBJ(obj, -1); uffs_ClearObjectErr(obj); ret = uffs_WriteObject(obj, data, len); uffs_set_error(-uffs_GetObjectErr(obj)); return ret; }
int uffs_truncate(int fd, long remain) { int ret; uffs_Object *obj = FD2OBJ(fd); CHK_OBJ(obj, -1); uffs_ClearObjectErr(obj); ret = (uffs_TruncateObject(obj, remain) == U_SUCC) ? 0 : -1; uffs_set_error(-uffs_GetObjectErr(obj)); return ret; }
int uffs_ftruncate(int fd, long remain) { int ret; uffs_Object *obj; CHK_OBJ_LOCK(fd, obj, -1); uffs_ClearObjectErr(obj); ret = (uffs_TruncateObject(obj, remain) == U_SUCC) ? 0 : -1; uffs_set_error(-uffs_GetObjectErr(obj)); uffs_GlobalFsLockUnlock(); return ret; }
int uffs_flush(int fd) { int ret; uffs_Object *obj; CHK_OBJ_LOCK(fd, obj, -1); uffs_ClearObjectErr(obj); ret = (uffs_FlushObject(obj) == U_SUCC) ? 0 : -1; uffs_set_error(-uffs_GetObjectErr(obj)); uffs_GlobalFsLockUnlock(); return ret; }
int uffs_eof(int fd) { int ret; uffs_Object *obj; CHK_OBJ_LOCK(fd, obj, -1); uffs_ClearObjectErr(obj); ret = uffs_EndOfFile(obj); uffs_set_error(-uffs_GetObjectErr(obj)); uffs_GlobalFsLockUnlock(); return ret; }
long uffs_tell(int fd) { long ret; uffs_Object *obj; CHK_OBJ_LOCK(fd, obj, -1); uffs_ClearObjectErr(obj); ret = (long) uffs_GetCurOffset(obj); uffs_set_error(-uffs_GetObjectErr(obj)); uffs_GlobalFsLockUnlock(); return ret; }
long uffs_seek(int fd, long offset, int origin) { int ret; uffs_Object *obj; CHK_OBJ_LOCK(fd, obj, -1); uffs_ClearObjectErr(obj); ret = uffs_SeekObject(obj, offset, origin); uffs_set_error(-uffs_GetObjectErr(obj)); uffs_GlobalFsLockUnlock(); return ret; }
int uffs_write(int fd, const void *data, int len) { int ret; uffs_Object *obj; CHK_OBJ_LOCK(fd, obj, -1); uffs_ClearObjectErr(obj); ret = uffs_WriteObject(obj, data, len); uffs_set_error(-uffs_GetObjectErr(obj)); uffs_GlobalFsLockUnlock(); return ret; }
int uffs_close(int fd) { int ret = 0; uffs_Object *obj = FD2OBJ(fd); CHK_OBJ(obj, -1); uffs_ClearObjectErr(obj); if (uffs_CloseObject(obj) == U_FAIL) { uffs_set_error(-uffs_GetObjectErr(obj)); ret = -1; } else { uffs_PutObject(obj); ret = 0; } return ret; }
int uffs_close(int fd) { int ret = 0; uffs_Object *obj; CHK_OBJ_LOCK(fd, obj, -1); uffs_ClearObjectErr(obj); if (uffs_CloseObject(obj) == U_FAIL) { uffs_set_error(-uffs_GetObjectErr(obj)); ret = -1; } else { uffs_PutObject(obj); ret = 0; } uffs_GlobalFsLockUnlock(); return ret; }
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; }
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; } } }