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