在这个文件中基于Fuse的用户态文件系统
int main(int argc, char* argv[])
{
umask(0);
return fuse_main(argc, argv, &xmp_oper, NULL);
}
static int xmp_getattr(const char* path, struct stat* stbuf)//类似start()
{
int res;
res = lstat(path, stbuf);
if (res == -1)
return -errno;//返回错误信息,没有该文件或目录
return 0;//执行成功返回0
}
static int xmp_fgetattr(const char* path, struct stat* stbuf,
struct fuse_file_info* fi)
{
int res;
(void)path;
res = fstat(fi->fh, stbuf);
if (res == -1)
return -errno;//返回错误信息,没有该文件或目录
return 0;//执行成功返回0
}
struct xmp_dirp {//读取目录内容
DIR* dp;
struct dirent* entry;
off_t offset;
};
static int xmp_opendir(const char* path, struct fuse_file_info* fi)
{
int res;
struct xmp_dirp* d = malloc(sizeof(struct xmp_dirp));
if (d == NULL)
return -ENOMEM;
d->dp = opendir(path);
if (d->dp == NULL) {
res = -errno;
free(d);
return res;
}
d->offset = 0;
d->entry = NULL;
fi->fh = (unsigned long)d;
return 0;
}
static inline struct xmp_dirp* get_dirp(struct fuse_file_info* fi)
{
return (struct xmp_dirp*)(uintptr_t)fi->fh;
}
static int xmp_readdir(const char* path, void* buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info* fi)
{
struct xmp_dirp* d = get_dirp(fi);
(void)path;
if (offset != d->offset) {
seekdir(d->dp, offset);
d->entry = NULL;
d->offset = offset;
}
while (1) {
struct stat st;
off_t nextoff;
if (!d->entry) {
d->entry = readdir(d->dp);
if (!d->entry)
break;
}
memset(&st, 0, sizeof(st));
st.st_ino = d->entry->d_ino;
st.st_mode = d->entry->d_type << 12;
nextoff = telldir(d->dp);
if (filler(buf, d->entry->d_name, &st, nextoff))
break;
d->entry = NULL;
d->offset = nextoff;
}
return 0;
}
static int xmp_mkdir(const char* path, mode_t mode)//创建目录
{
int res;
res = mkdir(path, mode);
if (res == -1)
return -errno;
return 0;
}
static int xmp_unlink(const char* path)//删除文件
{
int res;
res = unlink(path);
if (res == -1)
return -errno;
return 0;
}
static int xmp_rmdir(const char* path)//删除目录
{
int res;
res = rmdir(path);
if (res == -1)
return -errno;
return 0;
}
static int xmp_rename(const char* from, const char* to)//重命名文件(可选,非必需)
{
int res;
res = rename(from, to);
if (res == -1)
return -errno;
return 0;
}
static int xmp_chmod(const char* path, mode_t mode)//修改文件权限
{
int res;
res = chmod(path, mode);
if (res == -1)
return -errno;
return 0;
}
static int xmp_chown(const char* path, uid_t uid, gid_t gid)//修改文件属主
{
int res;
res = lchown(path, uid, gid);
if (res == -1)
return -errno;
return 0;
}
static int xmp_truncate(const char* path, off_t size)//修改文件大小(可选)
{
int res;
res = truncate(path, size);
if (res == -1)
return -errno;
return 0;
}
static int xmp_ftruncate(const char* path, off_t size,
struct fuse_file_info* fi)
{
int res;
(void)path;
res = ftruncate(fi->fh, size);
if (res == -1)
return -errno;
return 0;
}
#ifdef HAVE_UTIMENSAT
static int xmp_utimens(const char* path, const struct timespec ts[2])//修改文件访问/修改时间(可选)
{
int res;
/* don't use utime/utimes since they follow symlinks */
res = utimensat(0, path, ts, AT_SYMLINK_NOFOLLOW);
if (res == -1)
return -errno;
return 0;
}
#endif
static int xmp_create(const char* path, mode_t mode, struct fuse_file_info* fi)
{
int fd;
fd = open(path, fi->flags, mode);
if (fd == -1)
return -errno;
fi->fh = fd;
return 0;
}
static int xmp_open(const char* path, struct fuse_file_info* fi)
{
int fd;
fd = open(path, fi->flags);
if (fd == -1)
return -errno;
fi->fh = fd;
return 0;
}
static int xmp_read(const char* path, char* buf, size_t size, off_t offset,//打开文件读取数据
struct fuse_file_info* fi)
{
int res;
(void)path;
res = pread(fi->fh, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
static int xmp_read_buf(const char* path, struct fuse_bufvec** bufp,
size_t size, off_t offset, struct fuse_file_info* fi)
{
struct fuse_bufvec* src;
(void)path;
src = malloc(sizeof(struct fuse_bufvec));
if (src == NULL)
return -ENOMEM;
*src = FUSE_BUFVEC_INIT(size);
src->buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
src->buf[0].fd = fi->fh;
src->buf[0].pos = offset;
*bufp = src;
return 0;
}
static int xmp_write(const char* path, const char* buf, size_t size,//将数据写入文件
off_t offset, struct fuse_file_info* fi)
{
int res;
(void)path;
res = pwrite(fi->fh, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
static int xmp_write_buf(const char* path, struct fuse_bufvec* buf,
off_t offset, struct fuse_file_info* fi)
{
struct fuse_bufvec dst = FUSE_BUFVEC_INIT(fuse_buf_size(buf));
(void)path;
dst.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
dst.buf[0].fd = fi->fh;
dst.buf[0].pos = offset;
return fuse_buf_copy(&dst, buf, FUSE_BUF_SPLICE_NONBLOCK);
}
/*注册上面定义的函数*/
static struct fuse_operations xmp_oper = {
.getattr = xmp_getattr,
.fgetattr = xmp_fgetattr,
.opendir = xmp_opendir,
.readdir = xmp_readdir,
.releasedir = xmp_releasedir,
.mkdir = xmp_mkdir,
.unlink = xmp_unlink,
.rmdir = xmp_rmdir,
.rename = xmp_rename,
.chmod = xmp_chmod,
.chown = xmp_chown,
.truncate = xmp_truncate,
.ftruncate = xmp_ftruncate,
#ifdef HAVE_UTIMENSAT
.utimens = xmp_utimens,
#endif
.create = xmp_create,
.open = xmp_open,
.read = xmp_read,
.read_buf = xmp_read_buf,
.write = xmp_write,
.write_buf = xmp_write_buf,
};
这一块代码中,为什么我用VS编译会大量报错,能不能给我头文件以及编译所用的软件