Fix touching of files that in Windows have the read-only attribute set

This commit is contained in:
Andrea Mazzoleni 2026-06-09 12:41:47 +02:00
parent abcea4d9f3
commit e4aa186b1a
4 changed files with 57 additions and 0 deletions

View file

@ -63,6 +63,22 @@ void state_touch(struct snapraid_state* state)
/* open it */
f = open(path, flags);
#ifdef _WIN32
if (f == -1) {
/* LCOV_EXCL_START */
if (errno == EACCES) {
int attributes = windows_get_file_attributes(path);
if (attributes != -1 && (attributes & FILE_ATTRIBUTE_READONLY) != 0) {
/* remove read only attribute */
if (windows_set_file_attributes(path, attributes & ~FILE_ATTRIBUTE_READONLY) == 0) {
f = open(path, flags);
windows_set_file_attributes(path, attributes);
}
}
}
/* LCOV_EXCL_STOP */
}
#endif
if (f == -1) {
/* LCOV_EXCL_START */
log_error(errno, "Error opening file '%s'. %s.\n", path, strerror(errno));