FIO_openDstFile() : * condition : `dstFileName` must be non-NULL. * @result : FILE* to `dstFileName`, or NULL if it fails */
| 621 | * condition : `dstFileName` must be non-NULL. |
| 622 | * @result : FILE* to `dstFileName`, or NULL if it fails */ |
| 623 | static FILE* |
| 624 | FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs, |
| 625 | const char* srcFileName, const char* dstFileName) |
| 626 | { |
| 627 | if (prefs->testMode) return NULL; /* do not open file in test mode */ |
| 628 | |
| 629 | assert(dstFileName != NULL); |
| 630 | if (!strcmp (dstFileName, stdoutmark)) { |
| 631 | DISPLAYLEVEL(4,"Using stdout for output \n"); |
| 632 | SET_BINARY_MODE(stdout); |
| 633 | if (prefs->sparseFileSupport == 1) { |
| 634 | prefs->sparseFileSupport = 0; |
| 635 | DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n"); |
| 636 | } |
| 637 | return stdout; |
| 638 | } |
| 639 | |
| 640 | /* ensure dst is not the same as src */ |
| 641 | if (srcFileName != NULL && UTIL_isSameFile(srcFileName, dstFileName)) { |
| 642 | DISPLAYLEVEL(1, "zstd: Refusing to open an output file which will overwrite the input file \n"); |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | if (prefs->sparseFileSupport == 1) { |
| 647 | prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT; |
| 648 | } |
| 649 | |
| 650 | if (UTIL_isRegularFile(dstFileName)) { |
| 651 | /* Check if destination file already exists */ |
| 652 | FILE* const fCheck = fopen( dstFileName, "rb" ); |
| 653 | #if !defined(_WIN32) |
| 654 | /* this test does not work on Windows : |
| 655 | * `NUL` and `nul` are detected as regular files */ |
| 656 | if (!strcmp(dstFileName, nulmark)) { |
| 657 | EXM_THROW(40, "%s is unexpectedly categorized as a regular file", |
| 658 | dstFileName); |
| 659 | } |
| 660 | #endif |
| 661 | if (fCheck != NULL) { /* dst file exists, authorization prompt */ |
| 662 | fclose(fCheck); |
| 663 | if (!prefs->overwrite) { |
| 664 | if (g_display_prefs.displayLevel <= 1) { |
| 665 | /* No interaction possible */ |
| 666 | DISPLAY("zstd: %s already exists; not overwritten \n", |
| 667 | dstFileName); |
| 668 | return NULL; |
| 669 | } |
| 670 | DISPLAY("zstd: %s already exists; ", dstFileName); |
| 671 | if (UTIL_requireUserConfirmation("overwrite (y/n) ? ", "Not overwritten \n", "yY", fCtx->hasStdinInput)) |
| 672 | return NULL; |
| 673 | } |
| 674 | /* need to unlink */ |
| 675 | FIO_removeFile(dstFileName); |
| 676 | } } |
| 677 | |
| 678 | { FILE* const f = fopen( dstFileName, "wb" ); |
| 679 | if (f == NULL) { |
| 680 | DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno)); |
no test coverage detected