Back to index

avfs  1.0.1
avfsd.c
Go to the documentation of this file.
00001 /*
00002     FUSE: Filesystem in Userspace
00003     Copyright (C) 2001  Miklos Szeredi <miklos@szeredi.hu>
00004     Copyright (C) 2009-2010  Ralf Hoffmann (ralf@boomerangsworld.de)
00005 
00006     This program can be distributed under the terms of the GNU GPL.
00007     See the file COPYING.
00008 */
00009 
00010 #define FUSE_USE_VERSION 26
00011 
00012 #include <fuse.h>
00013 #include <virtual.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <unistd.h>
00018 #include <errno.h>
00019 #include <fcntl.h>
00020 #include <signal.h>
00021 #include <pthread.h>
00022 
00023 struct fuse *fuse;
00024 
00025 static pthread_mutex_t avfsd_mutexlock = PTHREAD_MUTEX_INITIALIZER;
00026 
00027 static int avfsd_getattr(const char *path, struct stat *stbuf)
00028 {
00029     int res;
00030 
00031     res = virt_lstat(path, stbuf);
00032     if (res == -1)
00033         return -errno;
00034 
00035     return 0;
00036 }
00037 
00038 static int avfsd_readlink(const char *path, char *buf, size_t size)
00039 {
00040     int res;
00041 
00042     res = virt_readlink(path, buf, size - 1);
00043     if (res == -1)
00044         return -errno;
00045 
00046     buf[res] = '\0';
00047     return 0;
00048 }
00049 
00050 
00051 static int avfsd_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
00052                          off_t offset, struct fuse_file_info *fi)
00053 {
00054     DIR *dp;
00055     struct dirent *de;
00056 
00057     (void) offset;
00058     (void) fi;
00059     dp = virt_opendir(path);
00060     if (dp == NULL)
00061         return -errno;
00062 
00063     while((de = virt_readdir(dp)) != NULL) {
00064         struct stat st;
00065         memset(&st, 0, sizeof(st));
00066         st.st_ino = de->d_ino;
00067         st.st_mode = de->d_type << 12;
00068         if (filler(buf, de->d_name, &st, 0))
00069             break;
00070     }
00071 
00072     virt_closedir(dp);
00073     return 0;
00074 }
00075 
00076 static int avfsd_mknod(const char *path, mode_t mode, dev_t rdev)
00077 {
00078     int res;
00079 
00080     res = virt_mknod(path, mode, rdev);
00081     if (res == -1)
00082         return -errno;
00083 
00084     return 0;
00085 }
00086 
00087 static int avfsd_mkdir(const char *path, mode_t mode)
00088 {
00089     int res;
00090 
00091     res = virt_mkdir(path, mode);
00092     if (res == -1)
00093         return -errno;
00094 
00095     return 0;
00096 }
00097 
00098 static int avfsd_unlink(const char *path)
00099 {
00100     int res;
00101 
00102     res = virt_unlink(path);
00103     if (res == -1)
00104         return -errno;
00105 
00106     return 0;
00107 }
00108 
00109 static int avfsd_rmdir(const char *path)
00110 {
00111     int res;
00112 
00113     res = virt_rmdir(path);
00114     if (res == -1)
00115         return -errno;
00116 
00117     return 0;
00118 }
00119 
00120 static int avfsd_symlink(const char *from, const char *to)
00121 {
00122     int res;
00123 
00124     res = virt_symlink(from, to);
00125     if (res == -1)
00126         return -errno;
00127 
00128     return 0;
00129 }
00130 
00131 static int avfsd_rename(const char *from, const char *to)
00132 {
00133     int res;
00134 
00135     res = virt_rename(from, to);
00136     if (res == -1)
00137         return -errno;
00138 
00139     return 0;
00140 }
00141 
00142 static int avfsd_link(const char *from, const char *to)
00143 {
00144     int res;
00145 
00146     res = virt_link(from, to);
00147     if (res == -1)
00148         return -errno;
00149 
00150     return 0;
00151 }
00152 
00153 static int avfsd_chmod(const char *path, mode_t mode)
00154 {
00155     int res;
00156 
00157     res = virt_chmod(path, mode);
00158     if (res == -1)
00159         return -errno;
00160 
00161     return 0;
00162 }
00163 
00164 static int avfsd_chown(const char *path, uid_t uid, gid_t gid)
00165 {
00166     int res;
00167 
00168     res = virt_lchown(path, uid, gid);
00169     if (res == -1)
00170         return -errno;
00171 
00172     return 0;
00173 }
00174 
00175 static int avfsd_truncate(const char *path, off_t size)
00176 {
00177     int res;
00178 
00179     res = virt_truncate(path, size);
00180     if (res == -1)
00181         return -errno;
00182 
00183     return 0;
00184 }
00185 
00186 static int avfsd_utime(const char *path, struct utimbuf *buf)
00187 {
00188     int res;
00189 
00190     res = virt_utime(path, buf);
00191     if (res == -1)
00192         return -errno;
00193 
00194     return 0;
00195 }
00196 
00197 
00198 static int avfsd_open(const char *path, struct fuse_file_info *fi)
00199 {
00200     int res;
00201 
00202     res = virt_open(path, fi->flags, 0);
00203     if (res == -1)
00204         return -errno;
00205 
00206     fi->fh = res;
00207     return 0;
00208 }
00209 
00210 static int avfsd_read(const char *path, char *buf, size_t size, off_t offset,
00211                       struct fuse_file_info *fi)
00212 {
00213     int res;
00214 
00215     (void) path;
00216 
00217     pthread_mutex_lock( &avfsd_mutexlock );
00218     if (virt_lseek(fi->fh, offset, SEEK_SET) == -1) {
00219         pthread_mutex_unlock( &avfsd_mutexlock );
00220         return -errno;
00221     }
00222 
00223     res = virt_read(fi->fh, buf, size);
00224     pthread_mutex_unlock( &avfsd_mutexlock );
00225 
00226     if (res == -1)
00227         return -errno;
00228 
00229     return res;
00230 }
00231 
00232 static int avfsd_write(const char *path, const char *buf, size_t size,
00233                        off_t offset, struct fuse_file_info *fi)
00234 {
00235     int res;
00236 
00237     (void) path;
00238 
00239     pthread_mutex_lock( &avfsd_mutexlock );
00240     if (virt_lseek(fi->fh, offset, SEEK_SET) == -1) {
00241         pthread_mutex_unlock( &avfsd_mutexlock );
00242         return -errno;
00243     }
00244 
00245     res = virt_write(fi->fh, buf, size);
00246     pthread_mutex_unlock( &avfsd_mutexlock );
00247 
00248     if (res == -1)
00249         return -errno;
00250 
00251     return res;
00252 }
00253 
00254 static int avfsd_release(const char *path, struct fuse_file_info *fi)
00255 {
00256     (void) path;
00257 
00258     pthread_mutex_lock( &avfsd_mutexlock );
00259     virt_close(fi->fh);
00260     pthread_mutex_unlock( &avfsd_mutexlock );
00261 
00262     return 0;
00263 }
00264 
00265 static int avfsd_access(const char *path, int mask)
00266 {
00267     int res;
00268 
00269     res = virt_access(path, mask);
00270     if (res == -1)
00271         return -errno;
00272 
00273     return 0;
00274 }
00275 
00276 static int avfsd_create(const char *path, mode_t mode, struct fuse_file_info *fi)
00277 {
00278     int res;
00279 
00280     /* open will handle the O_CREAT flag */
00281     res = virt_open(path, fi->flags | O_CREAT | O_TRUNC, mode);
00282     if (res == -1)
00283         return -errno;
00284 
00285     fi->fh = res;
00286     return 0;
00287 }
00288 
00289 static struct fuse_operations avfsd_oper = {
00290     getattr:  avfsd_getattr,
00291     readlink: avfsd_readlink,
00292     readdir:  avfsd_readdir,
00293     mknod:    avfsd_mknod,
00294     mkdir:    avfsd_mkdir,
00295     symlink:  avfsd_symlink,
00296     unlink:   avfsd_unlink,
00297     rmdir:    avfsd_rmdir,
00298     rename:   avfsd_rename,
00299     link:     avfsd_link,
00300     chmod:    avfsd_chmod,
00301     chown:    avfsd_chown,
00302     truncate: avfsd_truncate,
00303     utime:    avfsd_utime,
00304     open:     avfsd_open,
00305     read:     avfsd_read,
00306     write:    avfsd_write,
00307     release:  avfsd_release,
00308     access:   avfsd_access,
00309     create:   avfsd_create,
00310 };
00311 
00312 int main(int argc, char *argv[])
00313 {
00314     fuse_main(argc, argv, &avfsd_oper, NULL);
00315     return 0;
00316 }