summaryrefslogtreecommitdiff
path: root/macos/source/macopen.c
blob: 9e1873032fe5c5ede28cb5db7fd2176958b5181e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
  Copyright (c) 1990-1999 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 1999-Oct-05 or later
  (the contents of which are also included in zip.h) for terms of use.
  If, for some reason, both of these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.cdrom.com/pub/infozip/license.html
*/
/*** macopen.c; stuff only required for the Mac port ***/

#include "zip.h"

#include <string.h>
#include <fcntl.h>
#include <unix.h>
#include <sound.h>

#include "helpers.h"
#include "pathname.h"
#include "macopen.h"
#include "macstuff.h"

#ifdef MACZIP
#include "macglob.h"

extern char *zipfile;   /* filename of the Zipfile */
extern char *tempzip;   /* Temporary zip file name */

extern MacZipGlobals    MacZip;


/* don't include "osdep.h" otherwise we will trap into endless loop */
#undef open
#undef fopen



FILE *MacFopen(const char *path, const char *mode)
{
static char TruncPath[NAME_MAX];
OSErr   err = 0;

AssertStr(path,path)

            /* open zipfile or tempzip */
if (strcmp(zipfile,path) == 0)
    {
    GetCompletePath(MacZip.ZipFullPath,path,&MacZip.ZipFileSpec,&err);
    err = PrintUserHFSerr((err != -43) && (err != 0), err, path);
    printerr("GetCompletePath:",err,err,__LINE__,__FILE__,path);
    if (CheckMountedVolumes(MacZip.ZipFullPath) > 1)
        DoWarnUserDupVol(MacZip.ZipFullPath);

    /* tempfile should appear in the same directory of the zipfile
       -> save path of zipfile */
    TruncFilename(TruncPath, MacZip.ZipFullPath);
    return fopen(MacZip.ZipFullPath, mode);
    }

if (strcmp(tempzip,path) == 0)
    {  /* add path of zipfile */
    sstrcat(TruncPath,tempzip);
    GetCompletePath(MacZip.TempZipFullPath,TruncPath,&MacZip.TempZipFileSpec,&err);
    err = PrintUserHFSerr((err != -43) && (err != 0), err, path);
    printerr("GetCompletePath:",err,err,__LINE__,__FILE__,path);

    return fopen(MacZip.TempZipFullPath, mode);
    }

printerr("MacFopen:",err,err,__LINE__,__FILE__,path);
return NULL;
}




int MacOpen(const char *path,int oflag, ...)
{
char RealFname[NAME_MAX];

AssertStr(path,path)

RfDfFilen2Real(RealFname,path, MacZip.MacZipMode, MacZip.DataForkOnly, &MacZip.CurrentFork);
/* convert to real fname and init global var  MacZip.CurrentFork  !!  */

switch (MacZip.CurrentFork)
    {
    case DataFork:
        {
        return my_open(RealFname, oflag);
        break;
        }
    case ResourceFork:
        {
        return my_open( RealFname, oflag | O_RSRC);
        break;
        }
    default:  /* for now (Zip ver 2.3b) MacOpen should never reach this point */
        {     /* however, this may change in the future ... */
        printerr("open: no resource / datafork ",-1,-1,__LINE__,__FILE__,path);
        return -1;
        }
    }
}


#ifdef muell
 /* file to delete */
int destroy(char *path)
{
static char lastpath[NAME_MAX];
char    currpath[NAME_MAX];
static Boolean FirstCall = true;
long rc;

AssertStr(path,path)

RfDfFilen2Real(currpath, path, MacZip.MacZipMode, MacZip.DataForkOnly, &MacZip.CurrentFork);

if (FirstCall == true)
    {
    FirstCall = false;
    rc = remove(currpath);
    }
else if (strcmp(currpath,lastpath) == 0) return 0; /* ignore, file is already deleted */
        else rc = remove(currpath); /* we are removeing all the files only by their
        pathname this is dangerous on a mac but there is no other way without
         a complete rewrite of the port  */

strcpy(lastpath,currpath);

return rc;
}
#endif




/* this function replaces the function "replace()" defined in fileio.c */
int replace(char *new_f, char *temp_f)  /* destination and source file names */
{
OSErr   err = 0;
char    newfname[NAME_MAX];

AssertStr(new_f,new_f)
AssertStr(temp_f,temp_f)

UserStop();

GetFilename(newfname, new_f);

/* check zipfile name and tempfile name */
/* we are using this function only for replacing the tempfile with the zipfile */
if ((strcmp(zipfile,new_f) == 0) || (strcmp(tempzip,temp_f) == 0))
    {
    remove(MacZip.ZipFullPath);

    /* rename the temp file to the zip file */
    err = rename(MacZip.TempZipFullPath,MacZip.ZipFullPath);
    printerr("rename:",err,err,__LINE__,__FILE__,MacZip.TempZipFullPath);
if (err != 0) return ZE_CREAT;
    else return ZE_OK;
    }
else return ZE_CREAT;
}



 /* file to delete */
 /* we are removeing all the files only by their
    pathname this is dangerous on a mac but there is no
    other way without a complete rewrite of the port  */

int destroy(char *path)
{
static char lastpath[NAME_MAX];
static FSSpec  trashfolder;
static Boolean FirstCall = true;
static char Num = 0;
static Boolean  Immediate_File_Deletion = false;
char    currpath[NAME_MAX], *envptr;
FSSpec  fileToDelete;
OSErr   err;

/* init this function */
if ((path == NULL) ||
    (strlen(path) == 0))
    {
    FirstCall = true;
    Num = 0;
    return -1;
    }

UserStop();

RfDfFilen2Real(currpath, path, MacZip.MacZipMode,
                MacZip.DataForkOnly, &MacZip.CurrentFork);
GetCompletePath(currpath,currpath,&fileToDelete, &err);

if (FirstCall == true)
    {
    FirstCall = false;
    sstrcpy(lastpath,currpath);
    err = FSpFindFolder(fileToDelete.vRefNum, kTrashFolderType,
                      kDontCreateFolder,&trashfolder);
    printerr("FSpFindFolder:",err,err,__LINE__,__FILE__,path);

    envptr = getenv("Immediate_File_Deletion");
    if (!(envptr == (char *)NULL || *envptr == '\0'))
        {
        if (stricmp(envptr,"yes") == 0)
            Immediate_File_Deletion = true;
        else
            Immediate_File_Deletion = false;
        }

    if (Immediate_File_Deletion)
        {
        err = FSpDelete(&fileToDelete);
        return err;
        }

    err = CatMove (fileToDelete.vRefNum, fileToDelete.parID,
                   fileToDelete.name, trashfolder.parID, trashfolder.name);
    return err;
    }

if (strcmp(currpath,lastpath) == 0)
    {
    return 0; /* ignore, file is already deleted */
    }
else
    {

    if (Immediate_File_Deletion)
        {
        err = FSpDelete(&fileToDelete);
        sstrcpy(lastpath,path);
        return err;
        }

    err = CatMove (fileToDelete.vRefNum, fileToDelete.parID,
                   fileToDelete.name, trashfolder.parID, trashfolder.name);

    /* -48 = file is already existing so we have to rename it before
       moving the file */
    if (err == -48)
        {
        Num++;
        if (fileToDelete.name[0] >= 28) /* cut filename if to long */
            fileToDelete.name[0] = 28;
        P2CStr(fileToDelete.name);
        sprintf(currpath,"%s~%d",(char *)fileToDelete.name,Num);
        C2PStr(currpath);
        C2PStr((char *)fileToDelete.name);
        err = HRename (fileToDelete.vRefNum, fileToDelete.parID,
                       fileToDelete.name, (unsigned char *) currpath);
        err = CatMove (fileToDelete.vRefNum, fileToDelete.parID,
                       (unsigned char *) currpath, trashfolder.parID,
                       trashfolder.name);
        }
    }

sstrcpy(lastpath,currpath);
return err;
}



#endif  /* #ifdef MACZIP */




/*
 *  int open(const char *path, int oflag)
 *
 *      Opens a file stream.
 */
int my_open(char *path, int oflag)
{
    FSSpec          spec;
    char            permission;
    HParamBlockRec  hpb;
    OSErr           err, errno;
    Boolean targetIsFolder, wasAliased;

    AssertStr(path,path)

    /* Setup permission */
    if ((oflag & 0x03) == O_RDWR)
        permission = fsRdWrPerm;
    else
        permission = (oflag & O_RDONLY) ? fsRdPerm : 0 + (oflag & O_WRONLY) ? fsWrPerm : 0;

        FSpLocationFromFullPath(strlen(path),path, &spec);
        if ((oflag & (O_ALIAS | O_NRESOLVE)) == 0)
            ResolveAliasFile(&spec, true, &targetIsFolder, &wasAliased);
        hpb.fileParam.ioNamePtr = spec.name;
        hpb.fileParam.ioVRefNum = spec.vRefNum;
        hpb.fileParam.ioDirID = spec.parID;
        hpb.ioParam.ioPermssn = permission;

        if (oflag & O_RSRC)         /* open the resource fork of the file */
            err = PBHOpenRFSync(&hpb);
        else                        /* open the data fork of the file */
            err = PBHOpenDFSync(&hpb);

    if ((err == fnfErr) && (oflag & O_CREAT)) {
        hpb.fileParam.ioFlVersNum = 0;
        err = PBHCreateSync(&hpb);
        if (err == noErr) {
            /* Set the finder info */
            unsigned long secs;
            unsigned long isbinary = oflag & O_BINARY;

            hpb.fileParam.ioFlFndrInfo.fdType = '\?\?\?\?';
            hpb.fileParam.ioFlFndrInfo.fdCreator = '\?\?\?\?';
            hpb.fileParam.ioFlFndrInfo.fdFlags = 0;
            if (oflag & O_ALIAS)        /* set the alias bit */
                hpb.fileParam.ioFlFndrInfo.fdFlags = kIsAlias;
            else                                        /* clear all flags */
                hpb.fileParam.ioFlFndrInfo.fdFlags = 0;

            GetDateTime(&secs);
            hpb.fileParam.ioFlCrDat = hpb.fileParam.ioFlMdDat = secs;
            PBHSetFInfoSync(&hpb);
        }

        if (err && (err != dupFNErr)) {
            errno = err; return -1;
        }

            if (oflag & O_RSRC)         /* open the resource fork of the file */
                err = PBHOpenRFSync(&hpb);
            else                        /* open the data fork of the file */
                err = PBHOpenDFSync(&hpb);
    }

    if (err && (err != dupFNErr) && (err != opWrErr)) {
        errno = err; return -1;
    }

    if (oflag & O_TRUNC) {
        IOParam pb;

        pb.ioRefNum = hpb.ioParam.ioRefNum;
        pb.ioMisc = 0L;
        err = PBSetEOFSync((ParmBlkPtr)&pb);
        if (err != noErr) {
            errno = err; return -1;
        }
    }

    if (oflag & O_APPEND) lseek(hpb.ioParam.ioRefNum,0,SEEK_END);

    return (hpb.ioParam.ioRefNum);
}