macopen.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. Copyright (c) 1990-1999 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 1999-Oct-05 or later
  4. (the contents of which are also included in zip.h) for terms of use.
  5. If, for some reason, both of these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html
  7. */
  8. /*** macopen.c; stuff only required for the Mac port ***/
  9. #include "zip.h"
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include <unix.h>
  13. #include <sound.h>
  14. #include "helpers.h"
  15. #include "pathname.h"
  16. #include "macopen.h"
  17. #include "macstuff.h"
  18. #ifdef MACZIP
  19. #include "macglob.h"
  20. extern char *zipfile; /* filename of the Zipfile */
  21. extern char *tempzip; /* Temporary zip file name */
  22. extern MacZipGlobals MacZip;
  23. /* don't include "osdep.h" otherwise we will trap into endless loop */
  24. #undef open
  25. #undef fopen
  26. FILE *MacFopen(const char *path, const char *mode)
  27. {
  28. static char TruncPath[NAME_MAX];
  29. OSErr err = 0;
  30. AssertStr(path,path)
  31. /* open zipfile or tempzip */
  32. if (strcmp(zipfile,path) == 0)
  33. {
  34. GetCompletePath(MacZip.ZipFullPath,path,&MacZip.ZipFileSpec,&err);
  35. err = PrintUserHFSerr((err != -43) && (err != 0), err, path);
  36. printerr("GetCompletePath:",err,err,__LINE__,__FILE__,path);
  37. if (CheckMountedVolumes(MacZip.ZipFullPath) > 1)
  38. DoWarnUserDupVol(MacZip.ZipFullPath);
  39. /* tempfile should appear in the same directory of the zipfile
  40. -> save path of zipfile */
  41. TruncFilename(TruncPath, MacZip.ZipFullPath);
  42. return fopen(MacZip.ZipFullPath, mode);
  43. }
  44. if (strcmp(tempzip,path) == 0)
  45. { /* add path of zipfile */
  46. sstrcat(TruncPath,tempzip);
  47. GetCompletePath(MacZip.TempZipFullPath,TruncPath,&MacZip.TempZipFileSpec,&err);
  48. err = PrintUserHFSerr((err != -43) && (err != 0), err, path);
  49. printerr("GetCompletePath:",err,err,__LINE__,__FILE__,path);
  50. return fopen(MacZip.TempZipFullPath, mode);
  51. }
  52. printerr("MacFopen:",err,err,__LINE__,__FILE__,path);
  53. return NULL;
  54. }
  55. int MacOpen(const char *path,int oflag, ...)
  56. {
  57. char RealFname[NAME_MAX];
  58. AssertStr(path,path)
  59. RfDfFilen2Real(RealFname,path, MacZip.MacZipMode, MacZip.DataForkOnly, &MacZip.CurrentFork);
  60. /* convert to real fname and init global var MacZip.CurrentFork !! */
  61. switch (MacZip.CurrentFork)
  62. {
  63. case DataFork:
  64. {
  65. return my_open(RealFname, oflag);
  66. break;
  67. }
  68. case ResourceFork:
  69. {
  70. return my_open( RealFname, oflag | O_RSRC);
  71. break;
  72. }
  73. default: /* for now (Zip ver 2.3b) MacOpen should never reach this point */
  74. { /* however, this may change in the future ... */
  75. printerr("open: no resource / datafork ",-1,-1,__LINE__,__FILE__,path);
  76. return -1;
  77. }
  78. }
  79. }
  80. #ifdef muell
  81. /* file to delete */
  82. int destroy(char *path)
  83. {
  84. static char lastpath[NAME_MAX];
  85. char currpath[NAME_MAX];
  86. static Boolean FirstCall = true;
  87. long rc;
  88. AssertStr(path,path)
  89. RfDfFilen2Real(currpath, path, MacZip.MacZipMode, MacZip.DataForkOnly, &MacZip.CurrentFork);
  90. if (FirstCall == true)
  91. {
  92. FirstCall = false;
  93. rc = remove(currpath);
  94. }
  95. else if (strcmp(currpath,lastpath) == 0) return 0; /* ignore, file is already deleted */
  96. else rc = remove(currpath); /* we are removeing all the files only by their
  97. pathname this is dangerous on a mac but there is no other way without
  98. a complete rewrite of the port */
  99. strcpy(lastpath,currpath);
  100. return rc;
  101. }
  102. #endif
  103. /* this function replaces the function "replace()" defined in fileio.c */
  104. int replace(char *new_f, char *temp_f) /* destination and source file names */
  105. {
  106. OSErr err = 0;
  107. char newfname[NAME_MAX];
  108. AssertStr(new_f,new_f)
  109. AssertStr(temp_f,temp_f)
  110. UserStop();
  111. GetFilename(newfname, new_f);
  112. /* check zipfile name and tempfile name */
  113. /* we are using this function only for replacing the tempfile with the zipfile */
  114. if ((strcmp(zipfile,new_f) == 0) || (strcmp(tempzip,temp_f) == 0))
  115. {
  116. remove(MacZip.ZipFullPath);
  117. /* rename the temp file to the zip file */
  118. err = rename(MacZip.TempZipFullPath,MacZip.ZipFullPath);
  119. printerr("rename:",err,err,__LINE__,__FILE__,MacZip.TempZipFullPath);
  120. if (err != 0) return ZE_CREAT;
  121. else return ZE_OK;
  122. }
  123. else return ZE_CREAT;
  124. }
  125. /* file to delete */
  126. /* we are removeing all the files only by their
  127. pathname this is dangerous on a mac but there is no
  128. other way without a complete rewrite of the port */
  129. int destroy(char *path)
  130. {
  131. static char lastpath[NAME_MAX];
  132. static FSSpec trashfolder;
  133. static Boolean FirstCall = true;
  134. static char Num = 0;
  135. static Boolean Immediate_File_Deletion = false;
  136. char currpath[NAME_MAX], *envptr;
  137. FSSpec fileToDelete;
  138. OSErr err;
  139. /* init this function */
  140. if ((path == NULL) ||
  141. (strlen(path) == 0))
  142. {
  143. FirstCall = true;
  144. Num = 0;
  145. return -1;
  146. }
  147. UserStop();
  148. RfDfFilen2Real(currpath, path, MacZip.MacZipMode,
  149. MacZip.DataForkOnly, &MacZip.CurrentFork);
  150. GetCompletePath(currpath,currpath,&fileToDelete, &err);
  151. if (FirstCall == true)
  152. {
  153. FirstCall = false;
  154. sstrcpy(lastpath,currpath);
  155. err = FSpFindFolder(fileToDelete.vRefNum, kTrashFolderType,
  156. kDontCreateFolder,&trashfolder);
  157. printerr("FSpFindFolder:",err,err,__LINE__,__FILE__,path);
  158. envptr = getenv("Immediate_File_Deletion");
  159. if (!(envptr == (char *)NULL || *envptr == '\0'))
  160. {
  161. if (stricmp(envptr,"yes") == 0)
  162. Immediate_File_Deletion = true;
  163. else
  164. Immediate_File_Deletion = false;
  165. }
  166. if (Immediate_File_Deletion)
  167. {
  168. err = FSpDelete(&fileToDelete);
  169. return err;
  170. }
  171. err = CatMove (fileToDelete.vRefNum, fileToDelete.parID,
  172. fileToDelete.name, trashfolder.parID, trashfolder.name);
  173. return err;
  174. }
  175. if (strcmp(currpath,lastpath) == 0)
  176. {
  177. return 0; /* ignore, file is already deleted */
  178. }
  179. else
  180. {
  181. if (Immediate_File_Deletion)
  182. {
  183. err = FSpDelete(&fileToDelete);
  184. sstrcpy(lastpath,path);
  185. return err;
  186. }
  187. err = CatMove (fileToDelete.vRefNum, fileToDelete.parID,
  188. fileToDelete.name, trashfolder.parID, trashfolder.name);
  189. /* -48 = file is already existing so we have to rename it before
  190. moving the file */
  191. if (err == -48)
  192. {
  193. Num++;
  194. if (fileToDelete.name[0] >= 28) /* cut filename if to long */
  195. fileToDelete.name[0] = 28;
  196. P2CStr(fileToDelete.name);
  197. sprintf(currpath,"%s~%d",(char *)fileToDelete.name,Num);
  198. C2PStr(currpath);
  199. C2PStr((char *)fileToDelete.name);
  200. err = HRename (fileToDelete.vRefNum, fileToDelete.parID,
  201. fileToDelete.name, (unsigned char *) currpath);
  202. err = CatMove (fileToDelete.vRefNum, fileToDelete.parID,
  203. (unsigned char *) currpath, trashfolder.parID,
  204. trashfolder.name);
  205. }
  206. }
  207. sstrcpy(lastpath,currpath);
  208. return err;
  209. }
  210. #endif /* #ifdef MACZIP */
  211. /*
  212. * int open(const char *path, int oflag)
  213. *
  214. * Opens a file stream.
  215. */
  216. int my_open(char *path, int oflag)
  217. {
  218. FSSpec spec;
  219. char permission;
  220. HParamBlockRec hpb;
  221. OSErr err, errno;
  222. Boolean targetIsFolder, wasAliased;
  223. AssertStr(path,path)
  224. /* Setup permission */
  225. if ((oflag & 0x03) == O_RDWR)
  226. permission = fsRdWrPerm;
  227. else
  228. permission = (oflag & O_RDONLY) ? fsRdPerm : 0 + (oflag & O_WRONLY) ? fsWrPerm : 0;
  229. FSpLocationFromFullPath(strlen(path),path, &spec);
  230. if ((oflag & (O_ALIAS | O_NRESOLVE)) == 0)
  231. ResolveAliasFile(&spec, true, &targetIsFolder, &wasAliased);
  232. hpb.fileParam.ioNamePtr = spec.name;
  233. hpb.fileParam.ioVRefNum = spec.vRefNum;
  234. hpb.fileParam.ioDirID = spec.parID;
  235. hpb.ioParam.ioPermssn = permission;
  236. if (oflag & O_RSRC) /* open the resource fork of the file */
  237. err = PBHOpenRFSync(&hpb);
  238. else /* open the data fork of the file */
  239. err = PBHOpenDFSync(&hpb);
  240. if ((err == fnfErr) && (oflag & O_CREAT)) {
  241. hpb.fileParam.ioFlVersNum = 0;
  242. err = PBHCreateSync(&hpb);
  243. if (err == noErr) {
  244. /* Set the finder info */
  245. unsigned long secs;
  246. unsigned long isbinary = oflag & O_BINARY;
  247. hpb.fileParam.ioFlFndrInfo.fdType = '\?\?\?\?';
  248. hpb.fileParam.ioFlFndrInfo.fdCreator = '\?\?\?\?';
  249. hpb.fileParam.ioFlFndrInfo.fdFlags = 0;
  250. if (oflag & O_ALIAS) /* set the alias bit */
  251. hpb.fileParam.ioFlFndrInfo.fdFlags = kIsAlias;
  252. else /* clear all flags */
  253. hpb.fileParam.ioFlFndrInfo.fdFlags = 0;
  254. GetDateTime(&secs);
  255. hpb.fileParam.ioFlCrDat = hpb.fileParam.ioFlMdDat = secs;
  256. PBHSetFInfoSync(&hpb);
  257. }
  258. if (err && (err != dupFNErr)) {
  259. errno = err; return -1;
  260. }
  261. if (oflag & O_RSRC) /* open the resource fork of the file */
  262. err = PBHOpenRFSync(&hpb);
  263. else /* open the data fork of the file */
  264. err = PBHOpenDFSync(&hpb);
  265. }
  266. if (err && (err != dupFNErr) && (err != opWrErr)) {
  267. errno = err; return -1;
  268. }
  269. if (oflag & O_TRUNC) {
  270. IOParam pb;
  271. pb.ioRefNum = hpb.ioParam.ioRefNum;
  272. pb.ioMisc = 0L;
  273. err = PBSetEOFSync((ParmBlkPtr)&pb);
  274. if (err != noErr) {
  275. errno = err; return -1;
  276. }
  277. }
  278. if (oflag & O_APPEND) lseek(hpb.ioParam.ioRefNum,0,SEEK_END);
  279. return (hpb.ioParam.ioRefNum);
  280. }