1 /* common functions, defines, and structures for afile, achfile, and acleandir
3 Copyright (C) 2001 Sebastian Rittau.
6 This file may be distributed and/or modfied under the terms of the
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
12 1. Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17 3. Neither the name of the author nor the names of its contributors
18 may be used to endorse or promote products derived from this software
19 without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/types.h>
42 #include <atalk/adouble.h>
43 #include <netatalk/endian.h>
48 #define AD_PREFIX ".AppleDouble/"
49 #define PARENT_PREFIX "../"
52 char *dataname_to_adname(const char *dataname)
56 size_t adlen = strlen(AD_PREFIX);
59 /* Construct the AppleDouble file name from data fork file name. */
60 adname = calloc(adlen + strlen(dataname) + 1, sizeof(char));
61 filepart = rindex(dataname, '/');
62 if (filepart == NULL) {
63 /* Filename doesn't contain a path. */
64 strcpy(adname, AD_PREFIX);
65 strcpy(adname + adlen, dataname);
67 /* Filename does contain a path. */
68 dirlen = (size_t) (filepart - dataname);
69 strncpy(adname, dataname, dirlen + 1);
70 strcpy(adname + dirlen + 1, AD_PREFIX);
71 strcpy(adname + dirlen + adlen + 1, filepart + 1);
77 char *adname_to_dataname(const char *adname)
81 size_t plen = strlen(PARENT_PREFIX);
84 /* Construct the data file name from the AppleDouble file name. */
85 dataname = calloc(strlen(adname) + plen + 1, sizeof(char));
86 filepart = rindex(adname, '/');
87 if (filepart == NULL) {
88 strcpy(dataname, PARENT_PREFIX);
89 strcpy(dataname + plen, adname);
91 dirlen = (size_t) (filepart - adname);
92 strncpy(dataname, adname, dirlen + 1);
93 strcpy(dataname + dirlen + 1, PARENT_PREFIX);
94 strcpy(dataname + dirlen + plen + 1, filepart + 1);
101 #define FLAG_NONE 0x0000
102 #define FLAG_AD 0x0001
103 #define FLAG_DIR 0x0002
106 struct AFile *afile_new(const char *filename)
109 char adbuf[AD_DATASZ];
113 struct AFile *afile = (struct AFile *) calloc(sizeof(struct AFile), 1);
114 afile->filename = filename;
115 afile->fd = open(filename, O_RDONLY);
116 afile->flags = FLAG_NONE;
118 /* Try to open file. */
119 if (afile->fd == -1) {
124 fstat(afile->fd, &statbuf);
125 afile->mode = statbuf.st_mode;
127 /* Try to determine if this file is a regular file, a directory, or
130 fstat(afile->fd, &fdstat);
131 if (S_ISREG(fdstat.st_mode)) { /* it is a regular file */
132 /* Check if the opened file is the resource fork. */
133 sz = read(afile->fd, adbuf, AD_DATASZ);
135 /* If we can't read a whole AppleDouble header, this can't be a valid
138 if (sz == AD_DATASZ) {
139 /* ... but as we could, maybe this is. Now if only the magic number
140 * would be correct ...
142 if (ntohl(((unsigned int *) adbuf)[0]) == AD_MAGIC)
143 /* Great! It obviously is a AppleDouble file. */
144 afile->flags |= FLAG_AD;
145 afile->creator[0] = adbuf[ADEDOFF_FINDERI + 0];
146 afile->creator[1] = adbuf[ADEDOFF_FINDERI + 1];
147 afile->creator[2] = adbuf[ADEDOFF_FINDERI + 2];
148 afile->creator[3] = adbuf[ADEDOFF_FINDERI + 3];
149 afile->type [0] = adbuf[ADEDOFF_FINDERI + 4];
150 afile->type [1] = adbuf[ADEDOFF_FINDERI + 5];
151 afile->type [2] = adbuf[ADEDOFF_FINDERI + 6];
152 afile->type [3] = adbuf[ADEDOFF_FINDERI + 7];
160 } else if (S_ISDIR(fdstat.st_mode)) /* it is a directory */
161 afile->flags |= FLAG_DIR;
162 else { /* it is neither */
171 void afile_delete(struct AFile *afile)
178 const char *afile_filename(struct AFile *afile)
180 return afile->filename;
183 int afile_is_ad(struct AFile *afile)
185 return afile->flags & FLAG_AD;
188 int afile_is_dir(struct AFile *afile)
190 return afile->flags & FLAG_DIR;
193 mode_t afile_mode(struct AFile *afile)
198 const char *afile_creator(const struct AFile *afile)
200 return afile->creator;
203 const char *afile_type(const struct AFile *afile)