- for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
- if (inode->fds[i]) {
- if (inode->fds[i]->stream_id == stream_id) {
- wimlib_assert(inode->fds[i]->lte == old_lte);
- inode->fds[i]->lte = new_lte;
+ /* There may already be open file descriptors to this stream if
+ * it's previously been opened read-only, but just now we're
+ * opening it read-write. Identify those file descriptors and
+ * change their lookup table entry pointers to point to the new
+ * lookup table entry, and open staging file descriptors for
+ * them.
+ *
+ * At the same time, we need to count the number of these opened
+ * file descriptors to the new lookup table entry. If there's
+ * an old lookup table entry, this number needs to be subtracted
+ * from the fd's opened to the old entry. */
+ for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
+ struct wimlib_fd *fd = inode->fds[i];
+ if (fd) {
+ if (fd->stream_id == stream_id) {
+ wimlib_assert(fd->lte == old_lte);
+ fd->lte = new_lte;
+ new_lte->num_opened_fds++;
+ fd->staging_fd = open(staging_file_name, O_RDONLY);
+ if (fd->staging_fd == -1) {
+ ret = -errno;
+ goto out_revert_fd_changes;