]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/file.c
Add a parameter cachecnid for adouble v2, default off. Solution for
[netatalk.git] / etc / afpd / file.c
index 673ebbb655f274089596587dc621992b5f0e8f43..c97a4eff978c1bee4fc6e599fd59622edeee6fd2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: file.c,v 1.92.2.2.2.31.2.1 2004-10-20 19:48:38 didg Exp $
+ * $Id: file.c,v 1.92.2.2.2.31.2.10 2005-02-10 01:23:12 didg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
@@ -85,8 +85,11 @@ void *get_finderinfo(const char *mpath, struct adouble *adp, void *data)
     if (adp)
         ad_finder = ad_entry(adp, ADEID_FINDERI);
 
-    if ((adp != NULL) && (ad_finder != NULL)) {
+    if ((ad_finder != NULL)) {
         memcpy(data, ad_finder, 32);
+        /* default type ? */
+        if (!memcmp(ad_finder, ufinderi, 8))
+            chk_ext = 1;
     }
     else {
         memcpy(data, ufinderi, 32);
@@ -168,39 +171,17 @@ char *set_name(const struct vol *vol, char *data, cnid_t pid, char *name, cnid_t
 
 /* -------------------------- */
 u_int32_t get_id(struct vol *vol, struct adouble *adp,  const struct stat *st,
-             const cnid_t did, const char *upath, const int len) 
+             const cnid_t did, char *upath, const int len) 
 {
 u_int32_t aint = 0;
 
 #if AD_VERSION > AD_VERSION1
-dev_t  dev;
-ino_t  ino;
-cnid_t a_did;
-char   stamp[ADEDLEN_PRIVSYN];
-    /* look in AD v2 header 
-     * note inode and device are opaques and not in network order
-    */
-    if (adp 
-           && sizeof(dev_t) == ad_getentrylen(adp, ADEID_PRIVDEV)
-           && sizeof(ino_t) == ad_getentrylen(adp,ADEID_PRIVINO)
-           && sizeof(stamp) == ad_getentrylen(adp,ADEID_PRIVSYN)
-           && sizeof(cnid_t) == ad_getentrylen(adp, ADEID_DID)
-           && sizeof(cnid_t) == ad_getentrylen(adp, ADEID_PRIVID)
-           
-    ) {
-        memcpy(&dev, ad_entry(adp, ADEID_PRIVDEV), sizeof(dev_t));
-        memcpy(&ino, ad_entry(adp, ADEID_PRIVINO), sizeof(ino_t));
-        memcpy(stamp, ad_entry(adp, ADEID_PRIVSYN), sizeof(stamp));
-        memcpy(&a_did, ad_entry(adp, ADEID_DID), sizeof(cnid_t));
-
-        if (  ( (vol->v_flags & AFPVOL_NODEV) || dev == st->st_dev)
-              && ino == st->st_ino && a_did == did 
-              && !memcmp(vol->v_stamp, stamp, sizeof(stamp))) { 
-           memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));
-           return aint;
-        }
+
+    if ((aint = ad_getid(adp, st->st_dev, st->st_ino, did, vol->v_stamp))) {
+       return aint;
     }
 #endif
+
     if (vol->v_cdb != NULL) {
            aint = cnid_add(vol->v_cdb, st, did, upath, len, aint);
            /* Throw errors if cnid_add fails. */
@@ -225,7 +206,7 @@ char   stamp[ADEDLEN_PRIVSYN];
             /* update the ressource fork
              * for a folder adp is always null
              */
-            if (ad_setid(adp,(vol->v_flags & AFPVOL_NODEV)?0:st->st_dev, st->st_ino, aint, did, vol->v_stamp)) {
+            if (ad_setid(adp, st->st_dev, st->st_ino, aint, did, vol->v_stamp)) {
                 ad_flush(adp, ADFLAGS_HF);
             }
         }
@@ -525,7 +506,7 @@ int getfilparams(struct vol *vol,
            attrbits = ((of->of_ad->ad_df.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
            attrbits |= ((of->of_ad->ad_hf.adf_refcount > of->of_ad->ad_df.adf_refcount)? ATTRBIT_ROPEN : 0);
         } else {
-            ad_init(&ad, vol->v_adouble);
+            ad_init(&ad, vol->v_adouble, vol->v_ad_options);
             adp = &ad;
         }
 
@@ -631,7 +612,7 @@ int         ibuflen, *rbuflen;
     if ((of = of_findname(s_path))) {
         adp = of->of_ad;
     } else {
-        ad_init(&ad, vol->v_adouble);
+        ad_init(&ad, vol->v_adouble, vol->v_ad_options);
         adp = &ad;
     }
     if ( creatf) {
@@ -775,11 +756,10 @@ int setfilparams(struct vol *vol,
                  struct path *path, u_int16_t f_bitmap, char *buf )
 {
     struct adouble     ad, *adp;
-    struct ofork        *of;
     struct extmap      *em;
     int                        bit, isad = 1, err = AFP_OK;
     char                *upath;
-    u_char              achar, *fdType, xyy[4];
+    u_char              achar, *fdType, xyy[4]; /* uninitialized, OK 310105 */
     u_int16_t          ashort, bshort;
     u_int32_t          aint;
     u_int32_t          upriv;
@@ -802,12 +782,8 @@ int setfilparams(struct vol *vol,
 #endif /* DEBUG */
 
     upath = path->u_name;
-    if ((of = of_findname(path))) {
-        adp = of->of_ad;
-    } else {
-        ad_init(&ad, vol->v_adouble);
-        adp = &ad;
-    }
+    adp = of_ad(vol, path, &ad);
+    
 
     if (!vol_unix_priv(vol) && check_access(upath, OPENACC_WR ) < 0) {
         return AFPERR_ACCESS;
@@ -957,7 +933,6 @@ int setfilparams(struct vol *vol,
             )) {
                 memcpy(finder_buf, ufinderi, 8 );
             }
-
             memcpy(ad_entry( adp, ADEID_FINDERI ), finder_buf, 32 );
             break;
         case FILPBIT_UNIXPR :
@@ -1049,7 +1024,7 @@ struct adouble    *adp;
                /* FIXME  warning in syslog so admin'd know there's a conflict ?*/
                return AFPERR_OLOCK; /* little lie */
            }
-            if (AFP_OK != ( rc = copyfile(vol, vol, src, dst, newname )) ) {
+            if (AFP_OK != ( rc = copyfile(vol, vol, src, dst, newname, NULL )) ) {
                 /* on error copyfile delete dest */
                 return( rc );
             }
@@ -1079,7 +1054,7 @@ struct adouble    *adp;
              * create .AppleDouble if the file is already opened, so we
              * use a diff one, it's not a pb,ie it's not the same file, yet.
              */
-            ad_init(&ad, vol->v_adouble); 
+            ad_init(&ad, vol->v_adouble, vol->v_ad_options); 
             if (!ad_open(dst, ADFLAGS_HF, O_RDWR | O_CREAT, 0666, &ad)) {
                ad_close(&ad, ADFLAGS_HF);
                if (!unix_rename( adsrc, vol->ad_path( dst, 0 )) ) 
@@ -1125,7 +1100,21 @@ struct adouble    *adp;
     return( AFP_OK );
 }
 
-int copy_path_name(char *newname, char *ibuf)
+/* ---------------- 
+   convert a Mac long name to an utf8 name,
+*/
+size_t mtoUTF8(const struct vol *vol, const char *src, size_t srclen, char *dest, size_t destlen)
+{
+size_t    outlen;
+
+    if ((size_t)-1 == (outlen = convert_string ( vol->v_maccharset, CH_UTF8_MAC, src, srclen, dest, destlen)) ) {
+       return -1;
+    }
+    return outlen;
+}
+
+/* ---------------- */
+int copy_path_name(const struct vol *vol, char *newname, char *ibuf)
 {
 char        type = *ibuf;
 size_t      plen = 0;
@@ -1139,8 +1128,16 @@ u_int32_t   hint;
     switch (type) {
     case 2:
         if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
-            strncpy( newname, ibuf, plen );
-            newname[ plen ] = '\0';
+            if (afp_version >= 30) {
+                /* convert it to UTF8 
+                */
+                if ((plen = mtoUTF8(vol, ibuf, plen, newname, AFPOBJ_TMPSIZ)) == -1)
+                   return -1;
+            }
+            else {
+                strncpy( newname, ibuf, plen );
+                newname[ plen ] = '\0';
+            }
             if (strlen(newname) != plen) {
                 /* there's \0 in newname, e.g. it's a pathname not
                  * only a filename. 
@@ -1187,6 +1184,9 @@ int               ibuflen, *rbuflen;
     int         err, retvalue = AFP_OK;
     u_int16_t  svid, dvid;
 
+    struct adouble ad, *adp;
+    int denyreadset;
+    
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "begin afp_copyfile:");
 #endif /* DEBUG */
@@ -1224,11 +1224,20 @@ int             ibuflen, *rbuflen;
      *      and locks need to stay coherent. as a result,
      *      we just balk if the file is opened already. */
 
-    newname = obj->newtmp;
-    strcpy( newname, s_path->m_name );
+    adp = of_ad(s_vol, s_path, &ad);
 
-    if (of_findname(s_path))
+    if (ad_open(s_path->u_name , ADFLAGS_DF |ADFLAGS_HF | ADFLAGS_NOHF, O_RDONLY, 0, adp) < 0) {
+        return AFPERR_DENYCONF;
+    }
+    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;
+    }
+
+    newname = obj->newtmp;
+    strcpy( newname, s_path->m_name );
 
     p = ctoupath( s_vol, curdir, newname );
     if (!p) {
@@ -1257,7 +1266,7 @@ int               ibuflen, *rbuflen;
     }
 
     /* one of the handful of places that knows about the path type */
-    if (copy_path_name(newname, ibuf) < 0) {
+    if (copy_path_name(d_vol, newname, ibuf) < 0) {
         return( AFPERR_PARAM );
     }
     /* newname is always only a filename so curdir *is* its
@@ -1266,7 +1275,7 @@ int               ibuflen, *rbuflen;
     if (NULL == (upath = mtoupath(d_vol, newname, curdir->d_did, utf8_encoding()))) {
         return( AFPERR_PARAM );
     }
-    if ( (err = copyfile(s_vol, d_vol, p, upath , newname)) < 0 ) {
+    if ( (err = copyfile(s_vol, d_vol, p, upath , newname, adp)) < 0 ) {
         return err;
     }
     curdir->offcnt++;
@@ -1370,9 +1379,10 @@ static int copy_fd(int dfd, int sfd)
  * if newname is NULL (from directory.c) we don't want to copy ressource fork.
  * because we are doing it elsewhere.
  */
-int copyfile(s_vol, d_vol, src, dst, newname )
+int copyfile(s_vol, d_vol, src, dst, newname, adp )
 const struct vol *s_vol, *d_vol;
 char   *src, *dst, *newname;
+struct adouble *adp;
 {
     struct adouble     ads, add;
     int                        err = 0;
@@ -1385,42 +1395,45 @@ char    *src, *dst, *newname;
     LOG(log_info, logtype_afpd, "begin copyfile:");
 #endif /* DEBUG */
 
-    ad_init(&ads, s_vol->v_adouble); 
-    ad_init(&add, d_vol->v_adouble);
+    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, &ads) < 0) {
+    if (ad_open(src , adflags | ADFLAGS_NOHF, O_RDONLY, 0, adp) < 0) {
         ret_err = errno;
         goto done;
     }
 
-    if (ad_hfileno(&ads) == -1) {
+    if (ad_hfileno(adp) == -1) {
         /* no resource fork, don't create one for dst file */
         adflags &= ~ADFLAGS_HF;
     }
 
     if (ad_open(dst , adflags | noadouble, O_RDWR|O_CREAT|O_EXCL, 0666, &add) < 0) {
         ret_err = errno;
-        ad_close( &ads, adflags );
+        ad_close( adp, adflags );
         if (EEXIST != ret_err) {
             deletefile(d_vol, dst, 0);
             goto done;
         }
         return AFPERR_EXIST;
     }
-    if (ad_hfileno(&ads) == -1 || 0 == (err = copy_fd(ad_hfileno(&add), ad_hfileno(&ads)))){
+    if (ad_hfileno(adp) == -1 || 0 == (err = copy_fd(ad_hfileno(&add), ad_hfileno(adp)))){
         /* copy the data fork */
-       err = copy_fd(ad_dfileno(&add), ad_dfileno(&ads));
+       err = copy_fd(ad_dfileno(&add), ad_dfileno(adp));
     }
 
     /* Now, reopen destination file */
     if (err < 0) {
        ret_err = errno;
     }
-    ad_close( &ads, adflags );
+    ad_close( adp, adflags );
 
     if (ad_close( &add, adflags ) <0) {
         deletefile(d_vol, dst, 0);
@@ -1428,7 +1441,7 @@ char      *src, *dst, *newname;
         goto done;
     } 
     else {
-       ad_init(&add, d_vol->v_adouble);
+       ad_init(&add, d_vol->v_adouble, d_vol->v_ad_options);
        if (ad_open(dst , adflags | noadouble, O_RDWR, 0666, &add) < 0) {
            ret_err = errno;
        }
@@ -1504,8 +1517,8 @@ int         checkAttrib;
 
     /* try to open both forks at once */
     adflags = ADFLAGS_DF|ADFLAGS_HF;
+    ad_init(&ad, vol->v_adouble, vol->v_ad_options);  /* OK */
     while(1) {
-        ad_init(&ad, vol->v_adouble);  /* OK */
         if ( ad_open( file, adflags, O_RDONLY, 0, &ad ) < 0 ) {
             switch (errno) {
             case ENOENT:
@@ -1543,7 +1556,7 @@ int         checkAttrib;
         }
         else if (!adp) {
             /* was EACCESS error try to get only metadata */
-            ad_init(&ad, vol->v_adouble);  /* OK */
+            ad_init(&ad, vol->v_adouble, vol->v_ad_options);  /* OK */
             if ( ad_metadata( file , 0, &ad) == 0 ) {
                 ad_getattr(&ad, &bshort);
                 ad_close( &ad, ADFLAGS_HF );
@@ -1705,23 +1718,18 @@ reenumerate_id(const struct vol *vol, char *name, cnid_t did)
 
 #if AD_VERSION > AD_VERSION1
         if (aint != CNID_INVALID && !S_ISDIR(path.st.st_mode)) {
-            struct ofork    *of;
             struct adouble  ad, *adp;
 
             path.st_errno = 0;
             path.st_valid = 1;
             path.u_name = de->d_name;
             
-            if (!(of = of_findname(&path))) {
-                ad_init(&ad, vol->v_adouble);
-                adp = &ad;
-            } else
-                adp = of->of_ad;
+            adp = of_ad(vol, &path, &ad);
             
             if ( ad_open( de->d_name, ADFLAGS_HF, O_RDWR, 0, adp ) < 0 ) {
                 continue;
             }
-            if (ad_setid(adp,(vol->v_flags & AFPVOL_NODEV)?0:path.st.st_dev, path.st.st_ino, aint, did, vol->v_stamp)) {
+            if (ad_setid(adp, path.st.st_dev, path.st.st_ino, aint, did, vol->v_stamp)) {
                ad_flush(adp, ADFLAGS_HF);
             }
             ad_close(adp, ADFLAGS_HF);
@@ -2057,7 +2065,7 @@ int               ibuflen, *rbuflen;
         return AFPERR_PARAM ;
     }
     
-    ad_init(&ads, vol->v_adouble);
+    ad_init(&ads, vol->v_adouble, vol->v_ad_options);
     if (!(adsp = find_adouble( path, &s_of, &ads))) {
         return afp_errno;
     }
@@ -2090,7 +2098,7 @@ int               ibuflen, *rbuflen;
         goto err_exchangefile;
     }
 
-    ad_init(&add, vol->v_adouble);
+    ad_init(&add, vol->v_adouble, vol->v_ad_options);
     if (!(addp = find_adouble( path, &d_of, &add))) {
         err = afp_errno;
         goto err_exchangefile;
@@ -2121,7 +2129,7 @@ int               ibuflen, *rbuflen;
     }
     
     if (crossdev) {
-        /* we need to close fork for copy, both s_of and d_of are null */
+        /* FIXME we need to close fork for copy, both s_of and d_of are null */
        ad_close(adsp, ADFLAGS_HF);
        ad_close(addp, ADFLAGS_HF);
     }
@@ -2168,10 +2176,15 @@ int             ibuflen, *rbuflen;
     }
     
     /* here we need to reopen if crossdev */
-    if (sid)
-        ad_setid(addp,(vol->v_flags & AFPVOL_NODEV)?0:destst.st_dev, destst.st_ino,  sid, sdir->d_did, vol->v_stamp);
-    if (did)
-        ad_setid(adsp,(vol->v_flags & AFPVOL_NODEV)?0:srcst.st_dev, srcst.st_ino,  did, curdir->d_did, vol->v_stamp);
+    if (sid && ad_setid(addp, destst.st_dev, destst.st_ino,  sid, sdir->d_did, vol->v_stamp)) 
+    {
+       ad_flush( addp, ADFLAGS_HF );
+    }
+        
+    if (did && ad_setid(adsp, srcst.st_dev, srcst.st_ino,  did, curdir->d_did, vol->v_stamp)) 
+    {
+       ad_flush( adsp, ADFLAGS_HF );
+    }
 
     /* change perms, src gets dest perm and vice versa */
 
@@ -2236,11 +2249,9 @@ err_src_to_tmp:
 
 err_exchangefile:
     if ( !s_of && adsp && ad_hfileno(adsp) != -1 ) {
-       ad_flush( adsp, ADFLAGS_HF );
        ad_close(adsp, ADFLAGS_HF);
     }
     if ( !d_of && addp && ad_hfileno(addp) != -1 ) {
-       ad_flush( addp, ADFLAGS_HF );
        ad_close(addp, ADFLAGS_HF);
     }