2 * $Id: common.c,v 1.3 2001-06-29 14:14:46 rufustfirefly Exp $
4 common functions, defines, and structures for afile, achfile, and acleandir
6 Copyright (C) 2001 Sebastian Rittau.
9 This file may be distributed and/or modfied under the terms of the
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the author nor the names of its contributors
21 may be used to endorse or promote products derived from this software
22 without specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #endif /* HAVE_CONFIG_H */
44 #include <sys/types.h>
48 #endif /* HAVE_FCNTL_H */
51 #endif /* HAVE_UNISTD_H */
53 #include <atalk/adouble.h>
54 #include <netatalk/endian.h>
59 #define AD_PREFIX ".AppleDouble/"
60 #define PARENT_PREFIX "../"
63 char *dataname_to_adname(const char *dataname)
67 size_t adlen = strlen(AD_PREFIX);
70 /* Construct the AppleDouble file name from data fork file name. */
71 adname = calloc(adlen + strlen(dataname) + 1, sizeof(char));
72 filepart = rindex(dataname, '/');
73 if (filepart == NULL) {
74 /* Filename doesn't contain a path. */
75 strcpy(adname, AD_PREFIX);
76 strcpy(adname + adlen, dataname);
78 /* Filename does contain a path. */
79 dirlen = (size_t) (filepart - dataname);
80 strncpy(adname, dataname, dirlen + 1);
81 strcpy(adname + dirlen + 1, AD_PREFIX);
82 strcpy(adname + dirlen + adlen + 1, filepart + 1);
88 char *adname_to_dataname(const char *adname)
92 size_t plen = strlen(PARENT_PREFIX);
95 /* Construct the data file name from the AppleDouble file name. */
96 dataname = calloc(strlen(adname) + plen + 1, sizeof(char));
97 filepart = rindex(adname, '/');
98 if (filepart == NULL) {
99 strcpy(dataname, PARENT_PREFIX);
100 strcpy(dataname + plen, adname);
102 dirlen = (size_t) (filepart - adname);
103 strncpy(dataname, adname, dirlen + 1);
104 strcpy(dataname + dirlen + 1, PARENT_PREFIX);
105 strcpy(dataname + dirlen + plen + 1, filepart + 1);
112 #define FLAG_NONE 0x0000
113 #define FLAG_AD 0x0001
114 #define FLAG_DIR 0x0002
117 struct AFile *afile_new(const char *filename)
120 char adbuf[AD_DATASZ];
124 struct AFile *afile = (struct AFile *) calloc(sizeof(struct AFile), 1);
125 afile->filename = filename;
126 afile->fd = open(filename, O_RDONLY);
127 afile->flags = FLAG_NONE;
129 /* Try to open file. */
130 if (afile->fd == -1) {
135 fstat(afile->fd, &statbuf);
136 afile->mode = statbuf.st_mode;
138 /* Try to determine if this file is a regular file, a directory, or
141 fstat(afile->fd, &fdstat);
142 if (S_ISREG(fdstat.st_mode)) { /* it is a regular file */
143 /* Check if the opened file is the resource fork. */
144 sz = read(afile->fd, adbuf, AD_DATASZ);
146 /* If we can't read a whole AppleDouble header, this can't be a valid
149 if (sz == AD_DATASZ) {
150 /* ... but as we could, maybe this is. Now if only the magic number
151 * would be correct ...
153 if (ntohl(((unsigned int *) adbuf)[0]) == AD_MAGIC)
154 /* Great! It obviously is a AppleDouble file. */
155 afile->flags |= FLAG_AD;
156 afile->creator[0] = adbuf[ADEDOFF_FINDERI + 0];
157 afile->creator[1] = adbuf[ADEDOFF_FINDERI + 1];
158 afile->creator[2] = adbuf[ADEDOFF_FINDERI + 2];
159 afile->creator[3] = adbuf[ADEDOFF_FINDERI + 3];
160 afile->type [0] = adbuf[ADEDOFF_FINDERI + 4];
161 afile->type [1] = adbuf[ADEDOFF_FINDERI + 5];
162 afile->type [2] = adbuf[ADEDOFF_FINDERI + 6];
163 afile->type [3] = adbuf[ADEDOFF_FINDERI + 7];
171 } else if (S_ISDIR(fdstat.st_mode)) /* it is a directory */
172 afile->flags |= FLAG_DIR;
173 else { /* it is neither */
182 void afile_delete(struct AFile *afile)
189 const char *afile_filename(struct AFile *afile)
191 return afile->filename;
194 int afile_is_ad(struct AFile *afile)
196 return afile->flags & FLAG_AD;
199 int afile_is_dir(struct AFile *afile)
201 return afile->flags & FLAG_DIR;
204 mode_t afile_mode(struct AFile *afile)
209 const char *afile_creator(const struct AFile *afile)
211 return afile->creator;
214 const char *afile_type(const struct AFile *afile)