library name fixups
[libmvfs.git] / libmvfs / fileops.c
bloba08b129080117cdf2d6e217ccb59a2066631e0a8
1 /*
2 libmvfs - metux Virtual Filesystem Library
4 Client-side file operation frontend
6 Copyright (C) 2008 Enrico Weigelt, metux IT service <weigelt@metux.de>
7 This code is published under the terms of the GNU Public License 2.0
8 */
10 #include "mvfs-internal.h"
12 #include <string.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <malloc.h>
18 #include <mvfs/types.h>
19 #include <mvfs/default_ops.h>
22 off64_t mvfs_file_seek (MVFS_FILE* fp, off64_t offset, int whence)
24 if (fp==NULL)
25 return (off64_t) -EFAULT;
27 if (fp->ops.seek == NULL)
28 return mvfs_default_fileops_seek(fp, offset, whence);
30 return fp->ops.seek(fp, offset, whence);
33 ssize_t mvfs_file_read (MVFS_FILE* fp, void* buf, size_t count)
35 if (fp==NULL)
36 return (ssize_t) -EFAULT;
37 if (fp->ops.read == NULL)
38 return mvfs_default_fileops_read(fp, buf, count);
40 return fp->ops.read(fp, buf, count);
43 ssize_t mvfs_file_write (MVFS_FILE* fp, const void* buf, size_t count)
45 if (fp==NULL)
46 return (ssize_t) -EFAULT;
47 if (fp->ops.write == NULL)
48 return mvfs_default_fileops_write(fp, buf, count);
50 return fp->ops.write(fp, buf, count);
53 ssize_t mvfs_file_pread (MVFS_FILE* fp, void* buf, size_t count, off64_t offset)
55 if (fp==NULL)
56 return (ssize_t) -EFAULT;
57 if (fp->ops.read == NULL)
58 return mvfs_default_fileops_pread(fp, buf, count, offset);
60 return fp->ops.pread(fp, buf, count, offset);
63 ssize_t mvfs_file_pwrite (MVFS_FILE* fp, const void* buf, size_t count, off64_t offset)
65 if (fp==NULL)
66 return (ssize_t) -EFAULT;
67 if (fp->ops.write == NULL)
68 return mvfs_default_fileops_pwrite(fp, buf, count, offset);
70 return fp->ops.pwrite(fp, buf, count, offset);
73 int mvfs_file_setflag (MVFS_FILE* fp, MVFS_FILE_FLAG flag, long value)
75 if (fp==NULL)
76 return -EFAULT;
77 if (fp->ops.setflag == NULL)
78 return mvfs_default_fileops_setflag(fp, flag, value);
79 return fp->ops.setflag(fp, flag, value);
82 int mvfs_file_getflag (MVFS_FILE* fp, MVFS_FILE_FLAG flag, long* value)
84 if (fp==NULL)
85 return -EFAULT;
86 if (fp->ops.getflag == NULL)
87 return mvfs_default_fileops_getflag(fp, flag, value);
88 return fp->ops.getflag(fp, flag, value);
91 int mvfs_stat_free(MVFS_STAT*st)
93 if (st == NULL)
94 return -EFAULT;
95 if (st->name) free((char*)st->name);
96 if (st->uid) free((char*)st->uid);
97 if (st->gid) free((char*)st->gid);
98 free(st);
99 return 0;
102 MVFS_STAT* mvfs_stat_dup(MVFS_STAT* oldst)
104 if (oldst == NULL)
105 return NULL;
107 MVFS_STAT* newst = mvfs_stat_alloc(oldst->name, oldst->uid, oldst->gid);
108 newst->mode = oldst->mode;
109 newst->size = oldst->size;
110 newst->atime = oldst->atime;
111 newst->mtime = oldst->mtime;
112 newst->ctime = oldst->ctime;
113 return newst;
116 MVFS_STAT* mvfs_stat_alloc(const char* name, const char* uid, const char* gid)
118 MVFS_STAT* stat = (MVFS_STAT*)calloc(1,sizeof(MVFS_STAT));
119 stat->name = strdup(name ? name : "");
120 stat->uid = strdup(uid ? uid : "");
121 stat->gid = strdup(gid ? gid : "");
122 return stat;
125 MVFS_STAT* mvfs_file_stat(MVFS_FILE* fp)
127 if (fp==NULL)
128 return NULL;
129 if (fp->ops.stat == NULL)
130 return mvfs_default_fileops_stat(fp);
131 return fp->ops.stat(fp);
134 int mvfs_file_close(MVFS_FILE* file)
136 if (file==NULL)
137 return -EFAULT;
139 int ret;
140 if (file->ops.close == NULL)
141 ret = mvfs_default_fileops_close(file);
142 else
143 ret = file->ops.close(file);
144 mvfs_file_unref(file);
145 return ret;
148 MVFS_FILE* mvfs_file_alloc(MVFS_FILESYSTEM* fs, MVFS_FILE_OPS ops)
150 MVFS_FILE* fp = calloc(sizeof(MVFS_FILE),1);
151 fp->fs = fs;
152 fp->refcount = 1;
153 fp->ops = ops;
154 mvfs_fs_ref(fs);
156 return fp;
159 int mvfs_file_unref(MVFS_FILE* file)
161 if (file==NULL)
162 return -EFAULT;
164 file->refcount--;
165 if (file->refcount>0)
166 return file->refcount;
168 // file is not referenced anymore - call the free handler
169 // the free handler is also responsible for unref'ing the fs
170 if (file->ops.free == NULL)
171 mvfs_default_fileops_free(file);
172 else
173 file->ops.free(file);
175 // now we can assume, all additional data has been free()'d and fs ins unref'ed
176 free(file);
179 int mvfs_file_ref(MVFS_FILE* file)
181 if (file==NULL)
182 return -EFAULT;
183 file->refcount++;
184 return file->refcount;
187 int mvfs_file_eof(MVFS_FILE* file)
189 if (file==NULL)
190 return -EFAULT;
191 if (file->ops.eof == NULL)
192 return mvfs_default_fileops_eof(file);
193 return file->ops.eof(file);
196 MVFS_STAT* mvfs_file_scan(MVFS_FILE* file)
198 if (file == NULL)
199 return NULL;
201 if (file->ops.scan == NULL)
202 return mvfs_default_fileops_scan(file);
204 return file->ops.scan(file);
207 MVFS_FILE* mvfs_file_lookup(MVFS_FILE* file, const char* name)
209 if (file == NULL)
210 return NULL;
212 if (file->ops.lookup == NULL)
213 return mvfs_default_fileops_lookup(file, name);
215 return file->ops.lookup(file,name);
218 int mvfs_file_reset(MVFS_FILE* file)
220 if (file==NULL)
221 return -EFAULT;
223 if (file->ops.reset == NULL)
224 return mvfs_default_fileops_reset(file);
226 return file->ops.reset(file);