]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_attr.c
Cleanup
[netatalk.git] / libatalk / adouble / ad_attr.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif /* HAVE_CONFIG_H */
4
5 #include <string.h>
6 #include <atalk/adouble.h>
7
8 #define FILEIOFF_ATTR 14
9 #define AFPFILEIOFF_ATTR 2
10
11 /* 
12    Note:
13    the "shared" and "invisible" attributes are opaque and stored and
14    retrieved from the FinderFlags. This fixes Bug #2802236:
15    <https://sourceforge.net/tracker/?func=detail&aid=2802236&group_id=8642&atid=108642>
16  */
17 int ad_getattr(const struct adouble *ad, u_int16_t *attr)
18 {
19     u_int16_t fflags;
20     *attr = 0;
21
22     if (ad->ad_version == AD_VERSION2) {
23         if (ad_getentryoff(ad, ADEID_AFPFILEI)) {
24             memcpy(attr, ad_entry(ad, ADEID_AFPFILEI) + AFPFILEIOFF_ATTR, 2);
25
26             /* Now get opaque flags from FinderInfo */
27             memcpy(&fflags, ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, 2);
28             if (fflags & htons(FINDERINFO_INVISIBLE))
29                 *attr |= htons(ATTRBIT_INVISIBLE);
30             else
31                 *attr &= htons(~ATTRBIT_INVISIBLE);
32  /*
33    This one is tricky, I actually got it wrong the first time:
34    for directories bit 1<<1 is ATTRBIT_EXPFLDR and is NOT opaque !
35  */
36             if ( ! (ad->ad_adflags & ADFLAGS_DIR)) {
37                 if (fflags & htons(FINDERINFO_ISHARED))
38                     *attr |= htons(ATTRBIT_MULTIUSER);
39                 else
40                     *attr &= htons(~ATTRBIT_MULTIUSER);
41             }
42         }
43     }
44
45     *attr |= htons(ad->ad_open_forks);
46
47     return 0;
48 }
49
50 /* ----------------- */
51 int ad_setattr(const struct adouble *ad, const u_int16_t attribute)
52 {
53     u_int16_t *fflags;
54
55     /* we don't save open forks indicator */
56     u_int16_t attr = attribute & ~htons(ATTRBIT_DOPEN | ATTRBIT_ROPEN);
57
58     /* Proactively (10.4 does indeed try to set ATTRBIT_MULTIUSER (=ATTRBIT_EXPFLDR)
59        for dirs with SetFile -a M <dir> ) disable all flags not defined for dirs. */
60     if (ad->ad_adflags & ADFLAGS_DIR)
61         attr &= ~(ATTRBIT_MULTIUSER | ATTRBIT_NOWRITE | ATTRBIT_NOCOPY);
62
63     if (ad->ad_version == AD_VERSION2) {
64         if (ad_getentryoff(ad, ADEID_AFPFILEI)) {
65             memcpy(ad_entry(ad, ADEID_AFPFILEI) + AFPFILEIOFF_ATTR, &attr, sizeof(attr));
66             
67             /* Now set opaque flags in FinderInfo too */
68             fflags = (u_int16_t *)ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF;
69             if (attr & htons(ATTRBIT_INVISIBLE))
70                 *fflags |= htons(FINDERINFO_INVISIBLE);
71             else
72                 *fflags &= htons(~FINDERINFO_INVISIBLE);
73
74             /* See above comment in ad_getattr() */
75             if (attr & htons(ATTRBIT_MULTIUSER)) {
76                 if ( ! (ad->ad_adflags & ADFLAGS_DIR) )
77                     *fflags |= htons(FINDERINFO_ISHARED);
78             } else
79                     *fflags &= htons(~FINDERINFO_ISHARED);
80         }
81     }
82
83     return 0;
84 }
85
86 /* --------------
87  * save file/folder ID in AppleDoubleV2 netatalk private parameters
88  * return 1 if resource fork has been modified
89  */
90 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)
91 {
92     if ((adp->ad_flags == AD_VERSION2) && (adp->ad_options & ADVOL_CACHE)) {
93
94         /* ad_getid depends on this to detect presence of ALL entries */
95         ad_setentrylen( adp, ADEID_PRIVID, sizeof(id));
96         memcpy(ad_entry( adp, ADEID_PRIVID ), &id, sizeof(id));
97
98         ad_setentrylen( adp, ADEID_PRIVDEV, sizeof(dev_t));
99         if ((adp->ad_options & ADVOL_NODEV)) {
100             memset(ad_entry( adp, ADEID_PRIVDEV ), 0, sizeof(dev_t));
101         } else {
102             memcpy(ad_entry( adp, ADEID_PRIVDEV ), &dev, sizeof(dev_t));
103         }
104
105         ad_setentrylen( adp, ADEID_PRIVINO, sizeof(ino_t));
106         memcpy(ad_entry( adp, ADEID_PRIVINO ), &ino, sizeof(ino_t));
107
108         ad_setentrylen( adp, ADEID_DID, sizeof(did));
109         memcpy(ad_entry( adp, ADEID_DID ), &did, sizeof(did));
110
111         ad_setentrylen( adp, ADEID_PRIVSYN, ADEDLEN_PRIVSYN);
112         memcpy(ad_entry( adp, ADEID_PRIVSYN ), stamp, ADEDLEN_PRIVSYN);
113         return 1;
114     }
115     return 0;
116 }
117
118 /* ----------------------------- */
119 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)
120 {
121     u_int32_t aint = 0;
122     dev_t  dev;
123     ino_t  ino;
124     cnid_t a_did;
125     char   temp[ADEDLEN_PRIVSYN];
126
127     /* look in AD v2 header
128      * note inode and device are opaques and not in network order
129      * only use the ID if adouble is writable for us.
130      */
131     if (adp
132         && (adp->ad_options & ADVOL_CACHE)
133         && (adp->ad_md->adf_flags & O_RDWR )
134         && (sizeof(dev_t) == ad_getentrylen(adp, ADEID_PRIVDEV)) /* One check to ensure ALL values are there */
135         ) {
136         memcpy(&dev, ad_entry(adp, ADEID_PRIVDEV), sizeof(dev_t));
137         memcpy(&ino, ad_entry(adp, ADEID_PRIVINO), sizeof(ino_t));
138         memcpy(temp, ad_entry(adp, ADEID_PRIVSYN), sizeof(temp));
139         memcpy(&a_did, ad_entry(adp, ADEID_DID), sizeof(cnid_t));
140
141         if ( ((adp->ad_options & ADVOL_NODEV) || dev == st_dev)
142              && ino == st_ino
143              && (!did || a_did == did)
144              && (memcmp(stamp, temp, sizeof(temp)) == 0) ) {
145             memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));
146             return aint;
147         }
148     }
149     return 0;
150 }
151
152 /* ----------------------------- */
153 u_int32_t ad_forcegetid (struct adouble *adp)
154 {
155     u_int32_t aint = 0;
156
157     if (adp && (adp->ad_options & ADVOL_CACHE)) {
158         memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));
159         return aint;
160     }
161     return 0;
162 }
163
164 /* -----------------
165  * set resource fork filename attribute.
166  */
167 int ad_setname(struct adouble *ad, const char *path)
168 {
169     if (path && ad_getentryoff(ad, ADEID_NAME)) {
170         ad_setentrylen( ad, ADEID_NAME, strlen( path ));
171         memcpy(ad_entry( ad, ADEID_NAME ), path, ad_getentrylen( ad, ADEID_NAME ));
172         return 1;
173     }
174     return 0;
175 }