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