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