]> arthur.barton.de Git - netatalk.git/blob - include/atalk/adouble.h
Merge master
[netatalk.git] / include / atalk / adouble.h
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby granted,
7  * provided that the above copyright notice appears in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation, and that the name of The University
10  * of Michigan not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. This software is supplied as is without expressed or
13  * implied warranties of any kind.
14  *
15  *  Research Systems Unix Group
16  *  The University of Michigan
17  *  c/o Mike Clark
18  *  535 W. William Street
19  *  Ann Arbor, Michigan
20  *  +1-313-763-0525
21  *  netatalk@itd.umich.edu
22  */
23
24 /*!
25  * @file
26  * @brief Part of Netatalk's AppleDouble implementatation
27  */
28
29 #ifndef _ATALK_ADOUBLE_H
30 #define _ATALK_ADOUBLE_H
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <sys/cdefs.h>
40 #include <fcntl.h>
41 #include <sys/mman.h>
42 #include <sys/time.h>
43
44 #include <netatalk/endian.h>
45
46 /* version info */
47 #define AD_VERSION2     0x00020000
48 #define AD_VERSION2_OSX 0x00020001
49 #define AD_VERSION2_EA  0x00020002
50 #define AD_VERSION      AD_VERSION2_EA
51
52 /*
53  * AppleDouble entry IDs.
54  */
55 #define ADEID_DFORK         1
56 #define ADEID_RFORK         2
57 #define ADEID_NAME          3
58 #define ADEID_COMMENT       4
59 #define ADEID_ICONBW        5
60 #define ADEID_ICONCOL       6
61 #define ADEID_FILEI         7  /* v1, replaced by: */
62 #define ADEID_FILEDATESI    8  /* this */
63 #define ADEID_FINDERI       9
64 #define ADEID_MACFILEI      10 /* we don't use this */
65 #define ADEID_PRODOSFILEI   11 /* we store prodos info here */
66 #define ADEID_MSDOSFILEI    12 /* we don't use this */
67 #define ADEID_SHORTNAME     13
68 #define ADEID_AFPFILEI      14 /* where the rest of the FILEI info goes */
69 #define ADEID_DID           15
70
71 /* netatalk private note fileid reused DID */
72 #define ADEID_PRIVDEV       16
73 #define ADEID_PRIVINO       17
74 #define ADEID_PRIVSYN       18 /* in synch with database */
75 #define ADEID_PRIVID        19
76 #define ADEID_MAX           19
77
78 /* These are the real ids for these entries, as stored in the adouble file */
79 #define AD_DEV              0x80444556
80 #define AD_INO              0x80494E4F
81 #define AD_SYN              0x8053594E
82 #define AD_ID               0x8053567E
83
84 /* magic */
85 #define AD_APPLESINGLE_MAGIC 0x00051600
86 #define AD_APPLEDOUBLE_MAGIC 0x00051607
87 #define AD_MAGIC             AD_APPLEDOUBLE_MAGIC
88
89 /* sizes of relevant entry bits */
90 #define ADEDLEN_MAGIC       4
91 #define ADEDLEN_VERSION     4
92 #define ADEDLEN_FILLER      16
93 #define ADEDLEN_NENTRIES    2
94 #define AD_HEADER_LEN       (ADEDLEN_MAGIC + ADEDLEN_VERSION + ADEDLEN_FILLER + ADEDLEN_NENTRIES)
95 #define AD_ENTRY_LEN        12  /* size of a single entry header */
96
97 /* field widths */
98 #define ADEDLEN_NAME            255
99 #define ADEDLEN_COMMENT         200
100 #define ADEDLEN_FILEI           16
101 #define ADEDLEN_FINDERI         32
102 #define ADEDLEN_FILEDATESI      16
103 #define ADEDLEN_SHORTNAME       12 /* length up to 8.3 */
104 #define ADEDLEN_AFPFILEI        4
105 #define ADEDLEN_MACFILEI        4
106 #define ADEDLEN_PRODOSFILEI     8
107 #define ADEDLEN_MSDOSFILEI      2
108 #define ADEDLEN_DID             4
109 #define ADEDLEN_PRIVDEV         8
110 #define ADEDLEN_PRIVINO         8
111 #define ADEDLEN_PRIVSYN         8
112 #define ADEDLEN_PRIVID          4
113
114 #define ADEID_NUM_V2            13
115 #define ADEID_NUM_OSX           2
116 #define ADEID_NUM_EA            5
117 #define AD_DATASZ2 (AD_HEADER_LEN + ADEDLEN_NAME + ADEDLEN_COMMENT + ADEDLEN_FILEI + \
118                     ADEDLEN_FINDERI + ADEDLEN_DID + ADEDLEN_AFPFILEI + ADEDLEN_SHORTNAME + \
119                     ADEDLEN_PRODOSFILEI + ADEDLEN_PRIVDEV + ADEDLEN_PRIVINO + \
120                     ADEDLEN_PRIVSYN + ADEDLEN_PRIVID + (ADEID_NUM_V2 * AD_ENTRY_LEN))
121 #if AD_DATASZ2 != 741
122 #error bad size for AD_DATASZ2
123 #endif
124
125 #define AD_DATASZ_EA (AD_HEADER_LEN + (ADEID_NUM_EA * AD_ENTRY_LEN) + ADEID_FINDERI + \
126                       ADEID_COMMENT + ADEID_FILEDATESI + ADEID_AFPFILEI + ADEID_PRIVID)
127 #if AD_DATASZ_EA != 342
128 #error bad size for AD_DATASZ_EA
129 #endif
130
131 #define AD_DATASZ_MAX   1024
132 #if AD_VERSION == AD_VERSION2
133 #define AD_DATASZ   AD_DATASZ2
134 #elif AD_VERSION == AD_VERSION2_EA
135 #define AD_DATASZ   AD_DATASZ_EA
136 #endif
137
138 typedef u_int32_t cnid_t;
139
140 struct ad_entry {
141     uint32_t   ade_off;
142     uint32_t   ade_len;
143 };
144
145 typedef struct adf_lock_t {
146     struct flock lock;
147     int user;
148     int *refcount; /* handle read locks with multiple users */
149 } adf_lock_t;
150
151 struct ad_fd {
152     int          adf_fd;        /* -1: invalid, -2: symlink */
153 #ifndef HAVE_PREAD
154     off_t        adf_off;
155 #endif
156     char         *adf_syml;
157     int          adf_flags;
158     int          adf_excl;
159     adf_lock_t   *adf_lock;
160     int          adf_refcount, adf_lockcount, adf_lockmax;
161 };
162
163 /* some header protection */
164 #define AD_INITED  0xad494e54  /* ad"INT" */
165
166 struct adouble_fops {
167     char *(*ad_path)(const char *, int);
168     int  (*ad_mkrf)(char *);
169     int  (*ad_rebuild_header)(struct adouble *);
170     int  (*ad_check_header)(struct adouble *, struct stat *);
171     int  (*ad_header_read)(struct adouble *, struct stat *);
172     int  (*ad_header_upgrade)(struct adouble *, char *);
173 };
174
175 struct adouble {
176     u_int32_t           ad_magic;
177     u_int32_t           ad_version;
178     char                ad_filler[ 16 ];
179     struct ad_entry     ad_eid[ ADEID_MAX ];
180     struct ad_fd        ad_data_fork, ad_resource_fork, ad_metadata_fork;
181     struct ad_fd        *ad_md; /* either ad_resource or ad_metadata */
182     int                 ad_flags;    /* This really stores version info too (AD_VERSION*) */
183     int                 ad_adflags;  /* ad_open flags adflags like ADFLAGS_DIR */
184     unsigned int        ad_inited;
185     int                 ad_options;
186     int                 ad_fileordir;
187     int                 ad_refcount; /* used in afpd/ofork.c */
188     off_t               ad_rlen;     /* ressource fork len with AFP 3.0
189                                         the header parameter size is too small. */
190     char                *ad_m_name;   /* mac name for open fork */
191     int                 ad_m_namelen;
192     struct adouble_fops *ad_ops;
193     u_int16_t       ad_open_forks;      /* open forks (by others) */
194 #ifdef USE_MMAPPED_HEADERS
195     char                *ad_data;
196 #else
197     char        ad_data[AD_DATASZ_MAX];
198 #endif
199 };
200
201 #define ADFLAGS_DF        (1<<0)
202 #define ADFLAGS_HF        (1<<1)
203 #define ADFLAGS_DIR       (1<<2)
204 #define ADFLAGS_V1COMPAT  (1<<4)
205 #define ADFLAGS_NOHF      (1<<5)  /* not an error if no ressource fork */
206 #define ADFLAGS_RDONLY    (1<<6)  /* don't try readwrite */
207 #define ADFLAGS_OPENFORKS (1<<7)  /* check for open fork in ad_metadata function */
208 #define ADFLAGS_RF        (1<<8)
209 #define ADFLAGS_MD        ADFLAGS_HF /* (1<<9) */
210 #define ADFLAGS_CREATE    (1<<9)
211
212 #define ADVOL_NODEV      (1 << 0)
213 #define ADVOL_CACHE      (1 << 1)
214 #define ADVOL_UNIXPRIV   (1 << 2) /* adouble unix priv */
215 #define ADVOL_INVDOTS    (1 << 3) /* dot files (.DS_Store) are invisible) */
216 #define ADVOL_NOADOUBLE  (1 << 4)
217
218 /* lock flags */
219 #define ADLOCK_CLR      (0)
220 #define ADLOCK_RD       (1<<0)
221 #define ADLOCK_WR       (1<<1)
222 #define ADLOCK_MASK     (ADLOCK_RD | ADLOCK_WR)
223 #define ADLOCK_UPGRADE  (1<<2)
224 #define ADLOCK_FILELOCK (1<<3)
225
226 /* we use this so that we can use the same mechanism for both byte
227  * locks and file synchronization locks. i do this by co-opting either
228  * first bits on 32-bit machines or shifting above the last bit on
229  * 64-bit machines. this only matters for the data fork. */
230 #if defined(TRY_64BITOFF_T) && (~0UL > 0xFFFFFFFFU)
231 /* synchronization locks */
232 #define AD_FILELOCK_BASE (0x80000000)
233 #else
234 #define AD_FILELOCK_BASE (0x7FFFFFFF -9)
235 #endif
236
237 /* FIXME:
238  * AD_FILELOCK_BASE case
239  */
240 #if _FILE_OFFSET_BITS == 64
241 #define BYTELOCK_MAX (0x7FFFFFFFFFFFFFFFULL)
242 #else
243 /* Tru64 is an always-64-bit OS; version 4.0 does not set _FILE_OFFSET_BITS */
244 #if defined(TRU64)
245 #define BYTELOCK_MAX (0x7FFFFFFFFFFFFFFFULL)
246 #else
247 #define BYTELOCK_MAX (0x7FFFFFFFU)
248 #endif
249 #endif
250
251 #define AD_FILELOCK_OPEN_WR        (AD_FILELOCK_BASE + 0)
252 #define AD_FILELOCK_OPEN_RD        (AD_FILELOCK_BASE + 1)
253 #define AD_FILELOCK_DENY_WR        (AD_FILELOCK_BASE + 2)
254 #define AD_FILELOCK_DENY_RD        (AD_FILELOCK_BASE + 3)
255 #define AD_FILELOCK_OPEN_NONE      (AD_FILELOCK_BASE + 4)
256
257 /* time stuff. we overload the bits a little.  */
258 #define AD_DATE_CREATE         0
259 #define AD_DATE_MODIFY         4
260 #define AD_DATE_BACKUP         8
261 #define AD_DATE_ACCESS        12
262 #define AD_DATE_MASK          (AD_DATE_CREATE | AD_DATE_MODIFY | \
263                               AD_DATE_BACKUP | AD_DATE_ACCESS)
264 #define AD_DATE_UNIX          (1 << 10)
265 #define AD_DATE_START         htonl(0x80000000)
266 #define AD_DATE_DELTA         946684800
267 #define AD_DATE_FROM_UNIX(x)  htonl((x) - AD_DATE_DELTA)
268 #define AD_DATE_TO_UNIX(x)    (ntohl(x) + AD_DATE_DELTA)
269
270 /* various finder offset and info bits */
271 #define FINDERINFO_FRTYPEOFF   0
272 #define FINDERINFO_FRCREATOFF  4
273 #define FINDERINFO_FRFLAGOFF   8
274
275 /* FinderInfo Flags, char in `ad ls`, valid for files|dirs */
276 #define FINDERINFO_ISONDESK      (1)     /* "d", fd */
277 #define FINDERINFO_COLOR         (0x0e)
278 #define FINDERINFO_HIDEEXT       (1<<4)  /* "e", fd */
279 #define FINDERINFO_ISHARED       (1<<6)  /* "m", f  */
280 #define FINDERINFO_HASNOINITS    (1<<7)  /* "n", f  */
281 #define FINDERINFO_HASBEENINITED (1<<8)  /* "i", fd */
282 #define FINDERINFO_HASCUSTOMICON (1<<10) /* "c", fd */
283 #define FINDERINFO_ISSTATIONNERY (1<<11) /* "t", f  */
284 #define FINDERINFO_NAMELOCKED    (1<<12) /* "s", fd */
285 #define FINDERINFO_HASBUNDLE     (1<<13) /* "b", fd */
286 #define FINDERINFO_INVISIBLE     (1<<14) /* "v", fd */
287 #define FINDERINFO_ISALIAS       (1<<15) /* "a", fd */
288
289 #define FINDERINFO_FRVIEWOFF  14
290 #define FINDERINFO_CUSTOMICON 0x4
291 #define FINDERINFO_CLOSEDVIEW 0x100
292
293 /*
294   The "shared" and "invisible" attributes are opaque and stored and
295   retrieved from the FinderFlags. This fixes Bug #2802236:
296   <https://sourceforge.net/tracker/?func=detail&aid=2802236&group_id=8642&atid=108642>
297 */
298
299 /* AFP attributes, char in `ad ls`, valid for files|dirs */
300 #define ATTRBIT_INVISIBLE (1<<0)  /* opaque from FinderInfo */
301 #define ATTRBIT_MULTIUSER (1<<1)  /* file: opaque, dir: see below */
302 #define ATTRBIT_SYSTEM    (1<<2)  /* "y", fd */
303 #define ATTRBIT_DOPEN     (1<<3)  /* data fork already open. Not stored, computed on the fly */
304 #define ATTRBIT_ROPEN     (1<<4)  /* resource fork already open. Not stored, computed on the fly */
305 #define ATTRBIT_NOWRITE   (1<<5)  /* "w", f, write inhibit(v2)/read-only(v1) bit */
306 #define ATTRBIT_BACKUP    (1<<6)  /* "p", fd */
307 #define ATTRBIT_NORENAME  (1<<7)  /* "r", fd */
308 #define ATTRBIT_NODELETE  (1<<8)  /* "l", fd */
309 #define ATTRBIT_NOCOPY    (1<<10) /* "o", f */
310 #define ATTRBIT_SETCLR    (1<<15) /* set/clear bit (d) */
311
312 /* AFP attributes for dirs. These should probably be computed on the fly.
313  * We don't do that, nor does e.g. OS S X 10.5 Server */
314 #define ATTRBIT_EXPFLDR   (1<<1)  /* Folder is a sharepoint */
315 #define ATTRBIT_MOUNTED   (1<<3)  /* Directory is mounted by a user */
316 #define ATTRBIT_SHARED    (1<<4)  /* Shared area, called IsExpFolder in spec */
317
318 /* private AFPFileInfo bits */
319 #define AD_AFPFILEI_OWNER       (1 << 0) /* any owner */
320 #define AD_AFPFILEI_GROUP       (1 << 1) /* ignore group */
321 #define AD_AFPFILEI_BLANKACCESS (1 << 2) /* blank access permissions */
322
323 #define ad_data_fileno(ad)  ((ad)->ad_data_fork.adf_fd)
324 #define ad_reso_fileno(ad)  ((ad)->ad_resource_fork.adf_fd)
325 #define ad_meta_fileno(ad)  ((ad)->ad_md->adf_fd)
326
327 #define ad_getversion(ad)   ((ad)->ad_version)
328
329 #define ad_getentrylen(ad,eid)     ((ad)->ad_eid[(eid)].ade_len)
330 #define ad_setentrylen(ad,eid,len) ((ad)->ad_eid[(eid)].ade_len = (len))
331 #define ad_getentryoff(ad,eid)     ((ad)->ad_eid[(eid)].ade_off)
332 #define ad_entry(ad,eid)           ((caddr_t)(ad)->ad_data + (ad)->ad_eid[(eid)].ade_off)
333
334 #define ad_get_HF_flags(ad) ((ad)->ad_resource_fork.adf_flags)
335 #define ad_get_MD_flags(ad) ((ad)->ad_md->adf_flags)
336
337 /* ad_flush.c */
338 extern int ad_rebuild_adouble_header (struct adouble *);
339 extern int ad_rebuild_sfm_header (struct adouble *);
340 extern int ad_copy_header (struct adouble *, struct adouble *);
341 extern int ad_flush (struct adouble *);
342 extern int ad_close (struct adouble *, int);
343
344 /* ad_lock.c */
345 extern int ad_fcntl_lock    (struct adouble *, const u_int32_t /*eid*/,
346                                  const int /*type*/, const off_t /*offset*/,
347                                  const off_t /*len*/, const int /*user*/);
348 extern void ad_fcntl_unlock (struct adouble *, const int /*user*/);
349 extern int ad_fcntl_tmplock (struct adouble *, const u_int32_t /*eid*/,
350                                  const int /*type*/, const off_t /*offset*/,
351                                  const off_t /*len*/, const int /*user*/);
352 extern int ad_testlock      (struct adouble * /*adp*/, int /*eid*/, off_t /*off*/);
353
354 extern u_int16_t ad_openforks (struct adouble * /*adp*/, u_int16_t);
355 extern int ad_excl_lock     (struct adouble * /*adp*/, const u_int32_t /*eid*/);
356
357 #define ad_lock ad_fcntl_lock
358 #define ad_tmplock ad_fcntl_tmplock
359 #define ad_unlock ad_fcntl_unlock
360
361 /* ad_open.c */
362 extern int ad_setfuid     (const uid_t );
363 extern uid_t ad_getfuid   (void );
364 extern char *ad_dir       (const char *);
365 extern char *ad_path      (const char *, int);
366 extern char *ad_path_osx  (const char *, int);
367 extern char *ad_path_ads  (const char *, int);
368 extern char *ad_path_sfm  (const char *, int);
369 extern int ad_mode        (const char *, int);
370 extern int ad_mkdir       (const char *, int);
371 extern void ad_init       (struct adouble *, int, int );
372 extern int ad_open        (const char *, int, int, int, struct adouble *);
373 extern int ad_openat      (int dirfd, const char *, int, int, int, struct adouble *);
374 extern int ad_refresh     (struct adouble *);
375 extern int ad_stat        (const char *, struct stat *);
376 extern int ad_metadata    (const char *, int, struct adouble *);
377 extern int ad_metadataat  (int, const char *, int, struct adouble *);
378
379 #define ad_open_metadata(name, flags, mode, adp)\
380    ad_open(name, ADFLAGS_MD|(flags), O_RDWR |(mode), 0666, (adp))
381
382 #define ad_close_metadata(adp) ad_close( (adp), ADFLAGS_MD)
383
384 /* build a resource fork mode from the data fork mode:
385  * remove X mode and extend header to RW if R or W (W if R for locking),
386  */
387 static inline mode_t ad_hf_mode (mode_t mode)
388 {
389     mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
390     /* fnctl lock need write access */
391     if ((mode & S_IRUSR))
392         mode |= S_IWUSR;
393     if ((mode & S_IRGRP))
394         mode |= S_IWGRP;
395     if ((mode & S_IROTH))
396         mode |= S_IWOTH;
397
398     /* if write mode set add read mode */
399     if ((mode & S_IWUSR))
400         mode |= S_IRUSR;
401     if ((mode & S_IWGRP))
402         mode |= S_IRGRP;
403     if ((mode & S_IWOTH))
404         mode |= S_IROTH;
405
406     return mode;
407 }
408
409 /* ad_ea.c */
410 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size);
411 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size);
412 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size);
413 ssize_t sys_listxattr (const char *path, char *list, size_t size);
414 ssize_t sys_llistxattr (const char *path, char *list, size_t size);
415 ssize_t sys_flistxattr (int filedes, char *list, size_t size);
416 int sys_removexattr (const char *path, const char *name);
417 int sys_lremovexattr (const char *path, const char *name);
418 int sys_fremovexattr (int filedes, const char *name);
419 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags);
420 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags);
421 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags);
422 int sys_copyxattr (const char *src, const char *dst);
423
424 /* ad_read.c/ad_write.c */
425 extern int     sys_ftruncate(int fd, off_t length);
426
427 extern ssize_t ad_read (struct adouble *, const u_int32_t,
428                             const off_t, char *, const size_t);
429 extern ssize_t ad_pread (struct ad_fd *, void *, size_t, off_t);
430 extern ssize_t ad_write (struct adouble *, const u_int32_t, off_t,
431                              const int, const char *, const size_t);
432 extern ssize_t adf_pread  (struct ad_fd *, void *, size_t, off_t);
433 extern ssize_t adf_pwrite (struct ad_fd *, const void *, size_t, off_t);
434 extern int     ad_dtruncate (struct adouble *, const off_t);
435 extern int     ad_rtruncate (struct adouble *, const off_t);
436
437 /* ad_size.c */
438 extern off_t ad_size (const struct adouble *, const u_int32_t );
439
440 /* ad_mmap.c */
441 extern void *ad_mmapread (struct adouble *, const u_int32_t,
442                               const off_t, const size_t);
443 extern void *ad_mmapwrite (struct adouble *, const u_int32_t,
444                                const off_t, const int, const size_t);
445 #define ad_munmap(buf, len)  (munmap((buf), (len)))
446
447 /* ad_date.c */
448 extern int ad_setdate (struct adouble *, unsigned int, u_int32_t);
449 extern int ad_getdate (const struct adouble *, unsigned int, u_int32_t *);
450
451 /* ad_attr.c */
452 extern int       ad_setattr (const struct adouble *, const u_int16_t);
453 extern int       ad_getattr (const struct adouble *, u_int16_t *);
454 extern int       ad_setname (struct adouble *, const char *);
455 extern int       ad_setid (struct adouble *, const dev_t dev,const ino_t ino, const u_int32_t, const u_int32_t, const void *);
456 extern u_int32_t ad_getid (struct adouble *, const dev_t, const ino_t, const cnid_t, const void *);
457 extern u_int32_t ad_forcegetid (struct adouble *adp);
458
459 #ifdef WITH_SENDFILE
460 extern int ad_readfile_init(const struct adouble *ad, 
461                             const int eid, off_t *off,
462                             const int end);
463 #endif
464
465 #if 0
466 #ifdef HAVE_SENDFILE_WRITE
467 extern ssize_t ad_writefile (struct adouble *, const int,
468                                  const int, off_t, const int, const size_t);
469 #endif /* HAVE_SENDFILE_WRITE */
470 #endif /* 0 */
471
472 #endif /* _ATALK_ADOUBLE_H */