]> arthur.barton.de Git - netatalk.git/blob - bin/afile/common.c
Segfault fixes by Olaf Hering <olh@suse.de>.
[netatalk.git] / bin / afile / common.c
1 /*  common functions, defines, and structures for afile, achfile, and acleandir
2
3     Copyright (C) 2001 Sebastian Rittau.
4     All rights reserved.
5
6     This file may be distributed and/or modfied under the terms of the
7     following license:
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions
11     are met:
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.
20
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
31     SUCH DAMAGE.
32 */
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41
42 #include <atalk/adouble.h>
43 #include <netatalk/endian.h>
44
45 #include "common.h"
46
47
48 #define AD_PREFIX ".AppleDouble/"
49 #define PARENT_PREFIX "../"
50
51
52 char *dataname_to_adname(const char *dataname)
53 {
54   const char *filepart;
55   char *adname;
56   size_t adlen = strlen(AD_PREFIX);
57   size_t dirlen;
58
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);
66   } else {
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);
72   }
73
74   return adname;
75 }
76
77 char *adname_to_dataname(const char *adname)
78 {
79   const char *filepart;
80   char *dataname;
81   size_t plen = strlen(PARENT_PREFIX);
82   size_t dirlen;
83
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);
90   } else {
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);
95   }
96
97   return dataname;
98 }
99
100
101 #define FLAG_NONE 0x0000
102 #define FLAG_AD   0x0001
103 #define FLAG_DIR  0x0002
104
105
106 struct AFile *afile_new(const char *filename)
107 {
108   struct stat fdstat;
109   char adbuf[AD_DATASZ];
110   ssize_t sz;
111   struct stat statbuf;
112
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;
117
118   /* Try to open file. */
119   if (afile->fd == -1) {
120     free(afile);
121     return NULL;
122   }
123
124   fstat(afile->fd, &statbuf);
125   afile->mode = statbuf.st_mode;
126
127   /* Try to determine if this file is a regular file, a directory, or
128    * something else.
129    */
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);
134     if (sz >= 0) {
135       /* If we can't read a whole AppleDouble header, this can't be a valid
136        * AppleDouble file.
137        */
138       if (sz == AD_DATASZ) {
139         /* ... but as we could, maybe this is. Now if only the magic number
140          * would be correct ...
141          */
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];
153       }
154  
155     } else {
156       afile_delete(afile);
157       return NULL;
158     }
159     
160   } else if (S_ISDIR(fdstat.st_mode))                /* it is a directory */
161     afile->flags |= FLAG_DIR;
162   else {                                             /* it is neither */
163     afile_delete(afile);
164     return NULL;
165   }
166
167   return afile;
168 }
169
170
171 void afile_delete(struct AFile *afile)
172 {
173   close(afile->fd);
174   free(afile);
175 }
176
177
178 const char *afile_filename(struct AFile *afile)
179 {
180   return afile->filename;
181 }
182
183 int afile_is_ad(struct AFile *afile)
184 {
185   return afile->flags & FLAG_AD;
186 }
187
188 int afile_is_dir(struct AFile *afile)
189 {
190   return afile->flags & FLAG_DIR;
191 }
192
193 mode_t afile_mode(struct AFile *afile)
194 {
195   return afile->mode;
196 }
197
198 const char *afile_creator(const struct AFile *afile)
199 {
200   return afile->creator;
201 }
202
203 const char *afile_type(const struct AFile *afile)
204 {
205   return afile->type;
206 }