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