////////////////////////////////////////////////////////////////////////// BackupFile Renames the file, if it exists, to an appropiate backup name for the OS. Prints warning and returns false if file backup fails, otherwise returns true. An eFile(ERR_FILE_WRITE_FAILED) will be thrown if the file is not writable. This is because we don't want to overwrite read-only files, which would happen if we
| 189 | // if we were just renamed the file to a backup filename. |
| 190 | /////////////////////////////////////////////////////////////////////////////// |
| 191 | bool cFileUtil::BackupFile(const TSTRING& filename, bool printWarningOnFailure) // throw eFile() |
| 192 | { |
| 193 | // if the file does not exist, no worries |
| 194 | if (!cFileUtil::FileExists(filename)) |
| 195 | return true; |
| 196 | |
| 197 | // if the file is not a regular file, it is not appropriate to back it up |
| 198 | if (!cFileUtil::IsRegularFile(filename)) |
| 199 | return true; |
| 200 | |
| 201 | // if it is not writeable, throw error |
| 202 | if (!cFileUtil::FileWritable(filename)) |
| 203 | { |
| 204 | // if this assert goes off, you didn't check that the file was writeable |
| 205 | // before calling BackupFile(). |
| 206 | ASSERT(false); |
| 207 | throw eFileWrite(filename, iFSServices::GetInstance()->GetErrString()); |
| 208 | } |
| 209 | |
| 210 | #if IS_DOS_DJGPP |
| 211 | TSTRING backup_filename = cDosPath::BackupName(cDosPath::AsNative(filename)); |
| 212 | #else |
| 213 | TSTRING backup_filename = filename; |
| 214 | #endif |
| 215 | |
| 216 | backup_filename += iFSServices::GetInstance()->GetStandardBackupExtension(); |
| 217 | |
| 218 | // remove the backup file if it exists. We ingore the return value from |
| 219 | // _tunlink(), problems removing the file will be caught by _trename(). |
| 220 | _tunlink(backup_filename.c_str()); |
| 221 | |
| 222 | // back up the file, preserving permissions and ownership, if possible |
| 223 | if (cFileUtil::Copy(filename.c_str(), backup_filename.c_str()) == false) |
| 224 | { |
| 225 | if (printWarningOnFailure && iUserNotify::GetInstance()->GetVerboseLevel() >= iUserNotify::V_NORMAL) |
| 226 | { |
| 227 | TSTRING estr; |
| 228 | estr.assign(TSS_GetString(cUtil, util::STR_ERR2_BACKUP_FAILED1)); |
| 229 | estr.append(filename); |
| 230 | estr.append(TSS_GetString(cUtil, util::STR_ERR2_BACKUP_FAILED2)); |
| 231 | estr.append(backup_filename); |
| 232 | estr.append(TSS_GetString(cUtil, util::STR_ERR2_BACKUP_FAILED3)); |
| 233 | |
| 234 | cErrorReporter::PrintErrorMsg(eFileUtilBackup(estr, eError::NON_FATAL | eError::SUPRESS_THIRD_MSG)); |
| 235 | } |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | bool cFileUtil::Copy(const TSTRING& src_path, const TSTRING& dest_path) |
| 241 | { |
nothing calls this directly
no test coverage detected