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