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;
}
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;
}
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;
}
Esempio n. 4
0
File: uffs_fs.c Progetto: mazj/uffs
/**
 * Get free object handlers
 */
int uffs_GetFreeObjectHandlers(void)
{
	int count = 0;

	uffs_GlobalFsLockLock();
	count = uffs_PoolGetFreeCount(&_object_pool);
	uffs_GlobalFsLockUnlock();

	return count;
}
void uffs_flush_all(const char *mount_point)
{
	uffs_Device *dev = NULL;

	dev = uffs_GetDeviceFromMountPoint(mount_point);
	if (dev) {
		uffs_GlobalFsLockLock();
		uffs_BufFlushAll(dev);
		uffs_PutDevice(dev);
		uffs_GlobalFsLockUnlock();
	}
}
int uffs_rename(const char *old_name, const char *new_name)
{
	int err = 0;
	int ret = 0;

	uffs_GlobalFsLockLock();
	ret = (uffs_RenameObject(old_name, new_name, &err) == U_SUCC) ? 0 : -1;
	uffs_set_error(-err);
	uffs_GlobalFsLockUnlock();

	return ret;
}
long uffs_space_free(const char *mount_point)
{
	uffs_Device *dev = NULL;
	long ret = -1L;

	dev = uffs_GetDeviceFromMountPoint(mount_point);
	if (dev) {
		uffs_GlobalFsLockLock();
		ret = (long) uffs_GetDeviceFree(dev);
		uffs_GlobalFsLockUnlock();
	}

	return ret;
}
int uffs_format(const char *mount_point)
{
	uffs_Device *dev = NULL;
	URET ret = U_FAIL;

	dev = uffs_GetDeviceFromMountPoint(mount_point);
	if (dev) {
		uffs_GlobalFsLockLock();
		ret = uffs_FormatDevice(dev, U_TRUE);
		uffs_GlobalFsLockUnlock();
	}

	return ret == U_SUCC ? 0 : -1;
}
uffs_DIR * uffs_opendir(const char *path)
{
	int err = 0;
	uffs_DIR *ret = NULL;
	uffs_DIR *dirp;

	uffs_GlobalFsLockLock();

	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);
	uffs_GlobalFsLockUnlock();

	return ret;
}
int uffs_stat(const char *name, struct uffs_stat *buf)
{
	uffs_Object *obj;
	int ret = 0;
	int err = 0;
	URET result;

	uffs_GlobalFsLockLock();

	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);
	uffs_GlobalFsLockUnlock();

	return ret;
}
URET uffs_FormatDevice(uffs_Device *dev, UBOOL force)
{
	u16 i, slot;
	URET ret = U_SUCC;
	
	if (dev == NULL)
		return U_FAIL;

	if (dev->ops == NULL) 
		return U_FAIL;

	uffs_GlobalFsLockLock();

	ret = uffs_BufFlushAll(dev);

	if (dev->ref_count > 1 && !force) {
		uffs_Perror(UFFS_MSG_NORMAL,
					"can't format when dev->ref_count = %d",
					dev->ref_count);
		ret = U_FAIL;
	}

	if (ret == U_SUCC && force) {
		uffs_DirEntryBufPutAll(dev);
		uffs_PutAllObjectBuf(dev);
		uffs_FdSignatureIncrease();
	}

	if (ret == U_SUCC &&
		uffs_BufIsAllFree(dev) == U_FALSE &&
		!force)
	{
		uffs_Perror(UFFS_MSG_NORMAL, "some page still in used!");
		ret = U_FAIL;
	}

	if (!force) {
		for (slot = 0; ret == U_SUCC && slot < dev->cfg.dirty_groups; slot++) {
			if (dev->buf.dirtyGroup[slot].count > 0) {
				uffs_Perror(UFFS_MSG_SERIOUS, "there still have dirty pages!");
				ret = U_FAIL;
			}
		}
	}

	if (ret == U_SUCC)
		uffs_BufSetAllEmpty(dev);


	if (ret == U_SUCC && uffs_BlockInfoIsAllFree(dev) == U_FALSE && !force) {
		uffs_Perror(UFFS_MSG_NORMAL,
					"there still have block info cache ? fail to format");
		ret = U_FAIL;
	}

	if (ret == U_SUCC)
		uffs_BlockInfoExpireAll(dev);

	for (i = dev->par.start; ret == U_SUCC && i <= dev->par.end; i++) {
		if (uffs_FlashIsBadBlock(dev, i) == U_FALSE) {
			uffs_FlashEraseBlock(dev, i);
			if (HAVE_BADBLOCK(dev))
				uffs_BadBlockProcess(dev, NULL);
		}
		else {
#ifdef CONFIG_ENABLE_BAD_BLOCK_VERIFY
			_ForceFormatAndCheckBlock(dev, i);
#endif
		}
	}

	if (ret == U_SUCC && uffs_TreeRelease(dev) == U_FAIL) {
		ret = U_FAIL;
	}

	if (ret == U_SUCC && uffs_TreeInit(dev) == U_FAIL) {
		ret = U_FAIL;
	}

	if (ret == U_SUCC && uffs_BuildTree(dev) == U_FAIL) {
		ret = U_FAIL;
	}

	uffs_GlobalFsLockUnlock();

	return ret;
}