8a9640c3dbfa339f167ed52324673888c7c3906d
[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(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(lte->staging_file_name);
228                         struct stat native_stat;
229                         if (stat(lte->staging_file_name, &native_stat) != 0) {
230                                 DEBUG("Failed to stat `%s': %m",
231                                       lte->staging_file_name);
232                                 return -errno;
233                         }
234                         stbuf->st_size = native_stat.st_size;
235                 } else {
236                         stbuf->st_size = lte->resource_entry.original_size;
237                 }
238         } else {
239                 stbuf->st_size = 0;
240         }
241
242         stbuf->st_atime   = wim_timestamp_to_unix(dentry->last_access_time);
243         stbuf->st_mtime   = wim_timestamp_to_unix(dentry->last_write_time);
244         stbuf->st_ctime   = wim_timestamp_to_unix(dentry->creation_time);
245         stbuf->st_blocks  = (stbuf->st_size + 511) / 512;
246         return 0;
247 }
248
249 /* Creates a new staging file and returns its file descriptor opened for
250  * writing.
251  *
252  * @name_ret: A location into which the a pointer to the newly allocated name of
253  *                      the staging file is stored.
254  * @return:  The file descriptor for the new file.  Returns -1 and sets errno on
255  *              error, for any reason possible from the creat() function.
256  */
257 static int create_staging_file(char **name_ret, int open_flags)
258 {
259         size_t name_len;
260         char *name;
261         struct stat stbuf;
262         int fd;
263         int errno_save;
264
265         name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
266         name = MALLOC(name_len + 1);
267         if (!name) {
268                 errno = ENOMEM;
269                 return -1;
270         }
271
272         do {
273
274                 memcpy(name, staging_dir_name, staging_dir_name_len);
275                 name[staging_dir_name_len] = '/';
276                 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
277                                                 SHA1_HASH_SIZE);
278                 name[name_len] = '\0';
279
280
281         /* Just in case, verify that the randomly generated name doesn't name an
282          * existing file, and try again if so  */
283         } while (stat(name, &stbuf) == 0);
284
285         if (errno != ENOENT)
286                 /* other error! */
287                 return -1;
288
289         /* doesn't exist--- ok */
290
291         DEBUG("Creating staging file `%s'", name);
292
293         fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600); 
294         if (fd == -1) {
295                 errno_save = errno;
296                 FREE(name);
297                 errno = errno_save;
298         } else {
299                 *name_ret = name;
300         }
301         return fd;
302 }
303
304 /* 
305  * Removes open file descriptors from a lookup table entry @old_lte where the
306  * file descriptors have opened the corresponding file resource in the context
307  * of the hard link group @link_group; these file descriptors are extracted and
308  * placed in a new lookup table entry, which is returned.
309  */
310 static struct lookup_table_entry *
311 lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group)
312 {
313         int ret;
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 = %u, 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         if (ret != 0)
865                 return ret;
866
867         xml_update_image_info(w, w->current_image);
868
869         ret = wimlib_overwrite(w, check_integrity);
870         if (ret != 0) {
871                 ERROR("Failed to commit changes");
872                 return ret;
873         }
874         return ret;
875 }
876
877 /* Called when the filesystem is unmounted. */
878 static void wimfs_destroy(void *p)
879 {
880         /* For read-write mounts, the `imagex unmount' command, which is
881          * running in a separate process and is executing the
882          * wimlib_unmount() function, will send this process a byte
883          * through a message queue that indicates whether the --commit
884          * option was specified or not. */
885
886         int msgsize;
887         struct timespec timeout;
888         struct timeval now;
889         ssize_t bytes_received;
890         int ret;
891         char commit;
892         char check_integrity;
893         char status;
894
895         ret = open_message_queues(true);
896         if (ret != 0)
897                 exit(1);
898
899         msgsize = mq_get_msgsize(unmount_to_daemon_mq);
900         char msg[msgsize];
901         msg[0] = 0;
902         msg[1] = 0;
903
904         /* Wait at most 3 seconds before giving up and discarding changes. */
905         gettimeofday(&now, NULL);
906         timeout.tv_sec = now.tv_sec + 3;
907         timeout.tv_nsec = now.tv_usec * 1000;
908         DEBUG("Waiting for message telling us whether to commit or not, and "
909               "whether to include integrity checks.");
910
911         bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg, 
912                                          msgsize, NULL, &timeout);
913         commit = msg[0];
914         check_integrity = msg[1];
915         if (bytes_received == -1) {
916                 if (errno == ETIMEDOUT) {
917                         ERROR("Timed out.");
918                 } else {
919                         ERROR_WITH_ERRNO("mq_timedreceive()");
920                 }
921                 ERROR("Not committing.");
922         } else {
923                 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
924         }
925
926         status = 0;
927         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
928                 if (commit) {
929                         status = chdir(working_directory);
930                         if (status != 0) {
931                                 ERROR_WITH_ERRNO("chdir()");
932                                 status = WIMLIB_ERR_NOTDIR;
933                                 goto done;
934                         }
935                         status = rebuild_wim(w, (check_integrity != 0));
936                 }
937                 ret = delete_staging_dir();
938                 if (ret != 0) {
939                         ERROR_WITH_ERRNO("Failed to delete the staging "
940                                          "directory");
941                         if (status == 0)
942                                 status = ret;
943                 }
944         } else {
945                 DEBUG("Read-only mount");
946         }
947 done:
948         DEBUG("Sending status %u", status);
949         ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
950         if (ret == -1)
951                 ERROR_WITH_ERRNO("Failed to send status to unmount process");
952         close_message_queues();
953 }
954
955 static int wimfs_fallocate(const char *path, int mode,
956                            off_t offset, off_t len, struct fuse_file_info *fi)
957 {
958         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
959         wimlib_assert(fd->staging_fd != -1);
960         return fallocate(fd->staging_fd, mode, offset, len);
961 }
962
963 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
964                           struct fuse_file_info *fi)
965 {
966         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
967         return dentry_to_stbuf(fd->dentry, stbuf);
968 }
969
970 static int wimfs_ftruncate(const char *path, off_t size,
971                            struct fuse_file_info *fi)
972 {
973         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
974         int ret = ftruncate(fd->staging_fd, size);
975         if (ret != 0)
976                 return ret;
977         fd->lte->resource_entry.original_size = size;
978         return 0;
979 }
980
981 /*
982  * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
983  */
984 static int wimfs_getattr(const char *path, struct stat *stbuf)
985 {
986         const char *stream_name;
987         char *p = NULL;
988         struct dentry *dentry;
989         int ret;
990
991         ret = lookup_resource(w, path,
992                               get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
993                               &dentry, NULL, NULL);
994         if (ret != 0)
995                 return ret;
996         return dentry_to_stbuf(dentry, stbuf);
997 }
998
999 /* Read an alternate data stream through the XATTR interface, or get its size */
1000 static int wimfs_getxattr(const char *path, const char *name, char *value,
1001                           size_t size)
1002 {
1003         int ret;
1004         struct dentry *dentry;
1005         struct ads_entry *ads_entry;
1006         size_t res_size;
1007         struct lookup_table_entry *lte;
1008
1009         if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1010                 return -ENOTSUP;
1011
1012         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1013                 return -ENOATTR;
1014         name += 5;
1015
1016         dentry = get_dentry(w, path);
1017         if (!dentry)
1018                 return -ENOENT;
1019         ads_entry = dentry_get_ads_entry(dentry, name);
1020         if (!ads_entry)
1021                 return -ENOATTR;
1022
1023         lte = ads_entry->lte;
1024         res_size = wim_resource_size(lte);
1025
1026         if (size == 0)
1027                 return res_size;
1028         if (res_size > size)
1029                 return -ERANGE;
1030         ret = read_full_wim_resource(lte, value);
1031         if (ret != 0)
1032                 return -EIO;
1033         return res_size;
1034 }
1035
1036 /* Create a hard link */
1037 static int wimfs_link(const char *to, const char *from)
1038 {
1039         struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
1040         const char *link_name;
1041
1042         to_dentry = get_dentry(w, to);
1043         if (!to_dentry)
1044                 return -ENOENT;
1045         if (!dentry_is_regular_file(to_dentry))
1046                 return -EPERM;
1047
1048         from_dentry_parent = get_parent_dentry(w, from);
1049         if (!from_dentry_parent)
1050                 return -ENOENT;
1051         if (!dentry_is_directory(from_dentry_parent))
1052                 return -ENOTDIR;
1053
1054         link_name = path_basename(from);
1055         if (get_dentry_child_with_name(from_dentry_parent, link_name))
1056                 return -EEXIST;
1057         from_dentry = clone_dentry(to_dentry);
1058         if (!from_dentry)
1059                 return -ENOMEM;
1060         if (change_dentry_name(from_dentry, link_name) != 0) {
1061                 FREE(from_dentry);
1062                 return -ENOMEM;
1063         }
1064
1065         /* Add the new dentry to the dentry list for the link group */
1066         list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
1067
1068         /* Increment reference counts for the unnamed file stream and all
1069          * alternate data streams. */
1070         if (from_dentry->lte) {
1071                 list_add(&from_dentry->lte_group_list.list,
1072                          &to_dentry->lte_group_list.list);
1073                 from_dentry->lte->refcnt++;
1074         }
1075         for (u16 i = 0; i < from_dentry->num_ads; i++) {
1076                 struct ads_entry *ads_entry = &from_dentry->ads_entries[i];
1077                 if (ads_entry->lte)
1078                         ads_entry->lte->refcnt++;
1079         }
1080
1081         /* The ADS entries are owned by another dentry. */
1082         from_dentry->ads_entries_status = ADS_ENTRIES_USER;
1083
1084         link_dentry(from_dentry, from_dentry_parent);
1085         return 0;
1086 }
1087
1088 static int wimfs_listxattr(const char *path, char *list, size_t size)
1089 {
1090         struct dentry *dentry;
1091         int ret;
1092         char *p = list;
1093         size_t needed_size;
1094         unsigned i;
1095         if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1096                 return -ENOTSUP;
1097
1098         /* List alternate data streams, or get the list size */
1099
1100         ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1101         if (ret != 0)
1102                 return ret;
1103         if (size == 0) {
1104                 needed_size = 0;
1105                 for (i = 0; i < dentry->num_ads; i++)
1106                         needed_size += dentry->ads_entries[i].stream_name_utf8_len + 6;
1107                 return needed_size;
1108         } else {
1109                 for (i = 0; i < dentry->num_ads; i++) {
1110                         needed_size = dentry->ads_entries[i].stream_name_utf8_len + 6;
1111                         if (needed_size > size)
1112                                 return -ERANGE;
1113                         p += sprintf(p, "user.%s",
1114                                      dentry->ads_entries[i].stream_name_utf8) + 1;
1115                         size -= needed_size;
1116                 }
1117                 return p - list;
1118         }
1119 }
1120
1121 /* 
1122  * Create a directory in the WIM.  
1123  * @mode is currently ignored.
1124  */
1125 static int wimfs_mkdir(const char *path, mode_t mode)
1126 {
1127         struct dentry *parent;
1128         struct dentry *newdir;
1129         const char *basename;
1130         
1131         parent = get_parent_dentry(w, path);
1132         if (!parent)
1133                 return -ENOENT;
1134
1135         if (!dentry_is_directory(parent))
1136                 return -ENOTDIR;
1137
1138         basename = path_basename(path);
1139         if (get_dentry_child_with_name(parent, basename))
1140                 return -EEXIST;
1141
1142         newdir = new_dentry(basename);
1143         newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1144         newdir->resolved = true;
1145         newdir->link_group_id = next_link_group_id++;
1146         link_dentry(newdir, parent);
1147         return 0;
1148 }
1149
1150
1151 /* Creates a regular file. */
1152 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1153 {
1154         const char *stream_name;
1155         const char *file_name;
1156         if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1157              && (stream_name = path_stream_name(path))) {
1158                 /* Make an alternate data stream */
1159                 struct ads_entry *new_entry;
1160                 struct dentry *dentry;
1161
1162                 char *p = (char*)stream_name - 1;
1163                 wimlib_assert(*p == ':');
1164                 *p = '\0';
1165
1166                 dentry = get_dentry(w, path);
1167                 if (!dentry || !dentry_is_regular_file(dentry))
1168                         return -ENOENT;
1169                 if (dentry_get_ads_entry(dentry, stream_name))
1170                         return -EEXIST;
1171                 new_entry = dentry_add_ads(dentry, stream_name);
1172                 if (!new_entry)
1173                         return -ENOENT;
1174         } else {
1175                 struct dentry *dentry, *parent;
1176                 const char *basename;
1177
1178                 /* Make a normal file (not an alternate data stream) */
1179
1180                 /* Make sure that the parent of @path exists and is a directory, and
1181                  * that the dentry named by @path does not already exist.  */
1182                 parent = get_parent_dentry(w, path);
1183                 if (!parent)
1184                         return -ENOENT;
1185                 if (!dentry_is_directory(parent))
1186                         return -ENOTDIR;
1187
1188                 basename = path_basename(path);
1189                 if (get_dentry_child_with_name(parent, path))
1190                         return -EEXIST;
1191
1192                 dentry = new_dentry(basename);
1193                 if (!dentry)
1194                         return -ENOMEM;
1195                 dentry->resolved = true;
1196                 dentry->link_group_id = next_link_group_id++;
1197                 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
1198                 INIT_LIST_HEAD(&dentry->lte_group_list.list);
1199                 link_dentry(dentry, parent);
1200         }
1201         return 0;
1202 }
1203
1204
1205 /* Open a file.  */
1206 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1207 {
1208         struct dentry *dentry;
1209         struct lookup_table_entry *lte;
1210         u8 *dentry_hash;
1211         int ret;
1212         struct wimlib_fd *fd;
1213         unsigned stream_idx;
1214
1215         ret = lookup_resource(w, path, get_lookup_flags(), &dentry, &lte,
1216                               &stream_idx);
1217         if (ret != 0)
1218                 return ret;
1219
1220         if (!lte) {
1221                 /* Empty file with no lookup-table entry.  This is fine if it's
1222                  * a read-only filesystem.  Otherwise we need to create a lookup
1223                  * table entry so that we can keep track of the file descriptors
1224                  * (this is important in case someone opens the file for
1225                  * writing) */
1226                 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1227                         fi->fh = 0;
1228                         return 0;
1229                 }
1230
1231                 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1232                                                       &lte, 0);
1233                 if (ret != 0)
1234                         return ret;
1235         }
1236
1237         ret = alloc_wimlib_fd(lte, &fd);
1238         if (ret != 0)
1239                 return ret;
1240
1241         fd->dentry = dentry;
1242
1243         /* The file resource may be in the staging directory (read-write
1244          * mounts only) or in the WIM.  If it's in the staging
1245          * directory, we need to open a native file descriptor for the
1246          * corresponding file.  Otherwise, we can read the file resource
1247          * directly from the WIM file if we are opening it read-only,
1248          * but we need to extract the resource to the staging directory
1249          * if we are opening it writable. */
1250         if (flags_writable(fi->flags) &&
1251               lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1252                 ret = extract_resource_to_staging_dir(dentry, stream_idx, &lte,
1253                                                       lte->resource_entry.original_size);
1254                 if (ret != 0)
1255                         return ret;
1256         }
1257         if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1258                 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1259                 if (fd->staging_fd == -1) {
1260                         close_wimlib_fd(fd);
1261                         return -errno;
1262                 }
1263         }
1264         fi->fh = (uint64_t)fd;
1265         return 0;
1266 }
1267
1268 /* Opens a directory. */
1269 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1270 {
1271         struct dentry *dentry;
1272         
1273         dentry = get_dentry(w, path);
1274         if (!dentry)
1275                 return -ENOENT;
1276         if (!dentry_is_directory(dentry))
1277                 return -ENOTDIR;
1278         dentry->num_times_opened++;
1279         fi->fh = (uint64_t)dentry;
1280         return 0;
1281 }
1282
1283
1284 /*
1285  * Read data from a file in the WIM or in the staging directory. 
1286  */
1287 static int wimfs_read(const char *path, char *buf, size_t size, 
1288                       off_t offset, struct fuse_file_info *fi)
1289 {
1290         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1291
1292         if (!fd) {
1293                 /* Empty file with no lookup table entry on read-only mounted
1294                  * WIM */
1295                 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1296                 return 0;
1297         }
1298
1299         wimlib_assert(fd->lte);
1300
1301         if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1302                 /* Read from staging file */
1303
1304                 wimlib_assert(fd->lte->staging_file_name);
1305                 wimlib_assert(fd->staging_fd != -1);
1306
1307                 ssize_t ret;
1308                 DEBUG("Seek to offset %zu", offset);
1309
1310                 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1311                         return -errno;
1312                 ret = read(fd->staging_fd, buf, size);
1313                 if (ret == -1)
1314                         return -errno;
1315                 return ret;
1316         } else {
1317                 /* Read from WIM */
1318
1319                 const struct resource_entry *res_entry;
1320                 
1321                 res_entry = &fd->lte->resource_entry;
1322
1323                 if (offset > res_entry->original_size)
1324                         return -EOVERFLOW;
1325
1326                 size = min(size, res_entry->original_size - offset);
1327
1328                 if (read_wim_resource(fd->lte, buf, size, offset, false) != 0)
1329                         return -EIO;
1330                 return size;
1331         }
1332 }
1333
1334 /* Fills in the entries of the directory specified by @path using the
1335  * FUSE-provided function @filler.  */
1336 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, 
1337                          off_t offset, struct fuse_file_info *fi)
1338 {
1339         struct dentry *parent, *child;
1340         
1341         parent = (struct dentry*)fi->fh;
1342         wimlib_assert(parent);
1343         child = parent->children;
1344
1345         filler(buf, ".", NULL, 0);
1346         filler(buf, "..", NULL, 0);
1347
1348         if (!child)
1349                 return 0;
1350
1351         do {
1352                 if (filler(buf, child->file_name_utf8, NULL, 0))
1353                         return 0;
1354                 child = child->next;
1355         } while (child != parent->children);
1356         return 0;
1357 }
1358
1359
1360 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1361 {
1362         struct dentry *dentry = get_dentry(w, path);
1363         int ret;
1364         if (!dentry)
1365                 return -ENOENT;
1366         if (!dentry_is_symlink(dentry))
1367                 return -EINVAL;
1368
1369         ret = dentry_readlink(dentry, buf, buf_len, w);
1370         if (ret > 0)
1371                 ret = 0;
1372         return ret;
1373 }
1374
1375 /* Close a file. */
1376 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1377 {
1378         int ret;
1379         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1380
1381         if (!fd) {
1382                 /* Empty file with no lookup table entry on read-only mounted
1383                  * WIM */
1384                 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1385                 return 0;
1386         }
1387
1388         if (flags_writable(fi->flags) && fd->dentry) {
1389                 u64 now = get_wim_timestamp();
1390                 fd->dentry->last_access_time = now;
1391                 fd->dentry->last_write_time = now;
1392         }
1393
1394         return close_wimlib_fd(fd);
1395 }
1396
1397 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1398 {
1399         struct dentry *dentry = (struct dentry *)fi->fh;
1400
1401         wimlib_assert(dentry);
1402         wimlib_assert(dentry->num_times_opened);
1403         if (--dentry->num_times_opened == 0)
1404                 free_dentry(dentry);
1405         return 0;
1406 }
1407
1408 /* Remove an alternate data stream through the XATTR interface */
1409 static int wimfs_removexattr(const char *path, const char *name)
1410 {
1411         struct dentry *dentry;
1412         struct ads_entry *ads_entry;
1413         int ret;
1414         if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1415                 return -ENOTSUP;
1416
1417         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1418                 return -ENOATTR;
1419         name += 5;
1420
1421         dentry = get_dentry(w, path);
1422         if (!dentry)
1423                 return -ENOENT;
1424
1425         ads_entry = dentry_get_ads_entry(dentry, name);
1426         if (!ads_entry)
1427                 return -ENOATTR;
1428         remove_ads(dentry, ads_entry, w->lookup_table);
1429         return 0;
1430 }
1431
1432 /* Renames a file or directory.  See rename (3) */
1433 static int wimfs_rename(const char *from, const char *to)
1434 {
1435         struct dentry *src;
1436         struct dentry *dst;
1437         struct dentry *parent_of_dst;
1438         char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1439         u16 file_name_utf16_len, file_name_utf8_len;
1440         int ret;
1441
1442         /* This rename() implementation currently only supports actual files
1443          * (not alternate data streams) */
1444         
1445         src = get_dentry(w, from);
1446         if (!src)
1447                 return -ENOENT;
1448
1449         dst = get_dentry(w, to);
1450
1451
1452         ret = get_names(&file_name_utf16, &file_name_utf8,
1453                         &file_name_utf16_len, &file_name_utf8_len,
1454                         path_basename(to));
1455         if (ret != 0)
1456                 return -ENOMEM;
1457
1458         if (dst) {
1459                 /* Destination file exists */
1460
1461                 if (src == dst) /* Same file */
1462                         return 0;
1463
1464                 if (!dentry_is_directory(src)) {
1465                         /* Cannot rename non-directory to directory. */
1466                         if (dentry_is_directory(dst))
1467                                 return -EISDIR;
1468                 } else {
1469                         /* Cannot rename directory to a non-directory or a non-empty
1470                          * directory */
1471                         if (!dentry_is_directory(dst))
1472                                 return -ENOTDIR;
1473                         if (dst->children != NULL)
1474                                 return -ENOTEMPTY;
1475                 }
1476                 parent_of_dst = dst->parent;
1477                 remove_dentry(dst, w->lookup_table);
1478         } else {
1479                 /* Destination does not exist */
1480                 parent_of_dst = get_parent_dentry(w, to);
1481                 if (!parent_of_dst)
1482                         return -ENOENT;
1483
1484                 if (!dentry_is_directory(parent_of_dst))
1485                         return -ENOTDIR;
1486         }
1487
1488         FREE(src->file_name);
1489         FREE(src->file_name_utf8);
1490         src->file_name          = file_name_utf16;
1491         src->file_name_utf8     = file_name_utf8;
1492         src->file_name_len      = file_name_utf16_len;
1493         src->file_name_utf8_len = file_name_utf8_len;
1494
1495         unlink_dentry(src);
1496         link_dentry(src, parent_of_dst);
1497         return 0;
1498 }
1499
1500 /* Remove a directory */
1501 static int wimfs_rmdir(const char *path)
1502 {
1503         struct dentry *dentry;
1504         
1505         dentry = get_dentry(w, path);
1506         if (!dentry)
1507                 return -ENOENT;
1508
1509         if (!dentry_is_empty_directory(dentry))
1510                 return -ENOTEMPTY;
1511
1512         unlink_dentry(dentry);
1513         if (dentry->num_times_opened == 0)
1514                 free_dentry(dentry);
1515         return 0;
1516 }
1517
1518 /* Write an alternate data stream through the XATTR interface */
1519 static int wimfs_setxattr(const char *path, const char *name,
1520                           const char *value, size_t size, int flags)
1521 {
1522         struct dentry *dentry;
1523         struct ads_entry *existing_ads_entry;
1524         struct ads_entry *new_ads_entry;
1525         struct lookup_table_entry *existing_lte;
1526         struct lookup_table_entry *lte;
1527         u8 value_hash[SHA1_HASH_SIZE];
1528         int ret;
1529         int fd;
1530
1531         if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1532                 return -ENOTSUP;
1533
1534         if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1535                 return -ENOATTR;
1536         name += 5;
1537
1538         dentry = get_dentry(w, path);
1539         if (!dentry)
1540                 return -ENOENT;
1541         existing_ads_entry = dentry_get_ads_entry(dentry, name);
1542         if (existing_ads_entry) {
1543                 if (flags & XATTR_CREATE)
1544                         return -EEXIST;
1545                 remove_ads(dentry, existing_ads_entry, w->lookup_table);
1546         } else {
1547                 if (flags & XATTR_REPLACE)
1548                         return -ENOATTR;
1549         }
1550         new_ads_entry = dentry_add_ads(dentry, name);
1551         if (!new_ads_entry)
1552                 return -ENOMEM;
1553
1554         sha1_buffer(value, size, value_hash);
1555
1556         existing_lte = __lookup_resource(w->lookup_table, value_hash);
1557
1558         if (existing_lte) {
1559                 lte = existing_lte;
1560                 lte->refcnt++;
1561         } else {
1562                 char *value_copy;
1563                 lte = new_lookup_table_entry();
1564                 if (!lte)
1565                         return -ENOMEM;
1566                 value_copy = MALLOC(size);
1567                 if (!value_copy) {
1568                         FREE(lte);
1569                         return -ENOMEM;
1570                 }
1571                 memcpy(value_copy, value, size);
1572                 lte->resource_location            = RESOURCE_IN_ATTACHED_BUFFER;
1573                 lte->attached_buffer              = value_copy;
1574                 lte->resource_entry.original_size = size;
1575                 lte->resource_entry.size          = size;
1576                 lte->resource_entry.flags         = 0;
1577                 copy_hash(lte->hash, value_hash);
1578                 lookup_table_insert(w->lookup_table, lte);
1579         }
1580         new_ads_entry->lte = lte;
1581         return 0;
1582 }
1583
1584 static int wimfs_symlink(const char *to, const char *from)
1585 {
1586         struct dentry *dentry_parent, *dentry;
1587         const char *link_name;
1588         struct lookup_table_entry *lte;
1589         
1590         dentry_parent = get_parent_dentry(w, from);
1591         if (!dentry_parent)
1592                 return -ENOENT;
1593         if (!dentry_is_directory(dentry_parent))
1594                 return -ENOTDIR;
1595
1596         link_name = path_basename(from);
1597
1598         if (get_dentry_child_with_name(dentry_parent, link_name))
1599                 return -EEXIST;
1600         dentry = new_dentry(link_name);
1601         if (!dentry)
1602                 return -ENOMEM;
1603
1604         dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1605         dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1606         dentry->link_group_id = next_link_group_id++;
1607
1608         if (dentry_set_symlink(dentry, to, w->lookup_table, &lte) != 0)
1609                 goto out_free_dentry;
1610
1611         wimlib_assert(lte);
1612
1613         dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1614         list_add(&dentry->ads_entries[1].lte_group_list.list,
1615                  &lte->lte_group_list);
1616         wimlib_assert(dentry->resolved);
1617
1618         link_dentry(dentry, dentry_parent);
1619         return 0;
1620 out_free_dentry:
1621         free_dentry(dentry);
1622         return -ENOMEM;
1623 }
1624
1625
1626 /* Reduce the size of a file */
1627 static int wimfs_truncate(const char *path, off_t size)
1628 {
1629         struct dentry *dentry;
1630         struct lookup_table_entry *lte;
1631         int ret;
1632         unsigned stream_idx;
1633         
1634         ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1635                               &lte, &stream_idx);
1636
1637         if (ret != 0)
1638                 return ret;
1639
1640         if (!lte) /* Already a zero-length file */
1641                 return 0;
1642
1643         if (lte->staging_file_name) {
1644                 ret = truncate(lte->staging_file_name, size);
1645                 if (ret != 0)
1646                         return -errno;
1647                 lte->resource_entry.original_size = size;
1648         } else {
1649                 /* File in WIM.  Extract it to the staging directory, but only
1650                  * the first @size bytes of it. */
1651                 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1652                                                       &lte, size);
1653         }
1654         dentry_update_all_timestamps(dentry);
1655         return ret;
1656 }
1657
1658 /* Remove a regular file */
1659 static int wimfs_unlink(const char *path)
1660 {
1661         struct dentry *dentry;
1662         struct lookup_table_entry *lte;
1663         int ret;
1664         u8 *dentry_hash;
1665         unsigned stream_idx;
1666         
1667         ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1668                               &lte, &stream_idx);
1669
1670         if (ret != 0)
1671                 return ret;
1672
1673         if (stream_idx == 0) {
1674                 /* We are removing the full dentry including all alternate data
1675                  * streams. */
1676                 remove_dentry(dentry, w->lookup_table);
1677         } else {
1678                 /* We are removing an alternate data stream. */
1679                 remove_ads(dentry, &dentry->ads_entries[stream_idx - 1],
1680                            w->lookup_table);
1681         }
1682         /* Beware: The lookup table entry(s) may still be referenced by users
1683          * that have opened the corresponding streams.  They are freed later in
1684          * wimfs_release() when the last file user has closed the stream. */
1685         return 0;
1686 }
1687
1688 /* 
1689  * Change the timestamp on a file dentry. 
1690  *
1691  * Note that alternate data streams do not have their own timestamps.
1692  */
1693 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1694 {
1695         struct dentry *dentry = get_dentry(w, path);
1696         if (!dentry)
1697                 return -ENOENT;
1698         if (tv[0].tv_nsec != UTIME_OMIT) {
1699                 if (tv[0].tv_nsec == UTIME_NOW)
1700                         dentry->last_access_time = get_wim_timestamp();
1701                 else
1702                         dentry->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1703         }
1704         if (tv[1].tv_nsec != UTIME_OMIT) {
1705                 if (tv[1].tv_nsec == UTIME_NOW)
1706                         dentry->last_write_time = get_wim_timestamp();
1707                 else
1708                         dentry->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1709         }
1710         return 0;
1711 }
1712
1713 /* Writes to a file in the WIM filesystem. 
1714  * It may be an alternate data stream, but here we don't even notice because we
1715  * just get a lookup table entry. */
1716 static int wimfs_write(const char *path, const char *buf, size_t size, 
1717                        off_t offset, struct fuse_file_info *fi)
1718 {
1719         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1720         int ret;
1721
1722         wimlib_assert(fd);
1723         wimlib_assert(fd->lte);
1724         wimlib_assert(fd->lte->staging_file_name);
1725         wimlib_assert(fd->staging_fd != -1);
1726
1727         /* Seek to the requested position */
1728         if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1729                 return -errno;
1730
1731         /* Write the data. */
1732         ret = write(fd->staging_fd, buf, size);
1733         if (ret == -1)
1734                 return -errno;
1735
1736         return ret;
1737 }
1738
1739 static struct fuse_operations wimfs_operations = {
1740         .access      = wimfs_access,
1741         .destroy     = wimfs_destroy,
1742         .fallocate   = wimfs_fallocate,
1743         .fgetattr    = wimfs_fgetattr,
1744         .ftruncate   = wimfs_ftruncate,
1745         .getattr     = wimfs_getattr,
1746         .getxattr    = wimfs_getxattr,
1747         .link        = wimfs_link,
1748         .listxattr   = wimfs_listxattr,
1749         .mkdir       = wimfs_mkdir,
1750         .mknod       = wimfs_mknod,
1751         .open        = wimfs_open,
1752         .opendir     = wimfs_opendir,
1753         .read        = wimfs_read,
1754         .readdir     = wimfs_readdir,
1755         .readlink    = wimfs_readlink,
1756         .release     = wimfs_release,
1757         .releasedir  = wimfs_releasedir,
1758         .removexattr = wimfs_removexattr,
1759         .rename      = wimfs_rename,
1760         .rmdir       = wimfs_rmdir,
1761         .setxattr    = wimfs_setxattr,
1762         .symlink     = wimfs_symlink,
1763         .truncate    = wimfs_truncate,
1764         .unlink      = wimfs_unlink,
1765         .utimens     = wimfs_utimens,
1766         .write       = wimfs_write,
1767 };
1768
1769
1770 static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
1771 {
1772         size_t lte_group_size = 0;
1773         struct list_head *cur;
1774         list_for_each(cur, &lte->lte_group_list)
1775                 lte_group_size++;
1776         if (lte_group_size > lte->refcnt) {
1777 #ifdef ENABLE_ERROR_MESSAGES
1778                 struct dentry *example_dentry;
1779                 struct list_head *next;
1780                 struct stream_list_head *head;
1781                 WARNING("The following lookup table entry has a reference count "
1782                       "of %u, but", lte->refcnt);
1783                 WARNING("We found %u references to it", lte_group_size);
1784                 next = lte->lte_group_list.next;
1785                 head = container_of(next, struct stream_list_head, list);
1786                 if (head->type == STREAM_TYPE_NORMAL) {
1787                         example_dentry = container_of(head, struct dentry,
1788                                                       lte_group_list);
1789                         WARNING("(One dentry referencing it is at `%s')",
1790                                 example_dentry->full_path_utf8);
1791                 }
1792                 print_lookup_table_entry(lte);
1793 #endif
1794                 /* Guess what!  install.wim for Windows 8 contains a stream with
1795                  * 2 dentries referencing it, but the lookup table entry has
1796                  * reference count of 1.  So we will need to handle this case
1797                  * and not just make it be an error...  I'm just setting the
1798                  * reference count to the number of references we found. */
1799
1800                 #if 1
1801                 lte->refcnt = lte_group_size;
1802                 WARNING("Fixing reference count");
1803                 #else
1804                 return WIMLIB_ERR_INVALID_DENTRY;
1805                 #endif
1806         }
1807         return 0;
1808 }
1809
1810 /* Mounts a WIM file. */
1811 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir, 
1812                            int flags)
1813 {
1814         int argc = 0;
1815         char *argv[16];
1816         int ret;
1817         char *p;
1818
1819         DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1820                         wim, image, dir, flags);
1821
1822         if (!dir)
1823                 return WIMLIB_ERR_INVALID_PARAM;
1824
1825         ret = wimlib_select_image(wim, image);
1826
1827         if (ret != 0)
1828                 return ret;
1829
1830         DEBUG("Selected image %d", image);
1831
1832         next_link_group_id = assign_link_group_ids(wim->image_metadata[image - 1].lgt);
1833
1834         /* Resolve all the lookup table entries of the dentry tree */
1835         for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
1836                            wim->lookup_table);
1837
1838         ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
1839         if (ret != 0)
1840                 return ret;
1841
1842         if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1843                 wim_get_current_image_metadata(wim)->modified = true;
1844
1845         if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1846                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1847                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1848                 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1849
1850         mount_dir = dir;
1851         working_directory = getcwd(NULL, 0);
1852         if (!working_directory) {
1853                 ERROR_WITH_ERRNO("Could not determine current directory");
1854                 return WIMLIB_ERR_NOTDIR;
1855         }
1856
1857         p = STRDUP(dir);
1858         if (!p)
1859                 return WIMLIB_ERR_NOMEM;
1860
1861         argv[argc++] = "imagex";
1862         argv[argc++] = p;
1863         argv[argc++] = "-s"; /* disable multi-threaded operation */
1864
1865         if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1866                 argv[argc++] = "-d";
1867
1868         /* 
1869          * We provide the use_ino option because we are going to assign inode
1870          * numbers oursides.  We've already numbered the hard link groups with
1871          * unique numbers with the assign_link_groups() function, and the static
1872          * variable next_link_group_id is set to the next available link group
1873          * ID that we will assign to new dentries.
1874          */
1875         char optstring[256] = "use_ino";
1876         argv[argc++] = "-o";
1877         argv[argc++] = optstring;
1878         if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1879                 /* Read-write mount.  Make the staging directory */
1880                 make_staging_dir();
1881                 if (!staging_dir_name) {
1882                         FREE(p);
1883                         return WIMLIB_ERR_MKDIR;
1884                 }
1885         } else {
1886                 /* Read-only mount */
1887                 strcat(optstring, ",ro");
1888         }
1889         argv[argc] = NULL;
1890
1891 #ifdef ENABLE_DEBUG
1892         {
1893                 int i;
1894                 DEBUG("FUSE command line (argc = %d): ", argc);
1895                 for (i = 0; i < argc; i++) {
1896                         fputs(argv[i], stdout);
1897                         putchar(' ');
1898                 }
1899                 putchar('\n');
1900                 fflush(stdout);
1901         }
1902 #endif
1903
1904         /* Set static variables. */
1905         w = wim;
1906         mount_flags = flags;
1907
1908         ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1909
1910         return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1911 }
1912
1913
1914 /* 
1915  * Unmounts the WIM file that was previously mounted on @dir by using
1916  * wimlib_mount().
1917  */
1918 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1919 {
1920         pid_t pid;
1921         int status;
1922         int ret;
1923         char msg[2];
1924         struct timeval now;
1925         struct timespec timeout;
1926         int msgsize;
1927         int errno_save;
1928
1929         /* Execute `fusermount -u', which is installed setuid root, to unmount
1930          * the WIM.
1931          *
1932          * FUSE does not yet implement synchronous unmounts.  This means that
1933          * fusermount -u will return before the filesystem daemon returns from
1934          * wimfs_destroy().  This is partly what we want, because we need to
1935          * send a message from this process to the filesystem daemon telling
1936          * whether --commit was specified or not.  However, after that, the
1937          * unmount process must wait for the filesystem daemon to finish writing
1938          * the WIM file. 
1939          */
1940
1941         mount_dir = dir;
1942         pid = fork();
1943         if (pid == -1) {
1944                 ERROR_WITH_ERRNO("Failed to fork()");
1945                 return WIMLIB_ERR_FORK;
1946         }
1947         if (pid == 0) {
1948                 execlp("fusermount", "fusermount", "-u", dir, NULL);
1949                 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1950                 return WIMLIB_ERR_FUSERMOUNT;
1951         }
1952
1953         ret = waitpid(pid, &status, 0);
1954         if (ret == -1) {
1955                 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1956                                  "terminate");
1957                 return WIMLIB_ERR_FUSERMOUNT;
1958         }
1959
1960         if (status != 0) {
1961                 ERROR("fusermount exited with status %d", status);
1962                 return WIMLIB_ERR_FUSERMOUNT;
1963         }
1964
1965         /* Open message queues between the unmount process and the
1966          * filesystem daemon. */
1967         ret = open_message_queues(false);
1968         if (ret != 0)
1969                 return ret;
1970
1971         /* Send a message to the filesystem saying whether to commit or
1972          * not. */
1973         msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1974         msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1975
1976         DEBUG("Sending message: %s, %s", 
1977                         (msg[0] == 0) ? "don't commit" : "commit",
1978                         (msg[1] == 0) ? "don't check"  : "check");
1979         ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1980         if (ret == -1) {
1981                 ERROR("Failed to notify filesystem daemon whether we want to "
1982                       "commit changes or not");
1983                 close_message_queues();
1984                 return WIMLIB_ERR_MQUEUE;
1985         }
1986
1987         /* Wait for a message from the filesytem daemon indicating whether  the
1988          * filesystem was unmounted successfully (0) or an error occurred (1).
1989          * This may take a long time if a big WIM file needs to be rewritten. */
1990
1991         /* Wait at most 600??? seconds before giving up and returning false.
1992          * Either it's a really big WIM file, or (more likely) the
1993          * filesystem daemon has crashed or failed for some reason.
1994          *
1995          * XXX come up with some method to determine if the filesystem
1996          * daemon has really crashed or not. 
1997          *
1998          * XXX Idea: have mount daemon write its PID into the WIM file header?
1999          * */
2000
2001         gettimeofday(&now, NULL);
2002         timeout.tv_sec = now.tv_sec + 600;
2003         timeout.tv_nsec = now.tv_usec * 1000;
2004
2005         msgsize = mq_get_msgsize(daemon_to_unmount_mq);
2006         char mailbox[msgsize];
2007
2008         mailbox[0] = 0;
2009         DEBUG("Waiting for message telling us whether the unmount was "
2010                         "successful or not.");
2011         ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
2012                               NULL, &timeout);
2013         errno_save = errno;
2014         close_message_queues();
2015         if (ret == -1) {
2016                 if (errno_save == ETIMEDOUT) {
2017                         ERROR("Timed out- probably the filesystem daemon "
2018                               "crashed and the WIM was not written "
2019                               "successfully.");
2020                         return WIMLIB_ERR_TIMEOUT;
2021                 } else {
2022                         ERROR("mq_receive(): %s", strerror(errno_save));
2023                         return WIMLIB_ERR_MQUEUE;
2024                 }
2025
2026         }
2027         DEBUG("Received message: %s",
2028               (mailbox[0] == 0) ?  "Unmount OK" : "Unmount Failed");
2029         if (mailbox[0] != 0)
2030                 ERROR("Unmount failed");
2031         return mailbox[0];
2032 }
2033
2034 #else /* WITH_FUSE */
2035
2036
2037 static inline int mount_unsupported_error()
2038 {
2039         ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2040               "for mounting WIMs.");
2041         return WIMLIB_ERR_UNSUPPORTED;
2042 }
2043
2044 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2045 {
2046         return mount_unsupported_error();
2047 }
2048
2049 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir, 
2050                            int flags)
2051 {
2052         return mount_unsupported_error();
2053 }
2054
2055 #endif /* WITH_FUSE */