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