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