Skip to content

Commit

Permalink
Revert zsv_fopen_wx (#175)
Browse files Browse the repository at this point in the history
* Revert "add zsv_fopen_wx to util/file; use exclusive write for zvs_file_copy (#173)"
This reverts commit 50a7c83, except for:
- change configure/Makefile to work for compilers that do not work with -static
  • Loading branch information
liquidaty committed Jun 25, 2024
1 parent 50a7c83 commit 5819c89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 47 deletions.
56 changes: 15 additions & 41 deletions app/utils/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,6 @@ int zsv_file_exists(const char* filename) {
}
#endif

/**
* Open a file for exclusive write (same as fopen() but opens exclusively)
* mode must be 'w' or 'wb'
*/
FILE* zsv_fopen_wx(const char *filename, const char *mode) {
if(!mode || (strcmp(mode, "w") && strcmp(mode, "wb"))) {
fprintf(stderr, "fopen_wbx mode must be 'w' or 'wb'; got %s\n", mode ? mode : "(none)");
return NULL;
}

int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644); // exclusive binary write
if(fd == -1) {
return NULL;
}

FILE *file = fdopen(fd, "wb"); // Convert to FILE*
if(file == NULL)
close(fd);

return file;
}

/**
* Copy a file, given source and destination paths
* On error, output error message and return non-zero
Expand All @@ -155,22 +133,16 @@ int zsv_copy_file(const char *src, const char *dest) {
// copy the file
int err = 0;
FILE *fsrc = fopen(src, "rb");
if(!fsrc) {
err = errno ? errno : -1;
perror(src);
} else {
FILE *fdest = zsv_fopen_wx(dest, "wb");
if(!fdest) {
err = errno ? errno : -1;
perror(dest);
} else {
if(!fsrc)
err = errno ? errno : -1, perror(src);
else {
FILE *fdest = fopen(dest, "wb");
if(!fdest)
err = errno ? errno : -1, perror(dest);
else {
err = zsv_copy_file_ptr(fsrc, fdest);
if(err) {
if(err < 0)
fprintf(stderr, "Unknown error copying %s to %s\n", src, dest);
else
perror(dest);
}
if(err)
perror(dest);
fclose(fdest);
}
fclose(fsrc);
Expand All @@ -183,14 +155,16 @@ int zsv_copy_file(const char *src, const char *dest) {
* Return error number per errno.h
*/
int zsv_copy_file_ptr(FILE *src, FILE *dest) {
errno = 0;
int err = 0;
char buffer[4096];
size_t bytes_read;
while((bytes_read = fread(buffer, 1, sizeof(buffer), src)) > 0) {
if(fwrite(buffer, 1, bytes_read, dest) != bytes_read)
return errno ? errno : -1;
if(fwrite(buffer, 1, bytes_read, dest) != bytes_read) {
err = errno ? errno : -1;
break;
}
}
return errno;
return err;
}

size_t zsv_dir_len_basename(const char *filepath, const char **basename) {
Expand Down
6 changes: 0 additions & 6 deletions include/zsv/utils/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ size_t zsv_filter_write(void *FILEp, unsigned char *buff, size_t bytes_read);
*/
size_t zsv_dir_len_basename(const char *filepath, const char **basename);

/**
* Open a file for exclusive write (same as fopen() but opens exclusively)
* mode must be 'w' or 'wb'
*/
FILE* zsv_fopen_wx(const char *filename, const char *mode);

/**
* Copy a file. Create any needed directories
* On error, prints error message and returns non-zero
Expand Down

0 comments on commit 5819c89

Please sign in to comment.