/**
 * this function will get file information.
 *
 * @param path the file path.
 * @param buf the data buffer to save stat description.
 *
 * @return 0 on successful, -1 on failed.
 */
int dfs_file_stat(const char *path, struct stat *buf)
{
	int result;
	char* fullpath;
	struct dfs_filesystem* fs;

	fullpath = dfs_normalize_path(RT_NULL, path);
	if ( fullpath == RT_NULL )
	{
		return -1;
	}

	if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
	{
		dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath));
		rt_free(fullpath);
		return -DFS_STATUS_ENOENT;
	}

	if (fullpath[0] == '/' && fullpath[1] == '\0')
	{
		/* it's the root directory */
		buf->st_dev   = 0;

		buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
			DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
		buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;

		buf->st_size  = 0;
		buf->st_mtime = 0;
		buf->st_blksize = 512;

		/* release full path */
		rt_free(fullpath);

		return DFS_STATUS_OK;
	}
	
	/* get the real file path */
	
	if (fs->ops->stat == RT_NULL)
	{
		rt_free(fullpath);
		dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function"));
		return -DFS_STATUS_ENOSYS;
	}

	if (dfs_subdir(fs->path, fullpath) == RT_NULL)
		result = fs->ops->stat(fs, "/", buf);
	else
		result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);

	rt_free(fullpath);

	return result;
}
Esempio n. 2
0
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_rename(struct dfs_filesystem* fs, const char* oldpath, const char* newpath)
{
	FileRecord old_entry, new_entry;
	FileLocation old_loc, new_loc;
	efsl_fs* efsfs;
	eint8 fatfilename[11];
	
	efsfs = (efsl_fs*) fs->data ;
	RT_ASSERT(efsfs != RT_NULL);

	dir_getFatFileName((eint8 *)newpath, &fatfilename[0]);	
	
	/* parameters check */
	if (strlen(oldpath) > DFS_PATH_MAX || 
		strlen(newpath) > DFS_PATH_MAX ||
		/* old path is a directory that contains newpath */
		strncmp(oldpath, newpath, strlen(newpath)) == 0) 
		{
			dfs_log(DFS_DEBUG_ERROR, ("old path is a directory that contains newpath"));					
			return -DFS_STATUS_EINVAL;
		}	

	/* if can't find old direntry */
	if(fs_findFile(&efsfs->filesystem, (eint8 *)oldpath, &old_loc, 0) == 0)
	{
		dfs_log(DFS_DEBUG_ERROR, ("can't find old direntry"));		
		return -DFS_STATUS_ENOENT;
	}
	dir_getFileStructure(&efsfs->filesystem, &old_entry, &old_loc);

	/* if the new direntry exist */
	if(fs_findFile(&efsfs->filesystem, (eint8 *)newpath, &new_loc, 0) > 0)
	{
		dfs_log(DFS_DEBUG_ERROR, ("new direntry existed"));
		return -DFS_STATUS_ENOENT;
	}
	if(fs_findFreeFile(&efsfs->filesystem, (eint8 *)newpath, &new_loc, 0))
	{
		memCpy(&old_entry, &new_entry, sizeof(FileRecord));	
		memCpy(fatfilename, new_entry.FileName, 11);
		dir_createDirectoryEntry(&efsfs->filesystem, &new_entry, &new_loc);
	}

	/* delete the old direntry */
	old_entry.FileName[0] = 0xe5;
	dir_updateDirectoryEntry(&efsfs->filesystem, &old_entry, &old_loc);	

	return 0;
}
Esempio n. 3
0
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_stat(struct dfs_filesystem* fs, const char *path, struct dfs_stat *st)
{
	FileRecord entry;
	FileLocation loc;
	efsl_fs* efsfs = (efsl_fs*)(fs->data);

	/* parameter check */
	RT_ASSERT(efsfs != RT_NULL);
	
	/* file does not exist */
	if(fs_findFile(&efsfs->filesystem, (char *)path, &loc, 0) == 0)
	{
		dfs_log(DFS_DEBUG_ERROR, ("can't find direntry"));		
		return -DFS_STATUS_ENOENT;
	}

	dir_getFileStructure(&efsfs->filesystem, &entry, &loc);

	st->st_dev = fs->dev_id;
	st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | 
	DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
	if ( entry.Attribute & ATTR_DIRECTORY )
	{
		st->st_mode &= ~DFS_S_IFREG;
		st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
	}
	if ( entry.Attribute & ATTR_READ_ONLY )
		st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH);

	st->st_size  = entry.FileSize;
	st->st_mtime = entry.WriteTime;
	
	return 0;
}
Esempio n. 4
0
/*
+------------------------------------------------------------------------------
| Function    : dfile_raw_stat
+------------------------------------------------------------------------------
| Description : get the file or directory stat description
|
| Parameters  : path, the file or directory path
|               buf,  the stat description will be saved in it
| Returns     : 
|
+------------------------------------------------------------------------------
*/
int dfile_raw_stat(const char *path, struct dfs_stat *buf)
{
	struct dfs_filesystem* fs;
	char* fullpath, real_path[DFS_PATH_MAX + 1];
#ifdef DFS_USING_WORKDIR
	char full_path[DFS_PATH_MAX + 1];
#endif
	int fspathlen;

	fullpath = (char*)path;
	if ( fullpath[0] != '/' )
	{
#ifdef DFS_USING_WORKDIR
		/* build full path */
		fullpath = full_path;
		dfs_lock();
		build_fullpath(working_directory, path, fullpath);
		dfs_unlock();
#else
#ifdef RT_USING_FINSH
		rt_kprintf("not support working directory, bad filename\n");
#endif
		return -1;
#endif
	}

	if ( (fs = dfs_filesystem_lookup(fullpath)) == RT_NULL )
	{
		dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath));
		return -DFS_STATUS_ENOENT;
	}

	fspathlen = strlen(fs->path);
	rt_memset(real_path, 0, sizeof(real_path));
	if (*(fullpath + fspathlen) != '/') strcpy(real_path, "/");
	strcat(real_path, fullpath + fspathlen);

	if (fs->ops->stat == RT_NULL)
	{
		dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function"));
		return -DFS_STATUS_ENOSYS;
	}

	return fs->ops->stat(fs, real_path, buf);
}
Esempio n. 5
0
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_lseek(struct dfs_fd* file, rt_off_t offset)
{
	int result = 0;
	DirList* efsdir;
	efsl_fs* efsfs = (efsl_fs *)file->fs->data;

	/* parameter check */
	RT_ASSERT(efsfs   != RT_NULL);

	/* seek directory */
	if(file->flags & DFS_O_DIRECTORY)
	{
		efsdir = (DirList*)file->data;
		
		/* only support offset equels zero */
		if(offset == 0)
		{
			result = ls_openDir(efsdir, &efsfs->filesystem, file->path);
			if(result < 0)
			{
				dfs_log(DFS_DEBUG_INFO, ("open directory %s failed", file->path));
			}		
		}
		/* should implement in future version */
		else
		{
			dfs_log(DFS_DEBUG_INFO, ("not implement"));
		}
	}
	/* seek file */
	else
	{
		File* efsfile = file->data;
		
		/* parameter check */
		if ( efsfile == RT_NULL) return -DFS_STATUS_EBADF;

		result = file_setpos(efsfile, offset);
	}
	
	return result;
}
Esempio n. 6
0
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_close(struct dfs_fd* file)
{
	int result = 0;
	
	if (!file || !(file->flags & DFS_F_OPEN)) return -DFS_STATUS_EBADF;
	
	if(!(file->flags & DFS_F_DIRECTORY)) 
		result = file_fclose((File *)file->data);
	else
	{
		dfs_log(DFS_DEBUG_INFO, ("close a directory, %s", file->path));
	}

	dfs_log(DFS_DEBUG_INFO, ("close a file, %s", file->path));

	/* free directory or file */
	rt_free(file->data);
	file->data = RT_NULL;

	return result;
}
Esempio n. 7
0
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_read(struct dfs_fd* file, void* buf, rt_size_t len)
{
	File* efsfile = file->data;
	int result = 0;

	/* parameter check */
	RT_ASSERT(efsfile != RT_NULL);
	
	result = file_read(efsfile, len, buf);
	file->pos = efsfile->FilePtr;

	dfs_log(DFS_DEBUG_INFO, ("read file %s %d bytes", file->path, result));
	
	return result;
}
Esempio n. 8
0
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_write(struct dfs_fd* file, const void* buf, rt_size_t len)
{
	File* efsfile = file->data;
	int result = 0;

	/* parameter check */
	RT_ASSERT(efsfile != RT_NULL);
	
	result = file_write(efsfile, len, (euint8 *)buf);
	file->pos = efsfile->FilePtr;
	file->size = efsfile->FileSize;

	dfs_log(DFS_DEBUG_INFO, ("write file %s %d bytes", file->path, result));
	
	return result;
}
Esempio n. 9
0
/*
+------------------------------------------------------------------------------
| Function    : dfile_raw_open
+------------------------------------------------------------------------------
| Description : 
|
| Parameters  : 
| Returns     : 
|
+------------------------------------------------------------------------------
*/
int dfile_raw_open(struct dfs_fd* fd, const char *path, int flags)
{
	struct dfs_filesystem* fs;
	char *fullpath;
#ifdef DFS_USING_WORKDIR
	char full_path[DFS_PATH_MAX + 1];
#endif
	int fspathlen, result;

	/* parameter check */
	if ( fd == RT_NULL ) return -DFS_STATUS_EINVAL;

	/* make sure we have an absolute path */
	fullpath = (char*)path;
	if ( fullpath[0] != '/')
	{
#ifdef DFS_USING_WORKDIR
		/* build full path */
		fullpath = &full_path[0];
		dfs_lock();
		build_fullpath(working_directory, path, fullpath);
		dfs_unlock();
#else
#ifdef RT_USING_FINSH
		rt_kprintf("bad filename");
#endif
		return -1;
#endif
	}

	dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath));

	/* find filesystem */
	fs = dfs_filesystem_lookup(fullpath);
	if ( fs == RT_NULL ) return -DFS_STATUS_ENOENT;

	dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name));
	fd->fs = fs;

	/* initilize the fd item */
	fd->type = FT_REGULAR;
	//fd->ref_count = 1;
	fd->flags = flags;
	fd->size = 0;
	fd->pos = 0;

	fspathlen = strlen(fs->path);
	rt_memset(fd->path, 0, DFS_PATH_MAX + 1);
	if (*(fullpath + fspathlen) != '/') strcpy(fd->path, "/");
	strcat(fd->path, fullpath + fspathlen);

	/* specific file system open routine */
	if (fs->ops->open == RT_NULL)
	{
		/* clear fd */
		rt_memset(fd->path, 0, DFS_PATH_MAX + 1);

		fd->type = FT_REGULAR;
		fd->ref_count = 0;
		fd->fs = RT_NULL;
		fd->flags = 0;
		fd->size = 0;
		fd->pos = 0;
		fd->data = RT_NULL;

		return -DFS_STATUS_ENOSYS;
	}

	if ((result = fs->ops->open(fd)) < 0)
	{
		/* clear fd */
		fd->fs = RT_NULL;
		fd->flags = 0;
		fd->data = RT_NULL;

		dfs_log(DFS_DEBUG_INFO, ("open failed"));

		return result;
	}

	fd->flags |= DFS_F_OPEN;
	if ( flags & DFS_O_DIRECTORY )
	{
		fd->type = FT_DIRECTORY;
		fd->flags |= DFS_F_DIRECTORY;
	}

	dfs_log(DFS_DEBUG_INFO, ("open successful"));
	return 0;
}
Esempio n. 10
0
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_open(struct dfs_fd* file)
{
	File* efsfile;
	DirList* efsdir;
	efsl_fs* efsfs = (efsl_fs*)(file->fs->data);
	int result = 0;
	
	/* parameter check */
	if ( file == RT_NULL || file->fs == RT_NULL || 
		file->fs->data == RT_NULL ) 
	{	
		dfs_log(DFS_DEBUG_INFO, ("Invalid argument"));		
		return -DFS_STATUS_EINVAL;
	}

	/* open directory */
	if(file->flags & DFS_O_DIRECTORY)
	{
		/* write directory is not supported */
		if(file->flags & DFS_O_WRONLY || file->flags & DFS_O_RDWR)
		{
			dfs_log(DFS_DEBUG_INFO, ("write directory isn't supported"));			
			return -DFS_STATUS_EISDIR;
		}
		
		/* create directory */
		if(file->flags & DFS_O_CREAT)
		{
			dfs_log(DFS_DEBUG_INFO, ("create directory"));
			result = mk_dir(&efsfs->filesystem, file->path);
			if(result < 0) 
			{
				dfs_log(DFS_DEBUG_INFO, ("directory %s has existed", file->path));
				return -DFS_STATUS_EEXIST;
			}	
		}
		
		efsdir = (DirList*)rt_malloc(sizeof(DirList));
		if(efsdir == RT_NULL) 
		{
			dfs_log(DFS_DEBUG_INFO, ("memory alloc failed"));
			return -DFS_STATUS_ENOMEM;		
		}
		
		result = ls_openDir(efsdir, &efsfs->filesystem, file->path);
		if(result < 0)
		{
			dfs_log(DFS_DEBUG_INFO, ("open directory %s failed", file->path));
			rt_free(efsdir);
		}	
		else 
		{
			file->data = efsdir;
		}	
	}
	/* open file */
	else
	{
		efsfile = (File *)rt_malloc(sizeof(File));
		if (efsfile == RT_NULL) 
		{
			dfs_log(DFS_DEBUG_INFO, ("memory alloc failed"));			
			return -DFS_STATUS_ENOMEM;		
		}

		result = file_fopen(efsfile, &efsfs->filesystem, file->path, file->flags);
		if(result < 0)	
		{	
			dfs_log(DFS_DEBUG_INFO, ("open file %s failed", file->path));
			rt_free(efsfile);
		}	
		else
		{
			file->pos  = efsfile->FilePtr;
			file->size = efsfile->FileSize;
			file->data = efsfile;
		}
	}

	return result;
}
Esempio n. 11
0
/**
 * this function will open a file which specified by path with specified flags.
 *
 * @param fd the file descriptor pointer to return the corresponding result.
 * @param path the spaciefied file path.
 * @param flags the flags for open operator.
 *
 * @return 0 on successful, -1 on failed.
 */
int dfs_file_open(struct dfs_fd* fd, const char *path, int flags)
{
	struct dfs_filesystem* fs;
	char *fullpath;
	int result;

	/* parameter check */
	if ( fd == RT_NULL ) return -DFS_STATUS_EINVAL;

	/* make sure we have an absolute path */
	fullpath = dfs_normalize_path(RT_NULL, path);
	if (fullpath == RT_NULL)
	{
		return -1;
	}

	dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath));

	/* find filesystem */
	fs = dfs_filesystem_lookup(fullpath);
	if ( fs == RT_NULL )
	{
		rt_free(fullpath); /* release path */
		return -DFS_STATUS_ENOENT;
	}

	dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name));
	fd->fs = fs;

	/* initilize the fd item */
	fd->type = FT_REGULAR;
	fd->flags = flags;
	fd->size = 0;
	fd->pos = 0;

	if (dfs_subdir(fs->path, fullpath) == RT_NULL)
		fd->path = rt_strdup("/");
	else
		fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
	rt_free(fullpath);
	dfs_log(DFS_DEBUG_INFO, ("actul file path: %s\n", fd->path));

	/* specific file system open routine */
	if (fs->ops->open == RT_NULL)
	{
		/* clear fd */
		rt_free(fd->path);		
		rt_memset(fd, 0, sizeof(*fd));

		return -DFS_STATUS_ENOSYS;
	}

	if ((result = fs->ops->open(fd)) < 0)
	{
		/* clear fd */
		rt_free(fd->path);
		rt_memset(fd, 0, sizeof(*fd));

		dfs_log(DFS_DEBUG_INFO, ("open failed"));

		return result;
	}

	fd->flags |= DFS_F_OPEN;
	if ( flags & DFS_O_DIRECTORY )
	{
		fd->type = FT_DIRECTORY;
		fd->flags |= DFS_F_DIRECTORY;
	}

	dfs_log(DFS_DEBUG_INFO, ("open successful"));
	return 0;
}