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