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
10 #include "mvfs-internal.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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
95 if (st
->name
) free((char*)st
->name
);
96 if (st
->uid
) free((char*)st
->uid
);
97 if (st
->gid
) free((char*)st
->gid
);
102 MVFS_STAT
* mvfs_stat_dup(MVFS_STAT
* oldst
)
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
;
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
: "");
125 MVFS_STAT
* mvfs_file_stat(MVFS_FILE
* fp
)
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
)
140 if (file
->ops
.close
== NULL
)
141 ret
= mvfs_default_fileops_close(file
);
143 ret
= file
->ops
.close(file
);
144 mvfs_file_unref(file
);
148 MVFS_FILE
* mvfs_file_alloc(MVFS_FILESYSTEM
* fs
, MVFS_FILE_OPS ops
)
150 MVFS_FILE
* fp
= calloc(sizeof(MVFS_FILE
),1);
159 int mvfs_file_unref(MVFS_FILE
* file
)
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
);
173 file
->ops
.free(file
);
175 // now we can assume, all additional data has been free()'d and fs ins unref'ed
179 int mvfs_file_ref(MVFS_FILE
* file
)
184 return file
->refcount
;
187 int mvfs_file_eof(MVFS_FILE
* file
)
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
)
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
)
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
)
223 if (file
->ops
.reset
== NULL
)
224 return mvfs_default_fileops_reset(file
);
226 return file
->ops
.reset(file
);