]> wimlib.net Git - wimlib/blob - src/mount_image.c
7c480ebc76c02c7af9808b87f151c5188a8506ae
[wimlib] / src / mount_image.c
1 /*
2  * mount_image.c
3  *
4  * This file implements mounting of WIM files using FUSE, which stands for
5  * Filesystem in Userspace.  FUSE allows a filesystem to be implemented in a
6  * userspace process by implementing the filesystem primitives--- read(),
7  * write(), readdir(), etc.
8  */
9
10 /*
11  * Copyright (C) 2012, 2013 Eric Biggers
12  *
13  * This file is part of wimlib, a library for working with WIM files.
14  *
15  * wimlib is free software; you can redistribute it and/or modify it under the
16  * terms of the GNU General Public License as published by the Free
17  * Software Foundation; either version 3 of the License, or (at your option)
18  * any later version.
19  *
20  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22  * A PARTICULAR PURPOSE. See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with wimlib; if not, see http://www.gnu.org/licenses/.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 #endif
32
33 #include "wimlib.h"
34 #include "wimlib/error.h"
35
36 #ifdef WITH_FUSE
37
38 #ifdef __WIN32__
39 #  error "FUSE mount not supported on Win32!  Please configure --without-fuse"
40 #endif
41
42 #include "wimlib/encoding.h"
43 #include "wimlib/file_io.h"
44 #include "wimlib/dentry.h"
45 #include "wimlib/inode.h"
46 #include "wimlib/lookup_table.h"
47 #include "wimlib/metadata.h"
48 #include "wimlib/paths.h"
49 #include "wimlib/reparse.h"
50 #include "wimlib/resource.h"
51 #include "wimlib/timestamp.h"
52 #include "wimlib/version.h"
53 #include "wimlib/write.h"
54 #include "wimlib/xml.h"
55
56 #include <errno.h>
57 #include <ftw.h>
58 #include <limits.h>
59 #include <mqueue.h>
60 #include <signal.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sys/stat.h>
64 #include <sys/time.h>
65 #include <sys/types.h>
66 #include <sys/wait.h>
67 #include <unistd.h>
68 #include <utime.h>
69
70 #define FUSE_USE_VERSION 26
71 #include <fuse.h>
72
73 #ifdef ENABLE_XATTR
74 #include <attr/xattr.h>
75 #endif
76
77 #define MSG_VERSION_TOO_HIGH    -1
78 #define MSG_BREAK_LOOP          -2
79
80 /* File descriptor to a file open on the WIM filesystem. */
81 struct wimfs_fd {
82         struct wim_inode *f_inode;
83         struct wim_lookup_table_entry *f_lte;
84         struct filedes staging_fd;
85         u16 idx;
86         u32 stream_id;
87 };
88
89 struct wimfs_context {
90         /* The WIMStruct for the mounted WIM. */
91         WIMStruct *wim;
92
93         /* Name of the staging directory for a read-write mount.  Whenever a new file is
94          * created, it is done so in the staging directory.  Furthermore, whenever a
95          * file in the WIM is modified, it is extracted to the staging directory.  If
96          * changes are commited when the WIM is unmounted, the file resources are merged
97          * in from the staging directory when writing the new WIM. */
98         char *staging_dir_name;
99         size_t staging_dir_name_len;
100
101         /* Flags passed to wimlib_mount(). */
102         int mount_flags;
103
104         /* Default flags to use when looking up a WIM dentry (depends on whether
105          * the Windows interface to alternate data streams is being used or
106          * not). */
107         int default_lookup_flags;
108
109         /* Next inode number to be assigned.  Note: I didn't bother with a
110          * bitmap of free inode numbers since this isn't even a "real"
111          * filesystem anyway. */
112         u64 next_ino;
113
114         /* List of inodes in the mounted image */
115         struct list_head *image_inode_list;
116
117         /* Name and message queue descriptors for message queues between the
118          * filesystem daemon process and the unmount process.  These are used
119          * when the filesystem is unmounted and the process running
120          * wimlib_unmount_image() needs to communicate with the filesystem
121          * daemon running fuse_main() (i.e. the process created by a call to
122          * wimlib_mount_image().  */
123         char *unmount_to_daemon_mq_name;
124         char *daemon_to_unmount_mq_name;
125         mqd_t unmount_to_daemon_mq;
126         mqd_t daemon_to_unmount_mq;
127
128         uid_t default_uid;
129         gid_t default_gid;
130
131         int status;
132         bool have_status;
133 };
134
135 static void
136 init_wimfs_context(struct wimfs_context *ctx)
137 {
138         memset(ctx, 0, sizeof(*ctx));
139         ctx->unmount_to_daemon_mq = (mqd_t)-1;
140         ctx->daemon_to_unmount_mq = (mqd_t)-1;
141 }
142
143 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
144
145 static inline struct wimfs_context *
146 wimfs_get_context(void)
147 {
148         return WIMFS_CTX(fuse_get_context());
149 }
150
151 static inline WIMStruct *
152 wimfs_get_WIMStruct(void)
153 {
154         return wimfs_get_context()->wim;
155 }
156
157 static inline int
158 get_lookup_flags(const struct wimfs_context *ctx)
159 {
160         return ctx->default_lookup_flags;
161 }
162
163 /* Returns nonzero if write permission is requested on the file open flags */
164 static inline int
165 flags_writable(int open_flags)
166 {
167         return open_flags & (O_RDWR | O_WRONLY);
168 }
169
170 /*
171  * Allocate a file descriptor for a stream.
172  *
173  * @inode:      inode containing the stream we're opening
174  * @stream_id:  ID of the stream we're opening
175  * @lte:        Lookup table entry for the stream (may be NULL)
176  * @fd_ret:     Return the allocated file descriptor if successful.
177  *
178  * Return 0 iff successful or error code if unsuccessful.
179  */
180 static int
181 alloc_wimfs_fd(struct wim_inode *inode,
182                u32 stream_id,
183                struct wim_lookup_table_entry *lte,
184                struct wimfs_fd **fd_ret)
185 {
186         static const u16 fds_per_alloc = 8;
187         static const u16 max_fds = 0xffff;
188         int ret;
189
190         DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
191               "(open = %u, allocated = %u)",
192               stream_id, inode->i_ino, inode->i_num_opened_fds,
193               inode->i_num_allocated_fds);
194
195         if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
196                 struct wimfs_fd **fds;
197                 u16 num_new_fds;
198
199                 if (inode->i_num_allocated_fds == max_fds) {
200                         ret = -EMFILE;
201                         goto out;
202                 }
203                 num_new_fds = min(fds_per_alloc,
204                                   max_fds - inode->i_num_allocated_fds);
205
206                 fds = REALLOC(inode->i_fds,
207                               (inode->i_num_allocated_fds + num_new_fds) *
208                                 sizeof(inode->i_fds[0]));
209                 if (!fds) {
210                         ret = -ENOMEM;
211                         goto out;
212                 }
213                 memset(&fds[inode->i_num_allocated_fds], 0,
214                        num_new_fds * sizeof(fds[0]));
215                 inode->i_fds = fds;
216                 inode->i_num_allocated_fds += num_new_fds;
217         }
218         for (u16 i = 0; ; i++) {
219                 if (!inode->i_fds[i]) {
220                         struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
221                         if (!fd) {
222                                 ret = -ENOMEM;
223                                 break;
224                         }
225                         fd->f_inode     = inode;
226                         fd->f_lte       = lte;
227                         filedes_invalidate(&fd->staging_fd);
228                         fd->idx         = i;
229                         fd->stream_id   = stream_id;
230                         *fd_ret         = fd;
231                         inode->i_fds[i] = fd;
232                         inode->i_num_opened_fds++;
233                         if (lte)
234                                 lte->num_opened_fds++;
235                         DEBUG("Allocated fd (idx = %u)", fd->idx);
236                         ret = 0;
237                         break;
238                 }
239         }
240 out:
241         return ret;
242 }
243
244 static void
245 inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
246 {
247         wimlib_assert(inode != NULL);
248         wimlib_assert(fd->f_inode == inode);
249         wimlib_assert(inode->i_num_opened_fds != 0);
250         wimlib_assert(fd->idx < inode->i_num_allocated_fds);
251         wimlib_assert(inode->i_fds[fd->idx] == fd);
252
253         inode->i_fds[fd->idx] = NULL;
254         FREE(fd);
255         if (--inode->i_num_opened_fds == 0) {
256                 FREE(inode->i_fds);
257                 inode->i_fds = NULL;
258                 inode->i_num_allocated_fds = 0;
259                 if (inode->i_nlink == 0)
260                         free_inode(inode);
261         }
262 }
263
264 static int
265 lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
266 {
267         wimlib_assert(fd->f_lte == lte);
268
269         if (!lte) /* Empty stream with no lookup table entry */
270                 return 0;
271
272         /* Close staging file descriptor if needed. */
273
274         if (lte->resource_location == RESOURCE_IN_STAGING_FILE
275              && filedes_valid(&fd->staging_fd))
276         {
277                 if (filedes_close(&fd->staging_fd)) {
278                         ERROR_WITH_ERRNO("Failed to close staging file");
279                         return -errno;
280                 }
281         }
282         lte_decrement_num_opened_fds(lte);
283         return 0;
284 }
285
286 /* Close a file descriptor. */
287 static int
288 close_wimfs_fd(struct wimfs_fd *fd)
289 {
290         int ret;
291         DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
292               fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
293               fd->f_inode->i_num_allocated_fds);
294         ret = lte_put_fd(fd->f_lte, fd);
295         if (ret)
296                 return ret;
297
298         inode_put_fd(fd->f_inode, fd);
299         return 0;
300 }
301
302 static mode_t
303 fuse_mask_mode(mode_t mode, struct fuse_context *fuse_ctx)
304 {
305 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
306         mode &= ~fuse_ctx->umask;
307 #endif
308         return mode;
309 }
310
311 /*
312  * Add a new dentry with a new inode to a WIM image.
313  *
314  * Returns 0 on success, or negative error number on failure.
315  */
316 static int
317 create_dentry(struct fuse_context *fuse_ctx, const char *path,
318               mode_t mode, int attributes, struct wim_dentry **dentry_ret)
319 {
320         struct wim_dentry *parent;
321         struct wim_dentry *new;
322         const char *basename;
323         struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
324         int ret;
325
326         parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
327         if (!parent)
328                 return -errno;
329
330         if (!dentry_is_directory(parent))
331                 return -ENOTDIR;
332
333         basename = path_basename(path);
334         if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
335                 return -EEXIST;
336
337         ret = new_dentry_with_inode(basename, &new);
338         if (ret)
339                 return -ENOMEM;
340
341         new->d_inode->i_resolved = 1;
342         new->d_inode->i_ino = wimfs_ctx->next_ino++;
343         new->d_inode->i_attributes = attributes;
344
345         if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
346                 if (inode_set_unix_data(new->d_inode,
347                                         fuse_ctx->uid,
348                                         fuse_ctx->gid,
349                                         fuse_mask_mode(mode, fuse_ctx),
350                                         wimfs_ctx->wim->lookup_table,
351                                         UNIX_DATA_ALL | UNIX_DATA_CREATE))
352                 {
353                         free_dentry(new);
354                         return -ENOMEM;
355                 }
356         }
357         dentry_add_child(parent, new);
358         list_add_tail(&new->d_inode->i_list, wimfs_ctx->image_inode_list);
359         if (dentry_ret)
360                 *dentry_ret = new;
361         return 0;
362 }
363
364 static struct wim_inode *
365 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
366 {
367         struct wim_dentry *dentry;
368         dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
369         if (dentry)
370                 return dentry->d_inode;
371         else
372                 return NULL;
373 }
374
375 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
376  * corresponding inode.
377  *
378  * If there are no remaining references to the inode either through dentries or
379  * open file descriptors, the inode is freed.  Otherwise, the inode is not
380  * removed, but the dentry is unlinked and freed.
381  *
382  * Either way, all lookup table entries referenced by the inode have their
383  * reference count decremented.  If a lookup table entry has no open file
384  * descriptors and no references remaining, it is freed, and the corresponding
385  * staging file is unlinked.
386  */
387 static void
388 remove_dentry(struct wim_dentry *dentry,
389               struct wim_lookup_table *lookup_table)
390 {
391         struct wim_inode *inode = dentry->d_inode;
392         struct wim_lookup_table_entry *lte;
393         unsigned i;
394
395         for (i = 0; i <= inode->i_num_ads; i++) {
396                 lte = inode_stream_lte_resolved(inode, i);
397                 if (lte)
398                         lte_decrement_refcnt(lte, lookup_table);
399         }
400         unlink_dentry(dentry);
401         free_dentry(dentry);
402 }
403
404 static mode_t
405 inode_default_unix_mode(const struct wim_inode *inode)
406 {
407         if (inode_is_symlink(inode))
408                 return S_IFLNK | 0777;
409         else if (inode_is_directory(inode))
410                 return S_IFDIR | 0777;
411         else
412                 return S_IFREG | 0777;
413 }
414
415 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
416  *
417  * The lookup table entry tells us which stream in the inode we are statting.
418  * For a named data stream, everything returned is the same as the unnamed data
419  * stream except possibly the size and block count. */
420 static int
421 inode_to_stbuf(const struct wim_inode *inode,
422                const struct wim_lookup_table_entry *lte,
423                struct stat *stbuf)
424 {
425         const struct wimfs_context *ctx = wimfs_get_context();
426
427         memset(stbuf, 0, sizeof(struct stat));
428         stbuf->st_mode = inode_default_unix_mode(inode);
429         stbuf->st_uid = ctx->default_uid;
430         stbuf->st_gid = ctx->default_gid;
431         if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
432                 struct wimlib_unix_data unix_data;
433                 if (inode_get_unix_data(inode, &unix_data, NULL) == 0) {
434                         stbuf->st_uid = unix_data.uid;
435                         stbuf->st_gid = unix_data.gid;
436                         stbuf->st_mode = unix_data.mode;
437                 }
438         }
439         stbuf->st_ino = (ino_t)inode->i_ino;
440         stbuf->st_nlink = inode->i_nlink;
441         if (lte)
442                 stbuf->st_size = lte->size;
443         else
444                 stbuf->st_size = 0;
445 #ifdef HAVE_STAT_NANOSECOND_PRECISION
446         stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
447         stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
448         stbuf->st_ctim = stbuf->st_mtim;
449 #else
450         stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
451         stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
452         stbuf->st_ctime = stbuf->st_mtime;
453 #endif
454         stbuf->st_blocks = (stbuf->st_size + 511) / 512;
455         return 0;
456 }
457
458 static void
459 touch_inode(struct wim_inode *inode)
460 {
461         u64 now = get_wim_timestamp();
462         inode->i_last_access_time = now;
463         inode->i_last_write_time = now;
464 }
465
466 /* Creates a new staging file and returns its file descriptor opened for
467  * writing.
468  *
469  * @name_ret: A location into which the a pointer to the newly allocated name of
470  *            the staging file is stored.
471  *
472  * @ctx:      Context for the WIM filesystem; this provides the name of the
473  *            staging directory.
474  *
475  * On success, returns the file descriptor for the staging file, opened for
476  * writing.  On failure, returns -1 and sets errno.
477  */
478 static int
479 create_staging_file(char **name_ret, struct wimfs_context *ctx)
480 {
481         size_t name_len;
482         char *name;
483         struct stat stbuf;
484         int fd;
485         int errno_save;
486
487         static const size_t STAGING_FILE_NAME_LEN = 20;
488
489         name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
490         name = MALLOC(name_len + 1);
491         if (!name) {
492                 errno = ENOMEM;
493                 return -1;
494         }
495
496         do {
497
498                 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
499                 name[ctx->staging_dir_name_len] = '/';
500                 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
501                                                 STAGING_FILE_NAME_LEN);
502                 name[name_len] = '\0';
503
504
505         /* Just in case, verify that the randomly generated name doesn't name an
506          * existing file, and try again if so  */
507         } while (stat(name, &stbuf) == 0);
508
509         if (errno != ENOENT) /* other error?! */
510                 return -1;
511
512         /* doesn't exist--- ok */
513
514         DEBUG("Creating staging file `%s'", name);
515
516         fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
517         if (fd == -1) {
518                 errno_save = errno;
519                 FREE(name);
520                 errno = errno_save;
521         } else {
522                 *name_ret = name;
523         }
524         return fd;
525 }
526
527 /*
528  * Extract a WIM resource to the staging directory.
529  *
530  * @inode:  Inode that contains the stream we are extracting
531  *
532  * @stream_id: Identifier for the stream (it stays constant even if the indices
533  * of the stream entries are changed)
534  *
535  * @lte: Pointer to pointer to the lookup table entry for the stream we need to
536  * extract, or NULL if there was no lookup table entry present for the stream
537  *
538  * @size:  Number of bytes of the stream we want to extract (this supports the
539  * wimfs_truncate() function).  It may be more than the actual stream length, in
540  * which case the extra space is filled with zeroes.
541  *
542  * @ctx:  Context for the WIM filesystem.
543  *
544  * Returns 0 on success or a negative error code on failure.
545  */
546 static int
547 extract_resource_to_staging_dir(struct wim_inode *inode,
548                                 u32 stream_id,
549                                 struct wim_lookup_table_entry **lte,
550                                 off_t size,
551                                 struct wimfs_context *ctx)
552 {
553         char *staging_file_name;
554         int ret;
555         int fd;
556         struct wim_lookup_table_entry *old_lte, *new_lte;
557         off_t extract_size;
558
559         DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
560               "stream id %"PRIu32, inode->i_ino, stream_id);
561
562         old_lte = *lte;
563
564         wimlib_assert(old_lte == NULL ||
565                       old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
566
567         /* Create the staging file */
568         fd = create_staging_file(&staging_file_name, ctx);
569         if (fd == -1)
570                 return -errno;
571
572         /* Extract the stream to the staging file (possibly truncated) */
573         if (old_lte) {
574                 struct filedes wimlib_fd;
575                 filedes_init(&wimlib_fd, fd);
576                 extract_size = min(old_lte->size, size);
577                 ret = extract_stream_to_fd(old_lte, &wimlib_fd, extract_size);
578         } else {
579                 ret = 0;
580                 extract_size = 0;
581         }
582
583         /* In the case of truncate() to more than the file length, extend the
584          * file with zeroes by calling ftruncate() on the underlying staging
585          * file */
586         if (ret == 0 && size > extract_size)
587                 ret = ftruncate(fd, size);
588
589         /* Close the staging file descriptor and check for errors.  If there's
590          * an error, unlink the staging file. */
591         if (ret != 0 || close(fd) != 0) {
592                 if (errno != 0)
593                         ret = -errno;
594                 else
595                         ret = -EIO;
596                 close(fd);
597                 goto out_delete_staging_file;
598         }
599
600         /* Now deal with the lookup table entries.  We may be able to re-use the
601          * existing entry, but we may have to create a new one instead. */
602
603         if (old_lte && inode->i_nlink == old_lte->refcnt) {
604                 /* The reference count of the existing lookup table entry is the
605                  * same as the link count of the inode that contains the stream
606                  * we're opening.  Therefore, ALL the references to the lookup
607                  * table entry correspond to the stream we're trying to extract,
608                  * so the lookup table entry can be re-used.  */
609                 DEBUG("Re-using lookup table entry");
610                 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
611                 new_lte = old_lte;
612         } else {
613                 if (old_lte) {
614                         /* There's an existing lookup table entry, but its
615                          * reference count is greater than the link count for
616                          * the inode containing a stream we're opening.
617                          * Therefore, we need to split the lookup table entry.
618                          */
619                         wimlib_assert(old_lte->refcnt > inode->i_nlink);
620                         DEBUG("Splitting lookup table entry "
621                               "(inode->i_nlink = %u, old_lte->refcnt = %u)",
622                               inode->i_nlink, old_lte->refcnt);
623                 }
624
625                 new_lte = new_lookup_table_entry();
626                 if (!new_lte) {
627                         ret = -ENOMEM;
628                         goto out_delete_staging_file;
629                 }
630
631                 /* There may already be open file descriptors to this stream if
632                  * it's previously been opened read-only, but just now we're
633                  * opening it read-write.  Identify those file descriptors and
634                  * change their lookup table entry pointers to point to the new
635                  * lookup table entry, and open staging file descriptors for
636                  * them.
637                  *
638                  * At the same time, we need to count the number of these opened
639                  * file descriptors to the new lookup table entry.  If there's
640                  * an old lookup table entry, this number needs to be subtracted
641                  * from the fd's opened to the old entry. */
642                 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
643                         struct wimfs_fd *fd = inode->i_fds[i];
644                         if (fd) {
645                                 if (fd->stream_id == stream_id) {
646                                         int raw_fd;
647
648                                         wimlib_assert(fd->f_lte == old_lte);
649                                         wimlib_assert(!filedes_valid(&fd->staging_fd));
650                                         fd->f_lte = new_lte;
651                                         new_lte->num_opened_fds++;
652                                         raw_fd = open(staging_file_name, O_RDONLY);
653                                         if (raw_fd < 0) {
654                                                 ret = -errno;
655                                                 goto out_revert_fd_changes;
656                                         }
657                                         filedes_init(&fd->staging_fd, raw_fd);
658                                 }
659                                 j++;
660                         }
661                 }
662                 DEBUG("%hu fd's were already opened to the file we extracted",
663                       new_lte->num_opened_fds);
664                 if (old_lte) {
665                         old_lte->num_opened_fds -= new_lte->num_opened_fds;
666                         old_lte->refcnt -= inode->i_nlink;
667                 }
668         }
669
670         new_lte->refcnt              = inode->i_nlink;
671         new_lte->resource_location   = RESOURCE_IN_STAGING_FILE;
672         new_lte->staging_file_name   = staging_file_name;
673         new_lte->size                = size;
674
675         add_unhashed_stream(new_lte, inode, stream_id,
676                             &wim_get_current_image_metadata(ctx->wim)->unhashed_streams);
677         *retrieve_lte_pointer(new_lte) = new_lte;
678         *lte = new_lte;
679         return 0;
680 out_revert_fd_changes:
681         for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
682                 struct wimfs_fd *fd = inode->i_fds[i];
683                 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
684                         fd->f_lte = old_lte;
685                         if (filedes_valid(&fd->staging_fd)) {
686                                 filedes_close(&fd->staging_fd);
687                                 filedes_invalidate(&fd->staging_fd);
688                         }
689                         j++;
690                 }
691         }
692         free_lookup_table_entry(new_lte);
693 out_delete_staging_file:
694         unlink(staging_file_name);
695         FREE(staging_file_name);
696         return ret;
697 }
698
699 /*
700  * Creates a randomly named staging directory and saves its name in the
701  * filesystem context structure.
702  */
703 static int
704 make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
705 {
706         static const size_t random_suffix_len = 10;
707         static const char *common_suffix = ".staging";
708         static const size_t common_suffix_len = 8;
709
710         char *staging_dir_name = NULL;
711         size_t staging_dir_name_len;
712         size_t prefix_len;
713         const char *wim_basename;
714         char *real_user_prefix = NULL;
715         int ret;
716
717         if (user_prefix) {
718                 real_user_prefix = realpath(user_prefix, NULL);
719                 if (!real_user_prefix) {
720                         ERROR_WITH_ERRNO("Could not resolve `%s'",
721                                          real_user_prefix);
722                         ret = WIMLIB_ERR_NOTDIR;
723                         goto out;
724                 }
725                 wim_basename = path_basename(ctx->wim->filename);
726                 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
727         } else {
728                 prefix_len = strlen(ctx->wim->filename);
729         }
730
731         staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
732
733         staging_dir_name = MALLOC(staging_dir_name_len + 1);
734         if (!staging_dir_name) {
735                 ret = WIMLIB_ERR_NOMEM;
736                 goto out;
737         }
738
739         if (real_user_prefix)
740                 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
741         else
742                 strcpy(staging_dir_name, ctx->wim->filename);
743
744         strcat(staging_dir_name, common_suffix);
745
746         randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
747                                         random_suffix_len);
748
749         staging_dir_name[staging_dir_name_len] = '\0';
750
751         if (mkdir(staging_dir_name, 0700) != 0) {
752                 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
753                                  staging_dir_name);
754                 ret = WIMLIB_ERR_MKDIR;
755         } else {
756                 ret = 0;
757         }
758 out:
759         FREE(real_user_prefix);
760         if (ret == 0) {
761                 ctx->staging_dir_name = staging_dir_name;
762                 ctx->staging_dir_name_len = staging_dir_name_len;
763         } else {
764                 FREE(staging_dir_name);
765         }
766         return ret;
767 }
768
769 static int
770 remove_file_or_directory(const char *fpath, const struct stat *sb,
771                          int typeflag, struct FTW *ftwbuf)
772 {
773         if (remove(fpath) == 0)
774                 return 0;
775         else {
776                 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
777                 return WIMLIB_ERR_DELETE_STAGING_DIR;
778         }
779 }
780
781 /*
782  * Deletes the staging directory and all the files contained in it.
783  */
784 static int
785 delete_staging_dir(struct wimfs_context *ctx)
786 {
787         int ret;
788         ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
789                    10, FTW_DEPTH);
790         FREE(ctx->staging_dir_name);
791         ctx->staging_dir_name = NULL;
792         return ret;
793 }
794
795 static int
796 inode_close_fds(struct wim_inode *inode)
797 {
798         u16 num_opened_fds = inode->i_num_opened_fds;
799         for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
800                 struct wimfs_fd *fd = inode->i_fds[i];
801                 if (fd) {
802                         wimlib_assert(fd->f_inode == inode);
803                         int ret = close_wimfs_fd(fd);
804                         if (ret != 0)
805                                 return ret;
806                         j++;
807                 }
808         }
809         return 0;
810 }
811
812 /* Overwrites the WIM file, with changes saved. */
813 static int
814 rebuild_wim(struct wimfs_context *ctx, int write_flags,
815             wimlib_progress_func_t progress_func)
816 {
817         int ret;
818         struct wim_lookup_table_entry *lte, *tmp;
819         WIMStruct *wim = ctx->wim;
820         struct wim_image_metadata *imd = wim_get_current_image_metadata(ctx->wim);
821
822         DEBUG("Closing all staging file descriptors.");
823         image_for_each_unhashed_stream_safe(lte, tmp, imd) {
824                 ret = inode_close_fds(lte->back_inode);
825                 if (ret)
826                         return ret;
827         }
828
829         DEBUG("Freeing entries for zero-length streams");
830         image_for_each_unhashed_stream_safe(lte, tmp, imd) {
831                 wimlib_assert(lte->unhashed);
832                 if (lte->size == 0) {
833                         struct wim_lookup_table_entry **back_ptr;
834                         back_ptr = retrieve_lte_pointer(lte);
835                         *back_ptr = NULL;
836                         list_del(&lte->unhashed_list);
837                         free_lookup_table_entry(lte);
838                 }
839         }
840
841         xml_update_image_info(wim, wim->current_image);
842         ret = wimlib_overwrite(wim, write_flags, 0, progress_func);
843         if (ret)
844                 ERROR("Failed to commit changes to mounted WIM image");
845         return ret;
846 }
847
848 /* Simple function that returns the concatenation of 2 strings. */
849 static char *
850 strcat_dup(const char *s1, const char *s2, size_t max_len)
851 {
852         size_t len = strlen(s1) + strlen(s2);
853         if (len > max_len)
854                 len = max_len;
855         char *p = MALLOC(len + 1);
856         if (!p)
857                 return NULL;
858         snprintf(p, len + 1, "%s%s", s1, s2);
859         return p;
860 }
861
862 static int
863 set_message_queue_names(struct wimfs_context *ctx, const char *mount_dir)
864 {
865         static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
866         static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
867         char *dir_path;
868         char *p;
869         int ret;
870
871         dir_path = realpath(mount_dir, NULL);
872         if (!dir_path) {
873                 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
874                 if (errno == ENOMEM)
875                         return WIMLIB_ERR_NOMEM;
876                 else
877                         return WIMLIB_ERR_NOTDIR;
878         }
879
880         for (p = dir_path; *p; p++)
881                 if (*p == '/')
882                         *p = 0xff;
883
884         ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
885                                                     NAME_MAX);
886         if (!ctx->unmount_to_daemon_mq_name) {
887                 ret = WIMLIB_ERR_NOMEM;
888                 goto out_free_dir_path;
889         }
890         ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
891                                                     NAME_MAX);
892         if (!ctx->daemon_to_unmount_mq_name) {
893                 ret = WIMLIB_ERR_NOMEM;
894                 goto out_free_unmount_to_daemon_mq_name;
895         }
896
897         ret = 0;
898         goto out_free_dir_path;
899 out_free_unmount_to_daemon_mq_name:
900         FREE(ctx->unmount_to_daemon_mq_name);
901         ctx->unmount_to_daemon_mq_name = NULL;
902 out_free_dir_path:
903         FREE(dir_path);
904         return ret;
905 }
906
907 static void
908 free_message_queue_names(struct wimfs_context *ctx)
909 {
910         FREE(ctx->unmount_to_daemon_mq_name);
911         FREE(ctx->daemon_to_unmount_mq_name);
912         ctx->unmount_to_daemon_mq_name = NULL;
913         ctx->daemon_to_unmount_mq_name = NULL;
914 }
915
916 /*
917  * Opens two POSIX message queue: one for sending messages from the unmount
918  * process to the daemon process, and one to go the other way.  The names of the
919  * message queues, which must be system-wide unique, are be based on the mount
920  * point.
921  *
922  * @daemon specifies whether the calling process is the filesystem daemon or the
923  * unmount process.
924  */
925 static int
926 open_message_queues(struct wimfs_context *ctx, bool daemon)
927 {
928         int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
929         int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
930         mode_t mode;
931         mode_t orig_umask;
932         int ret;
933
934         if (daemon) {
935                 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
936                 mode = 0600;
937         } else {
938                 mode = 0666;
939         }
940
941         orig_umask = umask(0000);
942         DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
943         ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
944                                             unmount_to_daemon_mq_flags, mode, NULL);
945
946         if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
947                 ERROR_WITH_ERRNO("mq_open()");
948                 ret = WIMLIB_ERR_MQUEUE;
949                 goto out;
950         }
951
952         DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
953         ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
954                                             daemon_to_unmount_mq_flags, mode, NULL);
955
956         if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
957                 ERROR_WITH_ERRNO("mq_open()");
958                 mq_close(ctx->unmount_to_daemon_mq);
959                 mq_unlink(ctx->unmount_to_daemon_mq_name);
960                 ctx->unmount_to_daemon_mq = (mqd_t)-1;
961                 ret = WIMLIB_ERR_MQUEUE;
962                 goto out;
963         }
964         ret = 0;
965 out:
966         umask(orig_umask);
967         return ret;
968 }
969
970 /* Try to determine the maximum message size of a message queue.  The return
971  * value is the maximum message size, or a guess of 8192 bytes if it cannot be
972  * determined. */
973 static long
974 mq_get_msgsize(mqd_t mq)
975 {
976         static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
977         FILE *fp;
978         struct mq_attr attr;
979         long msgsize;
980
981         if (mq_getattr(mq, &attr) == 0) {
982                 msgsize = attr.mq_msgsize;
983         } else {
984                 ERROR_WITH_ERRNO("mq_getattr()");
985                 ERROR("Attempting to read %s", msgsize_max_file);
986                 fp = fopen(msgsize_max_file, "rb");
987                 if (fp) {
988                         if (fscanf(fp, "%ld", &msgsize) != 1) {
989                                 ERROR("Assuming message size of 8192");
990                                 msgsize = 8192;
991                         }
992                         fclose(fp);
993                 } else {
994                         ERROR_WITH_ERRNO("Failed to open the file `%s'",
995                                          msgsize_max_file);
996                         ERROR("Assuming message size of 8192");
997                         msgsize = 8192;
998                 }
999         }
1000         return msgsize;
1001 }
1002
1003 static int
1004 get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
1005             void **mailbox_ret)
1006 {
1007         long msgsize;
1008         void *mailbox;
1009
1010         msgsize = mq_get_msgsize(mq);
1011
1012         if (msgsize < needed_msgsize) {
1013                 ERROR("Message queue max size must be at least %ld!",
1014                       needed_msgsize);
1015                 return WIMLIB_ERR_MQUEUE;
1016         }
1017
1018         mailbox = MALLOC(msgsize);
1019         if (!mailbox) {
1020                 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
1021                 return WIMLIB_ERR_NOMEM;
1022         }
1023         *msgsize_ret = msgsize;
1024         *mailbox_ret = mailbox;
1025         return 0;
1026 }
1027
1028 static void
1029 unlink_message_queues(struct wimfs_context *ctx)
1030 {
1031         mq_unlink(ctx->unmount_to_daemon_mq_name);
1032         mq_unlink(ctx->daemon_to_unmount_mq_name);
1033 }
1034
1035 /* Closes the message queues, which are allocated in static variables */
1036 static void
1037 close_message_queues(struct wimfs_context *ctx)
1038 {
1039         DEBUG("Closing message queues");
1040         mq_close(ctx->unmount_to_daemon_mq);
1041         ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1042         mq_close(ctx->daemon_to_unmount_mq);
1043         ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1044         unlink_message_queues(ctx);
1045 }
1046
1047
1048 struct unmount_msg_hdr {
1049         u32 min_version;
1050         u32 cur_version;
1051         u32 msg_type;
1052         u32 msg_size;
1053 } _packed_attribute;
1054
1055 struct msg_unmount_request {
1056         struct unmount_msg_hdr hdr;
1057         u32 unmount_flags;
1058         u8 want_progress_messages;
1059 } _packed_attribute;
1060
1061 struct msg_daemon_info {
1062         struct unmount_msg_hdr hdr;
1063         pid_t daemon_pid;
1064         u32 mount_flags;
1065 } _packed_attribute;
1066
1067 struct msg_unmount_finished {
1068         struct unmount_msg_hdr hdr;
1069         s32 status;
1070 } _packed_attribute;
1071
1072 struct msg_write_streams_progress {
1073         struct unmount_msg_hdr hdr;
1074         union wimlib_progress_info info;
1075 } _packed_attribute;
1076
1077 enum {
1078         MSG_TYPE_UNMOUNT_REQUEST,
1079         MSG_TYPE_DAEMON_INFO,
1080         MSG_TYPE_WRITE_STREAMS_PROGRESS,
1081         MSG_TYPE_UNMOUNT_FINISHED,
1082         MSG_TYPE_MAX,
1083 };
1084
1085 struct msg_handler_context_hdr {
1086         int timeout_seconds;
1087 };
1088
1089 struct unmount_msg_handler_context {
1090         struct msg_handler_context_hdr hdr;
1091         pid_t daemon_pid;
1092         int mount_flags;
1093         int status;
1094         wimlib_progress_func_t progress_func;
1095 };
1096
1097 struct daemon_msg_handler_context {
1098         struct msg_handler_context_hdr hdr;
1099         struct wimfs_context *wimfs_ctx;
1100 };
1101
1102 static int
1103 send_unmount_request_msg(mqd_t mq, int unmount_flags, u8 want_progress_messages)
1104 {
1105         DEBUG("Sending unmount request msg");
1106         struct msg_unmount_request msg = {
1107                 .hdr = {
1108                         .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1109                         .cur_version = WIMLIB_VERSION_CODE,
1110                         .msg_type    = MSG_TYPE_UNMOUNT_REQUEST,
1111                         .msg_size    = sizeof(msg),
1112                 },
1113                 .unmount_flags = unmount_flags,
1114                 .want_progress_messages = want_progress_messages,
1115         };
1116
1117         if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1118                 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1119                 return WIMLIB_ERR_MQUEUE;
1120         }
1121         return 0;
1122 }
1123
1124 static int
1125 send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1126 {
1127         DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1128               pid, mount_flags);
1129
1130         struct msg_daemon_info msg = {
1131                 .hdr = {
1132                         .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1133                         .cur_version = WIMLIB_VERSION_CODE,
1134                         .msg_type = MSG_TYPE_DAEMON_INFO,
1135                         .msg_size = sizeof(msg),
1136                 },
1137                 .daemon_pid = pid,
1138                 .mount_flags = mount_flags,
1139         };
1140         if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1141                 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1142                 return WIMLIB_ERR_MQUEUE;
1143         }
1144         return 0;
1145 }
1146
1147 static void
1148 send_unmount_finished_msg(mqd_t mq, int status)
1149 {
1150         DEBUG("Sending unmount finished msg");
1151         struct msg_unmount_finished msg = {
1152                 .hdr = {
1153                         .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1154                         .cur_version = WIMLIB_VERSION_CODE,
1155                         .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1156                         .msg_size = sizeof(msg),
1157                 },
1158                 .status = status,
1159         };
1160         if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1161                 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1162 }
1163
1164 static int
1165 unmount_progress_func(enum wimlib_progress_msg msg,
1166                       const union wimlib_progress_info *info)
1167 {
1168         if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1169                 struct msg_write_streams_progress msg = {
1170                         .hdr = {
1171                                 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1172                                 .cur_version = WIMLIB_VERSION_CODE,
1173                                 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1174                                 .msg_size = sizeof(msg),
1175                         },
1176                         .info = *info,
1177                 };
1178                 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1179                             (void*)&msg, sizeof(msg), 1))
1180                 {
1181                         ERROR_WITH_ERRNO("Failed to send progress information "
1182                                          "to unmount process");
1183                 }
1184         }
1185         return 0;
1186 }
1187
1188 static int
1189 msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1190 {
1191         const struct msg_unmount_request *msg = _msg;
1192         struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1193         struct wimfs_context *wimfs_ctx;
1194         int status = 0;
1195         int ret;
1196         int unmount_flags;
1197         wimlib_progress_func_t progress_func;
1198
1199         DEBUG("Handling unmount request msg");
1200
1201         wimfs_ctx = handler_ctx->wimfs_ctx;
1202         if (msg->hdr.msg_size < sizeof(*msg)) {
1203                 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1204                 goto out;
1205         }
1206
1207         unmount_flags = msg->unmount_flags;
1208         if (msg->want_progress_messages)
1209                 progress_func = unmount_progress_func;
1210         else
1211                 progress_func = NULL;
1212
1213         ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1214                                    wimfs_ctx->mount_flags);
1215         if (ret != 0) {
1216                 status = ret;
1217                 goto out;
1218         }
1219
1220         if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1221                 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1222                         int write_flags = 0;
1223                         if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1224                                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1225                         if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1226                                 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1227                         if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1228                                 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1229                         status = rebuild_wim(wimfs_ctx, write_flags,
1230                                              progress_func);
1231                 }
1232         } else {
1233                 DEBUG("Read-only mount");
1234                 status = 0;
1235         }
1236
1237 out:
1238         if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1239                 ret = delete_staging_dir(wimfs_ctx);
1240                 if (ret != 0) {
1241                         ERROR("Failed to delete the staging directory");
1242                         if (status == 0)
1243                                 status = ret;
1244                 }
1245         }
1246         wimfs_ctx->status = status;
1247         wimfs_ctx->have_status = true;
1248         return MSG_BREAK_LOOP;
1249 }
1250
1251 static int
1252 msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1253 {
1254         const struct msg_daemon_info *msg = _msg;
1255         struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1256
1257         DEBUG("Handling daemon info msg");
1258         if (msg->hdr.msg_size < sizeof(*msg))
1259                 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1260         handler_ctx->daemon_pid = msg->daemon_pid;
1261         handler_ctx->mount_flags = msg->mount_flags;
1262         handler_ctx->hdr.timeout_seconds = 1;
1263         DEBUG("pid of daemon is %d; mount flags were %#x",
1264               handler_ctx->daemon_pid,
1265               handler_ctx->mount_flags);
1266         return 0;
1267 }
1268
1269 static int
1270 msg_write_streams_progress_handler(const void *_msg, void *_handler_ctx)
1271 {
1272         const struct msg_write_streams_progress *msg = _msg;
1273         struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1274
1275         if (msg->hdr.msg_size < sizeof(*msg))
1276                 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1277         if (handler_ctx->progress_func) {
1278                 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1279                                            &msg->info);
1280         }
1281         return 0;
1282 }
1283
1284 static int
1285 msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1286 {
1287         const struct msg_unmount_finished *msg = _msg;
1288         struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1289
1290         DEBUG("Handling unmount finished message");
1291         if (msg->hdr.msg_size < sizeof(*msg))
1292                 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1293         handler_ctx->status = msg->status;
1294         DEBUG("status is %d", handler_ctx->status);
1295         return MSG_BREAK_LOOP;
1296 }
1297
1298 static int
1299 unmount_timed_out_cb(void *_handler_ctx)
1300 {
1301         const struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1302
1303         if (handler_ctx->daemon_pid == 0 ||
1304             (kill(handler_ctx->daemon_pid, 0) != 0 && errno == ESRCH))
1305         {
1306                 ERROR("The filesystem daemon has crashed!  Changes to the "
1307                       "WIM may not have been commited.");
1308                 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1309         }
1310
1311         DEBUG("Filesystem daemon is still alive... "
1312               "Waiting another %d seconds", handler_ctx->hdr.timeout_seconds);
1313         return 0;
1314 }
1315
1316 static int
1317 daemon_timed_out_cb(void *_handler_ctx)
1318 {
1319         ERROR("Timed out waiting for unmount request! "
1320               "Changes to the mounted WIM will not be committed.");
1321         return WIMLIB_ERR_TIMEOUT;
1322 }
1323
1324 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1325
1326 struct msg_handler_callbacks {
1327         int (*timed_out)(void * _handler_ctx);
1328         msg_handler_t msg_handlers[MSG_TYPE_MAX];
1329 };
1330
1331 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1332         .timed_out = unmount_timed_out_cb,
1333         .msg_handlers = {
1334                 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1335                 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1336                 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1337         },
1338 };
1339
1340 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1341         .timed_out = daemon_timed_out_cb,
1342         .msg_handlers = {
1343                 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1344         },
1345 };
1346
1347 static int
1348 receive_message(mqd_t mq,
1349                 struct msg_handler_context_hdr *handler_ctx,
1350                 const msg_handler_t msg_handlers[],
1351                 long mailbox_size, void *mailbox)
1352 {
1353         struct timeval now;
1354         struct timespec timeout;
1355         ssize_t bytes_received;
1356         struct unmount_msg_hdr *hdr;
1357         int ret;
1358
1359         gettimeofday(&now, NULL);
1360         timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1361         timeout.tv_nsec = now.tv_usec * 1000;
1362
1363         bytes_received = mq_timedreceive(mq, mailbox,
1364                                          mailbox_size, NULL, &timeout);
1365         hdr = mailbox;
1366         if (bytes_received == -1) {
1367                 if (errno == ETIMEDOUT) {
1368                         ret = WIMLIB_ERR_TIMEOUT;
1369                 } else {
1370                         ERROR_WITH_ERRNO("mq_timedreceive()");
1371                         ret = WIMLIB_ERR_MQUEUE;
1372                 }
1373         } else if (bytes_received < sizeof(*hdr) ||
1374                    bytes_received != hdr->msg_size) {
1375                 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1376         } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1377                 /*ERROR("Cannot understand the received message. "*/
1378                       /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1379                       /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1380                       /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1381                       /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1382                 ret = MSG_VERSION_TOO_HIGH;
1383         } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1384                 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1385         } else if (msg_handlers[hdr->msg_type] == NULL) {
1386                 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1387         } else {
1388                 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1389         }
1390         return ret;
1391 }
1392
1393 static int
1394 message_loop(mqd_t mq,
1395              const struct msg_handler_callbacks *callbacks,
1396              struct msg_handler_context_hdr *handler_ctx)
1397 {
1398         static const size_t MAX_MSG_SIZE = 512;
1399         long msgsize;
1400         void *mailbox;
1401         int ret;
1402
1403         DEBUG("Entering message loop");
1404
1405         ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1406         if (ret != 0)
1407                 return ret;
1408         while (1) {
1409                 ret = receive_message(mq, handler_ctx,
1410                                       callbacks->msg_handlers,
1411                                       msgsize, mailbox);
1412                 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1413                         continue;
1414                 } else if (ret == MSG_BREAK_LOOP) {
1415                         ret = 0;
1416                         break;
1417                 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1418                         if (callbacks->timed_out)
1419                                 ret = callbacks->timed_out(handler_ctx);
1420                         if (ret == 0)
1421                                 continue;
1422                         else
1423                                 break;
1424                 } else {
1425                         ERROR_WITH_ERRNO("Error communicating with "
1426                                          "filesystem daemon");
1427                         break;
1428                 }
1429         }
1430         FREE(mailbox);
1431         DEBUG("Exiting message loop");
1432         return ret;
1433 }
1434
1435 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1436  *
1437  * FUSE does not yet implement synchronous unmounts.  This means that fusermount
1438  * -u will return before the filesystem daemon returns from wimfs_destroy().
1439  *  This is partly what we want, because we need to send a message from this
1440  *  process to the filesystem daemon telling whether --commit was specified or
1441  *  not.  However, after that, the unmount process must wait for the filesystem
1442  *  daemon to finish writing the WIM file.
1443  */
1444 static int
1445 execute_fusermount(const char *dir, bool lazy)
1446 {
1447         pid_t pid;
1448         int ret;
1449         int status;
1450
1451         pid = fork();
1452         if (pid == -1) {
1453                 ERROR_WITH_ERRNO("Failed to fork()");
1454                 return WIMLIB_ERR_FORK;
1455         }
1456         if (pid == 0) {
1457                 /* Child */
1458                 char *argv[10];
1459                 char **argp = argv;
1460                 *argp++ = "fusermount";
1461                 if (lazy)
1462                         *argp++ = "-z";
1463                 *argp++ = "-u";
1464                 *argp++ = (char*)dir;
1465                 *argp = NULL;
1466                 execvp("fusermount", argv);
1467                 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1468                 exit(WIMLIB_ERR_FUSERMOUNT);
1469         }
1470
1471         /* Parent */
1472         ret = waitpid(pid, &status, 0);
1473         if (ret == -1) {
1474                 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1475                                  "terminate");
1476                 return WIMLIB_ERR_FUSERMOUNT;
1477         }
1478
1479         if (!WIFEXITED(status)) {
1480                 ERROR("'fusermount' did not terminate normally!");
1481                 return WIMLIB_ERR_FUSERMOUNT;
1482         }
1483
1484         status = WEXITSTATUS(status);
1485
1486         if (status == 0)
1487                 return 0;
1488
1489         if (status != WIMLIB_ERR_FUSERMOUNT)
1490                 return WIMLIB_ERR_FUSERMOUNT;
1491
1492         /* Try again, but with the `umount' program.  This is required on other
1493          * FUSE implementations such as FreeBSD's that do not have a
1494          * `fusermount' program. */
1495         ERROR("Falling back to 'umount'.  Note: you may need to be "
1496               "root for this to work");
1497         pid = fork();
1498         if (pid == -1) {
1499                 ERROR_WITH_ERRNO("Failed to fork()");
1500                 return WIMLIB_ERR_FORK;
1501         }
1502         if (pid == 0) {
1503                 /* Child */
1504                 char *argv[10];
1505                 char **argp = argv;
1506                 *argp++ = "umount";
1507                 if (lazy)
1508                         *argp++ = "-l";
1509                 *argp++ = (char*)dir;
1510                 *argp = NULL;
1511                 execvp("umount", argv);
1512                 ERROR_WITH_ERRNO("Failed to execute `umount'");
1513                 exit(WIMLIB_ERR_FUSERMOUNT);
1514         }
1515
1516         /* Parent */
1517         ret = waitpid(pid, &status, 0);
1518         if (ret == -1) {
1519                 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1520                                  "terminate");
1521                 return WIMLIB_ERR_FUSERMOUNT;
1522         }
1523
1524         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1525                 ERROR("`umount' did not successfully complete");
1526                 return WIMLIB_ERR_FUSERMOUNT;
1527         }
1528
1529         return 0;
1530 }
1531
1532 static int
1533 wimfs_chmod(const char *path, mode_t mask)
1534 {
1535         struct wim_dentry *dentry;
1536         struct wimfs_context *ctx = wimfs_get_context();
1537         int ret;
1538
1539         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1540                 return -EPERM;
1541
1542         ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1543                                      &dentry, NULL, NULL);
1544         if (ret)
1545                 return ret;
1546
1547         ret = inode_set_unix_data(dentry->d_inode, ctx->default_uid,
1548                                   ctx->default_gid, mask,
1549                                   ctx->wim->lookup_table, UNIX_DATA_MODE);
1550         return ret ? -ENOMEM : 0;
1551 }
1552
1553 static int
1554 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1555 {
1556         struct wim_dentry *dentry;
1557         struct wimfs_context *ctx = wimfs_get_context();
1558         int ret;
1559
1560         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1561                 return -EPERM;
1562
1563         ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1564                                      &dentry, NULL, NULL);
1565         if (ret)
1566                 return ret;
1567
1568         ret = inode_set_unix_data(dentry->d_inode, uid, gid,
1569                                   inode_default_unix_mode(dentry->d_inode),
1570                                   ctx->wim->lookup_table,
1571                                   UNIX_DATA_UID | UNIX_DATA_GID);
1572         return ret ? -ENOMEM : 0;
1573 }
1574
1575 /* Called when the filesystem is unmounted. */
1576 static void
1577 wimfs_destroy(void *p)
1578 {
1579         struct wimfs_context *wimfs_ctx = wimfs_get_context();
1580         if (open_message_queues(wimfs_ctx, true) == 0) {
1581                 struct daemon_msg_handler_context handler_ctx = {
1582                         .hdr = {
1583                                 .timeout_seconds = 5,
1584                         },
1585                         .wimfs_ctx = wimfs_ctx,
1586                 };
1587                 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1588                              &daemon_msg_handler_callbacks,
1589                              &handler_ctx.hdr);
1590         }
1591 }
1592
1593 static int
1594 wimfs_fgetattr(const char *path, struct stat *stbuf,
1595                struct fuse_file_info *fi)
1596 {
1597         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1598         return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1599 }
1600
1601 static int
1602 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1603 {
1604         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1605         int ret = ftruncate(fd->staging_fd.fd, size);
1606         if (ret)
1607                 return -errno;
1608         touch_inode(fd->f_inode);
1609         fd->f_lte->size = size;
1610         return 0;
1611 }
1612
1613 /*
1614  * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1615  */
1616 static int
1617 wimfs_getattr(const char *path, struct stat *stbuf)
1618 {
1619         struct wim_dentry *dentry;
1620         struct wim_lookup_table_entry *lte;
1621         int ret;
1622         struct wimfs_context *ctx = wimfs_get_context();
1623
1624         ret = wim_pathname_to_stream(ctx->wim, path,
1625                                      get_lookup_flags(ctx) |
1626                                         LOOKUP_FLAG_DIRECTORY_OK,
1627                                      &dentry, &lte, NULL);
1628         if (ret != 0)
1629                 return ret;
1630         return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1631 }
1632
1633 #ifdef ENABLE_XATTR
1634 /* Read an alternate data stream through the XATTR interface, or get its size */
1635 static int
1636 wimfs_getxattr(const char *path, const char *name, char *value,
1637                size_t size)
1638 {
1639         int ret;
1640         struct wim_inode *inode;
1641         struct wim_ads_entry *ads_entry;
1642         u64 stream_size;
1643         struct wim_lookup_table_entry *lte;
1644         struct wimfs_context *ctx = wimfs_get_context();
1645
1646         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1647                 return -ENOTSUP;
1648
1649         if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
1650                 return -ENOATTR;
1651         name += 5;
1652
1653         inode = wim_pathname_to_inode(ctx->wim, path);
1654         if (!inode)
1655                 return -errno;
1656
1657         ads_entry = inode_get_ads_entry(inode, name, NULL);
1658         if (!ads_entry)
1659                 return -ENOATTR;
1660
1661         lte = ads_entry->lte;
1662         stream_size = lte->size;
1663
1664         if (size == 0)
1665                 return stream_size;
1666
1667         if (stream_size > size)
1668                 return -ERANGE;
1669
1670         ret = read_full_stream_into_buf(lte, value);
1671         if (ret) {
1672                 if (errno)
1673                         return -errno;
1674                 else
1675                         return -EIO;
1676         }
1677         return stream_size;
1678 }
1679 #endif
1680
1681 /* Create a hard link */
1682 static int
1683 wimfs_link(const char *to, const char *from)
1684 {
1685         struct wim_dentry *from_dentry, *from_dentry_parent;
1686         const char *link_name;
1687         struct wim_inode *inode;
1688         WIMStruct *wim = wimfs_get_WIMStruct();
1689         int ret;
1690
1691         inode = wim_pathname_to_inode(wim, to);
1692         if (!inode)
1693                 return -errno;
1694
1695         if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1696                                    FILE_ATTRIBUTE_REPARSE_POINT))
1697                 return -EPERM;
1698
1699         from_dentry_parent = get_parent_dentry(wim, from, WIMLIB_CASE_SENSITIVE);
1700         if (!from_dentry_parent)
1701                 return -errno;
1702         if (!dentry_is_directory(from_dentry_parent))
1703                 return -ENOTDIR;
1704
1705         link_name = path_basename(from);
1706         if (get_dentry_child_with_name(from_dentry_parent, link_name,
1707                                        WIMLIB_CASE_SENSITIVE))
1708                 return -EEXIST;
1709
1710         ret = new_dentry(link_name, &from_dentry);
1711         if (ret)
1712                 return -ENOMEM;
1713
1714         inode->i_nlink++;
1715         inode_ref_streams(inode);
1716         from_dentry->d_inode = inode;
1717         inode_add_dentry(from_dentry, inode);
1718         dentry_add_child(from_dentry_parent, from_dentry);
1719         return 0;
1720 }
1721
1722 #ifdef ENABLE_XATTR
1723 static int
1724 wimfs_listxattr(const char *path, char *list, size_t size)
1725 {
1726         size_t needed_size;
1727         struct wim_inode *inode;
1728         struct wimfs_context *ctx = wimfs_get_context();
1729         u16 i;
1730         char *p;
1731         bool size_only = (size == 0);
1732
1733         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1734                 return -ENOTSUP;
1735
1736         /* List alternate data streams, or get the list size */
1737
1738         inode = wim_pathname_to_inode(ctx->wim, path);
1739         if (!inode)
1740                 return -errno;
1741
1742         p = list;
1743         for (i = 0; i < inode->i_num_ads; i++) {
1744
1745                 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1746                         continue;
1747
1748                 char *stream_name_mbs;
1749                 size_t stream_name_mbs_nbytes;
1750                 int ret;
1751
1752                 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
1753                                       inode->i_ads_entries[i].stream_name_nbytes,
1754                                       &stream_name_mbs,
1755                                       &stream_name_mbs_nbytes);
1756                 if (ret)
1757                         return -errno;
1758
1759                 needed_size = stream_name_mbs_nbytes + 6;
1760                 if (!size_only) {
1761                         if (needed_size > size) {
1762                                 FREE(stream_name_mbs);
1763                                 return -ERANGE;
1764                         }
1765                         sprintf(p, "user.%s", stream_name_mbs);
1766                         size -= needed_size;
1767                 }
1768                 p += needed_size;
1769                 FREE(stream_name_mbs);
1770         }
1771         return p - list;
1772 }
1773 #endif
1774
1775
1776 /* Create a directory in the WIM image. */
1777 static int
1778 wimfs_mkdir(const char *path, mode_t mode)
1779 {
1780         return create_dentry(fuse_get_context(), path, mode | S_IFDIR,
1781                              FILE_ATTRIBUTE_DIRECTORY, NULL);
1782 }
1783
1784 /* Create a regular file or alternate data stream in the WIM image. */
1785 static int
1786 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1787 {
1788         const char *stream_name;
1789         struct fuse_context *fuse_ctx = fuse_get_context();
1790         struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1791
1792         if (!S_ISREG(mode))
1793                 return -EPERM;
1794
1795         if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1796              && (stream_name = path_stream_name(path))) {
1797                 /* Make an alternate data stream */
1798                 struct wim_ads_entry *new_entry;
1799                 struct wim_inode *inode;
1800
1801                 char *p = (char*)stream_name - 1;
1802                 wimlib_assert(*p == ':');
1803                 *p = '\0';
1804
1805                 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1806                 if (!inode)
1807                         return -errno;
1808                 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1809                         return -ENOENT;
1810                 if (inode_get_ads_entry(inode, stream_name, NULL))
1811                         return -EEXIST;
1812                 new_entry = inode_add_ads(inode, stream_name);
1813                 if (!new_entry)
1814                         return -ENOMEM;
1815                 return 0;
1816         } else {
1817                 /* Make a normal file (not an alternate data stream) */
1818                 return create_dentry(fuse_ctx, path, mode | S_IFREG,
1819                                      FILE_ATTRIBUTE_NORMAL, NULL);
1820         }
1821 }
1822
1823 /* Open a file.  */
1824 static int
1825 wimfs_open(const char *path, struct fuse_file_info *fi)
1826 {
1827         struct wim_dentry *dentry;
1828         struct wim_lookup_table_entry *lte;
1829         int ret;
1830         struct wimfs_fd *fd;
1831         struct wim_inode *inode;
1832         u16 stream_idx;
1833         u32 stream_id;
1834         struct wimfs_context *ctx = wimfs_get_context();
1835         struct wim_lookup_table_entry **back_ptr;
1836
1837         ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
1838                                      &dentry, &lte, &stream_idx);
1839         if (ret)
1840                 return ret;
1841
1842         inode = dentry->d_inode;
1843
1844         if (stream_idx == 0) {
1845                 stream_id = 0;
1846                 back_ptr = &inode->i_lte;
1847         } else {
1848                 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1849                 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
1850         }
1851
1852         /* The file resource may be in the staging directory (read-write mounts
1853          * only) or in the WIM.  If it's in the staging directory, we need to
1854          * open a native file descriptor for the corresponding file.  Otherwise,
1855          * we can read the file resource directly from the WIM file if we are
1856          * opening it read-only, but we need to extract the resource to the
1857          * staging directory if we are opening it writable. */
1858
1859         if (flags_writable(fi->flags) &&
1860             (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1861                 u64 size = (lte) ? lte->size : 0;
1862                 ret = extract_resource_to_staging_dir(inode, stream_id,
1863                                                       &lte, size, ctx);
1864                 if (ret)
1865                         return ret;
1866                 *back_ptr = lte;
1867         }
1868
1869         ret = alloc_wimfs_fd(inode, stream_id, lte, &fd);
1870         if (ret)
1871                 return ret;
1872
1873         if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1874                 int raw_fd;
1875
1876                 raw_fd = open(lte->staging_file_name, fi->flags);
1877                 if (raw_fd < 0) {
1878                         int errno_save = errno;
1879                         close_wimfs_fd(fd);
1880                         return -errno_save;
1881                 }
1882                 filedes_init(&fd->staging_fd, raw_fd);
1883         }
1884         fi->fh = (uintptr_t)fd;
1885         return 0;
1886 }
1887
1888 /* Opens a directory. */
1889 static int
1890 wimfs_opendir(const char *path, struct fuse_file_info *fi)
1891 {
1892         struct wim_inode *inode;
1893         int ret;
1894         struct wimfs_fd *fd = NULL;
1895         struct wimfs_context *ctx = wimfs_get_context();
1896         WIMStruct *wim = ctx->wim;
1897
1898         inode = wim_pathname_to_inode(wim, path);
1899         if (!inode)
1900                 return -errno;
1901         if (!inode_is_directory(inode))
1902                 return -ENOTDIR;
1903         ret = alloc_wimfs_fd(inode, 0, NULL, &fd);
1904         fi->fh = (uintptr_t)fd;
1905         return ret;
1906 }
1907
1908
1909 /*
1910  * Read data from a file in the WIM or in the staging directory.
1911  */
1912 static int
1913 wimfs_read(const char *path, char *buf, size_t size,
1914            off_t offset, struct fuse_file_info *fi)
1915 {
1916         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1917         ssize_t ret;
1918         u64 stream_size;
1919
1920         if (!fd)
1921                 return -EBADF;
1922
1923         if (size == 0)
1924                 return 0;
1925
1926         if (fd->f_lte)
1927                 stream_size = fd->f_lte->size;
1928         else
1929                 stream_size = 0;
1930
1931         if (offset > stream_size)
1932                 return -EOVERFLOW;
1933
1934         size = min(size, stream_size - offset);
1935         if (size == 0)
1936                 return 0;
1937
1938         switch (fd->f_lte->resource_location) {
1939         case RESOURCE_IN_STAGING_FILE:
1940                 ret = raw_pread(&fd->staging_fd, buf, size, offset);
1941                 if (ret == -1)
1942                         ret = -errno;
1943                 break;
1944         case RESOURCE_IN_WIM:
1945                 if (read_partial_wim_stream_into_buf(fd->f_lte, size,
1946                                                      offset, buf))
1947                         ret = errno ? -errno : -EIO;
1948                 else
1949                         ret = size;
1950                 break;
1951         case RESOURCE_IN_ATTACHED_BUFFER:
1952                 memcpy(buf, fd->f_lte->attached_buffer + offset, size);
1953                 ret = size;
1954                 break;
1955         default:
1956                 ERROR("Invalid resource location");
1957                 ret = -EIO;
1958                 break;
1959         }
1960         return ret;
1961 }
1962
1963 struct fill_params {
1964         void *buf;
1965         fuse_fill_dir_t filler;
1966 };
1967
1968 static int
1969 dentry_fuse_fill(struct wim_dentry *dentry, void *arg)
1970 {
1971         struct fill_params *fill_params = arg;
1972
1973         char *file_name_mbs;
1974         size_t file_name_mbs_nbytes;
1975         int ret;
1976
1977         ret = utf16le_to_tstr(dentry->file_name,
1978                               dentry->file_name_nbytes,
1979                               &file_name_mbs,
1980                               &file_name_mbs_nbytes);
1981         if (ret)
1982                 return -errno;
1983
1984         ret = fill_params->filler(fill_params->buf, file_name_mbs, NULL, 0);
1985         FREE(file_name_mbs);
1986         return ret;
1987 }
1988
1989 /* Fills in the entries of the directory specified by @path using the
1990  * FUSE-provided function @filler.  */
1991 static int
1992 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1993               off_t offset, struct fuse_file_info *fi)
1994 {
1995         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1996         struct wim_inode *inode;
1997
1998         if (!fd)
1999                 return -EBADF;
2000
2001         inode = fd->f_inode;
2002
2003         struct fill_params fill_params = {
2004                 .buf = buf,
2005                 .filler = filler,
2006         };
2007
2008         filler(buf, ".", NULL, 0);
2009         filler(buf, "..", NULL, 0);
2010
2011         return for_dentry_in_rbtree(inode->i_children.rb_node,
2012                                     dentry_fuse_fill, &fill_params);
2013 }
2014
2015
2016 static int
2017 wimfs_readlink(const char *path, char *buf, size_t buf_len)
2018 {
2019         struct wimfs_context *ctx = wimfs_get_context();
2020         struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
2021         int ret;
2022         if (!inode)
2023                 return -errno;
2024         if (!inode_is_symlink(inode))
2025                 return -EINVAL;
2026         if (buf_len == 0)
2027                 return -EINVAL;
2028         ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
2029         if (ret >= 0) {
2030                 wimlib_assert(ret <= buf_len - 1);
2031                 buf[ret] = '\0';
2032                 ret = 0;
2033         } else if (ret == -ENAMETOOLONG) {
2034                 buf[buf_len - 1] = '\0';
2035         }
2036         return ret;
2037 }
2038
2039 /* Close a file. */
2040 static int
2041 wimfs_release(const char *path, struct fuse_file_info *fi)
2042 {
2043         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2044         return close_wimfs_fd(fd);
2045 }
2046
2047 /* Close a directory */
2048 static int
2049 wimfs_releasedir(const char *path, struct fuse_file_info *fi)
2050 {
2051         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2052         return close_wimfs_fd(fd);
2053 }
2054
2055 #ifdef ENABLE_XATTR
2056 /* Remove an alternate data stream through the XATTR interface */
2057 static int
2058 wimfs_removexattr(const char *path, const char *name)
2059 {
2060         struct wim_inode *inode;
2061         struct wim_ads_entry *ads_entry;
2062         u16 ads_idx;
2063         struct wimfs_context *ctx = wimfs_get_context();
2064
2065         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2066                 return -ENOTSUP;
2067
2068         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2069                 return -ENOATTR;
2070         name += 5;
2071
2072         inode = wim_pathname_to_inode(ctx->wim, path);
2073         if (!inode)
2074                 return -errno;
2075
2076         ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2077         if (!ads_entry)
2078                 return -ENOATTR;
2079         inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2080         return 0;
2081 }
2082 #endif
2083
2084 /* Renames a file or directory.  See rename (3) */
2085 static int
2086 wimfs_rename(const char *from, const char *to)
2087 {
2088         return rename_wim_path(wimfs_get_WIMStruct(), from, to,
2089                                WIMLIB_CASE_SENSITIVE);
2090 }
2091
2092 /* Remove a directory */
2093 static int
2094 wimfs_rmdir(const char *path)
2095 {
2096         struct wim_dentry *dentry;
2097         WIMStruct *wim = wimfs_get_WIMStruct();
2098
2099         dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2100         if (!dentry)
2101                 return -errno;
2102
2103         if (!dentry_is_directory(dentry))
2104                 return -ENOTDIR;
2105
2106         if (dentry_has_children(dentry))
2107                 return -ENOTEMPTY;
2108
2109         remove_dentry(dentry, wim->lookup_table);
2110         return 0;
2111 }
2112
2113 #ifdef ENABLE_XATTR
2114 /* Write an alternate data stream through the XATTR interface */
2115 static int
2116 wimfs_setxattr(const char *path, const char *name,
2117                const char *value, size_t size, int flags)
2118 {
2119         struct wim_ads_entry *existing_ads_entry;
2120         struct wim_inode *inode;
2121         u16 ads_idx;
2122         struct wimfs_context *ctx = wimfs_get_context();
2123         int ret;
2124
2125         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2126                 return -ENOTSUP;
2127
2128         if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
2129                 return -ENOATTR;
2130         name += 5;
2131
2132         inode = wim_pathname_to_inode(ctx->wim, path);
2133         if (!inode)
2134                 return -errno;
2135
2136         existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2137         if (existing_ads_entry) {
2138                 if (flags & XATTR_CREATE)
2139                         return -EEXIST;
2140         } else {
2141                 if (flags & XATTR_REPLACE)
2142                         return -ENOATTR;
2143         }
2144
2145         ret = inode_add_ads_with_data(inode, name, value,
2146                                       size, ctx->wim->lookup_table);
2147         if (ret == 0) {
2148                 if (existing_ads_entry)
2149                         inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2150         } else {
2151                 ret = -ENOMEM;
2152         }
2153         return ret;
2154 }
2155 #endif
2156
2157 static int
2158 wimfs_symlink(const char *to, const char *from)
2159 {
2160         struct fuse_context *fuse_ctx = fuse_get_context();
2161         struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2162         struct wim_dentry *dentry;
2163         int ret;
2164
2165         ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777,
2166                             FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2167         if (ret == 0) {
2168                 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2169                 ret = wim_inode_set_symlink(dentry->d_inode, to,
2170                                             wimfs_ctx->wim->lookup_table);
2171                 if (ret) {
2172                         remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2173                         if (ret == WIMLIB_ERR_NOMEM)
2174                                 ret = -ENOMEM;
2175                         else
2176                                 ret = -EIO;
2177                 }
2178         }
2179         return ret;
2180 }
2181
2182
2183 /* Reduce the size of a file */
2184 static int
2185 wimfs_truncate(const char *path, off_t size)
2186 {
2187         struct wim_dentry *dentry;
2188         struct wim_lookup_table_entry *lte;
2189         int ret;
2190         u16 stream_idx;
2191         u32 stream_id;
2192         struct wim_inode *inode;
2193         struct wimfs_context *ctx = wimfs_get_context();
2194
2195         ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2196                                      &dentry, &lte, &stream_idx);
2197
2198         if (ret != 0)
2199                 return ret;
2200
2201         if (lte == NULL && size == 0)
2202                 return 0;
2203
2204         if (lte != NULL && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2205                 ret = truncate(lte->staging_file_name, size);
2206                 if (ret)
2207                         ret = -errno;
2208                 else
2209                         lte->size = size;
2210         } else {
2211                 /* File in WIM.  Extract it to the staging directory, but only
2212                  * the first @size bytes of it. */
2213                 struct wim_lookup_table_entry **back_ptr;
2214
2215                 inode = dentry->d_inode;
2216                 if (stream_idx == 0) {
2217                         stream_id = 0;
2218                         back_ptr = &inode->i_lte;
2219                 } else {
2220                         stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2221                         back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
2222                 }
2223                 ret = extract_resource_to_staging_dir(inode, stream_id,
2224                                                       &lte, size, ctx);
2225                 *back_ptr = lte;
2226         }
2227         return ret;
2228 }
2229
2230 /* Unlink a non-directory or alternate data stream */
2231 static int
2232 wimfs_unlink(const char *path)
2233 {
2234         struct wim_dentry *dentry;
2235         struct wim_lookup_table_entry *lte;
2236         int ret;
2237         u16 stream_idx;
2238         struct wimfs_context *ctx = wimfs_get_context();
2239
2240         ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2241                                      &dentry, &lte, &stream_idx);
2242
2243         if (ret != 0)
2244                 return ret;
2245
2246         if (inode_stream_name_nbytes(dentry->d_inode, stream_idx) == 0)
2247                 remove_dentry(dentry, ctx->wim->lookup_table);
2248         else
2249                 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2250                                  ctx->wim->lookup_table);
2251         return 0;
2252 }
2253
2254 #ifdef HAVE_UTIMENSAT
2255 /*
2256  * Change the timestamp on a file dentry.
2257  *
2258  * Note that alternate data streams do not have their own timestamps.
2259  */
2260 static int
2261 wimfs_utimens(const char *path, const struct timespec tv[2])
2262 {
2263         struct wim_dentry *dentry;
2264         struct wim_inode *inode;
2265         WIMStruct *wim = wimfs_get_WIMStruct();
2266
2267         dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2268         if (!dentry)
2269                 return -errno;
2270         inode = dentry->d_inode;
2271
2272         if (tv[0].tv_nsec != UTIME_OMIT) {
2273                 if (tv[0].tv_nsec == UTIME_NOW)
2274                         inode->i_last_access_time = get_wim_timestamp();
2275                 else
2276                         inode->i_last_access_time = timespec_to_wim_timestamp(tv[0]);
2277         }
2278         if (tv[1].tv_nsec != UTIME_OMIT) {
2279                 if (tv[1].tv_nsec == UTIME_NOW)
2280                         inode->i_last_write_time = get_wim_timestamp();
2281                 else
2282                         inode->i_last_write_time = timespec_to_wim_timestamp(tv[1]);
2283         }
2284         return 0;
2285 }
2286 #else /* HAVE_UTIMENSAT */
2287 static int
2288 wimfs_utime(const char *path, struct utimbuf *times)
2289 {
2290         struct wim_dentry *dentry;
2291         struct wim_inode *inode;
2292         WIMStruct *wim = wimfs_get_WIMStruct();
2293
2294         dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2295         if (!dentry)
2296                 return -errno;
2297         inode = dentry->d_inode;
2298
2299         inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2300         inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2301         return 0;
2302 }
2303 #endif /* !HAVE_UTIMENSAT */
2304
2305 /* Writes to a file in the WIM filesystem.
2306  * It may be an alternate data stream, but here we don't even notice because we
2307  * just get a lookup table entry. */
2308 static int
2309 wimfs_write(const char *path, const char *buf, size_t size,
2310             off_t offset, struct fuse_file_info *fi)
2311 {
2312         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2313         int ret;
2314
2315         if (!fd)
2316                 return -EBADF;
2317
2318         wimlib_assert(fd->f_lte != NULL);
2319         wimlib_assert(fd->f_lte->staging_file_name != NULL);
2320         wimlib_assert(filedes_valid(&fd->staging_fd));
2321         wimlib_assert(fd->f_inode != NULL);
2322
2323         /* Write the data. */
2324         ret = raw_pwrite(&fd->staging_fd, buf, size, offset);
2325         if (ret == -1)
2326                 return -errno;
2327
2328         /* Update file size */
2329         if (offset + size > fd->f_lte->size) {
2330                 DEBUG("Update file size %"PRIu64 " => %"PRIu64"",
2331                       fd->f_lte->size, offset + size);
2332                 fd->f_lte->size = offset + size;
2333         }
2334
2335         /* Update timestamps */
2336         touch_inode(fd->f_inode);
2337         return ret;
2338 }
2339
2340 static struct fuse_operations wimfs_operations = {
2341         .chmod       = wimfs_chmod,
2342         .chown       = wimfs_chown,
2343         .destroy     = wimfs_destroy,
2344         .fgetattr    = wimfs_fgetattr,
2345         .ftruncate   = wimfs_ftruncate,
2346         .getattr     = wimfs_getattr,
2347 #ifdef ENABLE_XATTR
2348         .getxattr    = wimfs_getxattr,
2349 #endif
2350         .link        = wimfs_link,
2351 #ifdef ENABLE_XATTR
2352         .listxattr   = wimfs_listxattr,
2353 #endif
2354         .mkdir       = wimfs_mkdir,
2355         .mknod       = wimfs_mknod,
2356         .open        = wimfs_open,
2357         .opendir     = wimfs_opendir,
2358         .read        = wimfs_read,
2359         .readdir     = wimfs_readdir,
2360         .readlink    = wimfs_readlink,
2361         .release     = wimfs_release,
2362         .releasedir  = wimfs_releasedir,
2363 #ifdef ENABLE_XATTR
2364         .removexattr = wimfs_removexattr,
2365 #endif
2366         .rename      = wimfs_rename,
2367         .rmdir       = wimfs_rmdir,
2368 #ifdef ENABLE_XATTR
2369         .setxattr    = wimfs_setxattr,
2370 #endif
2371         .symlink     = wimfs_symlink,
2372         .truncate    = wimfs_truncate,
2373         .unlink      = wimfs_unlink,
2374 #ifdef HAVE_UTIMENSAT
2375         .utimens     = wimfs_utimens,
2376 #else
2377         .utime       = wimfs_utime,
2378 #endif
2379         .write       = wimfs_write,
2380
2381         /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2382          * no need to have the file path provided on operations such as read()
2383          * where only the file descriptor is needed. */
2384 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2385         .flag_nullpath_ok = 1,
2386 #endif
2387 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2388         .flag_nopath = 1,
2389         .flag_utime_omit_ok = 1,
2390 #endif
2391 };
2392
2393
2394 /* API function documented in wimlib.h  */
2395 WIMLIBAPI int
2396 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2397                    int mount_flags, const char *staging_dir)
2398 {
2399         int argc;
2400         char *argv[16];
2401         int ret;
2402         char *dir_copy;
2403         struct wim_image_metadata *imd;
2404         struct wimfs_context ctx;
2405         struct wim_inode *inode;
2406
2407         DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2408               wim, image, dir, mount_flags);
2409
2410         if (!wim || !dir)
2411                 return WIMLIB_ERR_INVALID_PARAM;
2412
2413         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2414                 ret = can_delete_from_wim(wim);
2415                 if (ret)
2416                         return ret;
2417         }
2418
2419         ret = select_wim_image(wim, image);
2420         if (ret)
2421                 return ret;
2422
2423         DEBUG("Selected image %d", image);
2424
2425         imd = wim_get_current_image_metadata(wim);
2426
2427         if (imd->modified) {
2428                 /* wimfs_read() only supports a limited number of stream
2429                  * locations, not including RESOURCE_IN_FILE_ON_DISK,
2430                  * RESOURCE_IN_NTFS_VOLUME, etc. that might appear if files were
2431                  * added to the WIM image.  */
2432                 ERROR("Cannot mount an image with newly added files!");
2433                 return WIMLIB_ERR_INVALID_PARAM;
2434         }
2435
2436         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2437                 ret = lock_wim(wim, wim->in_fd.fd);
2438                 if (ret)
2439                         return ret;
2440         }
2441
2442         /* Use default stream interface if one was not specified */
2443         if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2444                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2445                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2446                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2447
2448         DEBUG("Initializing struct wimfs_context");
2449         init_wimfs_context(&ctx);
2450         ctx.wim = wim;
2451         ctx.mount_flags = mount_flags;
2452         ctx.image_inode_list = &imd->inode_list;
2453         ctx.default_uid = getuid();
2454         ctx.default_gid = getgid();
2455         wimlib_assert(list_empty(&imd->unhashed_streams));
2456         if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2457                 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2458
2459         DEBUG("Unlinking message queues in case they already exist");
2460         ret = set_message_queue_names(&ctx, dir);
2461         if (ret)
2462                 goto out_unlock;
2463         unlink_message_queues(&ctx);
2464
2465         DEBUG("Preparing arguments to fuse_main()");
2466
2467         dir_copy = STRDUP(dir);
2468         if (!dir_copy)
2469                 goto out_free_message_queue_names;
2470
2471         argc = 0;
2472         argv[argc++] = "wimlib";
2473         argv[argc++] = dir_copy;
2474
2475         /* disable multi-threaded operation */
2476         argv[argc++] = "-s";
2477
2478         if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2479                 argv[argc++] = "-d";
2480
2481         /*
2482          * We provide the use_ino option to the FUSE mount because we are going
2483          * to assign inode numbers ourselves. */
2484         char optstring[256] =
2485                 "use_ino"
2486                 ",subtype=wimfs"
2487                 ",attr_timeout=0"
2488 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2489                 ",hard_remove"
2490 #endif
2491                 ",default_permissions"
2492                 ;
2493         argv[argc++] = "-o";
2494         argv[argc++] = optstring;
2495         if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2496                 /* Read-write mount.  Make the staging directory */
2497                 ret = make_staging_dir(&ctx, staging_dir);
2498                 if (ret)
2499                         goto out_free_dir_copy;
2500         } else {
2501                 /* Read-only mount */
2502                 strcat(optstring, ",ro");
2503         }
2504         if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2505                 strcat(optstring, ",allow_other");
2506         argv[argc] = NULL;
2507
2508 #ifdef ENABLE_DEBUG
2509         {
2510                 int i;
2511                 DEBUG("FUSE command line (argc = %d): ", argc);
2512                 for (i = 0; i < argc; i++) {
2513                         fputs(argv[i], stdout);
2514                         putchar(' ');
2515                 }
2516                 putchar('\n');
2517                 fflush(stdout);
2518         }
2519 #endif
2520
2521         /* Mark dentry tree as modified if read-write mount. */
2522         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2523                 imd->modified = 1;
2524
2525         /* Resolve the lookup table entries for every inode in the image, and
2526          * assign inode numbers */
2527         DEBUG("Resolving lookup table entries and assigning inode numbers");
2528         ctx.next_ino = 1;
2529         image_for_each_inode(inode, imd)
2530                 inode->i_ino = ctx.next_ino++;
2531         DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2532
2533         DEBUG("Calling fuse_main()");
2534
2535         ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2536
2537         DEBUG("Returned from fuse_main() (ret = %d)", ret);
2538
2539         if (ret) {
2540                 ret = WIMLIB_ERR_FUSE;
2541         } else {
2542                 if (ctx.have_status)
2543                         ret = ctx.status;
2544                 else
2545                         ret = WIMLIB_ERR_TIMEOUT;
2546         }
2547         if (ctx.daemon_to_unmount_mq != (mqd_t)(-1)) {
2548                 send_unmount_finished_msg(ctx.daemon_to_unmount_mq, ret);
2549                 close_message_queues(&ctx);
2550         }
2551
2552         /* Try to delete the staging directory if a deletion wasn't yet
2553          * attempted due to an earlier error */
2554         if (ctx.staging_dir_name)
2555                 delete_staging_dir(&ctx);
2556 out_free_dir_copy:
2557         FREE(dir_copy);
2558 out_unlock:
2559         wim->wim_locked = 0;
2560 out_free_message_queue_names:
2561         free_message_queue_names(&ctx);
2562         return ret;
2563 }
2564
2565 /* API function documented in wimlib.h  */
2566 WIMLIBAPI int
2567 wimlib_unmount_image(const char *dir, int unmount_flags,
2568                      wimlib_progress_func_t progress_func)
2569 {
2570         int ret;
2571         struct wimfs_context wimfs_ctx;
2572
2573         init_wimfs_context(&wimfs_ctx);
2574
2575         ret = set_message_queue_names(&wimfs_ctx, dir);
2576         if (ret != 0)
2577                 goto out;
2578
2579         ret = open_message_queues(&wimfs_ctx, false);
2580         if (ret != 0)
2581                 goto out_free_message_queue_names;
2582
2583         ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2584                                        unmount_flags,
2585                                        progress_func != NULL);
2586         if (ret != 0)
2587                 goto out_close_message_queues;
2588
2589         ret = execute_fusermount(dir, (unmount_flags & WIMLIB_UNMOUNT_FLAG_LAZY) != 0);
2590         if (ret != 0)
2591                 goto out_close_message_queues;
2592
2593         struct unmount_msg_handler_context handler_ctx = {
2594                 .hdr = {
2595                         .timeout_seconds = 5,
2596                 },
2597                 .daemon_pid = 0,
2598                 .progress_func = progress_func,
2599         };
2600
2601         ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2602                            &unmount_msg_handler_callbacks,
2603                            &handler_ctx.hdr);
2604         if (ret == 0)
2605                 ret = handler_ctx.status;
2606 out_close_message_queues:
2607         close_message_queues(&wimfs_ctx);
2608 out_free_message_queue_names:
2609         free_message_queue_names(&wimfs_ctx);
2610 out:
2611         return ret;
2612 }
2613
2614 #else /* WITH_FUSE */
2615
2616
2617 static int
2618 mount_unsupported_error(void)
2619 {
2620 #if defined(__WIN32__)
2621         ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2622 #else
2623         ERROR("wimlib was compiled with --without-fuse, which disables support "
2624               "for mounting WIMs.");
2625 #endif
2626         return WIMLIB_ERR_UNSUPPORTED;
2627 }
2628
2629 WIMLIBAPI int
2630 wimlib_unmount_image(const tchar *dir, int unmount_flags,
2631                      wimlib_progress_func_t progress_func)
2632 {
2633         return mount_unsupported_error();
2634 }
2635
2636 WIMLIBAPI int
2637 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2638                    int mount_flags, const tchar *staging_dir)
2639 {
2640         return mount_unsupported_error();
2641 }
2642
2643 #endif /* !WITH_FUSE */