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