int ml_truncate(struct afp_volume * vol, const char * path, off_t offset) { int ret=0; char converted_path[AFP_MAX_PATH]; struct afp_file_info *fp; int flags; if (convert_path_to_afp(vol->server->path_encoding, converted_path,(char *) path,AFP_MAX_PATH)) return -EINVAL; /* The approach here is to get the forkid by calling ml_open() (and not afp_openfork). Note the fake afp_file_info used just to grab this forkid. */ if (invalid_filename(vol->server,converted_path)) return -ENAMETOOLONG; if (volume_is_readonly(vol)) return -EACCES; ret=appledouble_truncate(vol,path,offset); if (ret<0) return ret; if (ret==1) return 0; /* Here, we're going to use the untranslated path since it is translated through the ml_open() */ flags=O_WRONLY; if ((ml_open(vol,path,flags,&fp))) { return ret; }; if ((ret=ll_zero_file(vol,fp->forkid,0))) goto out; afp_closefork(vol,fp->forkid); remove_opened_fork(vol, fp); free(fp); out: return -ret; }
static int fuse_open(const char *path, struct fuse_file_info *fi) { struct afp_file_info * fp ; int ret; struct afp_volume * volume= (struct afp_volume *) ((struct fuse_context *)(fuse_get_context()))->private_data; unsigned char flags = fi->flags; log_fuse_event(AFPFSD,LOG_DEBUG, "*** Opening path %s\n",path); ret = ml_open(volume,path,flags,&fp); if (ret==0) fi->fh=(void *) fp; return ret; }
static int retrieve_file(char * arg,int fd, int silent, struct stat *stat, unsigned long long * amount_written) { int flags=O_RDONLY; int ret=0; struct afp_file_info * fp; char path[PATH_MAX]; off_t offset = 0; #define BUF_SIZE 102400 size_t size = BUF_SIZE; char buf[BUF_SIZE]; int eof; unsigned long long total=0; struct timeval starttv,endtv; *amount_written=0; if (server==NULL) { printf("You're not connected yet to a volume\n"); goto error; } get_server_path(arg,path); gettimeofday(&starttv,NULL); if ((ret=ml_getattr(vol,path,stat))!=0) { printf("Could not get file attributes for file %s, return code %d\n",path,ret); goto error; } ret = ml_open(vol,path,flags,&fp); if (ret) { printf("Could not open %s on server, AFP error %d\n",arg,ret); goto error; } ret =1; /* to get the loop going */ while (ret) { memset(buf,0,BUF_SIZE); ret = ml_read(vol,path,buf,size,offset,fp,&eof); if (ret<=0) goto out; total+=write(fd,buf,ret); offset+=ret; if ((eof==1) || (ret==0)) goto out; } out: if (fd>1) close(fd); ml_close(vol,path,fp); free(fp); if (silent==0) { gettimeofday(&endtv,NULL); printdiff(&starttv, &endtv,&total); } *amount_written=total; return 0; error: return -1; }
int com_put(char *arg) { int ret, amount_read; struct afp_file_info *fp; int offset=0; #define PUT_BUFSIZE 102400 char buf[PUT_BUFSIZE]; int fd; char server_fullname[AFP_MAX_PATH]; char * basename; uid_t uid; gid_t gid; struct stat localstat; unsigned long long amount_written=0; struct timeval starttv,endtv; char filename[AFP_MAX_PATH]; if ((server==NULL) || (vol==NULL)) { printf("You're not connected yet to a volume\n"); goto error; } if ((escape_paths(filename,NULL,arg))) { printf("expecting format: put <filename>\n"); goto error; } /* FIXME find basename */ basename=filename; get_server_path(basename,server_fullname); /* FIXME need a better way to get server's uid/gid */ uid=getuid(); gid=getgid(); if (stat(filename, &localstat)!=0) { perror("Opening local file"); } fd = open(filename,O_RDONLY); if (fd<0) { perror("Opening local file"); goto error; } gettimeofday(&starttv,NULL); ret = ml_open(vol,server_fullname,O_CREAT | O_RDWR,&fp); if (ret<0) { printf("Problem opening file %s on server\n",basename); goto out; } /* Now set various permissions */ ret=ml_chmod(vol,server_fullname,localstat.st_mode); if (ret==-ENOSYS) printf("cannot change permissions\n"); if (ret) { printf("Could not change permissions to 0%o\n", localstat.st_mode); } while (1) { amount_read=read(fd,buf,PUT_BUFSIZE); if (amount_read<0) { perror("Reading"); goto out; } if (amount_read==0) goto out; ret=ml_write(vol,server_fullname,buf,amount_read, offset,fp,uid,gid); offset+=amount_read; amount_written+=amount_read; if (ret<0) { printf("IO error when writing to server, error %d\n", ret); goto out; } } /* FIXME time */ out: gettimeofday(&endtv,NULL); printdiff(&starttv, &endtv,&amount_written); close(fd); ml_close(vol,server_fullname,fp); return 0; error: return -1; }