OSErr ForceMoveFile(short vRefNum, long parID, ConstStr255Param name, long newDirID) { OSErr err = noErr; FSSpec tmpFSp; err = CatMove(vRefNum, parID, name, newDirID, nil); if (err == dupFNErr) { // handle for stomping over old file err = FSMakeFSSpec(vRefNum, newDirID, name, &tmpFSp); err = FSpDelete(&tmpFSp); err = CatMove(vRefNum, parID, name, newDirID, nil); } return err; }
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; }