]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_attr.c
Check if entries are present before using them
[netatalk.git] / libatalk / adouble / ad_attr.c
1 /*
2  * $Id: ad_attr.c,v 1.13 2009-12-23 07:21:08 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_getid depends on this to detect presence of ALL entries */
120         ad_setentrylen( adp, ADEID_PRIVID, sizeof(id));
121         memcpy(ad_entry( adp, ADEID_PRIVID ), &id, sizeof(id));
122
123         ad_setentrylen( adp, ADEID_PRIVDEV, sizeof(dev_t));
124         if ((adp->ad_options & ADVOL_NODEV)) {
125             memset(ad_entry( adp, ADEID_PRIVDEV ), 0, sizeof(dev_t));
126         } else {
127             memcpy(ad_entry( adp, ADEID_PRIVDEV ), &dev, sizeof(dev_t));
128         }
129
130         ad_setentrylen( adp, ADEID_PRIVINO, sizeof(ino_t));
131         memcpy(ad_entry( adp, ADEID_PRIVINO ), &ino, sizeof(ino_t));
132
133         ad_setentrylen( adp, ADEID_DID, sizeof(did));
134         memcpy(ad_entry( adp, ADEID_DID ), &did, sizeof(did));
135
136         ad_setentrylen( adp, ADEID_PRIVSYN, ADEDLEN_PRIVSYN);
137         memcpy(ad_entry( adp, ADEID_PRIVSYN ), stamp, ADEDLEN_PRIVSYN);
138         return 1;
139     }
140     return 0;
141 }
142
143 /* ----------------------------- */
144 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)
145 {
146     u_int32_t aint = 0;
147     dev_t  dev;
148     ino_t  ino;
149     cnid_t a_did;
150     char   temp[ADEDLEN_PRIVSYN];
151
152     /* look in AD v2 header
153      * note inode and device are opaques and not in network order
154      * only use the ID if adouble is writable for us.
155      */
156     if (adp
157         && ( adp->ad_options & ADVOL_CACHE)
158         && (adp->ad_md->adf_flags & O_RDWR )
159         && (sizeof(dev_t) == ad_getentrylen(adp, ADEID_PRIVID)) /* One check to ensure ALL values are there */
160         ) {
161         memcpy(&dev, ad_entry(adp, ADEID_PRIVDEV), sizeof(dev_t));
162         memcpy(&ino, ad_entry(adp, ADEID_PRIVINO), sizeof(ino_t));
163         memcpy(temp, ad_entry(adp, ADEID_PRIVSYN), sizeof(temp));
164         memcpy(&a_did, ad_entry(adp, ADEID_DID), sizeof(cnid_t));
165
166         if ( ((adp->ad_options & ADVOL_NODEV) || dev == st_dev)
167              && ino == st_ino
168              && (!did || a_did == did)
169              && (memcmp(stamp, temp, sizeof(temp)) == 0) ) {
170             memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));
171             return aint;
172         }
173     }
174     return 0;
175 }
176
177 /* ----------------------------- */
178 u_int32_t ad_forcegetid (struct adouble *adp)
179 {
180     u_int32_t aint = 0;
181
182     if (adp && (adp->ad_options & ADVOL_CACHE)) {
183         memcpy(&aint, ad_entry(adp, ADEID_PRIVID), sizeof(aint));
184         return aint;
185     }
186     return 0;
187 }
188 #endif
189
190 /* -----------------
191  * set resource fork filename attribute.
192  */
193 int ad_setname(struct adouble *ad, const char *path)
194 {
195     if (ad_getentryoff(ad, ADEID_NAME)) {
196         ad_setentrylen( ad, ADEID_NAME, strlen( path ));
197         memcpy(ad_entry( ad, ADEID_NAME ), path, ad_getentrylen( ad, ADEID_NAME ));
198         return 1;
199     }
200     return 0;
201 }