X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=etc%2Fafpd%2Ffile.c;h=aa692842ed4a5c8d919080336292528d33dbcbd2;hb=refs%2Ftags%2Fafter-renameat;hp=1e45f19182321f3468357b28170a0077979b5eca;hpb=6f3a5b930085e56f39b415f20db02df98dbecfff;p=netatalk.git diff --git a/etc/afpd/file.c b/etc/afpd/file.c index 1e45f191..aa692842 100644 --- a/etc/afpd/file.c +++ b/etc/afpd/file.c @@ -1,5 +1,5 @@ /* - * $Id: file.c,v 1.130 2010-01-06 14:05:15 franklahm Exp $ + * $Id: file.c,v 1.141 2010-03-12 15:16:49 franklahm Exp $ * * Copyright (c) 1990,1993 Regents of The University of Michigan. * All Rights Reserved. See COPYRIGHT. @@ -89,7 +89,7 @@ static int default_type(void *finder) } /* FIXME path : unix or mac name ? (for now it's unix name ) */ -void *get_finderinfo(const struct vol *vol, const char *upath, struct adouble *adp, void *data) +void *get_finderinfo(const struct vol *vol, const char *upath, struct adouble *adp, void *data, int islink) { struct extmap *em; void *ad_finder = NULL; @@ -114,6 +114,17 @@ void *get_finderinfo(const struct vol *vol, const char *upath, struct adouble *a memcpy((char *)data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort)); } } + + if (islink){ + u_int16_t linkflag; + memcpy(&linkflag, (char *)data + FINDERINFO_FRFLAGOFF, 2); + linkflag |= htons(FINDERINFO_ISALIAS); + memcpy((char *)data + FINDERINFO_FRFLAGOFF, &linkflag, 2); + memcpy((char *)data + FINDERINFO_FRTYPEOFF,"slnk",4); + memcpy((char *)data + FINDERINFO_FRCREATOFF,"rhap",4); + chk_ext = 0; + } + /** Only enter if no appledouble information and no finder information found. */ if (chk_ext && (em = getextmap( upath ))) { memcpy(data, em->em_type, sizeof( em->em_type )); @@ -332,7 +343,7 @@ int getmetadata(struct vol *vol, break; case FILPBIT_FINFO : - get_finderinfo(vol, upath, adp, (char *)data); + get_finderinfo(vol, upath, adp, (char *)data,S_ISLNK(st->st_mode)); data += ADEDLEN_FINDERI; break; @@ -737,7 +748,7 @@ int setfilparams(struct vol *vol, int bit, isad = 1, err = AFP_OK; char *upath; u_char achar, *fdType, xyy[4]; /* uninitialized, OK 310105 */ - u_int16_t ashort, bshort; + u_int16_t ashort, bshort, oshort; u_int32_t aint; u_int32_t upriv; u_int16_t upriv_bit = 0; @@ -795,6 +806,28 @@ int setfilparams(struct vol *vol, case FILPBIT_FINFO : change_mdate = 1; memcpy(finder_buf, buf, 32 ); + if (memcmp(buf,"slnkrhap",8)==0 && !S_ISLNK(path->st.st_mode)){ + // SLFINFO + int fp; + ssize_t len; + int erc=1; + char buf[PATH_MAX+1]; + if ((fp=open(path->u_name,O_RDONLY))>=0){ + if ((len=read(fp,buf,PATH_MAX+1))){ + if (unlink(path->u_name)==0){ + buf[len]=0; + erc = symlink(buf, path->u_name); + if (!erc) + of_stat(path); + } + } + close(fp); + } + if (erc!=0){ + err=AFPERR_BITMAP; + goto setfilparam_done; + } + } buf += 32; break; case FILPBIT_UNIXPR : @@ -864,9 +897,9 @@ int setfilparams(struct vol *vol, * - change of modification date * - UNIX privs (Bug-ID #2863424) */ - if ( (f_bitmap & ~(1<vfs->vfs_renamefile(vol, src, dst) < 0 ) { + if (vol->vfs->vfs_renamefile(vol, sdir_fd, src, dst) < 0 ) { int err; err = errno; @@ -1020,7 +1051,7 @@ int renamefile(const struct vol *vol, char *src, char *dst, char *newname, struc * we know we are on the same device */ if (err) { - unix_rename( dst, src ); + unix_rename(-1, dst, sdir_fd, src ); /* return the first error */ switch ( err) { case ENOENT : @@ -1043,9 +1074,6 @@ int renamefile(const struct vol *vol, char *src, char *dst, char *newname, struc ad_flush( adp ); ad_close( adp, ADFLAGS_HF ); } -#ifdef DEBUG - LOG(log_debug9, logtype_afpd, "end renamefile:"); -#endif /* DEBUG */ return( AFP_OK ); } @@ -1174,9 +1202,10 @@ int afp_copyfile(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, si } denyreadset = (getforkmode(adp, ADEID_DFORK, AD_FILELOCK_DENY_RD) != 0 || getforkmode(adp, ADEID_RFORK, AD_FILELOCK_DENY_RD) != 0 ); - ad_close( adp, ADFLAGS_DF |ADFLAGS_HF ); + if (denyreadset) { - return AFPERR_DENYCONF; + retvalue = AFPERR_DENYCONF; + goto copy_exit; } newname = obj->newtmp; @@ -1184,42 +1213,54 @@ int afp_copyfile(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, si p = ctoupath( s_vol, curdir, newname ); if (!p) { - return AFPERR_PARAM; - + retvalue = AFPERR_PARAM; + goto copy_exit; } + #ifdef FORCE_UIDGID /* FIXME svid != dvid && dvid's user can't read svid */ #endif if (NULL == ( d_vol = getvolbyvid( dvid )) ) { - return( AFPERR_PARAM ); + retvalue = AFPERR_PARAM; + goto copy_exit; } - if (d_vol->v_flags & AFPVOL_RO) - return AFPERR_VLOCK; + if (d_vol->v_flags & AFPVOL_RO) { + retvalue = AFPERR_VLOCK; + goto copy_exit; + } if (NULL == ( dir = dirlookup( d_vol, ddid )) ) { - return afp_errno; + retvalue = afp_errno; + goto copy_exit; } if (( s_path = cname( d_vol, dir, &ibuf )) == NULL ) { - return get_afp_errno(AFPERR_NOOBJ); + retvalue = get_afp_errno(AFPERR_NOOBJ); + goto copy_exit; } + if ( *s_path->m_name != '\0' ) { - path_error(s_path, AFPERR_PARAM); + retvalue =path_error(s_path, AFPERR_NOOBJ); + goto copy_exit; } /* one of the handful of places that knows about the path type */ if (copy_path_name(d_vol, newname, ibuf) < 0) { - return( AFPERR_PARAM ); + retvalue = AFPERR_PARAM; + goto copy_exit; } /* newname is always only a filename so curdir *is* its * parent folder */ if (NULL == (upath = mtoupath(d_vol, newname, curdir->d_did, utf8_encoding()))) { - return( AFPERR_PARAM ); + retvalue =AFPERR_PARAM; + goto copy_exit; } - if ( (err = copyfile(s_vol, d_vol, p, upath , newname, adp)) < 0 ) { - return err; + + if ( (err = copyfile(s_vol, d_vol, -1, p, upath , newname, adp)) < 0 ) { + retvalue = err; + goto copy_exit; } curdir->offcnt++; @@ -1231,6 +1272,8 @@ int afp_copyfile(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, si setvoltime(obj, d_vol ); +copy_exit: + ad_close( adp, ADFLAGS_DF |ADFLAGS_HF ); return( retvalue ); } @@ -1337,8 +1380,13 @@ static int copy_fork(int eid, struct adouble *add, struct adouble *ads) * because we are doing it elsewhere. * currently if newname is NULL then adp is NULL. */ -int copyfile(const struct vol *s_vol, const struct vol*d_vol, - char *src, char *dst, char *newname, struct adouble *adp) +int copyfile(const struct vol *s_vol, + const struct vol *d_vol, + int sfd, + char *src, + char *dst, + char *newname, + struct adouble *adp) { struct adouble ads, add; int err = 0; @@ -1347,21 +1395,20 @@ int copyfile(const struct vol *s_vol, const struct vol*d_vol, int stat_result; struct stat st; -#ifdef DEBUG - LOG(log_debug9, logtype_afpd, "begin copyfile:"); -#endif /* DEBUG */ + LOG(log_debug, logtype_afpd, "copyfile(sfd:%d,s:'%s',d:'%s',n:'%s')", + sfd, src, dst, newname); if (adp == NULL) { ad_init(&ads, s_vol->v_adouble, s_vol->v_ad_options); adp = &ads; } - ad_init(&add, d_vol->v_adouble, d_vol->v_ad_options); + adflags = ADFLAGS_DF; if (newname) { adflags |= ADFLAGS_HF; } - if (ad_open(src , adflags | ADFLAGS_NOHF, O_RDONLY, 0, adp) < 0) { + if (ad_openat(sfd, src, adflags | ADFLAGS_NOHF, O_RDONLY, 0, adp) < 0) { ret_err = errno; goto done; } @@ -1378,11 +1425,12 @@ int copyfile(const struct vol *s_vol, const struct vol*d_vol, st.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; } + ad_init(&add, d_vol->v_adouble, d_vol->v_ad_options); if (ad_open(dst , adflags, O_RDWR|O_CREAT|O_EXCL, st.st_mode, &add) < 0) { ret_err = errno; ad_close( adp, adflags ); if (EEXIST != ret_err) { - deletefile(d_vol, dst, 0); + deletefile(d_vol, -1, dst, 0); goto done; } return AFPERR_EXIST; @@ -1394,7 +1442,7 @@ int copyfile(const struct vol *s_vol, const struct vol*d_vol, if (ad_reso_fileno(adp) == -1 || 0 == (err = copy_fork(ADEID_RFORK, &add, adp))){ /* copy the data fork */ if ((err = copy_fork(ADEID_DFORK, &add, adp)) == 0) { - err = d_vol->vfs->vfs_copyfile(d_vol, src, dst); + err = d_vol->vfs->vfs_copyfile(d_vol, sfd, src, dst); } } @@ -1415,7 +1463,7 @@ int copyfile(const struct vol *s_vol, const struct vol*d_vol, } if (ret_err) { - deletefile(d_vol, dst, 0); + deletefile(d_vol, -1, dst, 0); } else if (stat_result == 0) { /* set dest modification date to src date */ @@ -1428,10 +1476,6 @@ int copyfile(const struct vol *s_vol, const struct vol*d_vol, */ } -#ifdef DEBUG - LOG(log_debug9, logtype_afpd, "end copyfile:"); -#endif /* DEBUG */ - done: switch ( ret_err ) { case 0: @@ -1478,59 +1522,57 @@ u_int16_t bshort = 0; } return 0; } - -int deletefile(const struct vol *vol, char *file, int checkAttrib) +/* + * dirfd can be used for unlinkat semantics + */ +int deletefile(const struct vol *vol, int dirfd, char *file, int checkAttrib) { struct adouble ad; - struct adouble *adp = &ad; + struct adouble *adp = NULL; int adflags, err = AFP_OK; + int meta = 0; -#ifdef DEBUG - LOG(log_debug9, logtype_afpd, "begin deletefile:"); -#endif /* DEBUG */ + LOG(log_debug, logtype_afpd, "deletefile('%s')", file); - /* try to open both forks at once */ - adflags = ADFLAGS_DF|ADFLAGS_HF; + ad_init(&ad, vol->v_adouble, vol->v_ad_options); if (checkAttrib) { /* was EACCESS error try to get only metadata */ - ad_init(&ad, vol->v_adouble, vol->v_ad_options); /* we never want to create a resource fork here, we are going to delete it * moreover sometimes deletefile is called with a no existent file and * ad_open would create a 0 byte resource fork */ - if ( ad_metadata( file, ADFLAGS_OPENFORKS, &ad) == 0 ) { - ad_close( &ad, adflags ); + if ( ad_metadataat(dirfd, file, ADFLAGS_OPENFORKS, &ad) == 0 ) { if ((err = check_attrib(&ad))) { + ad_close_metadata(&ad); return err; } + meta = 1; } } - while(1) { - ad_init(&ad, vol->v_adouble, vol->v_ad_options); /* OK */ - if ( ad_open( file, adflags, O_RDONLY, 0, &ad ) < 0 ) { - switch (errno) { - case ENOENT: - if (adflags == ADFLAGS_DF) - return AFPERR_NOOBJ; - - /* that failed. now try to open just the data fork */ - adflags = ADFLAGS_DF; - continue; - - case EACCES: - adp = NULL; /* maybe it's a file with no write mode for us */ - break; /* was return AFPERR_ACCESS;*/ - case EROFS: - return AFPERR_VLOCK; - default: - return( AFPERR_PARAM ); - } + /* try to open both forks at once */ + adflags = ADFLAGS_DF; + if ( ad_openat(dirfd, file, adflags |ADFLAGS_HF|ADFLAGS_NOHF, O_RDONLY, 0, &ad ) < 0 ) { + switch (errno) { + case ENOENT: + err = AFPERR_NOOBJ; + goto end; + case EACCES: /* maybe it's a file with no write mode for us */ + break; /* was return AFPERR_ACCESS;*/ + case EROFS: + err = AFPERR_VLOCK; + goto end; + default: + err = AFPERR_PARAM; + goto end; } - break; /* from the while */ + } + else { + adp = &ad; } - if (adp && (adflags & ADFLAGS_HF) ) { + if ( adp && ad_reso_fileno( adp ) != -1 ) { /* there's a resource fork */ + adflags |= ADFLAGS_HF; /* FIXME we have a pb here because we want to know if a file is open * there's a 'priority inversion' if you can't open the ressource fork RW * you can delete it if it's open because you can't get a write lock. @@ -1541,28 +1583,27 @@ int deletefile(const struct vol *vol, char *file, int checkAttrib) * FIXME it doesn't work for RFORK open read only and fork open without deny mode */ if (ad_tmplock(&ad, ADEID_RFORK, ADLOCK_WR |ADLOCK_FILELOCK, 0, 0, 0) < 0 ) { - ad_close( &ad, adflags ); - return( AFPERR_BUSY ); + err = AFPERR_BUSY; + goto end; } } if (adp && ad_tmplock( &ad, ADEID_DFORK, ADLOCK_WR, 0, 0, 0 ) < 0) { err = AFPERR_BUSY; - } - else if (!(err = vol->vfs->vfs_deletefile(vol, file)) && !(err = netatalk_unlink( file )) ) { + } else if (!(err = vol->vfs->vfs_deletefile(vol, dirfd, file)) && !(err = netatalk_unlinkat(dirfd, file )) ) { cnid_t id; - if (checkAttrib && (id = cnid_get(vol->v_cdb, curdir->d_did, file, strlen(file)))) - { + if (checkAttrib && (id = cnid_get(vol->v_cdb, curdir->d_did, file, strlen(file)))) { cnid_delete(vol->v_cdb, id); } } + +end: + if (meta) + ad_close_metadata(&ad); + if (adp) ad_close( &ad, adflags ); /* ad_close removes locks if any */ -#ifdef DEBUG - LOG(log_debug9, logtype_afpd, "end deletefile:"); -#endif /* DEBUG */ - return err; } @@ -1654,7 +1695,7 @@ static int reenumerate_loop(struct dirent *de, char *mname _U_, void *data) cnid_t did = param->did; cnid_t aint; - if ( stat(de->d_name, &path.st)<0 ) + if ( lstat(de->d_name, &path.st)<0 ) return 0; /* update or add to cnid */ @@ -1699,7 +1740,7 @@ reenumerate_id(struct vol *vol, char *name, struct dir *dir) } /* FIXME use of_statdir ? */ - if (stat(name, &st)) { + if (lstat(name, &st)) { return -1; } @@ -1869,7 +1910,7 @@ int afp_deleteid(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_ } err = AFP_OK; - if ((movecwd(vol, dir) < 0) || (stat(upath, &st) < 0)) { + if ((movecwd(vol, dir) < 0) || (lstat(upath, &st) < 0)) { switch (errno) { case EACCES: case EPERM: @@ -2091,17 +2132,17 @@ int afp_exchangefiles(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U } /* now, quickly rename the file. we error if we can't. */ - if ((err = renamefile(vol, p, temp, temp, adsp)) != AFP_OK) + if ((err = renamefile(vol, -1, p, temp, temp, adsp)) != AFP_OK) goto err_exchangefile; of_rename(vol, s_of, sdir, spath, curdir, temp); /* rename destination to source */ - if ((err = renamefile(vol, upath, p, spath, addp)) != AFP_OK) + if ((err = renamefile(vol, -1, upath, p, spath, addp)) != AFP_OK) goto err_src_to_tmp; of_rename(vol, d_of, curdir, path->m_name, sdir, spath); /* rename temp to destination */ - if ((err = renamefile(vol, temp, upath, path->m_name, adsp)) != AFP_OK) + if ((err = renamefile(vol, -1, temp, upath, path->m_name, adsp)) != AFP_OK) goto err_dest_to_src; of_rename(vol, s_of, curdir, temp, curdir, path->m_name); @@ -2114,10 +2155,10 @@ int afp_exchangefiles(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U if (did) { cnid_delete(vol->v_cdb, did); } - if ((did && ( (crossdev && stat( upath, &srcst) < 0) || + if ((did && ( (crossdev && lstat( upath, &srcst) < 0) || cnid_update(vol->v_cdb, did, &srcst, curdir->d_did,upath, dlen) < 0)) || - (sid && ( (crossdev && stat(p, &destst) < 0) || + (sid && ( (crossdev && lstat(p, &destst) < 0) || cnid_update(vol->v_cdb, sid, &destst, sdir->d_did,supath, slen) < 0)) ) { switch (errno) { @@ -2186,17 +2227,17 @@ int afp_exchangefiles(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U * properly. */ err_temp_to_dest: /* rename dest to temp */ - renamefile(vol, upath, temp, temp, adsp); + renamefile(vol, -1, upath, temp, temp, adsp); of_rename(vol, s_of, curdir, upath, curdir, temp); err_dest_to_src: /* rename source back to dest */ - renamefile(vol, p, upath, path->m_name, addp); + renamefile(vol, -1, p, upath, path->m_name, addp); of_rename(vol, d_of, sdir, spath, curdir, path->m_name); err_src_to_tmp: /* rename temp back to source */ - renamefile(vol, temp, p, spath, adsp); + renamefile(vol, -1, temp, p, spath, adsp); of_rename(vol, s_of, curdir, temp, sdir, spath); err_exchangefile: