like mkstemp but forces permissions */
| 1077 | |
| 1078 | /* like mkstemp but forces permissions */ |
| 1079 | int do_mkstemp(char *template, mode_t perms) |
| 1080 | { |
| 1081 | RETURN_ERROR_IF(dry_run, 0); |
| 1082 | RETURN_ERROR_IF(read_only, EROFS); |
| 1083 | perms |= S_IWUSR; |
| 1084 | |
| 1085 | #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64) |
| 1086 | { |
| 1087 | int fd = mkstemp(template); |
| 1088 | if (fd == -1) |
| 1089 | return -1; |
| 1090 | if (fchmod(fd, perms) != 0 && preserve_perms) { |
| 1091 | int errno_save = errno; |
| 1092 | close(fd); |
| 1093 | unlink(template); |
| 1094 | errno = errno_save; |
| 1095 | return -1; |
| 1096 | } |
| 1097 | #if defined HAVE_SETMODE && O_BINARY |
| 1098 | setmode(fd, O_BINARY); |
| 1099 | #endif |
| 1100 | return fd; |
| 1101 | } |
| 1102 | #else |
| 1103 | if (!mktemp(template)) |
| 1104 | return -1; |
| 1105 | return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms); |
| 1106 | #endif |
| 1107 | } |
| 1108 | |
| 1109 | int do_stat(const char *path, STRUCT_STAT *st) |
| 1110 | { |
no test coverage detected