1 /***************************************************************************
5 begin : Thu Aug 15 2002
6 copyright : (C) 2002 by Martin Preuss
7 email : martin@libchipcard.de
9 ***************************************************************************
11 * This library is free software; you can redistribute it and/or *
12 * modify it under the terms of the GNU Lesser General Public *
13 * License as published by the Free Software Foundation; either *
14 * version 2.1 of the License, or (at your option) any later version. *
16 * This library is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19 * Lesser General Public License for more details. *
21 * You should have received a copy of the GNU Lesser General Public *
22 * License along with this library; if not, write to the Free Software *
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
26 ***************************************************************************/
35 #include <sys/types.h>
39 #include <lufs/proto.h>
47 cardfs_init(struct list_head *cfg, struct dir_cache *cache, struct credentials *cred, void **global_fs){
48 return (void*)new CardFS(cfg, cache);
52 cardfs_free(void *ctx){
53 CardFS *p = (CardFS*)ctx;
59 cardfs_mount(void *ctx){
60 return ((CardFS*)ctx)->do_mount();
64 cardfs_umount(void *ctx){
65 // return ((CardFS*)ctx)->do_umount();
69 cardfs_readdir(void *ctx, char *dir_name, struct directory *dir){
70 return ((CardFS*)ctx)->do_readdir(dir_name, dir);
74 cardfs_stat(void *ctx, char *name, struct lufs_fattr *fattr){
75 return ((CardFS*)ctx)->do_stat(name, fattr);
79 cardfs_mkdir(void *ctx, char *dir, int mode){
80 return ((CardFS*)ctx)->do_mkdir(dir, mode);
84 cardfs_rmdir(void *ctx, char *dir){
85 return ((CardFS*)ctx)->do_rmdir(dir);
89 cardfs_create(void *ctx, char *file, int mode){
90 return ((CardFS*)ctx)->do_create(file, mode);
94 cardfs_unlink(void *ctx, char *file){
95 return ((CardFS*)ctx)->do_unlink(file);
99 cardfs_rename(void *ctx, char *old_name, char *new_name){
100 return ((CardFS*)ctx)->do_rename(old_name, new_name);
104 cardfs_open(void *ctx, char *file, unsigned mode){
105 return ((CardFS*)ctx)->do_open(file, mode);
109 cardfs_release(void *ctx, char *file){
110 return ((CardFS*)ctx)->do_release(file);
114 cardfs_read(void *ctx, char *file, unsigned long offset, unsigned long count, char *buf){
115 return ((CardFS*)ctx)->do_read(file, offset, count, buf);
119 cardfs_write(void *ctx, char *file, unsigned long offset, unsigned long count, char *buf){
120 return ((CardFS*)ctx)->do_write(file, offset, count, buf);
124 cardfs_readlink(void *ctx, char *link, char *buf, int buflen){
125 return ((CardFS*)ctx)->do_readlink(link, buf, buflen);
129 cardfs_link(void *ctx, char *target, char *link){
130 return ((CardFS*)ctx)->do_link(target, link);
134 cardfs_symlink(void *ctx, char *target, char *link){
135 return ((CardFS*)ctx)->do_symlink(target, link);
139 cardfs_setattr(void *ctx, char *file, struct lufs_fattr *fattr){
140 return ((CardFS*)ctx)->do_setattr(file, fattr);
145 CardFS::CardFS(struct list_head *c, struct dir_cache *cache, struct credentials *cred)
151 _cid="LUFS:CardFS:"+CTMisc::num2string(getpid());
165 if (pos!=string::npos) {
166 _terminal=tmp.substr(0,pos);
167 if (pos+1<tmp.length())
168 _host=tmp.substr(pos+1);
173 FileClient client(_cid, InetAddress(_host.c_str()),
178 return client.mount(_terminal,
183 TRACE("Exception: "<<st.statusHistory());
191 FileClient client(_cid, InetAddress(_host.c_str()),
196 // force unmounting !
197 client.unmount(true);
200 struct lufs_fattr CardFS::_entry2fattr(const CTDirEntry &e) {
201 struct lufs_fattr fattr;
205 if (e.attributes() & CTDirEntry::Attr_DIR) {
212 if (e.attributes() & CTDirEntry::Attr_READ) {
215 if (e.attributes() & CTDirEntry::Attr_WRITE) {
221 fattr.f_uid = getuid();
222 fattr.f_gid = getgid();
223 fattr.f_size = e.size();
231 CardFS::do_readdir(char* d, struct directory *dir){
232 struct lufs_fattr fattr;
236 INFO("do_readdir "<<d);
237 FileClient client(_cid, InetAddress(_host.c_str()),
246 CTDirEntry::Attr_DIR |
247 CTDirEntry::Attr_READ |
248 CTDirEntry::Attr_WRITE);
249 fattr=_entry2fattr(entry);
250 lu_cache_add2dir(dir, (char*)entry.name().c_str(), NULL, &fattr);
255 CTDirEntry::Attr_DIR |
256 CTDirEntry::Attr_READ |
257 CTDirEntry::Attr_WRITE);
258 fattr=_entry2fattr(entry);
259 lu_cache_add2dir(dir, (char*)entry.name().c_str(), NULL, &fattr);
262 while(client.readDir(d,entry,i++)>=0) {
263 if (entry.attributes() & CTDirEntry::Attr_USED) {
264 fattr=_entry2fattr(entry);
265 lu_cache_add2dir(dir, (char*)entry.name().c_str(), NULL, &fattr);
274 CardFS::do_stat(char *nm, struct lufs_fattr *fattr){
278 INFO("do_stat "<<nm);
279 FileClient client(_cid, InetAddress(_host.c_str()),
285 result=client.statFile(nm,
289 if (entry.attributes() & CTDirEntry::Attr_USED)
290 *fattr=_entry2fattr(entry);
296 unsigned int CardFS::_mode2attribs(int mode) {
302 att|=CTDirEntry::Attr_READ;
304 att|=CTDirEntry::Attr_WRITE;
306 att|=CTDirEntry::Attr_DIR;
307 att|=CTDirEntry::Attr_USED;
311 int CardFS::do_mkdir(char *d, int mode){
314 FileClient client(_cid, InetAddress(_host.c_str()),
319 result=client.mkDir(d,_mode2attribs(mode));
325 CardFS::do_rmdir(char *d){
328 FileClient client(_cid, InetAddress(_host.c_str()),
333 result=client.rmDir(d);
338 CardFS::do_create(char *fn, int mode){
341 INFO("CardFS::do_create "<<fn);
342 FileClient client(_cid, InetAddress(_host.c_str()),
347 result=client.createFile(fn,_mode2attribs(mode));
353 CardFS::do_unlink(char *fn){
356 FileClient client(_cid, InetAddress(_host.c_str()),
361 result=client.unlinkFile(fn);
366 CardFS::do_rename(char *old, char *nnew){
369 INFO("do_rename "<<old<<"->"<<nnew);
371 FileClient client(_cid, InetAddress(_host.c_str()),
376 result=client.renameFile(old,nnew);
382 CardFS::do_open(char *fn, unsigned mode){
383 struct lufs_fattr fattr;
384 INFO("do_open "<<fn);
386 return do_stat(fn,&fattr);
391 CardFS::do_release(char *file){
392 INFO("do_release "<<file);
398 CardFS::do_read(char *fn, unsigned long offset, unsigned long count, char *buf){
402 INFO("do_read "<<fn);
403 FileClient client(_cid, InetAddress(_host.c_str()),
409 result=client.readFile(fn,
415 memmove(buf,tmp.c_str(),tmp.length());
420 CardFS::do_write(char *fn, unsigned long offset, unsigned long count, char *buf){
421 FileClient client(_cid, InetAddress(_host.c_str()),
426 return client.writeFile(fn,offset,count,buf);
430 CardFS::do_readlink(char *link, char *buf, int buflen){
431 INFO("CardFS::do_readlink "<<link);
436 CardFS::do_link(char *old, char *nnew){
437 INFO("CardFS::do_link "<<old<<"->"<<nnew);
441 int CardFS::do_symlink(char *old, char *nnew){
442 INFO("CardFS::do_symlink "<<old<<"->"<<nnew);
446 int CardFS::do_setattr(char *f, struct lufs_fattr *fattr){
448 INFO("CardFS::do_setattr "<<f<<"(ignored)");