static INT MoveOverwrite (LPTSTR fn) { /*ask the user if they want to override*/ INT res; ConOutResPrintf(STRING_MOVE_HELP1, fn); res = FilePromptYNA (0); return res; }
static DWORD DeleteFiles(LPTSTR FileName, DWORD* dwFlags, DWORD dwAttrFlags) { TCHAR szFullPath[MAX_PATH]; TCHAR szFileName[MAX_PATH]; LPTSTR pFilePart; HANDLE hFile; WIN32_FIND_DATA f; BOOL bExclusion; INT res; DWORD dwFiles = 0; _tcscpy(szFileName, FileName); if(_tcschr (szFileName, _T('*')) == NULL && IsExistingDirectory (szFileName)) { /* If it doesnt have a \ at the end already then on needs to be added */ if(szFileName[_tcslen(szFileName) - 1] != _T('\\')) _tcscat (szFileName, _T("\\")); /* Add a wildcard after the \ */ _tcscat (szFileName, _T("*")); } if(!_tcscmp (szFileName, _T("*")) || !_tcscmp (szFileName, _T("*.*")) || (szFileName[_tcslen(szFileName) - 2] == _T('\\') && szFileName[_tcslen(szFileName) - 1] == _T('*'))) { /* well, the user wants to delete everything but if they didnt yes DEL_YES, DEL_QUIET, or DEL_PROMPT then we are going to want to make sure that in fact they want to do that. */ if (!((*dwFlags & DEL_YES) || (*dwFlags & DEL_QUIET) || (*dwFlags & DEL_PROMPT))) { res = FilePromptYNA (STRING_DEL_HELP2); if ((res == PROMPT_NO) || (res == PROMPT_BREAK)) return 0x80000000; if(res == PROMPT_ALL) *dwFlags |= DEL_YES; } } GetFullPathName (szFileName, MAX_PATH, szFullPath, &pFilePart); hFile = FindFirstFile(szFullPath, &f); if (hFile != INVALID_HANDLE_VALUE) { do { bExclusion = FALSE; /*if it is going to be excluded by - no need to check attrs*/ if(*dwFlags & DEL_ATTRIBUTES && !bExclusion) { /*save if file attr check if user doesnt care about that attr anyways*/ if(dwAttrFlags & ATTR_ARCHIVE && !(f.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)) bExclusion = TRUE; if(dwAttrFlags & ATTR_HIDDEN && !(f.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) bExclusion = TRUE; if(dwAttrFlags & ATTR_SYSTEM && !(f.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)) bExclusion = TRUE; if(dwAttrFlags & ATTR_READ_ONLY && !(f.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) bExclusion = TRUE; if(dwAttrFlags & ATTR_N_ARCHIVE && (f.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)) bExclusion = TRUE; if(dwAttrFlags & ATTR_N_HIDDEN && (f.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) bExclusion = TRUE; if(dwAttrFlags & ATTR_N_SYSTEM && (f.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)) bExclusion = TRUE; if(dwAttrFlags & ATTR_N_READ_ONLY && (f.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) bExclusion = TRUE; } if(bExclusion) continue; /* ignore directories */ if (f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; _tcscpy (pFilePart, f.cFileName); /* We cant delete ourselves */ if(!_tcscmp (CMDPath,szFullPath)) continue; TRACE("Full filename: %s\n", debugstr_aw(szFullPath)); /* ask for deleting */ if (*dwFlags & DEL_PROMPT) { ConErrResPrintf(STRING_DEL_ERROR5, szFullPath); res = FilePromptYN (STRING_DEL_ERROR6); if ((res == PROMPT_NO) || (res == PROMPT_BREAK)) { nErrorLevel = 0; continue; } } /*user cant ask it to be quiet and tell you what it did*/ if (!(*dwFlags & DEL_QUIET) && !(*dwFlags & DEL_TOTAL)) { ConErrResPrintf(STRING_DEL_ERROR7, szFullPath); } /* delete the file */ if(*dwFlags & DEL_NOTHING) continue; if(RemoveFile (szFullPath, *dwFlags, &f)) dwFiles++; else { ErrorMessage (GetLastError(), _T("")); // FindClose(hFile); // return -1; } } while (FindNextFile (hFile, &f)); FindClose (hFile); } else error_sfile_not_found(szFullPath); return dwFiles; }
INT cmd_rmdir (LPTSTR param) { TCHAR ch; INT args; INT dirCount; LPTSTR *arg; INT i; BOOL RD_SUB = FALSE; BOOL RD_QUIET = FALSE; INT res; INT nError = 0; TCHAR szFullPath[MAX_PATH]; if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_RMDIR_HELP); return 0; } arg = split (param, &args, FALSE, FALSE); dirCount = 0; /* check for options anywhere in command line */ for (i = 0; i < args; i++) { if (*arg[i] == _T('/')) { /*found a command, but check to make sure it has something after it*/ if (_tcslen (arg[i]) == 2) { ch = _totupper (arg[i][1]); if (ch == _T('S')) { RD_SUB = TRUE; } else if (ch == _T('Q')) { RD_QUIET = TRUE; } } } else { dirCount++; } } if (dirCount == 0) { /* No folder to remove */ error_req_param_missing(); freep(arg); return 1; } for (i = 0; i < args; i++) { if (*arg[i] == _T('/')) continue; if (RD_SUB) { /* ask if they want to delete evrything in the folder */ if (!RD_QUIET) { res = FilePromptYNA (STRING_DEL_HELP2); if (res == PROMPT_NO || res == PROMPT_BREAK) { nError = 1; continue; } if (res == PROMPT_ALL) RD_QUIET = TRUE; } /* get the folder name */ GetFullPathName(arg[i],MAX_PATH,szFullPath,NULL); /* remove trailing \ if any, but ONLY if dir is not the root dir */ if (_tcslen (szFullPath) >= 2 && szFullPath[_tcslen (szFullPath) - 1] == _T('\\')) szFullPath[_tcslen(szFullPath) - 1] = _T('\0'); res = DeleteFolder(szFullPath); } else { res = RemoveDirectory(arg[i]); } if (!res) { /* Couldn't delete the folder, print out the error */ nError = GetLastError(); ErrorMessage(nError, _T("RD")); } } freep (arg); return nError; }
/* makes the replace */ INT replace(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], DWORD dwFlags, BOOL *doMore) { TCHAR d[MAX_PATH]; TCHAR s[MAX_PATH]; HANDLE hFileSrc, hFileDest; DWORD dwAttrib, dwRead, dwWritten; LPBYTE buffer; BOOL bEof = FALSE; FILETIME srcCreationTime, destCreationTime, srcLastAccessTime, destLastAccessTime; FILETIME srcLastWriteTime, destLastWriteTime; GetPathCase(source, s); GetPathCase(dest, d); s[0] = _totupper(s[0]); d[0] = _totupper(d[0]); // ConOutPrintf(_T("old-src: %s\n"), s); // ConOutPrintf(_T("old-dest: %s\n"), d); // ConOutPrintf(_T("src: %s\n"), source); // ConOutPrintf(_T("dest: %s\n"), dest); /* Open up the sourcefile */ hFileSrc = CreateFile (source, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, 0, NULL); if (hFileSrc == INVALID_HANDLE_VALUE) { ConOutResPrintf(STRING_COPY_ERROR1, source); return 0; } /* * Get the time from source file to be used in the comparison * with dest time if update switch is set. */ GetFileTime (hFileSrc, &srcCreationTime, &srcLastAccessTime, &srcLastWriteTime); /* * Retrieve the source attributes so that they later on * can be inserted in to the destination. */ dwAttrib = GetFileAttributes (source); if (IsExistingFile (dest)) { /* * Resets the attributes to avoid probles with read only files, * checks for read only has been made earlier. */ SetFileAttributes(dest,FILE_ATTRIBUTE_NORMAL); /* * Is the update flas set? The time has to be controled so that * only older files are replaced. */ if (dwFlags & REPLACE_UPDATE) { /* Read destination time */ hFileDest = CreateFile(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFileSrc == INVALID_HANDLE_VALUE) { ConOutResPrintf(STRING_COPY_ERROR1, dest); return 0; } /* Compare time */ GetFileTime (hFileDest, &destCreationTime, &destLastAccessTime, &destLastWriteTime); if (!((srcLastWriteTime.dwHighDateTime > destLastWriteTime.dwHighDateTime) || (srcLastWriteTime.dwHighDateTime == destLastWriteTime.dwHighDateTime && srcLastWriteTime.dwLowDateTime > destLastWriteTime.dwLowDateTime))) { CloseHandle (hFileSrc); CloseHandle (hFileDest); return 0; } CloseHandle (hFileDest); } /* Delete the old file */ DeleteFile (dest); } /* Check confirm flag, and take appropriate action */ if (dwFlags & REPLACE_CONFIRM) { /* Output depending on add flag */ if (dwFlags & REPLACE_ADD) ConOutResPrintf(STRING_REPLACE_HELP9, dest); else ConOutResPrintf(STRING_REPLACE_HELP10, dest); if ( !FilePromptYNA (0)) return 0; } /* Output depending on add flag */ if (dwFlags & REPLACE_ADD) ConOutResPrintf(STRING_REPLACE_HELP11, dest); else ConOutResPrintf(STRING_REPLACE_HELP5, dest); /* Make sure source and destination is not the same */ if (!_tcscmp(s, d)) { ConOutResPaging(TRUE, STRING_REPLACE_ERROR7); CloseHandle (hFileSrc); *doMore = FALSE; return 0; } /* Open destination file to write to */ hFileDest = CreateFile (dest, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if (hFileDest == INVALID_HANDLE_VALUE) { CloseHandle (hFileSrc); ConOutResPaging(TRUE, STRING_REPLACE_ERROR7); *doMore = FALSE; return 0; } /* Get buffer for the copy process */ buffer = VirtualAlloc(NULL, BUFF_SIZE, MEM_COMMIT, PAGE_READWRITE); if (buffer == NULL) { CloseHandle (hFileDest); CloseHandle (hFileSrc); ConOutResPaging(TRUE, STRING_ERROR_OUT_OF_MEMORY); return 0; } /* Put attribute and time to the new destination file */ SetFileAttributes (dest, dwAttrib); SetFileTime (hFileDest, &srcCreationTime, &srcLastAccessTime, &srcLastWriteTime); do { /* Read data from source */ ReadFile (hFileSrc, buffer, BUFF_SIZE, &dwRead, NULL); /* Done? */ if (dwRead == 0) break; /* Write to destination file */ WriteFile (hFileDest, buffer, dwRead, &dwWritten, NULL); /* Done! or ctrl break! */ if (dwWritten != dwRead || CheckCtrlBreak(BREAK_INPUT)) { ConOutResPuts(STRING_COPY_ERROR3); VirtualFree (buffer, 0, MEM_RELEASE); CloseHandle (hFileDest); CloseHandle (hFileSrc); nErrorLevel = 1; return 0; } } while (!bEof); /* Return memory and close files */ VirtualFree (buffer, 0, MEM_RELEASE); CloseHandle (hFileDest); CloseHandle (hFileSrc); /* Return one file replaced */ return 1; }