X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fwrite.c;h=aa3585a75b9fa7fd82c59cbed03a26539e3d2dc7;hp=27463d90d059879426f9df0f17121df8e08f4fea;hb=b9320c921af8d8d2ea3ea80c576ca1e4037af049;hpb=d82950b8ae73d50bffebb88c2b5c10b49f4082f4 diff --git a/src/write.c b/src/write.c index 27463d90..aa3585a7 100644 --- a/src/write.c +++ b/src/write.c @@ -451,7 +451,7 @@ begin_chunk_table(struct write_streams_ctx *ctx, u64 res_expected_size) reserve_size = expected_num_chunk_entries * get_chunk_entry_size(res_expected_size, 0 != (ctx->write_resource_flags & - WIM_RESHDR_FLAG_PACKED_STREAMS)); + WRITE_RESOURCE_FLAG_PACK_STREAMS)); if (ctx->write_resource_flags & WRITE_RESOURCE_FLAG_PACK_STREAMS) reserve_size += sizeof(struct alt_chunk_table_header_disk); memset(ctx->chunk_csizes, 0, reserve_size); @@ -499,7 +499,7 @@ end_chunk_table(struct write_streams_ctx *ctx, u64 res_actual_size, chunk_entry_size = get_chunk_entry_size(res_actual_size, 0 != (ctx->write_resource_flags & - WIM_RESHDR_FLAG_PACKED_STREAMS)); + WRITE_RESOURCE_FLAG_PACK_STREAMS)); typedef le64 __attribute__((may_alias)) aliased_le64_t; typedef le32 __attribute__((may_alias)) aliased_le32_t; @@ -2400,28 +2400,30 @@ finish_write(WIMStruct *wim, int image, int write_flags, } #if defined(HAVE_SYS_FILE_H) && defined(HAVE_FLOCK) + +/* Set advisory lock on WIM file (if not already done so) */ int -lock_wim(WIMStruct *wim, int fd) +lock_wim_for_append(WIMStruct *wim) { - int ret = 0; - if (fd != -1 && !wim->wim_locked) { - ret = flock(fd, LOCK_EX | LOCK_NB); - if (ret != 0) { - if (errno == EWOULDBLOCK) { - ERROR("`%"TS"' is already being modified or has been " - "mounted read-write\n" - " by another process!", wim->filename); - ret = WIMLIB_ERR_ALREADY_LOCKED; - } else { - WARNING_WITH_ERRNO("Failed to lock `%"TS"'", - wim->filename); - ret = 0; - } - } else { - wim->wim_locked = 1; - } + if (wim->locked_for_append) + return 0; + if (!flock(wim->in_fd.fd, LOCK_EX | LOCK_NB)) { + wim->locked_for_append = 1; + return 0; + } + if (errno != EWOULDBLOCK) + return 0; + return WIMLIB_ERR_ALREADY_LOCKED; +} + +/* Remove advisory lock on WIM file (if present) */ +void +unlock_wim_for_append(WIMStruct *wim) +{ + if (wim->locked_for_append) { + flock(wim->in_fd.fd, LOCK_UN); + wim->locked_for_append = 0; } - return ret; } #endif @@ -3062,7 +3064,7 @@ overwrite_wim_inplace(WIMStruct *wim, int write_flags, unsigned num_threads) if (ret) goto out_restore_memory_hdr; - ret = lock_wim(wim, wim->out_fd.fd); + ret = lock_wim_for_append(wim); if (ret) goto out_close_wim; @@ -3098,7 +3100,7 @@ overwrite_wim_inplace(WIMStruct *wim, int write_flags, unsigned num_threads) if (ret) goto out_truncate; - wim->wim_locked = 0; + unlock_wim_for_append(wim); return 0; out_truncate: @@ -3112,7 +3114,7 @@ out_truncate: out_restore_physical_hdr: (void)write_wim_header_flags(hdr_save.flags, &wim->out_fd); out_unlock_wim: - wim->wim_locked = 0; + unlock_wim_for_append(wim); out_close_wim: (void)close_wim_writable(wim, write_flags); out_restore_memory_hdr: