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