Debian Codegraph @

macos/source/unixlike.c

raw
1 /*
2 Copyright (c) 1990-1999 Info-ZIP. All rights reserved.
3
4 See the accompanying file LICENSE, version 1999-Oct-05 or later
5 (the contents of which are also included in zip.h) for terms of use.
6 If, for some reason, both of these files are missing, the Info-ZIP license
7 also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html
8 */
9 /*---------------------------------------------------------------------------
10
11 unixlike.c
12
13 Macintosh-specific routines to emulate unixfunctions.
14
15 ---------------------------------------------------------------------------*/
16
17 /*****************************************************************************/
18 /* Includes */
19 /*****************************************************************************/
20
21 #include "zip.h"
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <sound.h>
26
27 #include "unixlike.h"
28 #include "helpers.h"
29 #include "pathname.h"
30 #include "macstuff.h"
31 #include "macglob.h"
32 #include "mactime.h"
33
34 /*****************************************************************************/
35 /* Global Vars */
36 /*****************************************************************************/
37
38 extern MacZipGlobals MacZip;
39 extern int errno;
40
41
42 /*****************************************************************************/
43 /* Prototypes */
44 /*****************************************************************************/
45
46
47 /*****************************************************************************/
48 /* Functions */
49 /*****************************************************************************/
50
51
52
53
54 /*
55 *----------------------------------------------------------------------
56 *
57 * MacStat --
58 *
59 * This function replaces the library version of stat. The stat
60 * function provided by most Mac compiliers is rather broken and
61 * incomplete.
62 *
63 * Results:
64 * See stat documentation.
65 *
66 * Side effects:
67 * See stat documentation.
68 *
69 *----------------------------------------------------------------------
70 */
71
72 int Zmacstat(const char *Fname, struct stat *buf)
73 {
74 OSErr err, rc;
75 short fullPathLength;
76 Handle hFullPath;
77 char path[NAME_MAX], path2[NAME_MAX];
78 HVolumeParam vpb;
79 static unsigned long count_of_files = 0;
80
81 AssertStr(Fname,Fname)
82 Assert_it(buf,"","")
83
84 UserStop();
85
86 memset(buf, 0, sizeof(buf)); /* zero out all fields */
87
88 RfDfFilen2Real(path2, Fname, MacZip.MacZipMode, MacZip.DataForkOnly,
89 &MacZip.CurrentFork);
90 GetCompletePath(path, path2, &MacZip.fileSpec, &err);
91 err = PrintUserHFSerr((err != -43) && (err != 0), err, path);
92 printerr("GetCompletePath:", err, err, __LINE__, __FILE__, path);
93
94 if (err != noErr) {
95 errno = err;
96 return -1;
97 }
98
99 /* Collect here some more information, it's not related to Macstat.
100 (note: filespec gets changed later in this function) */
101 /* clear string-buffer */
102 memset(MacZip.FullPath, 0x00, sizeof(MacZip.FullPath));
103 rc = FSpGetFullPath(&MacZip.fileSpec, &fullPathLength, &hFullPath);
104 strncpy(MacZip.FullPath, *hFullPath, fullPathLength);
105 DisposeHandle(hFullPath); /* we don't need it any more */
106 /* Collect some more information not related to Macstat */
107
108
109 /*
110 * Fill the fpb & vpb struct up with info about file or directory.
111 */
112
113 FSpGetDirectoryID(&MacZip.fileSpec, &MacZip.dirID, &MacZip.isDirectory);
114 vpb.ioVRefNum = MacZip.fpb.hFileInfo.ioVRefNum = MacZip.fileSpec.vRefNum;
115 vpb.ioNamePtr = MacZip.fpb.hFileInfo.ioNamePtr = MacZip.fileSpec.name;
116
117 if (MacZip.isDirectory) {
118 MacZip.fpb.hFileInfo.ioDirID = MacZip.fileSpec.parID;
119 /*
120 * Directories are executable by everyone.
121 */
122 buf->st_mode |= UNX_IXUSR | UNX_IXGRP | UNX_IXOTH | UNX_IFDIR;
123 } else {
124 MacZip.fpb.hFileInfo.ioDirID = MacZip.dirID;
125 }
126
127 MacZip.fpb.hFileInfo.ioFDirIndex = 0;
128 err = PBGetCatInfoSync((CInfoPBPtr)&MacZip.fpb);
129
130 if (err == noErr) {
131 vpb.ioVolIndex = 0;
132 err = PBHGetVInfoSync((HParmBlkPtr)&vpb);
133 if (err == noErr && buf != NULL) {
134 /*
135 * Files are always readable by everyone.
136 */
137 buf->st_mode |= UNX_IRUSR | UNX_IRGRP | UNX_IROTH;
138
139 /*
140 * Use the Volume Info & File Info to fill out stat buf.
141 */
142 if (MacZip.fpb.hFileInfo.ioFlAttrib & 0x10) {
143 buf->st_mode |= UNX_IFDIR;
144 buf->st_nlink = 2;
145 } else {
146 buf->st_nlink = 1;
147 if (MacZip.fpb.hFileInfo.ioFlFndrInfo.fdFlags & 0x8000) {
148 buf->st_mode |= UNX_IFLNK;
149 } else {
150 buf->st_mode |= UNX_IFREG;
151 }
152 }
153
154 if (MacZip.fpb.hFileInfo.ioFlFndrInfo.fdType == 'APPL') {
155 /*
156 * Applications are executable by everyone.
157 */
158 buf->st_mode |= UNX_IXUSR | UNX_IXGRP | UNX_IXOTH;
159 }
160 if ((MacZip.fpb.hFileInfo.ioFlAttrib & 0x01) == 0){
161 /*
162 * If not locked, then everyone has write acces.
163 */
164 buf->st_mode |= UNX_IWUSR | UNX_IWGRP | UNX_IWOTH;
165 }
166
167 buf->st_ino = MacZip.fpb.hFileInfo.ioDirID;
168 buf->st_dev = MacZip.fpb.hFileInfo.ioVRefNum;
169 buf->st_uid = -1;
170 buf->st_gid = -1;
171 buf->st_rdev = 0;
172
173 if (MacZip.CurrentFork == ResourceFork)
174 buf->st_size = MacZip.fpb.hFileInfo.ioFlRLgLen;
175 else
176 buf->st_size = MacZip.fpb.hFileInfo.ioFlLgLen;
177
178 buf->st_blksize = vpb.ioVAlBlkSiz;
179 buf->st_blocks = (buf->st_size + buf->st_blksize - 1)
180 / buf->st_blksize;
181
182 /*
183 * The times returned by the Mac file system are in the
184 * local time zone. We convert them to GMT so that the
185 * epoch starts from GMT. This is also consistent with
186 * what is returned from "clock seconds".
187 */
188 if (!MacZip.isDirectory) {
189 MacZip.CreatDate = MacZip.fpb.hFileInfo.ioFlCrDat;
190 MacZip.ModDate = MacZip.fpb.hFileInfo.ioFlMdDat;
191 MacZip.BackDate = MacZip.fpb.hFileInfo.ioFlBkDat;
192 } else {
193 MacZip.CreatDate = MacZip.fpb.dirInfo.ioDrCrDat;
194 MacZip.ModDate = MacZip.fpb.dirInfo.ioDrMdDat;
195 MacZip.BackDate = MacZip.fpb.dirInfo.ioDrBkDat;
196 }
197
198 #ifdef IZ_CHECK_TZ
199 if (!zp_tz_is_valid)
200 {
201 MacZip.HaveGMToffset = false;
202 MacZip.Md_UTCoffs = 0L;
203 MacZip.Cr_UTCoffs = 0L;
204 MacZip.Bk_UTCoffs = 0L;
205 }
206 else
207 #endif
208 {
209 /* Do not use GMT offsets when Md_UTCoffs calculation
210 * fails, since this time stamp is used for time
211 * comparisons in Zip and UnZip operations.
212 * We do not bother when GMT offset calculation fails for
213 * any other time stamp value. Instead we simply assume
214 * a default value of 0.
215 */
216 MacZip.HaveGMToffset =
217 GetGMToffsetMac(MacZip.ModDate, &MacZip.Md_UTCoffs);
218 if (MacZip.HaveGMToffset) {
219 GetGMToffsetMac(MacZip.CreatDate, &MacZip.Cr_UTCoffs);
220 GetGMToffsetMac(MacZip.BackDate, &MacZip.Bk_UTCoffs);
221 } else {
222 MacZip.Cr_UTCoffs = 0L;
223 MacZip.Bk_UTCoffs = 0L;
224 }
225 }
226 #ifdef DEBUG_TIME
227 {
228 printf("\nZmacstat: MacZip.HaveGMToffset: %d",
229 MacZip.HaveGMToffset);
230 printf("\nZmacstat: Mac modif: %lu local -> UTOffset: %d",
231 MacZip.ModDate, MacZip.Md_UTCoffs);
232 printf("\nZmacstat: Mac creat: %lu local -> UTOffset: %d",
233 MacZip.CreatDate, MacZip.Cr_UTCoffs);
234 printf("\nZmacstat: Mac back: %lu local -> UTOffset: %d",
235 MacZip.BackDate, MacZip.Bk_UTCoffs);
236 }
237 #endif /* DEBUG_TIME */
238
239
240 buf->st_mtime = MacFtime2UnixFtime(MacZip.ModDate);
241 buf->st_ctime = MacFtime2UnixFtime(MacZip.CreatDate);
242 buf->st_atime = buf->st_mtime;
243
244 #ifdef DEBUG_TIME
245 {
246 printf("\nZmacstat: Unix modif: %lu UTC; Mac: %lu local",
247 buf->st_mtime, MacZip.ModDate);
248 printf("\nZmacstat: Unix creat: %lu UTC; Mac: %lu local\n",
249 buf->st_ctime, MacZip.CreatDate);
250 }
251 #endif /* DEBUG_TIME */
252
253 if (noisy)
254 {
255 if (MacZip.StatingProgress)
256 {
257 count_of_files++;
258 InformProgress(MacZip.RawCountOfItems, count_of_files );
259 }
260 else
261 count_of_files = 0;
262 }
263 }
264 }
265
266 if (err != noErr) {
267 errno = err;
268 }
269
270 MacZip.isMacStatValid = true;
271 return (err == noErr ? 0 : -1);
272 }
273
274
275
276
277
278 /*
279 *----------------------------------------------------------------------
280 *
281 * chmod --
282 *
283 * Results:
284 * See chmod documentation.
285 *
286 * Side effects:
287 * See chmod documentation.
288 *
289 *----------------------------------------------------------------------
290 */
291
292 int chmod(char *path, int mode)
293 {
294 HParamBlockRec hpb;
295 OSErr err;
296
297 hpb.fileParam.ioNamePtr = C2PStr(path);
298 hpb.fileParam.ioVRefNum = 0;
299 hpb.fileParam.ioDirID = 0;
300
301 if (mode & 0200) {
302 err = PBHRstFLockSync(&hpb);
303 } else {
304 err = PBHSetFLockSync(&hpb);
305 }
306
307 if (err != noErr) {
308 errno = err;
309 return -1;
310 }
311
312 return 0;
313 }