]> wimlib.net Git - wimlib/blob - src/mount.c
Fix various issues
[wimlib] / src / mount.c
1 /*
2  * mount.c
3  *
4  * This file implements mounting of WIM files using FUSE, which stands for
5  * Filesystem in Userspace.  FUSE allows a filesystem to be implemented in a
6  * userspace process by implementing the filesystem primitives--- read(),
7  * write(), readdir(), etc.
8  */
9
10 /*
11  * Copyright (C) 2012 Eric Biggers
12  *
13  * This file is part of wimlib, a library for working with WIM files.
14  *
15  * wimlib is free software; you can redistribute it and/or modify it under the
16  * terms of the GNU Lesser General Public License as published by the Free
17  * Software Foundation; either version 2.1 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 Lesser General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU Lesser General Public License
26  * along with wimlib; if not, see http://www.gnu.org/licenses/.
27  */
28
29 #include "wimlib_internal.h"
30
31 #ifdef WITH_FUSE
32 #include "sha1.h"
33 #include "lookup_table.h"
34 #include "xml.h"
35 #include "io.h"
36 #include "timestamp.h"
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <sys/wait.h>
40 #define FUSE_USE_VERSION 26
41 #include <errno.h>
42 #include <string.h>
43 #include <sys/time.h>
44 #include <fuse.h>
45 #include <ftw.h>
46 #include <mqueue.h>
47 #include <attr/xattr.h>
48
49 struct wimlib_fd {
50         u16 idx;
51         int staging_fd;
52         u64 hard_link_group;
53         struct lookup_table_entry *lte;
54         struct dentry *dentry;
55 };
56
57 /* The WIMStruct for the mounted WIM. */
58 static WIMStruct *w;
59
60 /* Working directory when `imagex mount' is run. */
61 static const char *working_directory;
62
63 /* Name of the staging directory for a read-write mount.  Whenever a new file is
64  * created, it is done so in the staging directory.  Furthermore, whenever a
65  * file in the WIM is modified, it is extracted to the staging directory.  If
66  * changes are commited when the WIM is unmounted, the file resources are merged
67  * in from the staging directory when writing the new WIM. */
68 static char *staging_dir_name;
69 static size_t staging_dir_name_len;
70
71 /* Flags passed to wimlib_mount(). */
72 static int mount_flags;
73
74 /* Name of the directory on which the WIM file is mounted. */
75 static const char *mount_dir;
76
77 /* Next hard link group ID to be assigned.  These are also used as the inode
78  * numbers. */
79 static u64 next_link_group_id;
80
81 /* List of lookup table entries in the staging directory */
82 static LIST_HEAD(staging_list);
83
84 static inline int get_lookup_flags()
85 {
86         if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
87                 return LOOKUP_FLAG_ADS_OK;
88         else
89                 return 0;
90 }
91
92 /* Returns nonzero if write permission is requested on the file open flags */
93 static inline int flags_writable(int open_flags)
94 {
95         return open_flags & (O_RDWR | O_WRONLY);
96 }
97
98 /* 
99  * Allocate a file descriptor for a lookup table entry
100  */
101 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
102                            struct wimlib_fd **fd_ret)
103 {
104         static const u16 fds_per_alloc = 8;
105         static const u16 max_fds = 0xffff;
106
107         if (lte->num_opened_fds == lte->num_allocated_fds) {
108                 struct wimlib_fd **fds;
109                 u16 num_new_fds;
110
111                 if (lte->num_allocated_fds == max_fds)
112                         return -EMFILE;
113                 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
114                 
115                 fds = REALLOC(lte->fds, (lte->num_allocated_fds + num_new_fds) *
116                                sizeof(lte->fds[0]));
117                 if (!fds)
118                         return -ENOMEM;
119                 memset(&fds[lte->num_allocated_fds], 0,
120                        num_new_fds * sizeof(fds[0]));
121                 lte->fds = fds;
122                 lte->num_allocated_fds += num_new_fds;
123         }
124         for (u16 i = 0; ; i++) {
125                 if (!lte->fds[i]) {
126                         struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
127                         if (!fd)
128                                 return -ENOMEM;
129                         fd->staging_fd = -1;
130                         fd->idx        = i;
131                         fd->lte        = lte;
132                         lte->fds[i]    = fd;
133                         lte->num_opened_fds++;
134                         *fd_ret        = fd;
135                         return 0;
136                 }
137         }
138 }
139
140 static int close_wimlib_fd(struct wimlib_fd *fd)
141 {
142         struct lookup_table_entry *lte = fd->lte;
143
144         wimlib_assert(lte);
145         wimlib_assert(lte->num_opened_fds);
146
147         if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
148                 wimlib_assert(lte->staging_file_name);
149                 wimlib_assert(fd->staging_fd != -1);
150                 if (close(fd->staging_fd) != 0)
151                         return -errno;
152         }
153         if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
154                 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
155                         wimlib_assert(lte->staging_file_name);
156                         unlink(lte->staging_file_name);
157                 }
158                 free_lookup_table_entry(lte);
159         }
160         wimlib_assert(lte->fds[fd->idx] == fd);
161         lte->fds[fd->idx] = NULL;
162         FREE(fd);
163         return 0;
164 }
165
166 /* Remove a dentry and all its alternate file streams */
167 static void remove_dentry(struct dentry *dentry,
168                           struct lookup_table *lookup_table)
169 {
170         wimlib_assert(dentry);
171         wimlib_assert(dentry->resolved);
172
173         struct lookup_table_entry *lte = dentry->lte;
174         u16 i = 0;
175         while (1) {
176                 lte = lte_decrement_refcnt(lte, lookup_table);
177                 if (lte && lte->num_opened_fds)
178                         for (u16 i = 0; i < lte->num_allocated_fds; i++)
179                                 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
180                                         lte->fds[i]->dentry = NULL;
181                 if (i == dentry->num_ads)
182                         break;
183                 lte = dentry->ads_entries[i].lte;
184                 i++;
185         }
186
187         unlink_dentry(dentry);
188         put_dentry(dentry);
189 }
190
191 static void remove_ads(struct dentry *dentry,
192                        struct ads_entry *ads_entry,
193                        struct lookup_table *lookup_table)
194 {
195         struct lookup_table_entry *lte;
196
197         wimlib_assert(dentry->resolved);
198
199         lte = lte_decrement_refcnt(ads_entry->lte, lookup_table);
200         if (lte)
201                 list_del(&ads_entry->lte_group_list.list);
202         dentry_remove_ads(dentry, ads_entry);
203 }
204
205 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
206 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf)
207 {
208         struct lookup_table_entry *lte;
209
210         if (dentry_is_symlink(dentry))
211                 stbuf->st_mode = S_IFLNK | 0777;
212         else if (dentry_is_directory(dentry))
213                 stbuf->st_mode = S_IFDIR | 0755;
214         else
215                 stbuf->st_mode = S_IFREG | 0644;
216
217         stbuf->st_ino = (ino_t)dentry->link_group_id;
218
219         stbuf->st_nlink = dentry_link_group_size(dentry);
220         stbuf->st_uid   = getuid();
221         stbuf->st_gid   = getgid();
222
223         /* Use the size of the unnamed (default) file stream. */
224         lte = dentry_unnamed_lte_resolved(dentry);
225         if (lte) {
226                 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
227                         wimlib_assert(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE);
228                         wimlib_assert(lte->staging_file_name);
229                         struct stat native_stat;
230                         if (stat(lte->staging_file_name, &native_stat) != 0) {
231                                 DEBUG("Failed to stat `%s': %m",
232                                       lte->staging_file_name);
233                                 return -errno;
234                         }
235                         stbuf->st_size = native_stat.st_size;
236                 } else {
237                         stbuf->st_size = lte->resource_entry.original_size;
238                 }
239         } else {
240                 stbuf->st_size = 0;
241         }
242
243         stbuf->st_atime   = wim_timestamp_to_unix(dentry->last_access_time);
244         stbuf->st_mtime   = wim_timestamp_to_unix(dentry->last_write_time);
245         stbuf->st_ctime   = wim_timestamp_to_unix(dentry->creation_time);
246         stbuf->st_blocks  = (stbuf->st_size + 511) / 512;
247         return 0;
248 }
249
250 /* Creates a new staging file and returns its file descriptor opened for
251  * writing.
252  *
253  * @name_ret: A location into which the a pointer to the newly allocated name of
254  *                      the staging file is stored.
255  * @return:  The file descriptor for the new file.  Returns -1 and sets errno on
256  *              error, for any reason possible from the creat() function.
257  */
258 static int create_staging_file(char **name_ret, int open_flags)
259 {
260         size_t name_len;
261         char *name;
262         struct stat stbuf;
263         int fd;
264         int errno_save;
265
266         name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
267         name = MALLOC(name_len + 1);
268         if (!name) {
269                 errno = ENOMEM;
270                 return -1;
271         }
272
273         do {
274
275                 memcpy(name, staging_dir_name, staging_dir_name_len);
276                 name[staging_dir_name_len] = '/';
277                 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
278                                                 SHA1_HASH_SIZE);
279                 name[name_len] = '\0';
280
281
282         /* Just in case, verify that the randomly generated name doesn't name an
283          * existing file, and try again if so  */
284         } while (stat(name, &stbuf) == 0);
285
286         if (errno != ENOENT)
287                 /* other error! */
288                 return -1;
289
290         /* doesn't exist--- ok */
291
292         DEBUG("Creating staging file `%s'", name);
293
294         fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600); 
295         if (fd == -1) {
296                 errno_save = errno;
297                 FREE(name);
298                 errno = errno_save;
299         } else {
300                 *name_ret = name;
301         }
302         return fd;
303 }
304
305 /* 
306  * Removes open file descriptors from a lookup table entry @old_lte where the
307  * file descriptors have opened the corresponding file resource in the context
308  * of the hard link group @link_group; these file descriptors are extracted and
309  * placed in a new lookup table entry, which is returned.
310  */
311 static struct lookup_table_entry *
312 lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group)
313 {
314         u16 num_transferred_fds;
315         struct lookup_table_entry *new_lte;
316
317         new_lte = new_lookup_table_entry();
318         if (!new_lte)
319                 return NULL;
320
321         num_transferred_fds = 0;
322         for (u16 i = 0; i < old_lte->num_allocated_fds; i++)
323                 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
324                     old_lte->fds[i]->dentry->link_group_id == link_group)
325                         num_transferred_fds++;
326         DEBUG("Transferring %u file descriptors",
327               num_transferred_fds);
328         new_lte->fds = MALLOC(num_transferred_fds * sizeof(new_lte->fds[0]));
329         if (!new_lte->fds) {
330                 FREE(new_lte);
331                 return NULL;
332         }
333         for (u16 i = 0, j = 0; ; i++) {
334                 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
335                     old_lte->fds[i]->dentry->link_group_id == link_group) {
336                         struct wimlib_fd *fd = old_lte->fds[i];
337                         old_lte->fds[i] = NULL;
338                         fd->lte = new_lte;
339                         fd->idx = j;
340                         new_lte->fds[j] = fd;
341                         if (++j == num_transferred_fds)
342                                 break;
343                 }
344         }
345         DEBUG("old_lte: %u fds open; new_lte: %u fds open",
346               old_lte->num_opened_fds, new_lte->num_opened_fds);
347         old_lte->num_opened_fds -= num_transferred_fds;
348         new_lte->num_opened_fds = num_transferred_fds;
349         new_lte->num_allocated_fds = num_transferred_fds;
350         return new_lte;
351 }
352
353 /* 
354  * Transfers an alternate data stream entry to a new lookup table entry
355  */
356 static void lte_transfer_ads_entry(struct lookup_table_entry *new_lte,
357                                    struct ads_entry *ads_entry)
358 {
359         list_del(&ads_entry->lte_group_list.list);
360         list_add(&ads_entry->lte_group_list.list, &new_lte->lte_group_list);
361         ads_entry->lte = new_lte;
362 }
363
364 /* 
365  * Transfers a dentry to a new lookup table entry
366  */
367 static void lte_transfer_dentry(struct lookup_table_entry *new_lte,
368                                 struct dentry *dentry)
369 {
370         wimlib_assert(dentry->lte_group_list.list.next);
371         wimlib_assert(new_lte->lte_group_list.next);
372         list_del(&dentry->lte_group_list.list);
373         list_add(&dentry->lte_group_list.list, &new_lte->lte_group_list);
374         dentry->lte = new_lte;
375 }
376
377 static void lte_transfer_stream_entries(struct lookup_table_entry *new_lte,
378                                         struct dentry *dentry,
379                                         unsigned stream_idx)
380 {
381         INIT_LIST_HEAD(&new_lte->lte_group_list);
382         if (stream_idx == 0) {
383                 struct list_head *pos = &dentry->link_group_list;
384                 do {
385                         struct dentry *d;
386                         d = container_of(pos, struct dentry, link_group_list);
387                         wimlib_assert(d->link_group_id == dentry->link_group_id);
388                         lte_transfer_dentry(new_lte, d);
389                         pos = pos->next;
390                 } while (pos != &dentry->link_group_list);
391         } else {
392                 struct ads_entry *ads_entry;
393                 wimlib_assert(stream_idx <= dentry->num_ads);
394                 ads_entry = &dentry->ads_entries[stream_idx - 1];
395                 lte_transfer_ads_entry(new_lte, ads_entry);
396         }
397 }
398
399 /* 
400  * Extract a WIM resource to the staging directory.
401  *
402  * @dentry, @stream_idx:  The stream on whose behalf we are modifying the lookup
403  * table entry (these may be more streams than this that reference the lookup
404  * table entry)
405  *
406  * @lte: Pointer to pointer to the lookup table entry for the stream we need to
407  * extract, or NULL if there was no lookup table entry present for the stream
408  *
409  * @size:  Number of bytes of the stream we want to extract (this supports the
410  * wimfs_truncate() function).
411  */
412 static int extract_resource_to_staging_dir(struct dentry *dentry,
413                                            unsigned stream_idx,
414                                            struct lookup_table_entry **lte,
415                                            off_t size)
416 {
417         char *staging_file_name;
418         int ret;
419         int fd;
420         struct lookup_table_entry *old_lte, *new_lte;
421         size_t link_group_size;
422
423         /*
424          * We need to:
425          * - Create a staging file for the WIM resource
426          * - Extract the resource to it
427          * - Create a new lte for the file resource
428          * - Transfer fds from the old lte to the new lte, but only if they share the
429          *   same hard link group as this dentry.  If there is no old lte, then this
430          *   step does not need to be done
431          * - Transfer stream entries from the old lte's list to the new lte's list.  If
432          *   there is no old lte, we instead transfer entries for the hard link group.
433          *
434          *   Note: *lte is permitted to be NULL, in which case there is no old
435          *   lookup table entry.
436          */
437
438         DEBUG("Extracting resource `%s' to staging directory", dentry->full_path_utf8);
439
440         old_lte = *lte;
441         fd = create_staging_file(&staging_file_name, O_WRONLY);
442         if (fd == -1)
443                 return -errno;
444
445         if (old_lte)
446                 ret = extract_wim_resource_to_fd(old_lte, fd, size);
447         else
448                 ret = 0;
449         if (ret != 0 || close(fd) != 0) {
450                 if (errno != 0)
451                         ret = -errno;
452                 else
453                         ret = -EIO;
454                 close(fd);
455                 goto out_delete_staging_file;
456         }
457
458         link_group_size = dentry_link_group_size(dentry);
459
460         if (old_lte) {
461                 if (link_group_size == old_lte->refcnt) {
462                         /* This hard link group is the only user of the lookup
463                          * table entry, so we can re-use it. */
464                         DEBUG("Re-using lookup table entry");
465                         lookup_table_unlink(w->lookup_table, old_lte);
466                         new_lte = old_lte;
467                 } else {
468                         DEBUG("Splitting lookup table entry "
469                               "(link_group_size = %zu, lte refcnt = %u)",
470                               link_group_size, old_lte->refcnt);
471                         /* Split a hard link group away from the "lookup table
472                          * entry" hard link group (i.e. we had two hard link
473                          * groups that were identical, but now we are changing
474                          * one of them) */
475
476                         /* XXX 
477                          * The ADS really complicate things here and not
478                          * everything is going to work correctly yet.  For
479                          * example it could be the same that a file contains two
480                          * file streams that are identical and therefore share
481                          * the same lookup table entry despite the fact that the
482                          * streams themselves are not hardlinked. 
483                          * XXX*/
484                         wimlib_assert(old_lte->refcnt > link_group_size);
485
486                         new_lte = lte_extract_fds(old_lte, dentry->link_group_id);
487                         if (!new_lte) {
488                                 ret = -ENOMEM;
489                                 goto out_delete_staging_file;
490                         }
491
492                         lte_transfer_stream_entries(new_lte, dentry, stream_idx);
493                         old_lte->refcnt -= link_group_size;
494                 } 
495         } else {
496                 /* No old_lte was supplied, so the resource had no lookup table
497                  * entry before (it must be an empty resource) */
498                 new_lte = new_lookup_table_entry();
499                 if (!new_lte) {
500                         ret = -ENOMEM;
501                         goto out_delete_staging_file;
502                 }
503                 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
504         }
505         new_lte->resource_entry.original_size = size;
506         new_lte->refcnt = link_group_size;
507         random_hash(new_lte->hash);
508         if (new_lte->staging_file_name)
509                 FREE(new_lte->staging_file_name);
510         new_lte->staging_file_name = staging_file_name;
511         new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
512
513         lookup_table_insert(w->lookup_table, new_lte);
514         list_add(&new_lte->staging_list, &staging_list);
515         *lte = new_lte;
516         return 0;
517 out_delete_staging_file:
518         unlink(staging_file_name);
519         FREE(staging_file_name);
520         return ret;
521 }
522
523 /* 
524  * Creates a randomly named staging directory and returns its name into the
525  * static variable staging_dir_name.
526  *
527  * If the staging directory cannot be created, staging_dir_name is set to NULL.
528  * */
529 static void make_staging_dir()
530 {
531         /* XXX Give the user an option of where to stage files */
532
533         static char prefix[] = "wimlib-staging-";
534         static const size_t prefix_len = 15;
535         static const size_t suffix_len = 10;
536
537         size_t pwd_len = strlen(working_directory);
538
539         staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
540
541         staging_dir_name = MALLOC(staging_dir_name_len + 1);
542         if (!staging_dir_name) {
543                 ERROR("Out of memory");
544                 return;
545         }
546
547         memcpy(staging_dir_name, working_directory, pwd_len);
548         staging_dir_name[pwd_len] = '/';
549         memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
550         randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
551                                 suffix_len);
552         staging_dir_name[staging_dir_name_len] = '\0';
553
554         if (mkdir(staging_dir_name, 0700) != 0) {
555                 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
556                                  staging_dir_name);
557                 FREE(staging_dir_name);
558                 staging_dir_name = NULL;
559         }
560 }
561
562 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
563                 int typeflag, struct FTW *ftwbuf)
564 {
565         if (remove(fpath) == 0)
566                 return 0;
567         else
568                 return WIMLIB_ERR_DELETE_STAGING_DIR;
569 }
570
571
572 /* 
573  * Deletes the staging directory and all the files contained in it. 
574  */
575 static inline int delete_staging_dir()
576 {
577         int ret;
578         
579         ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
580         staging_dir_name = NULL;
581         return ret;
582 }
583
584 /* Name and message queue descriptors for message queues between the filesystem
585  * daemon process and the unmount process.  These are used when the filesystem
586  * is unmounted and the process running wimlib_mount() (i.e. the `imagex
587  * unmount' command) needs to communicate with the filesystem daemon running
588  * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
589  * commands */
590 static char *unmount_to_daemon_mq_name;
591 static char *daemon_to_unmount_mq_name;
592 static int unmount_to_daemon_mq;
593 static int daemon_to_unmount_mq;
594
595 /* Simple function that returns the concatenation of 4 strings. */
596 static char *strcat_dup(const char *s1, const char *s2, const char *s3, 
597                                                         const char *s4)
598 {
599         size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
600         char *p = MALLOC(len);
601         if (!p)
602                 return NULL;
603         *p = '\0';
604         strcat(p, s1);
605         strcat(p, s2);
606         strcat(p, s3);
607         strcat(p, s4);
608         return p;
609 }
610
611 /* Removes trailing forward slashes in a string. */
612 static void remove_trailing_slashes(char *s)
613 {
614         long len = strlen(s);
615         for (long i = len - 1; i >= 1; i--) {
616                 if (s[i] == '/')
617                         s[i] = '\0';
618                 else
619                         break;
620         }
621 }
622
623 /* Changes forward slashes to underscores in a string. */
624 static void s_slashes_underscores_g(char *s)
625 {
626         while (*s) {
627                 if (*s == '/')
628                         *s = '_';
629                 s++;
630         }
631 }
632
633 /* 
634  * Opens two POSIX message queue: one for sending messages from the unmount
635  * process to the daemon process, and one to go the other way.  The names of the
636  * message queues, which must be system-wide unique, are be based on the mount
637  * point.  (There of course is still a possibility of a collision if one were to
638  * unmount two identically named directories simultaneously...)
639  *
640  * @daemon specifies whether the calling process is the filesystem daemon or the
641  * unmount process.
642  */
643 static int open_message_queues(bool daemon)
644 {
645         static const char *slash = "/";
646         static const char *prefix = "wimlib-";
647         static const char *u2d_suffix = "unmount-to-daemon-mq";
648         static const char *d2u_suffix = "daemon-to-unmount-mq";
649
650         const char *mount_dir_basename = path_basename(mount_dir);
651         int flags;
652         int ret;
653
654         unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
655                                                 prefix, u2d_suffix);
656         if (!unmount_to_daemon_mq_name) {
657                 ERROR("Out of memory");
658                 return WIMLIB_ERR_NOMEM;
659         }
660         daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
661                                                 prefix, d2u_suffix);
662         if (!daemon_to_unmount_mq_name) {
663                 ERROR("Out of memory");
664                 ret = WIMLIB_ERR_NOMEM;
665                 goto err1;
666         }
667
668         remove_trailing_slashes(unmount_to_daemon_mq_name);
669         remove_trailing_slashes(daemon_to_unmount_mq_name);
670         s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
671         s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
672
673         if (daemon)
674                 flags = O_RDONLY | O_CREAT;
675         else
676                 flags = O_WRONLY | O_CREAT;
677
678         unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags, 
679                                        0700, NULL);
680
681         if (unmount_to_daemon_mq == -1) {
682                 ERROR_WITH_ERRNO("mq_open()");
683                 ret = WIMLIB_ERR_MQUEUE;
684                 goto err2;
685         }
686
687         if (daemon)
688                 flags = O_WRONLY | O_CREAT;
689         else
690                 flags = O_RDONLY | O_CREAT;
691
692         daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags, 
693                                        0700, NULL);
694
695         if (daemon_to_unmount_mq == -1) {
696                 ERROR_WITH_ERRNO("mq_open()");
697                 ret = WIMLIB_ERR_MQUEUE;
698                 goto err3;
699         }
700         return 0;
701 err3:
702         mq_close(unmount_to_daemon_mq);
703         mq_unlink(unmount_to_daemon_mq_name);
704 err2:
705         FREE(daemon_to_unmount_mq_name);
706 err1:
707         FREE(unmount_to_daemon_mq_name);
708         return ret;
709 }
710
711 static int mq_get_msgsize(mqd_t mq)
712 {
713         static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
714         FILE *fp;
715         struct mq_attr attr;
716         int msgsize;
717
718         if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
719                 msgsize = attr.mq_msgsize;
720         } else {
721                 ERROR_WITH_ERRNO("mq_getattr()");
722                 ERROR("Attempting to read %s", msgsize_max_file);
723                 fp = fopen(msgsize_max_file, "rb");
724                 if (fp) {
725                         if (fscanf(fp, "%d", &msgsize) != 1) {
726                                 ERROR("Assuming message size of 8192");
727                                 msgsize = 8192;
728                         }
729                         fclose(fp);
730                 } else {
731                         ERROR_WITH_ERRNO("Failed to open the file `%s'",
732                                          msgsize_max_file);
733                         ERROR("Assuming message size of 8192");
734                         msgsize = 8192;
735                 }
736         }
737         return msgsize;
738 }
739
740 /* Closes the message queues, which are allocated in static variables */
741 static void close_message_queues()
742 {
743         mq_close(unmount_to_daemon_mq);
744         mq_close(daemon_to_unmount_mq);
745         mq_unlink(unmount_to_daemon_mq_name);
746         mq_unlink(daemon_to_unmount_mq_name);
747 }
748
749 static int wimfs_access(const char *path, int mask)
750 {
751         /* XXX Permissions not implemented */
752         return 0;
753 }
754
755 /* Closes the staging file descriptor associated with the lookup table entry, if
756  * it is opened. */
757 static int close_lte_fds(struct lookup_table_entry *lte)
758 {
759         for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
760                 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
761                         wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
762                         wimlib_assert(lte->staging_file_name);
763                         if (close(lte->fds[i]->staging_fd) != 0) {
764                                 ERROR_WITH_ERRNO("Failed close file `%s'",
765                                                  lte->staging_file_name);
766                                 return WIMLIB_ERR_WRITE;
767                         }
768                         j++;
769                 }
770         }
771         return 0;
772 }
773
774 static void lte_list_change_lte_ptr(struct lookup_table_entry *lte,
775                                     struct lookup_table_entry *newptr)
776 {
777         struct list_head *pos;
778         struct stream_list_head *head;
779         list_for_each(pos, &lte->lte_group_list) {
780                 head = container_of(pos, struct stream_list_head, list);
781                 if (head->type == STREAM_TYPE_ADS) {
782                         struct ads_entry *ads_entry;
783                         ads_entry = container_of(head, struct ads_entry, lte_group_list);
784
785                         ads_entry->lte = newptr;
786                 } else {
787                         wimlib_assert(head->type == STREAM_TYPE_NORMAL);
788
789                         struct dentry *dentry;
790                         dentry = container_of(head, struct dentry, lte_group_list);
791
792                         dentry->lte = newptr;
793                 }
794         }
795 }
796
797
798 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
799                                       struct lookup_table *table)
800 {
801         struct lookup_table_entry *duplicate_lte;
802         int ret;
803         u8 hash[SHA1_HASH_SIZE];
804         struct stat stbuf;
805
806         wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
807         wimlib_assert(lte->staging_file_name);
808
809         ret = sha1sum(lte->staging_file_name, hash);
810         if (ret != 0)
811                 return ret;
812
813         lookup_table_unlink(table, lte);
814
815         duplicate_lte = __lookup_resource(table, hash);
816
817         if (duplicate_lte) {
818                 /* Merge duplicate lookup table entries */
819
820                 lte_list_change_lte_ptr(lte, duplicate_lte);
821                 duplicate_lte->refcnt += lte->refcnt;
822                 list_splice(&lte->lte_group_list,
823                             &duplicate_lte->lte_group_list);
824
825                 free_lookup_table_entry(lte);
826         } else {
827                 if (stat(lte->staging_file_name, &stbuf) != 0) {
828                         ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
829                         return WIMLIB_ERR_STAT;
830                 }
831                 wimlib_assert(&lte->file_on_disk == &lte->staging_file_name);
832                 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
833                 copy_hash(lte->hash, hash);
834                 lte->resource_entry.original_size = stbuf.st_size;
835                 lte->resource_entry.size = stbuf.st_size;
836                 lookup_table_insert(table, lte);
837         }
838
839         return 0;
840 }
841
842 /* Overwrites the WIM file, with changes saved. */
843 static int rebuild_wim(WIMStruct *w, bool check_integrity)
844 {
845         int ret;
846         struct lookup_table_entry *lte, *tmp;
847
848         /* Close all the staging file descriptors. */
849         DEBUG("Closing all staging file descriptors.");
850         list_for_each_entry(lte, &staging_list, staging_list) {
851                 ret = close_lte_fds(lte);
852                 if (ret != 0)
853                         return ret;
854         }
855
856         /* Calculate SHA1 checksums for all staging files, and merge unnecessary
857          * lookup table entries. */
858         DEBUG("Calculating SHA1 checksums for all new staging files.");
859         list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
860                 ret = update_lte_of_staging_file(lte, w->lookup_table);
861                 if (ret != 0)
862                         return ret;
863         }
864
865         xml_update_image_info(w, w->current_image);
866
867         ret = wimlib_overwrite(w, check_integrity);
868         if (ret != 0) {
869                 ERROR("Failed to commit changes");
870                 return ret;
871         }
872         return ret;
873 }
874
875 /* Called when the filesystem is unmounted. */
876 static void wimfs_destroy(void *p)
877 {
878         /* For read-write mounts, the `imagex unmount' command, which is
879          * running in a separate process and is executing the
880          * wimlib_unmount() function, will send this process a byte
881          * through a message queue that indicates whether the --commit
882          * option was specified or not. */
883
884         int msgsize;
885         struct timespec timeout;
886         struct timeval now;
887         ssize_t bytes_received;
888         int ret;
889         char commit;
890         char check_integrity;
891         char status;
892
893         ret = open_message_queues(true);
894         if (ret != 0)
895                 exit(1);
896
897         msgsize = mq_get_msgsize(unmount_to_daemon_mq);
898         char msg[msgsize];
899         msg[0] = 0;
900         msg[1] = 0;
901
902         /* Wait at most 3 seconds before giving up and discarding changes. */
903         gettimeofday(&now, NULL);
904         timeout.tv_sec = now.tv_sec + 3;
905         timeout.tv_nsec = now.tv_usec * 1000;
906         DEBUG("Waiting for message telling us whether to commit or not, and "
907               "whether to include integrity checks.");
908
909         bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg, 
910                                          msgsize, NULL, &timeout);
911         commit = msg[0];
912         check_integrity = msg[1];
913         if (bytes_received == -1) {
914                 if (errno == ETIMEDOUT) {
915                         ERROR("Timed out.");
916                 } else {
917                         ERROR_WITH_ERRNO("mq_timedreceive()");
918                 }
919                 ERROR("Not committing.");
920         } else {
921                 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
922         }
923
924         status = 0;
925         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
926                 if (commit) {
927                         status = chdir(working_directory);
928                         if (status != 0) {
929                                 ERROR_WITH_ERRNO("chdir()");
930                                 status = WIMLIB_ERR_NOTDIR;
931                                 goto done;
932                         }
933                         status = rebuild_wim(w, (check_integrity != 0));
934                 }
935                 ret = delete_staging_dir();
936                 if (ret != 0) {
937                         ERROR_WITH_ERRNO("Failed to delete the staging "
938                                          "directory");
939                         if (status == 0)
940                                 status = ret;
941                 }
942         } else {
943                 DEBUG("Read-only mount");
944         }
945 done:
946         DEBUG("Sending status %u", status);
947         ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
948         if (ret == -1)
949                 ERROR_WITH_ERRNO("Failed to send status to unmount process");
950         close_message_queues();
951 }
952
953 static int wimfs_fallocate(const char *path, int mode,
954                            off_t offset, off_t len, struct fuse_file_info *fi)
955 {
956         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
957         wimlib_assert(fd->staging_fd != -1);
958         return fallocate(fd->staging_fd, mode, offset, len);
959 }
960
961 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
962                           struct fuse_file_info *fi)
963 {
964         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
965         return dentry_to_stbuf(fd->dentry, stbuf);
966 }
967
968 static int wimfs_ftruncate(const char *path, off_t size,
969                            struct fuse_file_info *fi)
970 {
971         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
972         int ret = ftruncate(fd->staging_fd, size);
973         if (ret != 0)
974                 return ret;
975         fd->lte->resource_entry.original_size = size;
976         return 0;
977 }
978
979 /*
980  * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
981  */
982 static int wimfs_getattr(const char *path, struct stat *stbuf)
983 {
984         struct dentry *dentry;
985         int ret;
986
987         ret = lookup_resource(w, path,
988                               get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
989                               &dentry, NULL, NULL);
990         if (ret != 0)
991                 return ret;
992         return dentry_to_stbuf(dentry, stbuf);
993 }
994
995 /* Read an alternate data stream through the XATTR interface, or get its size */
996 static int wimfs_getxattr(const char *path, const char *name, char *value,
997                           size_t size)
998 {
999         int ret;
1000         struct dentry *dentry;
1001         struct ads_entry *ads_entry;
1002         size_t res_size;
1003         struct lookup_table_entry *lte;
1004
1005         if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1006                 return -ENOTSUP;
1007
1008         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1009                 return -ENOATTR;
1010         name += 5;
1011
1012         dentry = get_dentry(w, path);
1013         if (!dentry)
1014                 return -ENOENT;
1015         ads_entry = dentry_get_ads_entry(dentry, name);
1016         if (!ads_entry)
1017                 return -ENOATTR;
1018
1019         lte = ads_entry->lte;
1020         res_size = wim_resource_size(lte);
1021
1022         if (size == 0)
1023                 return res_size;
1024         if (res_size > size)
1025                 return -ERANGE;
1026         ret = read_full_wim_resource(lte, (u8*)value);
1027         if (ret != 0)
1028                 return -EIO;
1029         return res_size;
1030 }
1031
1032 /* Create a hard link */
1033 static int wimfs_link(const char *to, const char *from)
1034 {
1035         struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
1036         const char *link_name;
1037
1038         to_dentry = get_dentry(w, to);
1039         if (!to_dentry)
1040                 return -ENOENT;
1041         if (!dentry_is_regular_file(to_dentry))
1042                 return -EPERM;
1043
1044         from_dentry_parent = get_parent_dentry(w, from);
1045         if (!from_dentry_parent)
1046                 return -ENOENT;
1047         if (!dentry_is_directory(from_dentry_parent))
1048                 return -ENOTDIR;
1049
1050         link_name = path_basename(from);
1051         if (get_dentry_child_with_name(from_dentry_parent, link_name))
1052                 return -EEXIST;
1053         from_dentry = clone_dentry(to_dentry);
1054         if (!from_dentry)
1055                 return -ENOMEM;
1056         if (change_dentry_name(from_dentry, link_name) != 0) {
1057                 FREE(from_dentry);
1058                 return -ENOMEM;
1059         }
1060
1061         /* Add the new dentry to the dentry list for the link group */
1062         list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
1063
1064         /* Increment reference counts for the unnamed file stream and all
1065          * alternate data streams. */
1066         if (from_dentry->lte) {
1067                 list_add(&from_dentry->lte_group_list.list,
1068                          &to_dentry->lte_group_list.list);
1069                 from_dentry->lte->refcnt++;
1070         }
1071         for (u16 i = 0; i < from_dentry->num_ads; i++) {
1072                 struct ads_entry *ads_entry = &from_dentry->ads_entries[i];
1073                 if (ads_entry->lte)
1074                         ads_entry->lte->refcnt++;
1075         }
1076
1077         /* The ADS entries are owned by another dentry. */
1078         from_dentry->ads_entries_status = ADS_ENTRIES_USER;
1079
1080         link_dentry(from_dentry, from_dentry_parent);
1081         return 0;
1082 }
1083
1084 static int wimfs_listxattr(const char *path, char *list, size_t size)
1085 {
1086         struct dentry *dentry;
1087         int ret;
1088         char *p = list;
1089         size_t needed_size;
1090         unsigned i;
1091         if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1092                 return -ENOTSUP;
1093
1094         /* List alternate data streams, or get the list size */
1095
1096         ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1097         if (ret != 0)
1098                 return ret;
1099         if (size == 0) {
1100                 needed_size = 0;
1101                 for (i = 0; i < dentry->num_ads; i++)
1102                         needed_size += dentry->ads_entries[i].stream_name_utf8_len + 6;
1103                 return needed_size;
1104         } else {
1105                 for (i = 0; i < dentry->num_ads; i++) {
1106                         needed_size = dentry->ads_entries[i].stream_name_utf8_len + 6;
1107                         if (needed_size > size)
1108                                 return -ERANGE;
1109                         p += sprintf(p, "user.%s",
1110                                      dentry->ads_entries[i].stream_name_utf8) + 1;
1111                         size -= needed_size;
1112                 }
1113                 return p - list;
1114         }
1115 }
1116
1117 /* 
1118  * Create a directory in the WIM.  
1119  * @mode is currently ignored.
1120  */
1121 static int wimfs_mkdir(const char *path, mode_t mode)
1122 {
1123         struct dentry *parent;
1124         struct dentry *newdir;
1125         const char *basename;
1126         
1127         parent = get_parent_dentry(w, path);
1128         if (!parent)
1129                 return -ENOENT;
1130
1131         if (!dentry_is_directory(parent))
1132                 return -ENOTDIR;
1133
1134         basename = path_basename(path);
1135         if (get_dentry_child_with_name(parent, basename))
1136                 return -EEXIST;
1137
1138         newdir = new_dentry(basename);
1139         newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1140         newdir->resolved = true;
1141         newdir->link_group_id = next_link_group_id++;
1142         link_dentry(newdir, parent);
1143         return 0;
1144 }
1145
1146
1147 /* Creates a regular file. */
1148 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1149 {
1150         const char *stream_name;
1151         if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1152              && (stream_name = path_stream_name(path))) {
1153                 /* Make an alternate data stream */
1154                 struct ads_entry *new_entry;
1155                 struct dentry *dentry;
1156
1157                 char *p = (char*)stream_name - 1;
1158                 wimlib_assert(*p == ':');
1159                 *p = '\0';
1160
1161                 dentry = get_dentry(w, path);
1162                 if (!dentry || !dentry_is_regular_file(dentry))
1163                         return -ENOENT;
1164                 if (dentry_get_ads_entry(dentry, stream_name))
1165                         return -EEXIST;
1166                 new_entry = dentry_add_ads(dentry, stream_name);
1167                 if (!new_entry)
1168                         return -ENOENT;
1169         } else {
1170                 struct dentry *dentry, *parent;
1171                 const char *basename;
1172
1173                 /* Make a normal file (not an alternate data stream) */
1174
1175                 /* Make sure that the parent of @path exists and is a directory, and
1176                  * that the dentry named by @path does not already exist.  */
1177                 parent = get_parent_dentry(w, path);
1178                 if (!parent)
1179                         return -ENOENT;
1180                 if (!dentry_is_directory(parent))
1181                         return -ENOTDIR;
1182
1183                 basename = path_basename(path);
1184                 if (get_dentry_child_with_name(parent, path))
1185                         return -EEXIST;
1186
1187                 dentry = new_dentry(basename);
1188                 if (!dentry)
1189                         return -ENOMEM;
1190                 dentry->resolved = true;
1191                 dentry->link_group_id = next_link_group_id++;
1192                 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
1193                 INIT_LIST_HEAD(&dentry->lte_group_list.list);
1194                 link_dentry(dentry, parent);
1195         }
1196         return 0;
1197 }
1198
1199
1200 /* Open a file.  */
1201 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1202 {
1203         struct dentry *dentry;
1204         struct lookup_table_entry *lte;
1205         int ret;
1206         struct wimlib_fd *fd;
1207         unsigned stream_idx;
1208
1209         ret = lookup_resource(w, path, get_lookup_flags(), &dentry, &lte,
1210                               &stream_idx);
1211         if (ret != 0)
1212                 return ret;
1213
1214         if (!lte) {
1215                 /* Empty file with no lookup-table entry.  This is fine if it's
1216                  * a read-only filesystem.  Otherwise we need to create a lookup
1217                  * table entry so that we can keep track of the file descriptors
1218                  * (this is important in case someone opens the file for
1219                  * writing) */
1220                 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1221                         fi->fh = 0;
1222                         return 0;
1223                 }
1224
1225                 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1226                                                       &lte, 0);
1227                 if (ret != 0)
1228                         return ret;
1229         }
1230
1231         ret = alloc_wimlib_fd(lte, &fd);
1232         if (ret != 0)
1233                 return ret;
1234
1235         fd->dentry = dentry;
1236
1237         /* The file resource may be in the staging directory (read-write
1238          * mounts only) or in the WIM.  If it's in the staging
1239          * directory, we need to open a native file descriptor for the
1240          * corresponding file.  Otherwise, we can read the file resource
1241          * directly from the WIM file if we are opening it read-only,
1242          * but we need to extract the resource to the staging directory
1243          * if we are opening it writable. */
1244         if (flags_writable(fi->flags) &&
1245               lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1246                 ret = extract_resource_to_staging_dir(dentry, stream_idx, &lte,
1247                                                       lte->resource_entry.original_size);
1248                 if (ret != 0)
1249                         return ret;
1250         }
1251         if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1252                 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1253                 if (fd->staging_fd == -1) {
1254                         close_wimlib_fd(fd);
1255                         return -errno;
1256                 }
1257         }
1258         fi->fh = (uint64_t)fd;
1259         return 0;
1260 }
1261
1262 /* Opens a directory. */
1263 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1264 {
1265         struct dentry *dentry;
1266         
1267         dentry = get_dentry(w, path);
1268         if (!dentry)
1269                 return -ENOENT;
1270         if (!dentry_is_directory(dentry))
1271                 return -ENOTDIR;
1272         dentry->num_times_opened++;
1273         fi->fh = (uint64_t)dentry;
1274         return 0;
1275 }
1276
1277
1278 /*
1279  * Read data from a file in the WIM or in the staging directory. 
1280  */
1281 static int wimfs_read(const char *path, char *buf, size_t size, 
1282                       off_t offset, struct fuse_file_info *fi)
1283 {
1284         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1285
1286         if (!fd) {
1287                 /* Empty file with no lookup table entry on read-only mounted
1288                  * WIM */
1289                 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1290                 return 0;
1291         }
1292
1293         wimlib_assert(fd->lte);
1294
1295         if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1296                 /* Read from staging file */
1297
1298                 wimlib_assert(fd->lte->staging_file_name);
1299                 wimlib_assert(fd->staging_fd != -1);
1300
1301                 ssize_t ret;
1302                 DEBUG("Seek to offset %zu", offset);
1303
1304                 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1305                         return -errno;
1306                 ret = read(fd->staging_fd, buf, size);
1307                 if (ret == -1)
1308                         return -errno;
1309                 return ret;
1310         } else {
1311                 /* Read from WIM */
1312
1313                 const struct resource_entry *res_entry;
1314                 
1315                 res_entry = &fd->lte->resource_entry;
1316
1317                 if (offset > res_entry->original_size)
1318                         return -EOVERFLOW;
1319
1320                 size = min(size, res_entry->original_size - offset);
1321
1322                 if (read_wim_resource(fd->lte, (u8*)buf,
1323                                       size, offset, false) != 0)
1324                         return -EIO;
1325                 return size;
1326         }
1327 }
1328
1329 /* Fills in the entries of the directory specified by @path using the
1330  * FUSE-provided function @filler.  */
1331 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, 
1332                          off_t offset, struct fuse_file_info *fi)
1333 {
1334         struct dentry *parent, *child;
1335         
1336         parent = (struct dentry*)fi->fh;
1337         wimlib_assert(parent);
1338         child = parent->children;
1339
1340         filler(buf, ".", NULL, 0);
1341         filler(buf, "..", NULL, 0);
1342
1343         if (!child)
1344                 return 0;
1345
1346         do {
1347                 if (filler(buf, child->file_name_utf8, NULL, 0))
1348                         return 0;
1349                 child = child->next;
1350         } while (child != parent->children);
1351         return 0;
1352 }
1353
1354
1355 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1356 {
1357         struct dentry *dentry = get_dentry(w, path);
1358         int ret;
1359         if (!dentry)
1360                 return -ENOENT;
1361         if (!dentry_is_symlink(dentry))
1362                 return -EINVAL;
1363
1364         ret = dentry_readlink(dentry, buf, buf_len, w);
1365         if (ret > 0)
1366                 ret = 0;
1367         return ret;
1368 }
1369
1370 /* Close a file. */
1371 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1372 {
1373         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1374
1375         if (!fd) {
1376                 /* Empty file with no lookup table entry on read-only mounted
1377                  * WIM */
1378                 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1379                 return 0;
1380         }
1381
1382         if (flags_writable(fi->flags) && fd->dentry) {
1383                 u64 now = get_wim_timestamp();
1384                 fd->dentry->last_access_time = now;
1385                 fd->dentry->last_write_time = now;
1386         }
1387
1388         return close_wimlib_fd(fd);
1389 }
1390
1391 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1392 {
1393         struct dentry *dentry = (struct dentry *)fi->fh;
1394
1395         wimlib_assert(dentry);
1396         wimlib_assert(dentry->num_times_opened);
1397         if (--dentry->num_times_opened == 0)
1398                 free_dentry(dentry);
1399         return 0;
1400 }
1401
1402 /* Remove an alternate data stream through the XATTR interface */
1403 static int wimfs_removexattr(const char *path, const char *name)
1404 {
1405         struct dentry *dentry;
1406         struct ads_entry *ads_entry;
1407         if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1408                 return -ENOTSUP;
1409
1410         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1411                 return -ENOATTR;
1412         name += 5;
1413
1414         dentry = get_dentry(w, path);
1415         if (!dentry)
1416                 return -ENOENT;
1417
1418         ads_entry = dentry_get_ads_entry(dentry, name);
1419         if (!ads_entry)
1420                 return -ENOATTR;
1421         remove_ads(dentry, ads_entry, w->lookup_table);
1422         return 0;
1423 }
1424
1425 /* Renames a file or directory.  See rename (3) */
1426 static int wimfs_rename(const char *from, const char *to)
1427 {
1428         struct dentry *src;
1429         struct dentry *dst;
1430         struct dentry *parent_of_dst;
1431         char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1432         u16 file_name_utf16_len, file_name_utf8_len;
1433         int ret;
1434
1435         /* This rename() implementation currently only supports actual files
1436          * (not alternate data streams) */
1437         
1438         src = get_dentry(w, from);
1439         if (!src)
1440                 return -ENOENT;
1441
1442         dst = get_dentry(w, to);
1443
1444
1445         ret = get_names(&file_name_utf16, &file_name_utf8,
1446                         &file_name_utf16_len, &file_name_utf8_len,
1447                         path_basename(to));
1448         if (ret != 0)
1449                 return -ENOMEM;
1450
1451         if (dst) {
1452                 /* Destination file exists */
1453
1454                 if (src == dst) /* Same file */
1455                         return 0;
1456
1457                 if (!dentry_is_directory(src)) {
1458                         /* Cannot rename non-directory to directory. */
1459                         if (dentry_is_directory(dst))
1460                                 return -EISDIR;
1461                 } else {
1462                         /* Cannot rename directory to a non-directory or a non-empty
1463                          * directory */
1464                         if (!dentry_is_directory(dst))
1465                                 return -ENOTDIR;
1466                         if (dst->children != NULL)
1467                                 return -ENOTEMPTY;
1468                 }
1469                 parent_of_dst = dst->parent;
1470                 remove_dentry(dst, w->lookup_table);
1471         } else {
1472                 /* Destination does not exist */
1473                 parent_of_dst = get_parent_dentry(w, to);
1474                 if (!parent_of_dst)
1475                         return -ENOENT;
1476
1477                 if (!dentry_is_directory(parent_of_dst))
1478                         return -ENOTDIR;
1479         }
1480
1481         FREE(src->file_name);
1482         FREE(src->file_name_utf8);
1483         src->file_name          = file_name_utf16;
1484         src->file_name_utf8     = file_name_utf8;
1485         src->file_name_len      = file_name_utf16_len;
1486         src->file_name_utf8_len = file_name_utf8_len;
1487
1488         unlink_dentry(src);
1489         link_dentry(src, parent_of_dst);
1490         return 0;
1491 }
1492
1493 /* Remove a directory */
1494 static int wimfs_rmdir(const char *path)
1495 {
1496         struct dentry *dentry;
1497         
1498         dentry = get_dentry(w, path);
1499         if (!dentry)
1500                 return -ENOENT;
1501
1502         if (!dentry_is_empty_directory(dentry))
1503                 return -ENOTEMPTY;
1504
1505         unlink_dentry(dentry);
1506         if (dentry->num_times_opened == 0)
1507                 free_dentry(dentry);
1508         return 0;
1509 }
1510
1511 /* Write an alternate data stream through the XATTR interface */
1512 static int wimfs_setxattr(const char *path, const char *name,
1513                           const char *value, size_t size, int flags)
1514 {
1515         struct dentry *dentry;
1516         struct ads_entry *existing_ads_entry;
1517         struct ads_entry *new_ads_entry;
1518         struct lookup_table_entry *existing_lte;
1519         struct lookup_table_entry *lte;
1520         u8 value_hash[SHA1_HASH_SIZE];
1521
1522         if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1523                 return -ENOTSUP;
1524
1525         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1526                 return -ENOATTR;
1527         name += 5;
1528
1529         dentry = get_dentry(w, path);
1530         if (!dentry)
1531                 return -ENOENT;
1532         existing_ads_entry = dentry_get_ads_entry(dentry, name);
1533         if (existing_ads_entry) {
1534                 if (flags & XATTR_CREATE)
1535                         return -EEXIST;
1536                 remove_ads(dentry, existing_ads_entry, w->lookup_table);
1537         } else {
1538                 if (flags & XATTR_REPLACE)
1539                         return -ENOATTR;
1540         }
1541         new_ads_entry = dentry_add_ads(dentry, name);
1542         if (!new_ads_entry)
1543                 return -ENOMEM;
1544
1545         sha1_buffer((const u8*)value, size, value_hash);
1546
1547         existing_lte = __lookup_resource(w->lookup_table, value_hash);
1548
1549         if (existing_lte) {
1550                 lte = existing_lte;
1551                 lte->refcnt++;
1552         } else {
1553                 u8 *value_copy;
1554                 lte = new_lookup_table_entry();
1555                 if (!lte)
1556                         return -ENOMEM;
1557                 value_copy = MALLOC(size);
1558                 if (!value_copy) {
1559                         FREE(lte);
1560                         return -ENOMEM;
1561                 }
1562                 memcpy(value_copy, value, size);
1563                 lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
1564                 lte->attached_buffer              = value_copy;
1565                 lte->resource_entry.original_size = size;
1566                 lte->resource_entry.size          = size;
1567                 lte->resource_entry.flags         = 0;
1568                 copy_hash(lte->hash, value_hash);
1569                 lookup_table_insert(w->lookup_table, lte);
1570         }
1571         new_ads_entry->lte = lte;
1572         return 0;
1573 }
1574
1575 static int wimfs_symlink(const char *to, const char *from)
1576 {
1577         struct dentry *dentry_parent, *dentry;
1578         const char *link_name;
1579         struct lookup_table_entry *lte;
1580         
1581         dentry_parent = get_parent_dentry(w, from);
1582         if (!dentry_parent)
1583                 return -ENOENT;
1584         if (!dentry_is_directory(dentry_parent))
1585                 return -ENOTDIR;
1586
1587         link_name = path_basename(from);
1588
1589         if (get_dentry_child_with_name(dentry_parent, link_name))
1590                 return -EEXIST;
1591         dentry = new_dentry(link_name);
1592         if (!dentry)
1593                 return -ENOMEM;
1594
1595         dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1596         dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1597         dentry->link_group_id = next_link_group_id++;
1598
1599         if (dentry_set_symlink(dentry, to, w->lookup_table, &lte) != 0)
1600                 goto out_free_dentry;
1601
1602         wimlib_assert(lte);
1603
1604         dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1605         list_add(&dentry->ads_entries[1].lte_group_list.list,
1606                  &lte->lte_group_list);
1607         wimlib_assert(dentry->resolved);
1608
1609         link_dentry(dentry, dentry_parent);
1610         return 0;
1611 out_free_dentry:
1612         free_dentry(dentry);
1613         return -ENOMEM;
1614 }
1615
1616
1617 /* Reduce the size of a file */
1618 static int wimfs_truncate(const char *path, off_t size)
1619 {
1620         struct dentry *dentry;
1621         struct lookup_table_entry *lte;
1622         int ret;
1623         unsigned stream_idx;
1624         
1625         ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1626                               &lte, &stream_idx);
1627
1628         if (ret != 0)
1629                 return ret;
1630
1631         if (!lte) /* Already a zero-length file */
1632                 return 0;
1633
1634         if (lte->staging_file_name) {
1635                 ret = truncate(lte->staging_file_name, size);
1636                 if (ret != 0)
1637                         return -errno;
1638                 lte->resource_entry.original_size = size;
1639         } else {
1640                 /* File in WIM.  Extract it to the staging directory, but only
1641                  * the first @size bytes of it. */
1642                 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1643                                                       &lte, size);
1644         }
1645         dentry_update_all_timestamps(dentry);
1646         return ret;
1647 }
1648
1649 /* Remove a regular file */
1650 static int wimfs_unlink(const char *path)
1651 {
1652         struct dentry *dentry;
1653         struct lookup_table_entry *lte;
1654         int ret;
1655         unsigned stream_idx;
1656         
1657         ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1658                               &lte, &stream_idx);
1659
1660         if (ret != 0)
1661                 return ret;
1662
1663         if (stream_idx == 0) {
1664                 /* We are removing the full dentry including all alternate data
1665                  * streams. */
1666                 remove_dentry(dentry, w->lookup_table);
1667         } else {
1668                 /* We are removing an alternate data stream. */
1669                 remove_ads(dentry, &dentry->ads_entries[stream_idx - 1],
1670                            w->lookup_table);
1671         }
1672         /* Beware: The lookup table entry(s) may still be referenced by users
1673          * that have opened the corresponding streams.  They are freed later in
1674          * wimfs_release() when the last file user has closed the stream. */
1675         return 0;
1676 }
1677
1678 /* 
1679  * Change the timestamp on a file dentry. 
1680  *
1681  * Note that alternate data streams do not have their own timestamps.
1682  */
1683 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1684 {
1685         struct dentry *dentry = get_dentry(w, path);
1686         if (!dentry)
1687                 return -ENOENT;
1688         if (tv[0].tv_nsec != UTIME_OMIT) {
1689                 if (tv[0].tv_nsec == UTIME_NOW)
1690                         dentry->last_access_time = get_wim_timestamp();
1691                 else
1692                         dentry->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1693         }
1694         if (tv[1].tv_nsec != UTIME_OMIT) {
1695                 if (tv[1].tv_nsec == UTIME_NOW)
1696                         dentry->last_write_time = get_wim_timestamp();
1697                 else
1698                         dentry->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1699         }
1700         return 0;
1701 }
1702
1703 /* Writes to a file in the WIM filesystem. 
1704  * It may be an alternate data stream, but here we don't even notice because we
1705  * just get a lookup table entry. */
1706 static int wimfs_write(const char *path, const char *buf, size_t size, 
1707                        off_t offset, struct fuse_file_info *fi)
1708 {
1709         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1710         int ret;
1711
1712         wimlib_assert(fd);
1713         wimlib_assert(fd->lte);
1714         wimlib_assert(fd->lte->staging_file_name);
1715         wimlib_assert(fd->staging_fd != -1);
1716
1717         /* Seek to the requested position */
1718         if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1719                 return -errno;
1720
1721         /* Write the data. */
1722         ret = write(fd->staging_fd, buf, size);
1723         if (ret == -1)
1724                 return -errno;
1725
1726         return ret;
1727 }
1728
1729 static struct fuse_operations wimfs_operations = {
1730         .access      = wimfs_access,
1731         .destroy     = wimfs_destroy,
1732         .fallocate   = wimfs_fallocate,
1733         .fgetattr    = wimfs_fgetattr,
1734         .ftruncate   = wimfs_ftruncate,
1735         .getattr     = wimfs_getattr,
1736         .getxattr    = wimfs_getxattr,
1737         .link        = wimfs_link,
1738         .listxattr   = wimfs_listxattr,
1739         .mkdir       = wimfs_mkdir,
1740         .mknod       = wimfs_mknod,
1741         .open        = wimfs_open,
1742         .opendir     = wimfs_opendir,
1743         .read        = wimfs_read,
1744         .readdir     = wimfs_readdir,
1745         .readlink    = wimfs_readlink,
1746         .release     = wimfs_release,
1747         .releasedir  = wimfs_releasedir,
1748         .removexattr = wimfs_removexattr,
1749         .rename      = wimfs_rename,
1750         .rmdir       = wimfs_rmdir,
1751         .setxattr    = wimfs_setxattr,
1752         .symlink     = wimfs_symlink,
1753         .truncate    = wimfs_truncate,
1754         .unlink      = wimfs_unlink,
1755         .utimens     = wimfs_utimens,
1756         .write       = wimfs_write,
1757 };
1758
1759
1760 static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
1761 {
1762         size_t lte_group_size = 0;
1763         struct list_head *cur;
1764         list_for_each(cur, &lte->lte_group_list)
1765                 lte_group_size++;
1766         if (lte_group_size > lte->refcnt) {
1767 #ifdef ENABLE_ERROR_MESSAGES
1768                 struct dentry *example_dentry;
1769                 struct list_head *next;
1770                 struct stream_list_head *head;
1771                 WARNING("The following lookup table entry has a reference count "
1772                       "of %u, but", lte->refcnt);
1773                 WARNING("We found %zu references to it", lte_group_size);
1774                 next = lte->lte_group_list.next;
1775                 head = container_of(next, struct stream_list_head, list);
1776                 if (head->type == STREAM_TYPE_NORMAL) {
1777                         example_dentry = container_of(head, struct dentry,
1778                                                       lte_group_list);
1779                         WARNING("(One dentry referencing it is at `%s')",
1780                                 example_dentry->full_path_utf8);
1781                 }
1782                 print_lookup_table_entry(lte);
1783 #endif
1784                 /* Guess what!  install.wim for Windows 8 contains a stream with
1785                  * 2 dentries referencing it, but the lookup table entry has
1786                  * reference count of 1.  So we will need to handle this case
1787                  * and not just make it be an error...  I'm just setting the
1788                  * reference count to the number of references we found. */
1789
1790                 #if 1
1791                 lte->refcnt = lte_group_size;
1792                 WARNING("Fixing reference count");
1793                 #else
1794                 return WIMLIB_ERR_INVALID_DENTRY;
1795                 #endif
1796         }
1797         return 0;
1798 }
1799
1800 /* Mounts a WIM file. */
1801 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir, 
1802                            int flags)
1803 {
1804         int argc = 0;
1805         char *argv[16];
1806         int ret;
1807         char *p;
1808
1809         DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1810                         wim, image, dir, flags);
1811
1812         if (!dir)
1813                 return WIMLIB_ERR_INVALID_PARAM;
1814
1815         ret = wimlib_select_image(wim, image);
1816
1817         if (ret != 0)
1818                 return ret;
1819
1820         DEBUG("Selected image %d", image);
1821
1822         next_link_group_id = assign_link_group_ids(wim->image_metadata[image - 1].lgt);
1823
1824         /* Resolve all the lookup table entries of the dentry tree */
1825         for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
1826                            wim->lookup_table);
1827
1828         ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
1829         if (ret != 0)
1830                 return ret;
1831
1832         if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1833                 wim_get_current_image_metadata(wim)->modified = true;
1834
1835         if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1836                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1837                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1838                 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1839
1840         mount_dir = dir;
1841         working_directory = getcwd(NULL, 0);
1842         if (!working_directory) {
1843                 ERROR_WITH_ERRNO("Could not determine current directory");
1844                 return WIMLIB_ERR_NOTDIR;
1845         }
1846
1847         p = STRDUP(dir);
1848         if (!p)
1849                 return WIMLIB_ERR_NOMEM;
1850
1851         argv[argc++] = "imagex";
1852         argv[argc++] = p;
1853         argv[argc++] = "-s"; /* disable multi-threaded operation */
1854
1855         if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1856                 argv[argc++] = "-d";
1857
1858         /* 
1859          * We provide the use_ino option because we are going to assign inode
1860          * numbers oursides.  We've already numbered the hard link groups with
1861          * unique numbers with the assign_link_groups() function, and the static
1862          * variable next_link_group_id is set to the next available link group
1863          * ID that we will assign to new dentries.
1864          */
1865         char optstring[256] = "use_ino";
1866         argv[argc++] = "-o";
1867         argv[argc++] = optstring;
1868         if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1869                 /* Read-write mount.  Make the staging directory */
1870                 make_staging_dir();
1871                 if (!staging_dir_name) {
1872                         FREE(p);
1873                         return WIMLIB_ERR_MKDIR;
1874                 }
1875         } else {
1876                 /* Read-only mount */
1877                 strcat(optstring, ",ro");
1878         }
1879         argv[argc] = NULL;
1880
1881 #ifdef ENABLE_DEBUG
1882         {
1883                 int i;
1884                 DEBUG("FUSE command line (argc = %d): ", argc);
1885                 for (i = 0; i < argc; i++) {
1886                         fputs(argv[i], stdout);
1887                         putchar(' ');
1888                 }
1889                 putchar('\n');
1890                 fflush(stdout);
1891         }
1892 #endif
1893
1894         /* Set static variables. */
1895         w = wim;
1896         mount_flags = flags;
1897
1898         ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1899
1900         return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1901 }
1902
1903
1904 /* 
1905  * Unmounts the WIM file that was previously mounted on @dir by using
1906  * wimlib_mount().
1907  */
1908 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1909 {
1910         pid_t pid;
1911         int status;
1912         int ret;
1913         char msg[2];
1914         struct timeval now;
1915         struct timespec timeout;
1916         int msgsize;
1917         int errno_save;
1918
1919         /* Execute `fusermount -u', which is installed setuid root, to unmount
1920          * the WIM.
1921          *
1922          * FUSE does not yet implement synchronous unmounts.  This means that
1923          * fusermount -u will return before the filesystem daemon returns from
1924          * wimfs_destroy().  This is partly what we want, because we need to
1925          * send a message from this process to the filesystem daemon telling
1926          * whether --commit was specified or not.  However, after that, the
1927          * unmount process must wait for the filesystem daemon to finish writing
1928          * the WIM file. 
1929          */
1930
1931         mount_dir = dir;
1932         pid = fork();
1933         if (pid == -1) {
1934                 ERROR_WITH_ERRNO("Failed to fork()");
1935                 return WIMLIB_ERR_FORK;
1936         }
1937         if (pid == 0) {
1938                 execlp("fusermount", "fusermount", "-u", dir, NULL);
1939                 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1940                 return WIMLIB_ERR_FUSERMOUNT;
1941         }
1942
1943         ret = waitpid(pid, &status, 0);
1944         if (ret == -1) {
1945                 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1946                                  "terminate");
1947                 return WIMLIB_ERR_FUSERMOUNT;
1948         }
1949
1950         if (status != 0) {
1951                 ERROR("fusermount exited with status %d", status);
1952                 return WIMLIB_ERR_FUSERMOUNT;
1953         }
1954
1955         /* Open message queues between the unmount process and the
1956          * filesystem daemon. */
1957         ret = open_message_queues(false);
1958         if (ret != 0)
1959                 return ret;
1960
1961         /* Send a message to the filesystem saying whether to commit or
1962          * not. */
1963         msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1964         msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1965
1966         DEBUG("Sending message: %s, %s", 
1967                         (msg[0] == 0) ? "don't commit" : "commit",
1968                         (msg[1] == 0) ? "don't check"  : "check");
1969         ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1970         if (ret == -1) {
1971                 ERROR("Failed to notify filesystem daemon whether we want to "
1972                       "commit changes or not");
1973                 close_message_queues();
1974                 return WIMLIB_ERR_MQUEUE;
1975         }
1976
1977         /* Wait for a message from the filesytem daemon indicating whether  the
1978          * filesystem was unmounted successfully (0) or an error occurred (1).
1979          * This may take a long time if a big WIM file needs to be rewritten. */
1980
1981         /* Wait at most 600??? seconds before giving up and returning false.
1982          * Either it's a really big WIM file, or (more likely) the
1983          * filesystem daemon has crashed or failed for some reason.
1984          *
1985          * XXX come up with some method to determine if the filesystem
1986          * daemon has really crashed or not. 
1987          *
1988          * XXX Idea: have mount daemon write its PID into the WIM file header?
1989          * */
1990
1991         gettimeofday(&now, NULL);
1992         timeout.tv_sec = now.tv_sec + 600;
1993         timeout.tv_nsec = now.tv_usec * 1000;
1994
1995         msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1996         char mailbox[msgsize];
1997
1998         mailbox[0] = 0;
1999         DEBUG("Waiting for message telling us whether the unmount was "
2000                         "successful or not.");
2001         ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
2002                               NULL, &timeout);
2003         errno_save = errno;
2004         close_message_queues();
2005         if (ret == -1) {
2006                 if (errno_save == ETIMEDOUT) {
2007                         ERROR("Timed out- probably the filesystem daemon "
2008                               "crashed and the WIM was not written "
2009                               "successfully.");
2010                         return WIMLIB_ERR_TIMEOUT;
2011                 } else {
2012                         ERROR("mq_receive(): %s", strerror(errno_save));
2013                         return WIMLIB_ERR_MQUEUE;
2014                 }
2015
2016         }
2017         DEBUG("Received message: %s",
2018               (mailbox[0] == 0) ?  "Unmount OK" : "Unmount Failed");
2019         if (mailbox[0] != 0)
2020                 ERROR("Unmount failed");
2021         return mailbox[0];
2022 }
2023
2024 #else /* WITH_FUSE */
2025
2026
2027 static inline int mount_unsupported_error()
2028 {
2029         ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2030               "for mounting WIMs.");
2031         return WIMLIB_ERR_UNSUPPORTED;
2032 }
2033
2034 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2035 {
2036         return mount_unsupported_error();
2037 }
2038
2039 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir, 
2040                            int flags)
2041 {
2042         return mount_unsupported_error();
2043 }
2044
2045 #endif /* WITH_FUSE */