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;
}
Beispiel #2
0
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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
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;
}
Beispiel #5
0
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;
}
Beispiel #6
0
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;
}
Beispiel #7
0
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;
}
Beispiel #8
0
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;
}
Beispiel #9
0
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;
}
Beispiel #16
0
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;
}
Beispiel #18
0
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;
		}
	} 
}