| 1 |
/* |
| 2 |
* Copyright (c) 2022 |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* MP support header */ |
| 18 |
#include "MacportsLegacySupport.h" |
| 19 |
|
| 20 |
/* copyfile and its associated functions wrap */ |
| 21 |
#if __MP_LEGACY_SUPPORT_COPYFILE_WRAP__ |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <dlfcn.h> |
| 25 |
#include <sys/stat.h> |
| 26 |
|
| 27 |
#include <copyfile.h> |
| 28 |
|
| 29 |
int copyfile_state_get(copyfile_state_t s, uint32_t flag, void * dst) |
| 30 |
{ |
| 31 |
int (*real_copyfile_state_get)(copyfile_state_t s, uint32_t flag, void * dst); |
| 32 |
int ret; |
| 33 |
char *file_name; |
| 34 |
int file_descriptor; |
| 35 |
struct stat file_stat; |
| 36 |
|
| 37 |
real_copyfile_state_get = dlsym(RTLD_NEXT, "copyfile_state_get"); |
| 38 |
if (real_copyfile_state_get == NULL) { |
| 39 |
exit(EXIT_FAILURE); |
| 40 |
} |
| 41 |
|
| 42 |
switch(flag) |
| 43 |
{ |
| 44 |
case COPYFILE_STATE_COPIED: |
| 45 |
/* since copyfile did not recored how many bytes were copied, return the size of the destination file */ |
| 46 |
/* first check if the file descriptor has been set */ |
| 47 |
ret = real_copyfile_state_get(s, COPYFILE_STATE_DST_FD, &file_descriptor); |
| 48 |
if ( ret < 0 ) { |
| 49 |
return ret; |
| 50 |
} |
| 51 |
if ( file_descriptor != -2 ) { |
| 52 |
ret = fstat(file_descriptor, &file_stat); |
| 53 |
return ret; |
| 54 |
} else { |
| 55 |
/* the file descriptor was not set, so check the file name */ |
| 56 |
ret = real_copyfile_state_get(s, COPYFILE_STATE_DST_FILENAME, &file_name); |
| 57 |
if ( ret < 0 ) { |
| 58 |
return ret; |
| 59 |
} |
| 60 |
if ( file_name == NULL ) { |
| 61 |
/* neither the file descriptor nor the file name has been set */ |
| 62 |
return real_copyfile_state_get(s, flag, dst); |
| 63 |
} |
| 64 |
ret = stat(file_name, &file_stat); |
| 65 |
} |
| 66 |
*(off_t*)dst = file_stat.st_size; |
| 67 |
return 0; |
| 68 |
break; |
| 69 |
case COPYFILE_STATE_STATUS_CB: |
| 70 |
case COPYFILE_STATE_STATUS_CTX: |
| 71 |
/* copyfile did not run the callback function, so return default (which is an error) */ |
| 72 |
default: |
| 73 |
return real_copyfile_state_get(s, flag, dst); |
| 74 |
} |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
#endif /* __MP_LEGACY_SUPPORT_COPYFILE_WRAP__ */ |