]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_attr.c
Fix open flags
[netatalk.git] / libatalk / adouble / ad_attr.c
index 37c84e03f685c08cb39d7ed6076c4e0c1edcd82f..3b550bd0f08cb9edc73d280b300f9aea56bbc46c 100644 (file)
@@ -5,6 +5,7 @@
 #include <string.h>
 #include <arpa/inet.h>
 #include <atalk/adouble.h>
+#include <atalk/logger.h>
 
 #define FILEIOFF_ATTR 14
 #define AFPFILEIOFF_ATTR 2
    retrieved from the FinderFlags. This fixes Bug #2802236:
    <https://sourceforge.net/tracker/?func=detail&aid=2802236&group_id=8642&atid=108642>
  */
-int ad_getattr(const struct adouble *ad, u_int16_t *attr)
+int ad_getattr(const struct adouble *ad, uint16_t *attr)
 {
-    u_int16_t fflags;
+    uint16_t fflags;
     *attr = 0;
 
-    if (ad->ad_version == AD_VERSION2) {
-        if (ad_getentryoff(ad, ADEID_AFPFILEI)) {
-            memcpy(attr, ad_entry(ad, ADEID_AFPFILEI) + AFPFILEIOFF_ATTR, 2);
-
-            /* Now get opaque flags from FinderInfo */
-            memcpy(&fflags, ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, 2);
-            if (fflags & htons(FINDERINFO_INVISIBLE))
-                *attr |= htons(ATTRBIT_INVISIBLE);
+    if (ad_getentryoff(ad, ADEID_AFPFILEI)) {
+        memcpy(attr, ad_entry(ad, ADEID_AFPFILEI) + AFPFILEIOFF_ATTR, 2);
+
+        /* Now get opaque flags from FinderInfo */
+        memcpy(&fflags, ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, 2);
+        if (fflags & htons(FINDERINFO_INVISIBLE))
+            *attr |= htons(ATTRBIT_INVISIBLE);
+        else
+            *attr &= htons(~ATTRBIT_INVISIBLE);
+        /*
+         * This one is tricky, I actually got it wrong the first time:
+         * for directories bit 1<<1 is ATTRBIT_EXPFLDR and is NOT opaque !
+         */
+        if ( ! (ad->ad_adflags & ADFLAGS_DIR)) {
+            if (fflags & htons(FINDERINFO_ISHARED))
+                *attr |= htons(ATTRBIT_MULTIUSER);
             else
-                *attr &= htons(~ATTRBIT_INVISIBLE);
- /*
-   This one is tricky, I actually got it wrong the first time:
-   for directories bit 1<<1 is ATTRBIT_EXPFLDR and is NOT opaque !
- */
-            if ( ! (ad->ad_adflags & ADFLAGS_DIR)) {
-                if (fflags & htons(FINDERINFO_ISHARED))
-                    *attr |= htons(ATTRBIT_MULTIUSER);
-                else
-                    *attr &= htons(~ATTRBIT_MULTIUSER);
-            }
+                *attr &= htons(~ATTRBIT_MULTIUSER);
         }
     }
 
@@ -49,38 +48,36 @@ int ad_getattr(const struct adouble *ad, u_int16_t *attr)
 }
 
 /* ----------------- */
-int ad_setattr(const struct adouble *ad, const u_int16_t attribute)
+int ad_setattr(const struct adouble *ad, const uint16_t attribute)
 {
     uint16_t fflags;
 
     /* we don't save open forks indicator */
-    u_int16_t attr = attribute & ~htons(ATTRBIT_DOPEN | ATTRBIT_ROPEN);
+    uint16_t attr = attribute & ~htons(ATTRBIT_DOPEN | ATTRBIT_ROPEN);
 
     /* Proactively (10.4 does indeed try to set ATTRBIT_MULTIUSER (=ATTRBIT_EXPFLDR)
        for dirs with SetFile -a M <dir> ) disable all flags not defined for dirs. */
     if (ad->ad_adflags & ADFLAGS_DIR)
         attr &= ~(ATTRBIT_MULTIUSER | ATTRBIT_NOWRITE | ATTRBIT_NOCOPY);
 
-    if (ad->ad_version == AD_VERSION2) {
-        if (ad_getentryoff(ad, ADEID_AFPFILEI) && ad_getentryoff(ad, ADEID_FINDERI)) {
-            memcpy(ad_entry(ad, ADEID_AFPFILEI) + AFPFILEIOFF_ATTR, &attr, sizeof(attr));
+    if (ad_getentryoff(ad, ADEID_AFPFILEI) && ad_getentryoff(ad, ADEID_FINDERI)) {
+        memcpy(ad_entry(ad, ADEID_AFPFILEI) + AFPFILEIOFF_ATTR, &attr, sizeof(attr));
             
-            /* Now set opaque flags in FinderInfo too */
-            memcpy(&fflags, ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, 2);
-            if (attr & htons(ATTRBIT_INVISIBLE))
-                fflags |= htons(FINDERINFO_INVISIBLE);
-            else
-                fflags &= htons(~FINDERINFO_INVISIBLE);
-
-            /* See above comment in ad_getattr() */
-            if (attr & htons(ATTRBIT_MULTIUSER)) {
-                if ( ! (ad->ad_adflags & ADFLAGS_DIR) )
-                    fflags |= htons(FINDERINFO_ISHARED);
-            } else
-                    fflags &= htons(~FINDERINFO_ISHARED);
-
-            memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &fflags, 2);
-        }
+        /* Now set opaque flags in FinderInfo too */
+        memcpy(&fflags, ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, 2);
+        if (attr & htons(ATTRBIT_INVISIBLE))
+            fflags |= htons(FINDERINFO_INVISIBLE);
+        else
+            fflags &= htons(~FINDERINFO_INVISIBLE);
+
+        /* See above comment in ad_getattr() */
+        if (attr & htons(ATTRBIT_MULTIUSER)) {
+            if ( ! (ad->ad_adflags & ADFLAGS_DIR) )
+                fflags |= htons(FINDERINFO_ISHARED);
+        } else
+            fflags &= htons(~FINDERINFO_ISHARED);
+
+        memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &fflags, 2);
     }
 
     return 0;
@@ -90,14 +87,18 @@ int ad_setattr(const struct adouble *ad, const u_int16_t attribute)
  * save file/folder ID in AppleDoubleV2 netatalk private parameters
  * return 1 if resource fork has been modified
  */
-int ad_setid (struct adouble *adp, const dev_t dev, const ino_t ino , const u_int32_t id, const cnid_t did, const void *stamp)
+int ad_setid (struct adouble *adp, const dev_t dev, const ino_t ino , const uint32_t id, const cnid_t did, const void *stamp)
 {
-    if ((adp->ad_flags == AD_VERSION2) && (adp->ad_options & ADVOL_CACHE)) {
+    uint32_t tmp;
 
-        /* ad_getid depends on this to detect presence of ALL entries */
-        ad_setentrylen( adp, ADEID_PRIVID, sizeof(id));
-        memcpy(ad_entry( adp, ADEID_PRIVID ), &id, sizeof(id));
+    LOG(log_maxdebug, logtype_afpd, "ad_setid(\"%s\"): CNID: %" PRIu32 "", adp->ad_m_name, ntohl(id));
+    ad_setentrylen( adp, ADEID_PRIVID, sizeof(id));
+    tmp = id;
+    if (adp->ad_vers == AD_VERSION_EA)
+        tmp = htonl(tmp);
+    memcpy(ad_entry( adp, ADEID_PRIVID ), &tmp, sizeof(tmp));
 
+    if (adp->ad_vers == AD_VERSION2) {
         ad_setentrylen( adp, ADEID_PRIVDEV, sizeof(dev_t));
         if ((adp->ad_options & ADVOL_NODEV)) {
             memset(ad_entry( adp, ADEID_PRIVDEV ), 0, sizeof(dev_t));
@@ -113,49 +114,47 @@ int ad_setid (struct adouble *adp, const dev_t dev, const ino_t ino , const u_in
 
         ad_setentrylen( adp, ADEID_PRIVSYN, ADEDLEN_PRIVSYN);
         memcpy(ad_entry( adp, ADEID_PRIVSYN ), stamp, ADEDLEN_PRIVSYN);
-        return 1;
     }
-    return 0;
+    return 1;
 }
 
 /* ----------------------------- */
-u_int32_t ad_getid (struct adouble *adp, const dev_t st_dev, const ino_t st_ino , const cnid_t did, const void *stamp)
+uint32_t ad_getid (struct adouble *adp, const dev_t st_dev, const ino_t st_ino , const cnid_t did, const void *stamp)
 {
-    u_int32_t aint = 0;
+    uint32_t aint = 0;
     dev_t  dev;
     ino_t  ino;
     cnid_t a_did;
     char   temp[ADEDLEN_PRIVSYN];
 
-    /* look in AD v2 header
-     * note inode and device are opaques and not in network order
-     * only use the ID if adouble is writable for us.
-     */
-    if (adp
-        && (adp->ad_options & ADVOL_CACHE)
-        && (adp->ad_md->adf_flags & O_RDWR )
-        && (sizeof(dev_t) == ad_getentrylen(adp, ADEID_PRIVDEV)) /* One check to ensure ALL values are there */
-        ) {
-        memcpy(&dev, ad_entry(adp, ADEID_PRIVDEV), sizeof(dev_t));
-        memcpy(&ino, ad_entry(adp, ADEID_PRIVINO), sizeof(ino_t));
-        memcpy(temp, ad_entry(adp, ADEID_PRIVSYN), sizeof(temp));
-        memcpy(&a_did, ad_entry(adp, ADEID_DID), sizeof(cnid_t));
-
-        if ( ((adp->ad_options & ADVOL_NODEV) || dev == st_dev)
-             && ino == st_ino
-             && (!did || a_did == did)
-             && (memcmp(stamp, temp, sizeof(temp)) == 0) ) {
+    if (adp) {
+        if ((adp->ad_vers == AD_VERSION2)
+            && (sizeof(dev_t) == ad_getentrylen(adp, ADEID_PRIVDEV))) {
+            memcpy(&dev, ad_entry(adp, ADEID_PRIVDEV), sizeof(dev_t));
+            memcpy(&ino, ad_entry(adp, ADEID_PRIVINO), sizeof(ino_t));
+            memcpy(temp, ad_entry(adp, ADEID_PRIVSYN), sizeof(temp));
+            memcpy(&a_did, ad_entry(adp, ADEID_DID), sizeof(cnid_t));
+
+            if ( ((adp->ad_options & ADVOL_NODEV) || dev == st_dev)
+                 && ino == st_ino
+                 && (!did || a_did == did)
+                 && (memcmp(stamp, temp, sizeof(temp)) == 0) ) {
+                memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));
+                return aint;
+            }
+        } else {
             memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));
-            return aint;
+            LOG(log_maxdebug, logtype_afpd, "ad_getid(\"%s\"): CNID: %" PRIu32 "", adp->ad_m_name, ntohl(aint));
+            return ntohl(aint);
         }
     }
     return 0;
 }
 
 /* ----------------------------- */
-u_int32_t ad_forcegetid (struct adouble *adp)
+uint32_t ad_forcegetid (struct adouble *adp)
 {
-    u_int32_t aint = 0;
+    uint32_t aint = 0;
 
     if (adp && (adp->ad_options & ADVOL_CACHE)) {
         memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));