]> wimlib.net Git - wimlib/blob - src/mount_image.c
lzx_record_match(): Remove dead assignments to formatted_offset
[wimlib] / src / mount_image.c
1 /*
2  * mount_image.c
3  *
4  * This file implements mounting of WIM files using FUSE, which stands for
5  * Filesystem in Userspace.  FUSE allows a filesystem to be implemented in a
6  * userspace process by implementing the filesystem primitives--- read(),
7  * write(), readdir(), etc.
8  */
9
10 /*
11  * Copyright (C) 2012 Eric Biggers
12  *
13  * This file is part of wimlib, a library for working with WIM files.
14  *
15  * wimlib is free software; you can redistribute it and/or modify it under the
16  * terms of the GNU General Public License as published by the Free
17  * Software Foundation; either version 3 of the License, or (at your option)
18  * any later version.
19  *
20  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22  * A PARTICULAR PURPOSE. See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with wimlib; if not, see http://www.gnu.org/licenses/.
27  */
28
29 #include "wimlib_internal.h"
30
31 #ifdef WITH_FUSE
32
33 #include "sha1.h"
34 #include "lookup_table.h"
35 #include "xml.h"
36 #include "buffer_io.h"
37 #include "timestamp.h"
38 #include <limits.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/wait.h>
42 #define FUSE_USE_VERSION 26
43 #include <errno.h>
44 #include <string.h>
45 #include <sys/time.h>
46 #include <fuse.h>
47 #include <ftw.h>
48 #include <mqueue.h>
49 #include <utime.h>
50 #include <signal.h>
51
52 #ifdef ENABLE_XATTR
53 #include <attr/xattr.h>
54 #endif
55
56 #define MSG_VERSION_TOO_HIGH    -1
57 #define MSG_BREAK_LOOP          -2
58
59 /* File descriptor to a file open on the WIM filesystem. */
60 struct wimfs_fd {
61         struct wim_inode *f_inode;
62         struct wim_lookup_table_entry *f_lte;
63         int staging_fd;
64         u16 idx;
65         u32 stream_id;
66 };
67
68 struct wimfs_context {
69         /* The WIMStruct for the mounted WIM. */
70         WIMStruct *wim;
71
72         /* Name of the staging directory for a read-write mount.  Whenever a new file is
73          * created, it is done so in the staging directory.  Furthermore, whenever a
74          * file in the WIM is modified, it is extracted to the staging directory.  If
75          * changes are commited when the WIM is unmounted, the file resources are merged
76          * in from the staging directory when writing the new WIM. */
77         char *staging_dir_name;
78         size_t staging_dir_name_len;
79
80         /* Flags passed to wimlib_mount(). */
81         int mount_flags;
82
83         /* Default flags to use when looking up a WIM dentry (depends on whether
84          * the Windows interface to alternate data streams is being used or
85          * not). */
86         int default_lookup_flags;
87
88         /* Next inode number to be assigned.  Note: I didn't bother with a
89          * bitmap of free inode numbers since this isn't even a "real"
90          * filesystem anyway. */
91         u64 next_ino;
92
93         /* List of lookup table entries for files in the staging directory */
94         struct list_head staging_list;
95
96         /* List of inodes in the mounted image */
97         struct hlist_head *image_inode_list;
98
99         /* Name and message queue descriptors for message queues between the
100          * filesystem daemon process and the unmount process.  These are used
101          * when the filesystem is unmounted and the process running
102          * wimlib_unmount_image() (i.e. the `imagex unmount' command) needs to
103          * communicate with the filesystem daemon running fuse_main() (i.e. the
104          * daemon created by the `imagex mount' or `imagex mountrw' commands */
105         char *unmount_to_daemon_mq_name;
106         char *daemon_to_unmount_mq_name;
107         mqd_t unmount_to_daemon_mq;
108         mqd_t daemon_to_unmount_mq;
109 };
110
111 static void init_wimfs_context(struct wimfs_context *ctx)
112 {
113         memset(ctx, 0, sizeof(*ctx));
114         ctx->unmount_to_daemon_mq = (mqd_t)-1;
115         ctx->daemon_to_unmount_mq = (mqd_t)-1;
116         INIT_LIST_HEAD(&ctx->staging_list);
117 }
118
119 static inline struct wimfs_context *wimfs_get_context()
120 {
121         return (struct wimfs_context*)fuse_get_context()->private_data;
122 }
123
124 static inline WIMStruct *wimfs_get_WIMStruct()
125 {
126         return wimfs_get_context()->wim;
127 }
128
129 static inline bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
130 {
131         return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
132 }
133
134 static inline int get_lookup_flags(const struct wimfs_context *ctx)
135 {
136         return ctx->default_lookup_flags;
137 }
138
139 /* Returns nonzero if write permission is requested on the file open flags */
140 static inline int flags_writable(int open_flags)
141 {
142         return open_flags & (O_RDWR | O_WRONLY);
143 }
144
145 /*
146  * Allocate a file descriptor for a stream.
147  *
148  * @inode:      inode containing the stream we're opening
149  * @stream_id:  ID of the stream we're opening
150  * @lte:        Lookup table entry for the stream (may be NULL)
151  * @fd_ret:     Return the allocated file descriptor if successful.
152  * @readonly:   True if this is a read-only mount.
153  *
154  * Return 0 iff successful or error code if unsuccessful.
155  */
156 static int alloc_wimfs_fd(struct wim_inode *inode,
157                           u32 stream_id,
158                           struct wim_lookup_table_entry *lte,
159                           struct wimfs_fd **fd_ret,
160                           bool readonly)
161 {
162         static const u16 fds_per_alloc = 8;
163         static const u16 max_fds = 0xffff;
164         int ret;
165
166         pthread_mutex_lock(&inode->i_mutex);
167
168         DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
169               "(open = %u, allocated = %u)",
170               stream_id, inode->i_ino, inode->i_num_opened_fds,
171               inode->i_num_allocated_fds);
172
173         if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
174                 struct wimfs_fd **fds;
175                 u16 num_new_fds;
176
177                 if (inode->i_num_allocated_fds == max_fds) {
178                         ret = -EMFILE;
179                         goto out;
180                 }
181                 num_new_fds = min(fds_per_alloc,
182                                   max_fds - inode->i_num_allocated_fds);
183
184                 fds = REALLOC(inode->i_fds,
185                               (inode->i_num_allocated_fds + num_new_fds) *
186                                 sizeof(inode->i_fds[0]));
187                 if (!fds) {
188                         ret = -ENOMEM;
189                         goto out;
190                 }
191                 memset(&fds[inode->i_num_allocated_fds], 0,
192                        num_new_fds * sizeof(fds[0]));
193                 inode->i_fds = fds;
194                 inode->i_num_allocated_fds += num_new_fds;
195         }
196         for (u16 i = 0; ; i++) {
197                 if (!inode->i_fds[i]) {
198                         struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
199                         if (!fd) {
200                                 ret = -ENOMEM;
201                                 break;
202                         }
203                         fd->f_inode    = inode;
204                         fd->f_lte      = lte;
205                         fd->staging_fd = -1;
206                         fd->idx        = i;
207                         fd->stream_id  = stream_id;
208                         *fd_ret        = fd;
209                         inode->i_fds[i]  = fd;
210                         inode->i_num_opened_fds++;
211                         if (lte && !readonly)
212                                 lte->num_opened_fds++;
213                         DEBUG("Allocated fd (idx = %u)", fd->idx);
214                         ret = 0;
215                         break;
216                 }
217         }
218 out:
219         pthread_mutex_unlock(&inode->i_mutex);
220         return ret;
221 }
222
223 static void inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
224 {
225         wimlib_assert(inode != NULL);
226
227         pthread_mutex_lock(&inode->i_mutex);
228
229         wimlib_assert(fd->f_inode == inode);
230         wimlib_assert(inode->i_num_opened_fds != 0);
231         wimlib_assert(fd->idx < inode->i_num_allocated_fds);
232         wimlib_assert(inode->i_fds[fd->idx] == fd);
233
234         inode->i_fds[fd->idx] = NULL;
235         FREE(fd);
236         if (--inode->i_num_opened_fds == 0 && inode->i_nlink == 0) {
237                 pthread_mutex_unlock(&inode->i_mutex);
238                 free_inode(inode);
239         } else {
240                 pthread_mutex_unlock(&inode->i_mutex);
241         }
242 }
243
244 static int lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
245 {
246         wimlib_assert(fd->f_lte == lte);
247
248         if (!lte) /* Empty stream with no lookup table entry */
249                 return 0;
250
251         /* Close staging file descriptor if needed. */
252
253         if (lte->resource_location == RESOURCE_IN_STAGING_FILE
254              && fd->staging_fd != -1)
255         {
256                 if (close(fd->staging_fd) != 0) {
257                         ERROR_WITH_ERRNO("Failed to close staging file");
258                         return -errno;
259                 }
260         }
261         lte_decrement_num_opened_fds(lte);
262         return 0;
263 }
264
265 /* Close a file descriptor. */
266 static int close_wimfs_fd(struct wimfs_fd *fd)
267 {
268         int ret;
269         DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
270               fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
271               fd->f_inode->i_num_allocated_fds);
272         ret = lte_put_fd(fd->f_lte, fd);
273         if (ret != 0)
274                 return ret;
275
276         inode_put_fd(fd->f_inode, fd);
277         return 0;
278 }
279
280 /*
281  * Add a new dentry with a new inode to a WIM image.
282  *
283  * @ctx:         Context for the mounted WIM image.
284  * @path:        Path to create the dentry at.
285  * @dentry_ret:  Return the pointer to the dentry if successful.
286  *
287  * Returns 0 on success, or negative error number on failure.
288  */
289 static int create_dentry(struct wimfs_context *ctx, const char *path,
290                          struct wim_dentry **dentry_ret)
291 {
292         struct wim_dentry *parent;
293         struct wim_dentry *new;
294         const char *basename;
295
296         parent = get_parent_dentry(ctx->wim, path);
297         if (!parent)
298                 return -errno;
299
300         if (!dentry_is_directory(parent))
301                 return -ENOTDIR;
302
303         basename = path_basename(path);
304         if (get_dentry_child_with_name(parent, basename))
305                 return -EEXIST;
306
307         new = new_dentry_with_inode(basename);
308         if (!new)
309                 return -errno;
310
311         new->d_inode->i_resolved = 1;
312         new->d_inode->i_ino = ctx->next_ino++;
313         dentry_add_child(parent, new);
314         hlist_add_head(&new->d_inode->i_hlist, ctx->image_inode_list);
315         *dentry_ret = new;
316         return 0;
317 }
318
319 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
320  * corresponding inode.
321  *
322  * If there are no remaining references to the inode either through dentries or
323  * open file descriptors, the inode is freed.  Otherwise, the inode is not
324  * removed, but the dentry is unlinked and freed.
325  *
326  * Either way, all lookup table entries referenced by the inode have their
327  * reference count decremented.  If a lookup table entry has no open file
328  * descriptors and no references remaining, it is freed, and the corresponding
329  * staging file is unlinked.
330  */
331 static void remove_dentry(struct wim_dentry *dentry,
332                           struct wim_lookup_table *lookup_table)
333 {
334         struct wim_inode *inode = dentry->d_inode;
335         struct wim_lookup_table_entry *lte;
336         unsigned i;
337
338         for (i = 0; i <= inode->i_num_ads; i++) {
339                 lte = inode_stream_lte_resolved(inode, i);
340                 if (lte)
341                         lte_decrement_refcnt(lte, lookup_table);
342         }
343         unlink_dentry(dentry);
344         put_dentry(dentry);
345 }
346
347 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
348  *
349  * The lookup table entry tells us which stream in the inode we are statting.
350  * For a named data stream, everything returned is the same as the unnamed data
351  * stream except possibly the size and block count. */
352 static int inode_to_stbuf(const struct wim_inode *inode,
353                           const struct wim_lookup_table_entry *lte,
354                           struct stat *stbuf)
355 {
356         if (inode_is_symlink(inode))
357                 stbuf->st_mode = S_IFLNK | 0777;
358         else if (inode_is_directory(inode))
359                 stbuf->st_mode = S_IFDIR | 0755;
360         else
361                 stbuf->st_mode = S_IFREG | 0755;
362
363         stbuf->st_ino   = (ino_t)inode->i_ino;
364         stbuf->st_nlink = inode->i_nlink;
365         stbuf->st_uid   = getuid();
366         stbuf->st_gid   = getgid();
367
368         if (lte) {
369                 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
370                         struct stat native_stat;
371                         if (stat(lte->staging_file_name, &native_stat) != 0) {
372                                 DEBUG("Failed to stat `%s': %m",
373                                       lte->staging_file_name);
374                                 return -errno;
375                         }
376                         stbuf->st_size = native_stat.st_size;
377                 } else {
378                         stbuf->st_size = wim_resource_size(lte);
379                 }
380         } else {
381                 stbuf->st_size = 0;
382         }
383
384         stbuf->st_atime   = wim_timestamp_to_unix(inode->i_last_access_time);
385         stbuf->st_mtime   = wim_timestamp_to_unix(inode->i_last_write_time);
386         stbuf->st_ctime   = wim_timestamp_to_unix(inode->i_creation_time);
387         stbuf->st_blocks  = (stbuf->st_size + 511) / 512;
388         return 0;
389 }
390
391 /* Creates a new staging file and returns its file descriptor opened for
392  * writing.
393  *
394  * @name_ret: A location into which the a pointer to the newly allocated name of
395  *            the staging file is stored.
396  *
397  * @ctx:      Context for the WIM filesystem; this provides the name of the
398  *            staging directory.
399  *
400  * On success, returns the file descriptor for the staging file, opened for
401  * writing.  On failure, returns -1 and sets errno.
402  */
403 static int create_staging_file(char **name_ret, struct wimfs_context *ctx)
404 {
405         size_t name_len;
406         char *name;
407         struct stat stbuf;
408         int fd;
409         int errno_save;
410
411         static const size_t STAGING_FILE_NAME_LEN = 20;
412
413         name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
414         name = MALLOC(name_len + 1);
415         if (!name) {
416                 errno = ENOMEM;
417                 return -1;
418         }
419
420         do {
421
422                 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
423                 name[ctx->staging_dir_name_len] = '/';
424                 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
425                                                 STAGING_FILE_NAME_LEN);
426                 name[name_len] = '\0';
427
428
429         /* Just in case, verify that the randomly generated name doesn't name an
430          * existing file, and try again if so  */
431         } while (stat(name, &stbuf) == 0);
432
433         if (errno != ENOENT) /* other error?! */
434                 return -1;
435
436         /* doesn't exist--- ok */
437
438         DEBUG("Creating staging file `%s'", name);
439
440         fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
441         if (fd == -1) {
442                 errno_save = errno;
443                 FREE(name);
444                 errno = errno_save;
445         } else {
446                 *name_ret = name;
447         }
448         return fd;
449 }
450
451 /*
452  * Extract a WIM resource to the staging directory.
453  *
454  * @inode:  Inode that contains the stream we are extracting
455  *
456  * @stream_id: Identifier for the stream (it stays constant even if the indices
457  * of the stream entries are changed)
458  *
459  * @lte: Pointer to pointer to the lookup table entry for the stream we need to
460  * extract, or NULL if there was no lookup table entry present for the stream
461  *
462  * @size:  Number of bytes of the stream we want to extract (this supports the
463  * wimfs_truncate() function).  It may be more than the actual stream length, in
464  * which case the extra space is filled with zeroes.
465  *
466  * @ctx:  Context for the WIM filesystem.
467  *
468  * Returns 0 on success or a negative error code on failure.
469  */
470 static int extract_resource_to_staging_dir(struct wim_inode *inode,
471                                            u32 stream_id,
472                                            struct wim_lookup_table_entry **lte,
473                                            off_t size,
474                                            struct wimfs_context *ctx)
475 {
476         char *staging_file_name;
477         int ret;
478         int fd;
479         struct wim_lookup_table_entry *old_lte, *new_lte;
480         off_t extract_size;
481
482         DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
483               "stream id %"PRIu32, inode->i_ino, stream_id);
484
485         old_lte = *lte;
486
487         wimlib_assert(old_lte == NULL ||
488                       old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
489
490         /* Create the staging file */
491         fd = create_staging_file(&staging_file_name, ctx);
492         if (fd == -1)
493                 return -errno;
494
495         /* Extract the stream to the staging file (possibly truncated) */
496         if (old_lte) {
497                 extract_size = min(wim_resource_size(old_lte), size);
498                 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
499         } else {
500                 ret = 0;
501                 extract_size = 0;
502         }
503
504         /* In the case of truncate() to more than the file length, extend the
505          * file with zeroes by calling ftruncate() on the underlying staging
506          * file */
507         if (ret == 0 && size > extract_size)
508                 ret = ftruncate(fd, size);
509
510         /* Close the staging file descriptor and check for errors.  If there's
511          * an error, unlink the staging file. */
512         if (ret != 0 || close(fd) != 0) {
513                 if (errno != 0)
514                         ret = -errno;
515                 else
516                         ret = -EIO;
517                 close(fd);
518                 goto out_delete_staging_file;
519         }
520
521         /* Now deal with the lookup table entries.  We may be able to re-use the
522          * existing entry, but we may have to create a new one instead. */
523
524         if (old_lte && inode->i_nlink == old_lte->refcnt) {
525                 /* The reference count of the existing lookup table entry is the
526                  * same as the link count of the inode that contains the stream
527                  * we're opening.  Therefore, ALL the references to the lookup
528                  * table entry correspond to the stream we're trying to extract,
529                  * so the lookup table entry can be re-used.  */
530                 DEBUG("Re-using lookup table entry");
531                 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
532                 new_lte = old_lte;
533         } else {
534                 if (old_lte) {
535                         /* There's an existing lookup table entry, but its
536                          * reference count is greater than the link count for
537                          * the inode containing a stream we're opening.
538                          * Therefore, we need to split the lookup table entry.
539                          */
540                         wimlib_assert(old_lte->refcnt > inode->i_nlink);
541                         DEBUG("Splitting lookup table entry "
542                               "(inode->i_nlink = %u, old_lte->refcnt = %u)",
543                               inode->i_nlink, old_lte->refcnt);
544                 }
545
546                 new_lte = new_lookup_table_entry();
547                 if (!new_lte) {
548                         ret = -ENOMEM;
549                         goto out_delete_staging_file;
550                 }
551
552                 /* There may already be open file descriptors to this stream if
553                  * it's previously been opened read-only, but just now we're
554                  * opening it read-write.  Identify those file descriptors and
555                  * change their lookup table entry pointers to point to the new
556                  * lookup table entry, and open staging file descriptors for
557                  * them.
558                  *
559                  * At the same time, we need to count the number of these opened
560                  * file descriptors to the new lookup table entry.  If there's
561                  * an old lookup table entry, this number needs to be subtracted
562                  * from the fd's opened to the old entry. */
563                 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
564                         struct wimfs_fd *fd = inode->i_fds[i];
565                         if (fd) {
566                                 if (fd->stream_id == stream_id) {
567                                         wimlib_assert(fd->f_lte == old_lte);
568                                         wimlib_assert(fd->staging_fd == -1);
569                                         fd->f_lte = new_lte;
570                                         new_lte->num_opened_fds++;
571                                         fd->staging_fd = open(staging_file_name, O_RDONLY);
572                                         if (fd->staging_fd == -1) {
573                                                 ret = -errno;
574                                                 goto out_revert_fd_changes;
575                                         }
576                                 }
577                                 j++;
578                         }
579                 }
580                 DEBUG("%hu fd's were already opened to the file we extracted",
581                       new_lte->num_opened_fds);
582                 if (old_lte) {
583                         old_lte->num_opened_fds -= new_lte->num_opened_fds;
584                         old_lte->refcnt -= inode->i_nlink;
585                 }
586         }
587
588         new_lte->refcnt                       = inode->i_nlink;
589         new_lte->resource_location            = RESOURCE_IN_STAGING_FILE;
590         new_lte->staging_file_name            = staging_file_name;
591         new_lte->lte_inode                    = inode;
592         random_hash(new_lte->hash);
593
594         if (stream_id == 0)
595                 inode->i_lte = new_lte;
596         else
597                 for (u16 i = 0; i < inode->i_num_ads; i++)
598                         if (inode->i_ads_entries[i].stream_id == stream_id)
599                                 inode->i_ads_entries[i].lte = new_lte;
600
601         lookup_table_insert(ctx->wim->lookup_table, new_lte);
602         list_add(&new_lte->staging_list, &ctx->staging_list);
603         *lte = new_lte;
604         return 0;
605 out_revert_fd_changes:
606         for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
607                 struct wimfs_fd *fd = inode->i_fds[i];
608                 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
609                         fd->f_lte = old_lte;
610                         if (fd->staging_fd != -1) {
611                                 close(fd->staging_fd);
612                                 fd->staging_fd = -1;
613                         }
614                         j++;
615                 }
616         }
617         free_lookup_table_entry(new_lte);
618 out_delete_staging_file:
619         unlink(staging_file_name);
620         FREE(staging_file_name);
621         return ret;
622 }
623
624 /*
625  * Creates a randomly named staging directory and saves its name in the
626  * filesystem context structure.
627  */
628 static int make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
629 {
630         static const size_t random_suffix_len = 10;
631         static const char *common_suffix = ".staging";
632         static const size_t common_suffix_len = 8;
633
634         char *staging_dir_name = NULL;
635         size_t staging_dir_name_len;
636         size_t prefix_len;
637         const char *wim_basename;
638         char *real_user_prefix = NULL;
639         int ret;
640
641         if (user_prefix) {
642                 real_user_prefix = realpath(user_prefix, NULL);
643                 if (!real_user_prefix) {
644                         ERROR_WITH_ERRNO("Could not resolve `%s'",
645                                          real_user_prefix);
646                         ret = WIMLIB_ERR_NOTDIR;
647                         goto out;
648                 }
649                 wim_basename = path_basename(ctx->wim->filename);
650                 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
651         } else {
652                 prefix_len = strlen(ctx->wim->filename);
653         }
654
655         staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
656
657         staging_dir_name = MALLOC(staging_dir_name_len + 1);
658         if (!staging_dir_name) {
659                 ret = WIMLIB_ERR_NOMEM;
660                 goto out;
661         }
662
663         if (real_user_prefix)
664                 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
665         else
666                 strcpy(staging_dir_name, ctx->wim->filename);
667
668         strcat(staging_dir_name, common_suffix);
669
670         randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
671                                         random_suffix_len);
672
673         staging_dir_name[staging_dir_name_len] = '\0';
674
675         if (mkdir(staging_dir_name, 0700) != 0) {
676                 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
677                                  staging_dir_name);
678                 ret = WIMLIB_ERR_MKDIR;
679         } else {
680                 ret = 0;
681         }
682 out:
683         FREE(real_user_prefix);
684         if (ret == 0) {
685                 ctx->staging_dir_name = staging_dir_name;
686                 ctx->staging_dir_name_len = staging_dir_name_len;
687         } else {
688                 FREE(staging_dir_name);
689         }
690         return ret;
691 }
692
693 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
694                                     int typeflag, struct FTW *ftwbuf)
695 {
696         if (remove(fpath) == 0)
697                 return 0;
698         else {
699                 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
700                 return WIMLIB_ERR_DELETE_STAGING_DIR;
701         }
702 }
703
704 /*
705  * Deletes the staging directory and all the files contained in it.
706  */
707 static int delete_staging_dir(struct wimfs_context *ctx)
708 {
709         int ret;
710         ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
711                    10, FTW_DEPTH);
712         FREE(ctx->staging_dir_name);
713         ctx->staging_dir_name = NULL;
714         return ret;
715 }
716
717 static void inode_update_lte_ptr(struct wim_inode *inode,
718                                  struct wim_lookup_table_entry *old_lte,
719                                  struct wim_lookup_table_entry *new_lte)
720 {
721         if (inode->i_lte == old_lte) {
722                 inode->i_lte = new_lte;
723         } else {
724                 for (unsigned i = 0; i < inode->i_num_ads; i++) {
725                         if (inode->i_ads_entries[i].lte == old_lte) {
726                                 inode->i_ads_entries[i].lte = new_lte;
727                                 break;
728                         }
729                 }
730         }
731 }
732
733 static int update_lte_of_staging_file(struct wim_lookup_table_entry *lte,
734                                       struct wim_lookup_table *table)
735 {
736         struct wim_lookup_table_entry *duplicate_lte;
737         int ret;
738         u8 hash[SHA1_HASH_SIZE];
739         struct stat stbuf;
740
741         ret = sha1sum(lte->staging_file_name, hash);
742         if (ret != 0)
743                 return ret;
744         lookup_table_unlink(table, lte);
745         duplicate_lte = __lookup_resource(table, hash);
746         if (duplicate_lte) {
747                 /* Merge duplicate lookup table entries */
748                 duplicate_lte->refcnt += lte->refcnt;
749                 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
750                 free_lookup_table_entry(lte);
751         } else {
752                 if (stat(lte->staging_file_name, &stbuf) != 0) {
753                         ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
754                         return WIMLIB_ERR_STAT;
755                 }
756                 if (stbuf.st_size == 0) {
757                         /* Zero-length stream.  No lookup table entry needed. */
758                         inode_update_lte_ptr(lte->lte_inode, lte, NULL);
759                         free_lookup_table_entry(lte);
760                 } else {
761                         wimlib_assert(&lte->file_on_disk == &lte->staging_file_name);
762                         lte->resource_entry.original_size = stbuf.st_size;
763                         lte->resource_entry.size = stbuf.st_size;
764                         lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
765                         lte->file_on_disk_fp = NULL;
766                         copy_hash(lte->hash, hash);
767                         lookup_table_insert(table, lte);
768                 }
769         }
770         return 0;
771 }
772
773 static int inode_close_fds(struct wim_inode *inode)
774 {
775         u16 num_opened_fds = inode->i_num_opened_fds;
776         for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
777                 struct wimfs_fd *fd = inode->i_fds[i];
778                 if (fd) {
779                         wimlib_assert(fd->f_inode == inode);
780                         int ret = close_wimfs_fd(fd);
781                         if (ret != 0)
782                                 return ret;
783                         j++;
784                 }
785         }
786         return 0;
787 }
788
789 /* Overwrites the WIM file, with changes saved. */
790 static int rebuild_wim(struct wimfs_context *ctx, int write_flags,
791                        wimlib_progress_func_t progress_func)
792 {
793         int ret;
794         struct wim_lookup_table_entry *lte, *tmp;
795         WIMStruct *w = ctx->wim;
796
797         DEBUG("Closing all staging file descriptors.");
798         list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
799                 ret = inode_close_fds(lte->lte_inode);
800                 if (ret != 0)
801                         return ret;
802         }
803
804         DEBUG("Calculating SHA1 checksums for all new staging files.");
805         list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
806                 ret = update_lte_of_staging_file(lte, w->lookup_table);
807                 if (ret != 0)
808                         return ret;
809         }
810
811         xml_update_image_info(w, w->current_image);
812         ret = wimlib_overwrite(w, write_flags, 0, progress_func);
813         if (ret != 0)
814                 ERROR("Failed to commit changes to mounted WIM image");
815         return ret;
816 }
817
818
819
820 /* Simple function that returns the concatenation of 2 strings. */
821 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
822 {
823         size_t len = strlen(s1) + strlen(s2);
824         if (len > max_len)
825                 len = max_len;
826         char *p = MALLOC(len + 1);
827         if (!p)
828                 return NULL;
829         snprintf(p, len + 1, "%s%s", s1, s2);
830         return p;
831 }
832
833 static int set_message_queue_names(struct wimfs_context *ctx,
834                                    const char *mount_dir)
835 {
836         static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
837         static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
838         char *dir_path;
839         char *p;
840         int ret;
841
842         dir_path = realpath(mount_dir, NULL);
843         if (!dir_path) {
844                 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
845                 if (errno == ENOMEM)
846                         return WIMLIB_ERR_NOMEM;
847                 else
848                         return WIMLIB_ERR_NOTDIR;
849         }
850
851         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_DIRECTORY |
1617                                    FILE_ATTRIBUTE_REPARSE_POINT))
1618                 return -EPERM;
1619
1620         from_dentry_parent = get_parent_dentry(w, from);
1621         if (!from_dentry_parent)
1622                 return -errno;
1623         if (!dentry_is_directory(from_dentry_parent))
1624                 return -ENOTDIR;
1625
1626         link_name = path_basename(from);
1627         if (get_dentry_child_with_name(from_dentry_parent, link_name))
1628                 return -EEXIST;
1629         from_dentry = new_dentry(link_name);
1630         if (!from_dentry)
1631                 return -errno;
1632
1633         inode_add_dentry(from_dentry, inode);
1634         from_dentry->d_inode = inode;
1635         inode->i_nlink++;
1636
1637         for (i = 0; i <= inode->i_num_ads; i++) {
1638                 lte = inode_stream_lte_resolved(inode, i);
1639                 if (lte)
1640                         lte->refcnt++;
1641         }
1642         dentry_add_child(from_dentry_parent, from_dentry);
1643         return 0;
1644 }
1645
1646 #ifdef ENABLE_XATTR
1647 static int wimfs_listxattr(const char *path, char *list, size_t size)
1648 {
1649         size_t needed_size;
1650         struct wim_inode *inode;
1651         struct wimfs_context *ctx = wimfs_get_context();
1652         u16 i;
1653         char *p;
1654
1655         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1656                 return -ENOTSUP;
1657
1658         /* List alternate data streams, or get the list size */
1659
1660         inode = wim_pathname_to_inode(ctx->wim, path);
1661         if (!inode)
1662                 return -errno;
1663
1664         if (size == 0) {
1665                 needed_size = 0;
1666                 for (i = 0; i < inode->i_num_ads; i++)
1667                         needed_size += inode->i_ads_entries[i].stream_name_utf8_len + 6;
1668                 return needed_size;
1669         } else {
1670                 p = list;
1671                 for (i = 0; i < inode->i_num_ads; i++) {
1672                         needed_size = inode->i_ads_entries[i].stream_name_utf8_len + 6;
1673                         if (needed_size > size)
1674                                 return -ERANGE;
1675                         p += sprintf(p, "user.%s",
1676                                      inode->i_ads_entries[i].stream_name_utf8) + 1;
1677                         size -= needed_size;
1678                 }
1679                 return p - list;
1680         }
1681 }
1682 #endif
1683
1684
1685 /* Create a directory in the WIM.
1686  * @mode is currently ignored.  */
1687 static int wimfs_mkdir(const char *path, mode_t mode)
1688 {
1689         struct wim_dentry *dentry;
1690         int ret;
1691
1692         ret = create_dentry(wimfs_get_context(), path, &dentry);
1693         if (ret == 0)
1694                 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1695         return ret;
1696 }
1697
1698 /* Create a regular file in the WIM.
1699  * @mode is currently ignored.  */
1700 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1701 {
1702         const char *stream_name;
1703         struct wimfs_context *ctx = wimfs_get_context();
1704         if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1705              && (stream_name = path_stream_name(path))) {
1706                 /* Make an alternate data stream */
1707                 struct wim_ads_entry *new_entry;
1708                 struct wim_inode *inode;
1709
1710                 char *p = (char*)stream_name - 1;
1711                 wimlib_assert(*p == ':');
1712                 *p = '\0';
1713
1714                 inode = wim_pathname_to_inode(ctx->wim, path);
1715                 if (!inode)
1716                         return -errno;
1717                 if (inode->i_attributes &
1718                     (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1719                         return -ENOENT;
1720                 if (inode_get_ads_entry(inode, stream_name, NULL))
1721                         return -EEXIST;
1722                 new_entry = inode_add_ads(inode, stream_name);
1723                 if (!new_entry)
1724                         return -ENOMEM;
1725                 return 0;
1726         } else {
1727                 struct wim_dentry *dentry;
1728                 int ret;
1729
1730                 /* Make a normal file (not an alternate data stream) */
1731                 ret = create_dentry(ctx, path, &dentry);
1732                 if (ret == 0)
1733                         dentry->d_inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
1734                 return ret;
1735         }
1736 }
1737
1738
1739 /* Open a file.  */
1740 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1741 {
1742         struct wim_dentry *dentry;
1743         struct wim_lookup_table_entry *lte;
1744         int ret;
1745         struct wimfs_fd *fd;
1746         struct wim_inode *inode;
1747         u16 stream_idx;
1748         u32 stream_id;
1749         struct wimfs_context *ctx = wimfs_get_context();
1750
1751         ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1752                               &dentry, &lte, &stream_idx);
1753         if (ret != 0)
1754                 return ret;
1755
1756         inode = dentry->d_inode;
1757
1758         if (stream_idx == 0)
1759                 stream_id = 0;
1760         else
1761                 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1762
1763         /* The file resource may be in the staging directory (read-write mounts
1764          * only) or in the WIM.  If it's in the staging directory, we need to
1765          * open a native file descriptor for the corresponding file.  Otherwise,
1766          * we can read the file resource directly from the WIM file if we are
1767          * opening it read-only, but we need to extract the resource to the
1768          * staging directory if we are opening it writable. */
1769
1770         if (flags_writable(fi->flags) &&
1771             (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1772                 u64 size = (lte) ? wim_resource_size(lte) : 0;
1773                 ret = extract_resource_to_staging_dir(inode, stream_id,
1774                                                       &lte, size, ctx);
1775                 if (ret != 0)
1776                         return ret;
1777         }
1778
1779         ret = alloc_wimfs_fd(inode, stream_id, lte, &fd,
1780                              wimfs_ctx_readonly(ctx));
1781         if (ret != 0)
1782                 return ret;
1783
1784         if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1785                 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1786                 if (fd->staging_fd == -1) {
1787                         int errno_save = errno;
1788                         close_wimfs_fd(fd);
1789                         return -errno_save;
1790                 }
1791         }
1792         fi->fh = (uintptr_t)fd;
1793         return 0;
1794 }
1795
1796 /* Opens a directory. */
1797 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1798 {
1799         struct wim_inode *inode;
1800         int ret;
1801         struct wimfs_fd *fd = NULL;
1802         struct wimfs_context *ctx = wimfs_get_context();
1803         WIMStruct *w = ctx->wim;
1804
1805         inode = wim_pathname_to_inode(w, path);
1806         if (!inode)
1807                 return -errno;
1808         if (!inode_is_directory(inode))
1809                 return -ENOTDIR;
1810         ret = alloc_wimfs_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1811         fi->fh = (uintptr_t)fd;
1812         return ret;
1813 }
1814
1815
1816 /*
1817  * Read data from a file in the WIM or in the staging directory.
1818  */
1819 static int wimfs_read(const char *path, char *buf, size_t size,
1820                       off_t offset, struct fuse_file_info *fi)
1821 {
1822         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1823         ssize_t ret;
1824
1825         if (!fd)
1826                 return -EBADF;
1827
1828         if (!fd->f_lte) /* Empty stream with no lookup table entry */
1829                 return 0;
1830
1831         if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1832                 /* Read from staging file */
1833
1834                 wimlib_assert(fd->f_lte->staging_file_name);
1835                 wimlib_assert(fd->staging_fd != -1);
1836
1837                 DEBUG("Seek to offset %"PRIu64, offset);
1838
1839                 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1840                         return -errno;
1841                 ret = read(fd->staging_fd, buf, size);
1842                 if (ret == -1)
1843                         return -errno;
1844                 return ret;
1845         } else {
1846                 /* Read from WIM */
1847                 u64 res_size = wim_resource_size(fd->f_lte);
1848                 if (offset > res_size)
1849                         return -EOVERFLOW;
1850                 size = min(size, res_size - offset);
1851                 if (read_wim_resource(fd->f_lte, (u8*)buf,
1852                                       size, offset,
1853                                       WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1854                         return -EIO;
1855                 return size;
1856         }
1857 }
1858
1859 struct fill_params {
1860         void *buf;
1861         fuse_fill_dir_t filler;
1862 };
1863
1864 static int dentry_fuse_fill(struct wim_dentry *dentry, void *arg)
1865 {
1866         struct fill_params *fill_params = arg;
1867         return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1868                                    NULL, 0);
1869 }
1870
1871 /* Fills in the entries of the directory specified by @path using the
1872  * FUSE-provided function @filler.  */
1873 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1874                          off_t offset, struct fuse_file_info *fi)
1875 {
1876         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1877         struct wim_inode *inode;
1878
1879         if (!fd)
1880                 return -EBADF;
1881
1882         inode = fd->f_inode;
1883
1884         struct fill_params fill_params = {
1885                 .buf = buf,
1886                 .filler = filler,
1887         };
1888
1889         filler(buf, ".", NULL, 0);
1890         filler(buf, "..", NULL, 0);
1891
1892         return for_dentry_in_rbtree(inode->i_children.rb_node,
1893                                     dentry_fuse_fill, &fill_params);
1894 }
1895
1896
1897 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1898 {
1899         struct wimfs_context *ctx = wimfs_get_context();
1900         struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
1901         int ret;
1902         if (!inode)
1903                 return -errno;
1904         if (!inode_is_symlink(inode))
1905                 return -EINVAL;
1906
1907         ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1908                              WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1909         if (ret > 0)
1910                 ret = 0;
1911         return ret;
1912 }
1913
1914 /* Close a file. */
1915 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1916 {
1917         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1918         return close_wimfs_fd(fd);
1919 }
1920
1921 /* Close a directory */
1922 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1923 {
1924         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1925         return close_wimfs_fd(fd);
1926 }
1927
1928 #ifdef ENABLE_XATTR
1929 /* Remove an alternate data stream through the XATTR interface */
1930 static int wimfs_removexattr(const char *path, const char *name)
1931 {
1932         struct wim_inode *inode;
1933         struct wim_ads_entry *ads_entry;
1934         u16 ads_idx;
1935         struct wimfs_context *ctx = wimfs_get_context();
1936
1937         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1938                 return -ENOTSUP;
1939
1940         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1941                 return -ENOATTR;
1942         name += 5;
1943
1944         inode = wim_pathname_to_inode(ctx->wim, path);
1945         if (!inode)
1946                 return -errno;
1947
1948         ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1949         if (!ads_entry)
1950                 return -ENOATTR;
1951         inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1952         return 0;
1953 }
1954 #endif
1955
1956 /* Renames a file or directory.  See rename (3) */
1957 static int wimfs_rename(const char *from, const char *to)
1958 {
1959         struct wim_dentry *src;
1960         struct wim_dentry *dst;
1961         struct wim_dentry *parent_of_dst;
1962         WIMStruct *w = wimfs_get_WIMStruct();
1963         int ret;
1964
1965         /* This rename() implementation currently only supports actual files
1966          * (not alternate data streams) */
1967
1968         src = get_dentry(w, from);
1969         if (!src)
1970                 return -errno;
1971
1972         dst = get_dentry(w, to);
1973
1974         if (dst) {
1975                 /* Destination file exists */
1976
1977                 if (src == dst) /* Same file */
1978                         return 0;
1979
1980                 if (!dentry_is_directory(src)) {
1981                         /* Cannot rename non-directory to directory. */
1982                         if (dentry_is_directory(dst))
1983                                 return -EISDIR;
1984                 } else {
1985                         /* Cannot rename directory to a non-directory or a non-empty
1986                          * directory */
1987                         if (!dentry_is_directory(dst))
1988                                 return -ENOTDIR;
1989                         if (inode_has_children(dst->d_inode))
1990                                 return -ENOTEMPTY;
1991                 }
1992                 parent_of_dst = dst->parent;
1993         } else {
1994                 /* Destination does not exist */
1995                 parent_of_dst = get_parent_dentry(w, to);
1996                 if (!parent_of_dst)
1997                         return -errno;
1998
1999                 if (!dentry_is_directory(parent_of_dst))
2000                         return -ENOTDIR;
2001         }
2002
2003         ret = set_dentry_name(src, path_basename(to));
2004         if (ret != 0)
2005                 return -ENOMEM;
2006         if (dst)
2007                 remove_dentry(dst, w->lookup_table);
2008         unlink_dentry(src);
2009         dentry_add_child(parent_of_dst, src);
2010         return 0;
2011 }
2012
2013 /* Remove a directory */
2014 static int wimfs_rmdir(const char *path)
2015 {
2016         struct wim_dentry *parent, *dentry;
2017         WIMStruct *w = wimfs_get_WIMStruct();
2018
2019         dentry = get_dentry(w, path);
2020         if (!dentry)
2021                 return -errno;
2022
2023         if (!dentry_is_directory(dentry))
2024                 return -ENOTDIR;
2025
2026         if (dentry_has_children(dentry))
2027                 return -ENOTEMPTY;
2028
2029         remove_dentry(dentry, w->lookup_table);
2030         return 0;
2031 }
2032
2033 #ifdef ENABLE_XATTR
2034 /* Write an alternate data stream through the XATTR interface */
2035 static int wimfs_setxattr(const char *path, const char *name,
2036                           const char *value, size_t size, int flags)
2037 {
2038         struct wim_ads_entry *existing_ads_entry;
2039         struct wim_ads_entry *new_ads_entry;
2040         struct wim_lookup_table_entry *existing_lte;
2041         struct wim_lookup_table_entry *lte;
2042         struct wim_inode *inode;
2043         u8 value_hash[SHA1_HASH_SIZE];
2044         u16 ads_idx;
2045         struct wimfs_context *ctx = wimfs_get_context();
2046
2047         if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2048                 return -ENOTSUP;
2049
2050         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2051                 return -ENOATTR;
2052         name += 5;
2053
2054         inode = wim_pathname_to_inode(ctx->wim, path);
2055         if (!inode)
2056                 return -errno;
2057
2058         existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2059         if (existing_ads_entry) {
2060                 if (flags & XATTR_CREATE)
2061                         return -EEXIST;
2062                 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2063         } else {
2064                 if (flags & XATTR_REPLACE)
2065                         return -ENOATTR;
2066         }
2067         new_ads_entry = inode_add_ads(inode, name);
2068         if (!new_ads_entry)
2069                 return -ENOMEM;
2070
2071         sha1_buffer((const u8*)value, size, value_hash);
2072
2073         existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
2074
2075         if (existing_lte) {
2076                 lte = existing_lte;
2077                 lte->refcnt++;
2078         } else {
2079                 u8 *value_copy;
2080                 lte = new_lookup_table_entry();
2081                 if (!lte)
2082                         return -ENOMEM;
2083                 value_copy = MALLOC(size);
2084                 if (!value_copy) {
2085                         FREE(lte);
2086                         return -ENOMEM;
2087                 }
2088                 memcpy(value_copy, value, size);
2089                 lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
2090                 lte->attached_buffer              = value_copy;
2091                 lte->resource_entry.original_size = size;
2092                 lte->resource_entry.size          = size;
2093                 lte->resource_entry.flags         = 0;
2094                 copy_hash(lte->hash, value_hash);
2095                 lookup_table_insert(ctx->wim->lookup_table, lte);
2096         }
2097         new_ads_entry->lte = lte;
2098         return 0;
2099 }
2100 #endif
2101
2102 static int wimfs_symlink(const char *to, const char *from)
2103 {
2104         struct wimfs_context *ctx = wimfs_get_context();
2105         struct wim_dentry *dentry;
2106         int ret;
2107
2108         ret = create_dentry(ctx, from, &dentry);
2109         if (ret == 0) {
2110                 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
2111                 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2112                 if (inode_set_symlink(dentry->d_inode, to,
2113                                       ctx->wim->lookup_table, NULL))
2114                 {
2115                         unlink_dentry(dentry);
2116                         free_dentry(dentry);
2117                         ret = -ENOMEM;
2118                 }
2119         }
2120         return ret;
2121 }
2122
2123
2124 /* Reduce the size of a file */
2125 static int wimfs_truncate(const char *path, off_t size)
2126 {
2127         struct wim_dentry *dentry;
2128         struct wim_lookup_table_entry *lte;
2129         int ret;
2130         u16 stream_idx;
2131         u32 stream_id;
2132         struct wim_inode *inode;
2133         struct wimfs_context *ctx = wimfs_get_context();
2134
2135         ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2136                               &dentry, &lte, &stream_idx);
2137
2138         if (ret != 0)
2139                 return ret;
2140
2141         if (lte == NULL && size == 0)
2142                 return 0;
2143
2144         inode = dentry->d_inode;
2145         if (stream_idx == 0)
2146                 stream_id = 0;
2147         else
2148                 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2149
2150         if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2151                 ret = truncate(lte->staging_file_name, size);
2152                 if (ret != 0)
2153                         ret = -errno;
2154         } else {
2155                 /* File in WIM.  Extract it to the staging directory, but only
2156                  * the first @size bytes of it. */
2157                 ret = extract_resource_to_staging_dir(inode, stream_id,
2158                                                       &lte, size, ctx);
2159         }
2160         return ret;
2161 }
2162
2163 /* Unlink a non-directory or alternate data stream */
2164 static int wimfs_unlink(const char *path)
2165 {
2166         struct wim_dentry *dentry;
2167         struct wim_lookup_table_entry *lte;
2168         int ret;
2169         u16 stream_idx;
2170         struct wimfs_context *ctx = wimfs_get_context();
2171
2172         ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2173                               &dentry, &lte, &stream_idx);
2174
2175         if (ret != 0)
2176                 return ret;
2177
2178         if (stream_idx == 0)
2179                 remove_dentry(dentry, ctx->wim->lookup_table);
2180         else
2181                 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2182                                  ctx->wim->lookup_table);
2183         return 0;
2184 }
2185
2186 #ifdef HAVE_UTIMENSAT
2187 /*
2188  * Change the timestamp on a file dentry.
2189  *
2190  * Note that alternate data streams do not have their own timestamps.
2191  */
2192 static int wimfs_utimens(const char *path, const struct timespec tv[2])
2193 {
2194         struct wim_dentry *dentry;
2195         struct wim_inode *inode;
2196         WIMStruct *w = wimfs_get_WIMStruct();
2197
2198         dentry = get_dentry(w, path);
2199         if (!dentry)
2200                 return -errno;
2201         inode = dentry->d_inode;
2202
2203         if (tv[0].tv_nsec != UTIME_OMIT) {
2204                 if (tv[0].tv_nsec == UTIME_NOW)
2205                         inode->i_last_access_time = get_wim_timestamp();
2206                 else
2207                         inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
2208         }
2209         if (tv[1].tv_nsec != UTIME_OMIT) {
2210                 if (tv[1].tv_nsec == UTIME_NOW)
2211                         inode->i_last_write_time = get_wim_timestamp();
2212                 else
2213                         inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
2214         }
2215         return 0;
2216 }
2217 #else
2218 static int wimfs_utime(const char *path, struct utimbuf *times)
2219 {
2220         struct wim_dentry *dentry;
2221         struct wim_inode *inode;
2222         WIMStruct *w = wimfs_get_WIMStruct();
2223
2224         dentry = get_dentry(w, path);
2225         if (!dentry)
2226                 return -errno;
2227         inode = dentry->d_inode;
2228
2229         inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2230         inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2231         return 0;
2232 }
2233 #endif
2234
2235 /* Writes to a file in the WIM filesystem.
2236  * It may be an alternate data stream, but here we don't even notice because we
2237  * just get a lookup table entry. */
2238 static int wimfs_write(const char *path, const char *buf, size_t size,
2239                        off_t offset, struct fuse_file_info *fi)
2240 {
2241         struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2242         int ret;
2243         u64 now;
2244
2245         if (!fd)
2246                 return -EBADF;
2247
2248         wimlib_assert(fd->f_lte);
2249         wimlib_assert(fd->f_lte->staging_file_name);
2250         wimlib_assert(fd->staging_fd != -1);
2251         wimlib_assert(fd->f_inode);
2252
2253         /* Seek to the requested position */
2254         if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2255                 return -errno;
2256
2257         /* Write the data. */
2258         ret = write(fd->staging_fd, buf, size);
2259         if (ret == -1)
2260                 return -errno;
2261
2262         now = get_wim_timestamp();
2263         fd->f_inode->i_last_write_time = now;
2264         fd->f_inode->i_last_access_time = now;
2265         return ret;
2266 }
2267
2268 static struct fuse_operations wimfs_operations = {
2269         .access      = wimfs_access,
2270         .chmod       = wimfs_chmod,
2271         .destroy     = wimfs_destroy,
2272 #if 0
2273         .fallocate   = wimfs_fallocate,
2274 #endif
2275         .fgetattr    = wimfs_fgetattr,
2276         .ftruncate   = wimfs_ftruncate,
2277         .getattr     = wimfs_getattr,
2278 #ifdef ENABLE_XATTR
2279         .getxattr    = wimfs_getxattr,
2280 #endif
2281         .link        = wimfs_link,
2282 #ifdef ENABLE_XATTR
2283         .listxattr   = wimfs_listxattr,
2284 #endif
2285         .mkdir       = wimfs_mkdir,
2286         .mknod       = wimfs_mknod,
2287         .open        = wimfs_open,
2288         .opendir     = wimfs_opendir,
2289         .read        = wimfs_read,
2290         .readdir     = wimfs_readdir,
2291         .readlink    = wimfs_readlink,
2292         .release     = wimfs_release,
2293         .releasedir  = wimfs_releasedir,
2294 #ifdef ENABLE_XATTR
2295         .removexattr = wimfs_removexattr,
2296 #endif
2297         .rename      = wimfs_rename,
2298         .rmdir       = wimfs_rmdir,
2299 #ifdef ENABLE_XATTR
2300         .setxattr    = wimfs_setxattr,
2301 #endif
2302         .symlink     = wimfs_symlink,
2303         .truncate    = wimfs_truncate,
2304         .unlink      = wimfs_unlink,
2305 #ifdef HAVE_UTIMENSAT
2306         .utimens     = wimfs_utimens,
2307 #else
2308         .utime       = wimfs_utime,
2309 #endif
2310         .write       = wimfs_write,
2311
2312         /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2313          * no need to have the file path provided on operations such as read()
2314          * where only the file descriptor is needed. */
2315 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2316         .flag_nullpath_ok = 1,
2317 #endif
2318 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2319         .flag_nopath = 1,
2320         .flag_utime_omit_ok = 1,
2321 #endif
2322 };
2323
2324
2325 /* Mounts an image from a WIM file. */
2326 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2327                                  int mount_flags, WIMStruct **additional_swms,
2328                                  unsigned num_additional_swms,
2329                                  const char *staging_dir)
2330 {
2331         int argc = 0;
2332         char *argv[16];
2333         int ret;
2334         char *dir_copy;
2335         struct wim_lookup_table *joined_tab, *wim_tab_save;
2336         struct wim_image_metadata *imd;
2337         struct wimfs_context ctx;
2338         struct hlist_node *cur_node;
2339         struct wim_inode *inode;
2340
2341         DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2342               wim, image, dir, mount_flags);
2343
2344         if (!wim || !dir)
2345                 return WIMLIB_ERR_INVALID_PARAM;
2346
2347         ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2348         if (ret != 0)
2349                 return ret;
2350
2351         if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2352                 ERROR("Cannot mount a split WIM read-write");
2353                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2354         }
2355
2356         if (num_additional_swms) {
2357                 ret = new_joined_lookup_table(wim, additional_swms,
2358                                               num_additional_swms,
2359                                               &joined_tab);
2360                 if (ret != 0)
2361                         return ret;
2362                 wim_tab_save = wim->lookup_table;
2363                 wim->lookup_table = joined_tab;
2364         }
2365
2366         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2367                 ret = wim_run_full_verifications(wim);
2368                 if (ret != 0)
2369                         goto out;
2370         }
2371
2372         ret = select_wim_image(wim, image);
2373         if (ret != 0)
2374                 goto out;
2375
2376         DEBUG("Selected image %d", image);
2377
2378         imd = wim_get_current_image_metadata(wim);
2379
2380         if (imd->root_dentry->refcnt != 1) {
2381                 ERROR("Cannot mount image that was just exported with "
2382                       "wimlib_export_image()");
2383                 ret = WIMLIB_ERR_INVALID_PARAM;
2384                 goto out;
2385         }
2386
2387         if (imd->inode_list.first) /* May be unneeded? */
2388                 imd->inode_list.first->pprev = &imd->inode_list.first;
2389
2390         if (imd->modified) {
2391                 ERROR("Cannot mount image that was added "
2392                       "with wimlib_add_image()");
2393                 ret = WIMLIB_ERR_INVALID_PARAM;
2394                 goto out;
2395         }
2396
2397         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2398                 ret = lock_wim(wim, wim->fp);
2399                 if (ret != 0)
2400                         goto out;
2401         }
2402
2403         /* Use default stream interface if one was not specified */
2404         if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2405                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2406                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2407                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2408
2409
2410         DEBUG("Initializing struct wimfs_context");
2411         init_wimfs_context(&ctx);
2412         ctx.wim = wim;
2413         ctx.mount_flags = mount_flags;
2414         ctx.image_inode_list = &imd->inode_list;
2415
2416         if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2417                 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2418
2419         DEBUG("Unlinking message queues in case they already exist");
2420         ret = set_message_queue_names(&ctx, dir);
2421         if (ret != 0)
2422                 goto out_unlock;
2423         unlink_message_queues(&ctx);
2424
2425         DEBUG("Preparing arguments to fuse_main()");
2426
2427         dir_copy = STRDUP(dir);
2428         if (!dir_copy)
2429                 goto out_free_message_queue_names;
2430
2431         argv[argc++] = "imagex";
2432         argv[argc++] = dir_copy;
2433
2434         /* disable multi-threaded operation for read-write mounts */
2435         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2436                 argv[argc++] = "-s";
2437
2438         if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2439                 argv[argc++] = "-d";
2440
2441         /*
2442          * We provide the use_ino option to the FUSE mount because we are going
2443          * to assign inode numbers ourselves. */
2444         char optstring[256] =
2445                 "use_ino"
2446                 ",subtype=wimfs"
2447                 ",attr_timeout=0"
2448 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2449                 ",hard_remove"
2450 #endif
2451                 ;
2452         argv[argc++] = "-o";
2453         argv[argc++] = optstring;
2454         if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2455                 /* Read-write mount.  Make the staging directory */
2456                 ret = make_staging_dir(&ctx, staging_dir);
2457                 if (ret != 0)
2458                         goto out_free_dir_copy;
2459         } else {
2460                 /* Read-only mount */
2461                 strcat(optstring, ",ro");
2462         }
2463         argv[argc] = NULL;
2464
2465 #ifdef ENABLE_DEBUG
2466         {
2467                 int i;
2468                 DEBUG("FUSE command line (argc = %d): ", argc);
2469                 for (i = 0; i < argc; i++) {
2470                         fputs(argv[i], stdout);
2471                         putchar(' ');
2472                 }
2473                 putchar('\n');
2474                 fflush(stdout);
2475         }
2476 #endif
2477
2478         /* Mark dentry tree as modified if read-write mount. */
2479         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2480                 imd->modified = 1;
2481                 imd->has_been_mounted_rw = 1;
2482         }
2483
2484         /* Resolve the lookup table entries for every inode in the image, and
2485          * assign inode numbers */
2486         DEBUG("Resolving lookup table entries and assigning inode numbers");
2487         ctx.next_ino = 1;
2488         hlist_for_each_entry(inode, cur_node, &imd->inode_list, i_hlist) {
2489                 inode_resolve_ltes(inode, wim->lookup_table);
2490                 inode->i_ino = ctx.next_ino++;
2491         }
2492         DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2493
2494         DEBUG("Calling fuse_main()");
2495
2496         ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2497
2498         DEBUG("Returned from fuse_main() (ret = %d)", ret);
2499
2500         if (ret)
2501                 ret = WIMLIB_ERR_FUSE;
2502 out_free_dir_copy:
2503         FREE(dir_copy);
2504 out_unlock:
2505         wim->wim_locked = 0;
2506 out_free_message_queue_names:
2507         free_message_queue_names(&ctx);
2508 out:
2509         if (num_additional_swms) {
2510                 free_lookup_table(wim->lookup_table);
2511                 wim->lookup_table = wim_tab_save;
2512         }
2513         return ret;
2514 }
2515
2516 /*
2517  * Unmounts the WIM file that was previously mounted on @dir by using
2518  * wimlib_mount_image().
2519  */
2520 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2521                                    wimlib_progress_func_t progress_func)
2522 {
2523         int ret;
2524         struct wimfs_context wimfs_ctx;
2525
2526         init_wimfs_context(&wimfs_ctx);
2527
2528         ret = set_message_queue_names(&wimfs_ctx, dir);
2529         if (ret != 0)
2530                 goto out;
2531
2532         ret = open_message_queues(&wimfs_ctx, false);
2533         if (ret != 0)
2534                 goto out_free_message_queue_names;
2535
2536         ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2537                                        unmount_flags,
2538                                        progress_func != NULL);
2539         if (ret != 0)
2540                 goto out_close_message_queues;
2541
2542         ret = execute_fusermount(dir);
2543         if (ret != 0)
2544                 goto out_close_message_queues;
2545
2546         struct unmount_msg_handler_context handler_ctx = {
2547                 .hdr = {
2548                         .timeout_seconds = 5,
2549                 },
2550                 .daemon_pid = 0,
2551                 .progress_func = progress_func,
2552         };
2553
2554         ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2555                            &unmount_msg_handler_callbacks,
2556                            &handler_ctx.hdr);
2557         if (ret == 0)
2558                 ret = handler_ctx.status;
2559 out_close_message_queues:
2560         close_message_queues(&wimfs_ctx);
2561 out_free_message_queue_names:
2562         free_message_queue_names(&wimfs_ctx);
2563 out:
2564         return ret;
2565 }
2566
2567 #else /* WITH_FUSE */
2568
2569
2570 static inline int mount_unsupported_error()
2571 {
2572         ERROR("wimlib was compiled with --without-fuse, which disables support "
2573               "for mounting WIMs.");
2574         return WIMLIB_ERR_UNSUPPORTED;
2575 }
2576
2577 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2578                                    wimlib_progress_func_t progress_func)
2579 {
2580         return mount_unsupported_error();
2581 }
2582
2583 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2584                                  int mount_flags, WIMStruct **additional_swms,
2585                                  unsigned num_additional_swms,
2586                                  const char *staging_dir)
2587 {
2588         return mount_unsupported_error();
2589 }
2590
2591 #endif /* WITH_FUSE */