]> wimlib.net Git - wimlib/blob - src/mount.c
symlink fixes
[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
48 struct wimlib_fd {
49         u16 idx;
50         int staging_fd;
51         u64 hard_link_group;
52         struct lookup_table_entry *lte;
53         struct dentry *dentry;
54 };
55
56 /* The WIMStruct for the mounted WIM. */
57 static WIMStruct *w;
58
59 /* Working directory when `imagex mount' is run. */
60 static const char *working_directory;
61
62 /* Name of the staging directory for a read-write mount.  Whenever a new file is
63  * created, it is done so in the staging directory.  Furthermore, whenever a
64  * file in the WIM is modified, it is extracted to the staging directory.  If
65  * changes are commited when the WIM is unmounted, the file resources are merged
66  * in from the staging directory when writing the new WIM. */
67 static char *staging_dir_name;
68 static size_t staging_dir_name_len;
69
70 /* Flags passed to wimlib_mount(). */
71 static int mount_flags;
72
73 /* Name of the directory on which the WIM file is mounted. */
74 static const char *mount_dir;
75
76
77 static inline int get_lookup_flags()
78 {
79         if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
80                 return LOOKUP_FLAG_ADS_OK;
81         else
82                 return 0;
83 }
84
85 static inline int flags_writable(int open_flags)
86 {
87         return open_flags & (O_RDWR | O_WRONLY);
88 }
89
90 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
91                            struct wimlib_fd **fd_ret)
92 {
93         static const u16 fds_per_alloc = 8;
94         static const u16 max_fds = 0xffff;
95
96         if (lte->num_opened_fds == lte->num_allocated_fds) {
97                 struct wimlib_fd **fds;
98                 u16 num_new_fds;
99
100                 if (lte->num_allocated_fds == max_fds)
101                         return -EMFILE;
102                 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
103                 
104                 fds = CALLOC(lte->num_allocated_fds + num_new_fds,
105                              sizeof(lte->fds[0]));
106                 if (!fds)
107                         return -ENOMEM;
108                 memcpy(fds, lte->fds,
109                        lte->num_allocated_fds * sizeof(lte->fds[0]));
110                 FREE(lte->fds);
111                 lte->fds = fds;
112                 lte->num_allocated_fds += num_new_fds;
113         }
114         for (u16 i = 0; ; i++) {
115                 if (!lte->fds[i]) {
116                         struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
117                         if (!fd)
118                                 return -ENOMEM;
119                         fd->staging_fd = -1;
120                         fd->idx        = i;
121                         fd->lte        = lte;
122                         lte->fds[i]    = fd;
123                         lte->num_opened_fds++;
124                         *fd_ret        = fd;
125                         return 0;
126                 }
127         }
128 }
129
130 static int close_wimlib_fd(struct wimlib_fd *fd)
131 {
132         struct lookup_table_entry *lte = fd->lte;
133
134         wimlib_assert(lte);
135         wimlib_assert(lte->num_opened_fds);
136
137         if (lte->staging_file_name) {
138                 wimlib_assert(fd->staging_fd != -1);
139                 if (close(fd->staging_fd) != 0)
140                         return -errno;
141         }
142         if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
143                 free_lookup_table_entry(lte);
144         lte->fds[fd->idx] = NULL;
145         FREE(fd);
146         return 0;
147 }
148
149 /* Creates a new staging file and returns its file descriptor opened for
150  * writing.
151  *
152  * @name_ret: A location into which the a pointer to the newly allocated name of
153  *                      the staging file is stored.
154  * @return:  The file descriptor for the new file.  Returns -1 and sets errno on
155  *              error, for any reason possible from the creat() function.
156  */
157 static int create_staging_file(char **name_ret, int open_flags)
158 {
159         size_t name_len;
160         char *name;
161         struct stat stbuf;
162         int fd;
163         int errno_save;
164
165         name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
166         name = MALLOC(name_len + 1);
167         if (!name) {
168                 errno = ENOMEM;
169                 return -1;
170         }
171
172         do {
173
174                 memcpy(name, staging_dir_name, staging_dir_name_len);
175                 name[staging_dir_name_len] = '/';
176                 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
177                                                 WIM_HASH_SIZE);
178                 name[name_len] = '\0';
179
180
181         /* Just in case, verify that the randomly generated name doesn't name an
182          * existing file, and try again if so  */
183         } while (stat(name, &stbuf) == 0);
184
185         if (errno != ENOENT)
186                 /* other error! */
187                 return -1;
188
189         /* doesn't exist--- ok */
190
191         DEBUG("Creating staging file `%s'", name);
192
193         fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600); 
194         if (fd == -1) {
195                 errno_save = errno;
196                 FREE(name);
197                 errno = errno_save;
198         } else {
199                 *name_ret = name;
200         }
201         return fd;
202 }
203
204 /* 
205  * Extract a WIM resource to the staging directory.
206  *
207  * We need to:
208  * - Create a staging file for the WIM resource
209  * - Extract the resource to it
210  * - Create a new lte for the file resource
211  * - Transfer fds from the old lte to the new lte, but
212  *   only if they share the same hard link group as this
213  *   dentry
214  */
215 static int extract_resource_to_staging_dir(struct dentry *dentry,
216                                            struct lookup_table_entry **lte,
217                                            off_t size)
218 {
219         char *staging_file_name;
220         int ret;
221         int fd;
222         struct lookup_table_entry *old_lte, *new_lte;
223         size_t link_group_size;
224         
225         old_lte = *lte;
226         fd = create_staging_file(&staging_file_name, O_WRONLY);
227         if (fd == -1)
228                 return -errno;
229
230         if (old_lte)
231                 ret = extract_resource_to_fd(w, &old_lte->resource_entry, fd,
232                                              size);
233         else
234                 ret = 0;
235         if (ret != 0 || close(fd) != 0) {
236                 if (errno != 0)
237                         ret = -errno;
238                 else
239                         ret = -EIO;
240                 close(fd);
241                 unlink(staging_file_name);
242                 FREE(staging_file_name);
243                 return ret;
244         }
245
246         link_group_size = dentry_link_group_size(dentry);
247
248         if (old_lte) {
249                 if (link_group_size == old_lte->refcnt) {
250                         /* This hard link group is the only user of the lookup
251                          * table entry, so we can re-use it. */
252                         lookup_table_remove(w->lookup_table, old_lte);
253                         new_lte = old_lte;
254                 } else {
255                         /* Split a hard link group away from the "lookup table
256                          * entry" hard link group (i.e. we had two hard link
257                          * groups that were identical, but now we are changing
258                          * one of them) */
259
260                         /* XXX The ADS really complicate things here and not
261                          * everything is going to work correctly yet.  For
262                          * example it could be the same that a file contains two
263                          * file streams that are identical and therefore share
264                          * the same lookup table entry despite the fact that the
265                          * streams themselves are not hardlinked. */
266                         wimlib_assert(old_lte->refcnt > link_group_size);
267
268                         new_lte = new_lookup_table_entry();
269                         if (!new_lte)
270                                 return -ENOMEM;
271
272                         u16 num_transferred_fds = 0;
273                         for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
274                                 if (old_lte->fds[i] &&
275                                     old_lte->fds[i]->dentry->hard_link ==
276                                       dentry->hard_link)
277                                 {
278                                         num_transferred_fds++;
279                                 }
280                         }
281                         new_lte->fds = MALLOC(num_transferred_fds *
282                                               sizeof(new_lte->fds[0]));
283                         if (!new_lte->fds) {
284                                 free_lookup_table_entry(new_lte);
285                                 return -ENOMEM;
286                         }
287                         u16 j = 0;
288                         for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
289                                 if (old_lte->fds[i] &&
290                                     old_lte->fds[i]->dentry->hard_link ==
291                                       dentry->hard_link)
292                                 {
293                                         struct wimlib_fd *fd = old_lte->fds[i];
294                                         old_lte->fds[i] = NULL;
295                                         fd->lte = new_lte;
296                                         fd->idx = j;
297                                         new_lte->fds[j] = fd;
298                                 }
299                         }
300                         old_lte->refcnt -= link_group_size;
301                         old_lte->num_opened_fds -= j;
302                         new_lte->num_opened_fds = j;
303
304                 } 
305         }
306         new_lte->resource_entry.original_size = size;
307         new_lte->refcnt = link_group_size;
308         randomize_byte_array(new_lte->hash, WIM_HASH_SIZE);
309         new_lte->staging_file_name = staging_file_name;
310
311         lookup_table_insert(w->lookup_table, new_lte);
312         *lte = new_lte;
313         return 0;
314 }
315
316 /* 
317  * Creates a randomly named staging directory and returns its name into the
318  * static variable staging_dir_name.
319  *
320  * If the staging directory cannot be created, staging_dir_name is set to NULL.
321  * */
322 static void make_staging_dir()
323 {
324         /* XXX Give the user an option of where to stage files */
325
326         static char prefix[] = "wimlib-staging-";
327         static const size_t prefix_len = 15;
328         static const size_t suffix_len = 10;
329
330         size_t pwd_len = strlen(working_directory);
331
332         staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
333
334         staging_dir_name = MALLOC(staging_dir_name_len + 1);
335         if (!staging_dir_name) {
336                 ERROR("Out of memory");
337                 return;
338         }
339
340         memcpy(staging_dir_name, working_directory, pwd_len);
341         staging_dir_name[pwd_len] = '/';
342         memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
343         randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
344                                 suffix_len);
345         staging_dir_name[staging_dir_name_len] = '\0';
346
347         if (mkdir(staging_dir_name, 0700) != 0) {
348                 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
349                                  staging_dir_name);
350                 FREE(staging_dir_name);
351                 staging_dir_name = NULL;
352         }
353 }
354
355 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
356                 int typeflag, struct FTW *ftwbuf)
357 {
358         if (remove(fpath) == 0)
359                 return 0;
360         else
361                 return WIMLIB_ERR_DELETE_STAGING_DIR;
362 }
363
364
365 /* 
366  * Deletes the staging directory and all the files contained in it. 
367  */
368 static inline int delete_staging_dir()
369 {
370         int ret;
371         
372         ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
373         staging_dir_name = NULL;
374         return ret;
375 }
376
377 /* Name and message queue descriptors for message queues between the filesystem
378  * daemon process and the unmount process.  These are used when the filesystem
379  * is unmounted and the process running wimlib_mount() (i.e. the `imagex
380  * unmount' command) needs to communicate with the filesystem daemon running
381  * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
382  * commands */
383 static char *unmount_to_daemon_mq_name;
384 static char *daemon_to_unmount_mq_name;
385 static int unmount_to_daemon_mq;
386 static int daemon_to_unmount_mq;
387
388 /* Simple function that returns the concatenation of 4 strings. */
389 static char *strcat_dup(const char *s1, const char *s2, const char *s3, 
390                                                         const char *s4)
391 {
392         size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
393         char *p = MALLOC(len);
394         if (!p)
395                 return NULL;
396         *p = '\0';
397         strcat(p, s1);
398         strcat(p, s2);
399         strcat(p, s3);
400         strcat(p, s4);
401         return p;
402 }
403
404 /* Removes trailing forward slashes in a string. */
405 static void remove_trailing_slashes(char *s)
406 {
407         long len = strlen(s);
408         for (long i = len - 1; i >= 1; i--) {
409                 if (s[i] == '/')
410                         s[i] = '\0';
411                 else
412                         break;
413         }
414 }
415
416 /* Changes forward slashes to underscores in a string. */
417 static void s_slashes_underscores_g(char *s)
418 {
419         while (*s) {
420                 if (*s == '/')
421                         *s = '_';
422                 s++;
423         }
424 }
425
426 /* 
427  * Opens two POSIX message queue: one for sending messages from the unmount
428  * process to the daemon process, and one to go the other way.  The names of the
429  * message queues, which must be system-wide unique, are be based on the mount
430  * point.  (There of course is still a possibility of a collision if one were to
431  * unmount two identically named directories simultaneously...)
432  *
433  * @daemon specifies whether the calling process is the filesystem daemon or the
434  * unmount process.
435  */
436 static int open_message_queues(bool daemon)
437 {
438         static const char *slash = "/";
439         static const char *prefix = "wimlib-";
440         static const char *u2d_suffix = "unmount-to-daemon-mq";
441         static const char *d2u_suffix = "daemon-to-unmount-mq";
442
443         const char *mount_dir_basename = path_basename(mount_dir);
444         int flags;
445         int ret;
446
447         unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
448                                                 prefix, u2d_suffix);
449         if (!unmount_to_daemon_mq_name) {
450                 ERROR("Out of memory");
451                 return WIMLIB_ERR_NOMEM;
452         }
453         daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
454                                                 prefix, d2u_suffix);
455         if (!daemon_to_unmount_mq_name) {
456                 ERROR("Out of memory");
457                 ret = WIMLIB_ERR_NOMEM;
458                 goto err1;
459         }
460
461         remove_trailing_slashes(unmount_to_daemon_mq_name);
462         remove_trailing_slashes(daemon_to_unmount_mq_name);
463         s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
464         s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
465
466         if (daemon)
467                 flags = O_RDONLY | O_CREAT;
468         else
469                 flags = O_WRONLY | O_CREAT;
470
471         unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags, 
472                                        0700, NULL);
473
474         if (unmount_to_daemon_mq == -1) {
475                 ERROR_WITH_ERRNO("mq_open()");
476                 ret = WIMLIB_ERR_MQUEUE;
477                 goto err2;
478         }
479
480         if (daemon)
481                 flags = O_WRONLY | O_CREAT;
482         else
483                 flags = O_RDONLY | O_CREAT;
484
485         daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags, 
486                                        0700, NULL);
487
488         if (daemon_to_unmount_mq == -1) {
489                 ERROR_WITH_ERRNO("mq_open()");
490                 ret = WIMLIB_ERR_MQUEUE;
491                 goto err3;
492         }
493         return 0;
494 err3:
495         mq_close(unmount_to_daemon_mq);
496         mq_unlink(unmount_to_daemon_mq_name);
497 err2:
498         FREE(daemon_to_unmount_mq_name);
499 err1:
500         FREE(unmount_to_daemon_mq_name);
501         return ret;
502 }
503
504 static int mq_get_msgsize(mqd_t mq)
505 {
506         static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
507         FILE *fp;
508         struct mq_attr attr;
509         int msgsize;
510
511         if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
512                 msgsize = attr.mq_msgsize;
513         } else {
514                 ERROR_WITH_ERRNO("mq_getattr()");
515                 ERROR("Attempting to read %s", msgsize_max_file);
516                 fp = fopen(msgsize_max_file, "rb");
517                 if (fp) {
518                         if (fscanf(fp, "%d", &msgsize) != 1) {
519                                 ERROR("Assuming message size of 8192");
520                                 msgsize = 8192;
521                         }
522                         fclose(fp);
523                 } else {
524                         ERROR_WITH_ERRNO("Failed to open the file `%s'",
525                                          msgsize_max_file);
526                         ERROR("Assuming message size of 8192");
527                         msgsize = 8192;
528                 }
529         }
530         return msgsize;
531 }
532
533 /* Closes the message queues, which are allocated in static variables */
534 static void close_message_queues()
535 {
536         mq_close(unmount_to_daemon_mq);
537         mq_close(daemon_to_unmount_mq);
538         mq_unlink(unmount_to_daemon_mq_name);
539         mq_unlink(daemon_to_unmount_mq_name);
540 }
541
542 static int wimfs_access(const char *path, int mask)
543 {
544         /* XXX Permissions not implemented */
545         return 0;
546 }
547
548 /* Closes the staging file descriptor associated with the lookup table entry, if
549  * it is opened. */
550 static int close_lte_fds(struct lookup_table_entry *lte, void *ignore)
551 {
552         for (u16 i = 0; i < lte->num_opened_fds; i++) {
553                 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
554                         if (close(lte->fds[i]->staging_fd) != 0) {
555                                 ERROR_WITH_ERRNO("Failed close file `%s'",
556                                                  lte->staging_file_name);
557                                 return WIMLIB_ERR_WRITE;
558                         }
559                 }
560         }
561         return 0;
562 }
563
564
565 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
566  * file.  Updates the SHA1 sum in the dentry and the lookup table entry.  If
567  * there is already a lookup table entry with the same checksum, increment its
568  * reference count and destroy the lookup entry with the updated checksum. */
569 static int calculate_sha1sum_for_staging_file(struct dentry *dentry,
570                                               void *_lookup_table)
571 {
572         struct lookup_table *lookup_table =  _lookup_table;
573         u8 *hash = dentry->hash;
574         u16 i = 0;
575         while (1) {
576                 struct lookup_table_entry *lte = __lookup_resource(lookup_table, hash);
577                 if (lte && lte->staging_file_name) {
578                         struct lookup_table_entry *existing;
579                         int ret;
580
581                         DEBUG("Calculating SHA1 hash for file `%s'",
582                               dentry->file_name_utf8);
583                         ret = sha1sum(lte->staging_file_name, lte->hash);
584                         if (ret != 0)
585                                 return ret;
586
587                         lookup_table_unlink(lookup_table, lte);
588                         memcpy(hash, lte->hash, WIM_HASH_SIZE);
589                         existing = __lookup_resource(lookup_table, hash);
590                         if (existing) {
591                                 DEBUG("Merging duplicate lookup table entries for file "
592                                       "`%s'", dentry->file_name_utf8);
593                                 free_lookup_table_entry(lte);
594                                 existing->refcnt++;
595                         } else {
596                                 lookup_table_insert(lookup_table, lte);
597                         }
598                 }
599                 if (i == dentry->num_ads)
600                         break;
601                 hash = dentry->ads_entries[i].hash;
602                 i++;
603         }
604         return 0;
605 }
606
607 /* Overwrites the WIM file, with changes saved. */
608 static int rebuild_wim(WIMStruct *w, bool check_integrity)
609 {
610         int ret;
611         struct dentry *root;
612
613         root = wim_root_dentry(w);
614
615         DEBUG("Closing all staging file descriptors.");
616         /* Close all the staging file descriptors. */
617         ret = for_lookup_table_entry(w->lookup_table, close_lte_fds, NULL);
618         if (ret != 0) {
619                 ERROR("Failed to close all staging files");
620                 return ret;
621         }
622
623         DEBUG("Calculating SHA1 checksums for all new staging files.");
624         /* Calculate SHA1 checksums for all staging files, and merge unnecessary
625          * lookup table entries. */
626         ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
627                                  w->lookup_table);
628         if (ret != 0) {
629                 ERROR("Failed to calculate new SHA1 checksums");
630                 return ret;
631         }
632
633         xml_update_image_info(w, w->current_image);
634
635         ret = wimlib_overwrite(w, check_integrity);
636         if (ret != 0) {
637                 ERROR("Failed to commit changes");
638                 return ret;
639         }
640         return ret;
641 }
642
643 /* Called when the filesystem is unmounted. */
644 static void wimfs_destroy(void *p)
645 {
646
647         /* For read-write mounts, the `imagex unmount' command, which is
648          * running in a separate process and is executing the
649          * wimlib_unmount() function, will send this process a byte
650          * through a message queue that indicates whether the --commit
651          * option was specified or not. */
652
653         int msgsize;
654         struct timespec timeout;
655         struct timeval now;
656         ssize_t bytes_received;
657         int ret;
658         char commit;
659         char check_integrity;
660         char status;
661
662         ret = open_message_queues(true);
663         if (ret != 0)
664                 exit(1);
665
666         msgsize = mq_get_msgsize(unmount_to_daemon_mq);
667         char msg[msgsize];
668         msg[0] = 0;
669         msg[1] = 0;
670
671         /* Wait at most 3 seconds before giving up and discarding changes. */
672         gettimeofday(&now, NULL);
673         timeout.tv_sec = now.tv_sec + 3;
674         timeout.tv_nsec = now.tv_usec * 1000;
675         DEBUG("Waiting for message telling us whether to commit or not, and "
676               "whether to include integrity checks.");
677
678         bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg, 
679                                          msgsize, NULL, &timeout);
680         commit = msg[0];
681         check_integrity = msg[1];
682         if (bytes_received == -1) {
683                 if (errno == ETIMEDOUT) {
684                         ERROR("Timed out.");
685                 } else {
686                         ERROR_WITH_ERRNO("mq_timedreceive()");
687                 }
688                 ERROR("Not committing.");
689         } else {
690                 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
691         }
692
693         status = 0;
694         if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
695                 if (commit) {
696                         status = chdir(working_directory);
697                         if (status != 0) {
698                                 ERROR_WITH_ERRNO("chdir()");
699                                 status = WIMLIB_ERR_NOTDIR;
700                                 goto done;
701                         }
702                         status = rebuild_wim(w, (check_integrity != 0));
703                 }
704                 ret = delete_staging_dir();
705                 if (ret != 0) {
706                         ERROR_WITH_ERRNO("Failed to delete the staging "
707                                          "directory");
708                         if (status == 0)
709                                 status = ret;
710                 }
711         }
712 done:
713         ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
714         if (ret == -1)
715                 ERROR_WITH_ERRNO("Failed to send status to unmount process");
716         close_message_queues();
717 }
718
719 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
720                           struct fuse_file_info *fi)
721 {
722         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
723         dentry_to_stbuf(fd->dentry, stbuf, w->lookup_table);
724         return 0;
725 }
726
727 static int wimfs_ftruncate(const char *path, off_t size,
728                            struct fuse_file_info *fi)
729 {
730         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
731         int ret = ftruncate(fd->staging_fd, size);
732         if (ret != 0)
733                 return ret;
734         fd->lte->resource_entry.original_size = size;
735         return 0;
736 }
737
738 /*
739  * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
740  */
741 static int wimfs_getattr(const char *path, struct stat *stbuf)
742 {
743         struct dentry *dentry = get_dentry(w, path);
744         if (!dentry)
745                 return -ENOENT;
746         dentry_to_stbuf(dentry, stbuf, w->lookup_table);
747         return 0;
748 }
749
750 /* Create a hard link */
751 static int wimfs_link(const char *to, const char *from)
752 {
753         struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
754         const char *link_name;
755
756         to_dentry = get_dentry(w, to);
757         if (!to_dentry)
758                 return -ENOENT;
759         if (!dentry_is_regular_file(to_dentry))
760                 return -EPERM;
761
762         from_dentry_parent = get_parent_dentry(w, from);
763         if (!from_dentry_parent)
764                 return -ENOENT;
765         if (!dentry_is_directory(from_dentry_parent))
766                 return -ENOTDIR;
767
768         link_name = path_basename(from);
769         if (get_dentry_child_with_name(from_dentry_parent, link_name))
770                 return -EEXIST;
771         from_dentry = clone_dentry(to_dentry);
772         if (!from_dentry)
773                 return -ENOMEM;
774         if (change_dentry_name(from_dentry, link_name) != 0) {
775                 FREE(from_dentry);
776                 return -ENOMEM;
777         }
778         list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
779         link_dentry(from_dentry, from_dentry_parent);
780         return 0;
781 }
782
783 /* 
784  * Create a directory in the WIM.  
785  * @mode is currently ignored.
786  */
787 static int wimfs_mkdir(const char *path, mode_t mode)
788 {
789         struct dentry *parent;
790         struct dentry *newdir;
791         const char *basename;
792         
793         parent = get_parent_dentry(w, path);
794         if (!parent)
795                 return -ENOENT;
796
797         if (!dentry_is_directory(parent))
798                 return -ENOTDIR;
799
800         basename = path_basename(path);
801         if (get_dentry_child_with_name(parent, basename))
802                 return -EEXIST;
803
804         newdir = new_dentry(basename);
805         newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
806         link_dentry(newdir, parent);
807         return 0;
808 }
809
810
811 /* Creates a regular file. */
812 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
813 {
814         const char *stream_name;
815         if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
816              && (stream_name = path_stream_name(path))) {
817                 struct ads_entry *new_entry;
818                 struct dentry *dentry;
819
820                 dentry = get_dentry(w, path);
821                 if (!dentry || !dentry_is_regular_file(dentry))
822                         return -ENOENT;
823                 if (dentry_get_ads_entry(dentry, stream_name))
824                         return -EEXIST;
825                 new_entry = dentry_add_ads(dentry, stream_name);
826                 if (!new_entry)
827                         return -ENOENT;
828         } else {
829                 struct dentry *dentry, *parent;
830                 const char *basename;
831
832                 /* Make sure that the parent of @path exists and is a directory, and
833                  * that the dentry named by @path does not already exist.  */
834                 parent = get_parent_dentry(w, path);
835                 if (!parent)
836                         return -ENOENT;
837                 if (!dentry_is_directory(parent))
838                         return -ENOTDIR;
839                 basename = path_basename(path);
840                 if (get_dentry_child_with_name(parent, path))
841                         return -EEXIST;
842
843                 dentry = new_dentry(basename);
844                 link_dentry(dentry, parent);
845         }
846         return 0;
847 }
848
849
850 /* Open a file.  */
851 static int wimfs_open(const char *path, struct fuse_file_info *fi)
852 {
853         struct dentry *dentry;
854         struct lookup_table_entry *lte;
855         u8 *dentry_hash;
856         int ret;
857         struct wimlib_fd *fd;
858
859         ret = lookup_resource(w, path, get_lookup_flags(), &dentry, &lte,
860                               &dentry_hash);
861         if (ret != 0)
862                 return ret;
863
864         if (!lte) {
865                 /* Empty file with no lookup-table entry.  This is fine if it's
866                  * a read-only filesystem.  Otherwise we need to create a lookup
867                  * table entry so that we can keep track of the file descriptors
868                  * (this is important in case someone opens the file for
869                  * writing) */
870                 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
871                         fi->fh = 0;
872                         return 0;
873                 }
874
875                 ret = extract_resource_to_staging_dir(dentry, &lte,
876                                                       lte->resource_entry.original_size);
877                 if (ret != 0)
878                         return ret;
879         }
880
881         ret = alloc_wimlib_fd(lte, &fd);
882         if (ret != 0)
883                 return ret;
884
885         fd->dentry = dentry;
886
887         /* The file resource may be in the staging directory (read-write
888          * mounts only) or in the WIM.  If it's in the staging
889          * directory, we need to open a native file descriptor for the
890          * corresponding file.  Otherwise, we can read the file resource
891          * directly from the WIM file if we are opening it read-only,
892          * but we need to extract the resource to the staging directory
893          * if we are opening it writable. */
894         if (flags_writable(fi->flags) && !lte->staging_file_name) {
895                 ret = extract_resource_to_staging_dir(dentry, &lte,
896                                                       lte->resource_entry.original_size);
897                 if (ret != 0)
898                         return ret;
899         }
900         if (lte->staging_file_name) {
901                 fd->staging_fd = open(lte->staging_file_name, fi->flags);
902                 if (fd->staging_fd == -1) {
903                         close_wimlib_fd(fd);
904                         return -errno;
905                 }
906         }
907         fi->fh = (uint64_t)fd;
908         return 0;
909 }
910
911 /* Opens a directory. */
912 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
913 {
914         struct dentry *dentry;
915         
916         dentry = get_dentry(w, path);
917         if (!dentry || !dentry_is_directory(dentry))
918                 return -ENOTDIR;
919         fi->fh = (uint64_t)dentry;
920         return 0;
921 }
922
923
924 /*
925  * Read data from a file in the WIM or in the staging directory. 
926  */
927 static int wimfs_read(const char *path, char *buf, size_t size, 
928                       off_t offset, struct fuse_file_info *fi)
929 {
930         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
931
932         if (!fd) {
933                 /* Empty file with no lookup table entry on read-only mounted
934                  * WIM */
935                 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
936                 return 0;
937         }
938
939         if (fd->lte->staging_file_name) {
940                 /* Read from staging file */
941
942                 wimlib_assert(fd->staging_fd != -1);
943
944                 ssize_t ret;
945
946                 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
947                         return -errno;
948                 ret = read(fd->staging_fd, buf, size);
949                 if (ret == -1)
950                         return -errno;
951                 return ret;
952         } else {
953
954                 /* Read from WIM */
955
956                 struct resource_entry *res_entry;
957                 int ctype;
958                 
959                 res_entry = &fd->lte->resource_entry;
960
961                 ctype = wim_resource_compression_type(w, res_entry);
962
963                 if (offset > res_entry->original_size)
964                         return -EOVERFLOW;
965
966                 size = min(size, res_entry->original_size - offset);
967
968                 if (read_resource(w->fp, res_entry->size, 
969                                   res_entry->original_size,
970                                   res_entry->offset, ctype, size, 
971                                   offset, buf) != 0)
972                         return -EIO;
973                 return size;
974         }
975 }
976
977 /* Fills in the entries of the directory specified by @path using the
978  * FUSE-provided function @filler.  */
979 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, 
980                                 off_t offset, struct fuse_file_info *fi)
981 {
982         struct dentry *parent, *child;
983         
984         parent = (struct dentry*)fi->fh;
985         child = parent->children;
986
987         filler(buf, ".", NULL, 0);
988         filler(buf, "..", NULL, 0);
989
990         if (!child)
991                 return 0;
992
993         do {
994                 if (filler(buf, child->file_name_utf8, NULL, 0))
995                         return 0;
996                 child = child->next;
997         } while (child != parent->children);
998         return 0;
999 }
1000
1001
1002 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1003 {
1004         struct dentry *dentry = get_dentry(w, path);
1005         int ret;
1006         if (!dentry)
1007                 return -ENOENT;
1008         if (!dentry_is_symlink(dentry))
1009                 return -EINVAL;
1010
1011         ret = dentry_readlink(dentry, buf, buf_len, w);
1012         if (ret > 0)
1013                 ret = 0;
1014         return ret;
1015 }
1016
1017 /* Close a file. */
1018 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1019 {
1020         int ret;
1021         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1022
1023         if (!fd) {
1024                 /* Empty file with no lookup table entry on read-only mounted
1025                  * WIM */
1026                 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1027                 return 0;
1028         }
1029         return close_wimlib_fd(fd);
1030 }
1031
1032 /* Renames a file or directory.  See rename (3) */
1033 static int wimfs_rename(const char *from, const char *to)
1034 {
1035         struct dentry *src;
1036         struct dentry *dst;
1037         struct dentry *parent_of_dst;
1038         
1039         src = get_dentry(w, from);
1040         if (!src)
1041                 return -ENOENT;
1042
1043         dst = get_dentry(w, to);
1044
1045         if (dst) {
1046                 if (!dentry_is_directory(src)) {
1047                         /* Cannot rename non-directory to directory. */
1048                         if (dentry_is_directory(dst))
1049                                 return -EISDIR;
1050                 } else {
1051                         /* Cannot rename directory to a non-directory or a non-empty
1052                          * directory */
1053                         if (!dentry_is_directory(dst))
1054                                 return -ENOTDIR;
1055                         if (dst->children != NULL)
1056                                 return -ENOTEMPTY;
1057                 }
1058                 parent_of_dst = dst->parent;
1059                 unlink_dentry(dst);
1060                 lookup_table_decrement_refcnt(w->lookup_table, dst->hash);
1061                 free_dentry(dst);
1062         } else {
1063                 parent_of_dst = get_parent_dentry(w, to);
1064                 if (!parent_of_dst)
1065                         return -ENOENT;
1066         }
1067
1068         unlink_dentry(src);
1069         change_dentry_name(src, path_basename(to));
1070         link_dentry(src, parent_of_dst);
1071         /*calculate_dentry_full_path(src);*/
1072         return 0;
1073 }
1074
1075 /* Remove a directory */
1076 static int wimfs_rmdir(const char *path)
1077 {
1078         struct dentry *dentry;
1079         
1080         dentry = get_dentry(w, path);
1081         if (!dentry)
1082                 return -ENOENT;
1083
1084         if (!dentry_is_empty_directory(dentry))
1085                 return -ENOTEMPTY;
1086
1087         unlink_dentry(dentry);
1088         free_dentry(dentry);
1089         return 0;
1090 }
1091
1092 static int wimfs_symlink(const char *to, const char *from)
1093 {
1094         struct dentry *dentry_parent, *dentry;
1095         const char *link_name;
1096         
1097         dentry_parent = get_parent_dentry(w, from);
1098         if (!dentry_parent)
1099                 return -ENOENT;
1100         if (!dentry_is_directory(dentry_parent))
1101                 return -ENOTDIR;
1102
1103         link_name = path_basename(from);
1104
1105         if (get_dentry_child_with_name(dentry_parent, link_name))
1106                 return -EEXIST;
1107         dentry = new_dentry(link_name);
1108         if (!dentry)
1109                 return -ENOMEM;
1110
1111         dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1112         dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1113
1114         if (dentry_set_symlink(dentry, to, w->lookup_table) != 0)
1115                 goto out_free_dentry;
1116
1117         link_dentry(dentry, dentry_parent);
1118         return 0;
1119 out_free_dentry:
1120         free_dentry(dentry);
1121         return -ENOMEM;
1122 }
1123
1124
1125 /* Reduce the size of a file */
1126 static int wimfs_truncate(const char *path, off_t size)
1127 {
1128         struct dentry *dentry;
1129         struct lookup_table_entry *lte;
1130         int ret;
1131         u8 *dentry_hash;
1132         
1133         ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1134                               &lte, &dentry_hash);
1135
1136         if (ret != 0)
1137                 return ret;
1138
1139         if (!lte) /* Already a zero-length file */
1140                 return 0;
1141
1142         if (lte->staging_file_name) {
1143                 ret = truncate(lte->staging_file_name, size);
1144                 if (ret != 0)
1145                         return -errno;
1146                 lte->resource_entry.original_size = size;
1147         } else {
1148                 /* File in WIM.  Extract it to the staging directory, but only
1149                  * the first @size bytes of it. */
1150                 ret = extract_resource_to_staging_dir(dentry, &lte, size);
1151         }
1152         dentry_update_all_timestamps(dentry);
1153         return ret;
1154 }
1155
1156 /* Remove a regular file */
1157 static int wimfs_unlink(const char *path)
1158 {
1159         struct dentry *dentry;
1160         struct lookup_table_entry *lte;
1161         int ret;
1162         u8 *dentry_hash;
1163         
1164         ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1165                               &lte, &dentry_hash);
1166
1167         if (ret != 0)
1168                 return ret;
1169
1170         if (lte && lte->staging_file_name)
1171                 if (unlink(lte->staging_file_name) != 0)
1172                         return -errno;
1173
1174         if (dentry_hash == dentry->hash) {
1175                 /* We are removing the full dentry including all alternate data
1176                  * streams. */
1177                 const u8 *hash = dentry->hash;
1178                 u16 i = 0;
1179                 while (1) {
1180                         lookup_table_decrement_refcnt(w->lookup_table, hash);
1181                         if (i == dentry->num_ads)
1182                                 break;
1183                         hash = dentry->ads_entries[i].hash;
1184                         i++;
1185                 }
1186
1187                 unlink_dentry(dentry);
1188                 free_dentry(dentry);
1189         } else {
1190                 /* We are removing an alternate data stream. */
1191                 struct ads_entry *cur_entry = dentry->ads_entries;
1192                 while (cur_entry->hash != dentry_hash)
1193                         cur_entry++;
1194                 lookup_table_decrement_refcnt(w->lookup_table, cur_entry->hash);
1195                 
1196                 dentry_remove_ads(dentry, cur_entry);
1197         }
1198         /* Beware: The lookup table entry(s) may still be referenced by users
1199          * that have opened the corresponding streams.  They are freed later in
1200          * wimfs_release() when the last file user has closed the stream. */
1201         return 0;
1202 }
1203
1204 /* Change the timestamp on a file dentry. 
1205  *
1206  * There is no distinction between a file and its alternate data streams here.  */
1207 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1208 {
1209         struct dentry *dentry = get_dentry(w, path);
1210         if (!dentry)
1211                 return -ENOENT;
1212         time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ? 
1213                                 time(NULL) : tv[0].tv_sec;
1214         dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1215         time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?  
1216                                 time(NULL) : tv[1].tv_sec;
1217         dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1218         return 0;
1219 }
1220
1221 /* Writes to a file in the WIM filesystem. 
1222  * It may be an alternate data stream, but here we don't even notice because we
1223  * just get a lookup table entry. */
1224 static int wimfs_write(const char *path, const char *buf, size_t size, 
1225                        off_t offset, struct fuse_file_info *fi)
1226 {
1227         struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1228         int ret;
1229
1230         wimlib_assert(fd->lte);
1231         wimlib_assert(fd->lte->staging_file_name);
1232         wimlib_assert(fd->staging_fd != -1);
1233
1234         /* Write the data. */
1235         ret = write(fd->staging_fd, buf, size);
1236         if (ret == -1)
1237                 return -errno;
1238
1239         /* The file has been modified, so all its timestamps must be
1240          * updated. */
1241         dentry_update_all_timestamps(fd->dentry);
1242
1243         return ret;
1244 }
1245
1246
1247 static struct fuse_operations wimfs_operations = {
1248         .access    = wimfs_access,
1249         .destroy   = wimfs_destroy,
1250         .fgetattr  = wimfs_fgetattr,
1251         .ftruncate = wimfs_ftruncate,
1252         .getattr   = wimfs_getattr,
1253         .link      = wimfs_link,
1254         .mkdir     = wimfs_mkdir,
1255         .mknod     = wimfs_mknod,
1256         .open      = wimfs_open,
1257         .opendir   = wimfs_opendir,
1258         .read      = wimfs_read,
1259         .readdir   = wimfs_readdir,
1260         .readlink  = wimfs_readlink,
1261         .release   = wimfs_release,
1262         .rename    = wimfs_rename,
1263         .rmdir     = wimfs_rmdir,
1264         .symlink   = wimfs_symlink,
1265         .truncate  = wimfs_truncate,
1266         .unlink    = wimfs_unlink,
1267         .utimens   = wimfs_utimens,
1268         .write     = wimfs_write,
1269 };
1270
1271
1272 /* Mounts a WIM file. */
1273 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir, 
1274                            int flags)
1275 {
1276         int argc = 0;
1277         char *argv[6];
1278         int ret;
1279         char *p;
1280
1281         DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1282                         wim, image, dir, flags);
1283
1284         if (!dir)
1285                 return WIMLIB_ERR_INVALID_PARAM;
1286
1287         ret = wimlib_select_image(wim, image);
1288
1289         if (ret != 0)
1290                 return ret;
1291
1292         if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1293                 wim_get_current_image_metadata(wim)->modified = true;
1294
1295         if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1296                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1297                        WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1298                 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1299
1300         mount_dir = dir;
1301         working_directory = getcwd(NULL, 0);
1302         if (!working_directory) {
1303                 ERROR_WITH_ERRNO("Could not determine current directory");
1304                 return WIMLIB_ERR_NOTDIR;
1305         }
1306
1307         p = STRDUP(dir);
1308         if (!p)
1309                 return WIMLIB_ERR_NOMEM;
1310
1311         argv[argc++] = "mount";
1312         argv[argc++] = p;
1313         argv[argc++] = "-s"; /* disable multi-threaded operation */
1314
1315         if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1316                 argv[argc++] = "-d";
1317         }
1318         if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1319                 argv[argc++] = "-o";
1320                 argv[argc++] = "ro";
1321         } else {
1322                 make_staging_dir();
1323                 if (!staging_dir_name) {
1324                         FREE(p);
1325                         return WIMLIB_ERR_MKDIR;
1326                 }
1327         }
1328
1329 #ifdef ENABLE_DEBUG
1330         {
1331                 int i;
1332                 DEBUG("FUSE command line (argc = %d): ", argc);
1333                 for (i = 0; i < argc; i++) {
1334                         fputs(argv[i], stdout);
1335                         putchar(' ');
1336                 }
1337                 putchar('\n');
1338                 fflush(stdout);
1339         }
1340 #endif
1341
1342         /* Set static variables. */
1343         w = wim;
1344         mount_flags = flags;
1345
1346         ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1347
1348         return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1349 }
1350
1351
1352 /* 
1353  * Unmounts the WIM file that was previously mounted on @dir by using
1354  * wimlib_mount().
1355  */
1356 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1357 {
1358         pid_t pid;
1359         int status;
1360         int ret;
1361         char msg[2];
1362         struct timeval now;
1363         struct timespec timeout;
1364         int msgsize;
1365         int errno_save;
1366
1367         /* Execute `fusermount -u', which is installed setuid root, to unmount
1368          * the WIM.
1369          *
1370          * FUSE does not yet implement synchronous unmounts.  This means that
1371          * fusermount -u will return before the filesystem daemon returns from
1372          * wimfs_destroy().  This is partly what we want, because we need to
1373          * send a message from this process to the filesystem daemon telling
1374          * whether --commit was specified or not.  However, after that, the
1375          * unmount process must wait for the filesystem daemon to finish writing
1376          * the WIM file. 
1377          */
1378
1379         mount_dir = dir;
1380         pid = fork();
1381         if (pid == -1) {
1382                 ERROR_WITH_ERRNO("Failed to fork()");
1383                 return WIMLIB_ERR_FORK;
1384         }
1385         if (pid == 0) {
1386                 execlp("fusermount", "fusermount", "-u", dir, NULL);
1387                 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1388                 return WIMLIB_ERR_FUSERMOUNT;
1389         }
1390
1391         ret = waitpid(pid, &status, 0);
1392         if (ret == -1) {
1393                 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1394                                  "terminate");
1395                 return WIMLIB_ERR_FUSERMOUNT;
1396         }
1397
1398         if (status != 0) {
1399                 ERROR("fusermount exited with status %d", status);
1400                 return WIMLIB_ERR_FUSERMOUNT;
1401         }
1402
1403         /* Open message queues between the unmount process and the
1404          * filesystem daemon. */
1405         ret = open_message_queues(false);
1406         if (ret != 0)
1407                 return ret;
1408
1409         /* Send a message to the filesystem saying whether to commit or
1410          * not. */
1411         msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1412         msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1413
1414         DEBUG("Sending message: %s, %s", 
1415                         (msg[0] == 0) ? "don't commit" : "commit",
1416                         (msg[1] == 0) ? "don't check"  : "check");
1417         ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1418         if (ret == -1) {
1419                 ERROR("Failed to notify filesystem daemon whether we want to "
1420                       "commit changes or not");
1421                 close_message_queues();
1422                 return WIMLIB_ERR_MQUEUE;
1423         }
1424
1425         /* Wait for a message from the filesytem daemon indicating whether  the
1426          * filesystem was unmounted successfully (0) or an error occurred (1).
1427          * This may take a long time if a big WIM file needs to be rewritten. */
1428
1429         /* Wait at most 600??? seconds before giving up and returning false.
1430          * Either it's a really big WIM file, or (more likely) the
1431          * filesystem daemon has crashed or failed for some reason.
1432          *
1433          * XXX come up with some method to determine if the filesystem
1434          * daemon has really crashed or not. */
1435
1436         gettimeofday(&now, NULL);
1437         timeout.tv_sec = now.tv_sec + 600;
1438         timeout.tv_nsec = now.tv_usec * 1000;
1439
1440         msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1441         char mailbox[msgsize];
1442
1443         mailbox[0] = 0;
1444         DEBUG("Waiting for message telling us whether the unmount was "
1445                         "successful or not.");
1446         ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1447                               NULL, &timeout);
1448         errno_save = errno;
1449         close_message_queues();
1450         if (ret == -1) {
1451                 if (errno_save == ETIMEDOUT) {
1452                         ERROR("Timed out- probably the filesystem daemon "
1453                               "crashed and the WIM was not written "
1454                               "successfully.");
1455                         return WIMLIB_ERR_TIMEOUT;
1456                 } else {
1457                         ERROR("mq_receive(): %s", strerror(errno_save));
1458                         return WIMLIB_ERR_MQUEUE;
1459                 }
1460
1461         }
1462         DEBUG("Received message: %s",
1463               (mailbox[0] == 0) ?  "Unmount OK" : "Unmount Failed");
1464         if (mailbox[0] != 0)
1465                 ERROR("Unmount failed");
1466         return mailbox[0];
1467 }
1468
1469 #else /* WITH_FUSE */
1470
1471
1472 static inline int mount_unsupported_error()
1473 {
1474         ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1475               "for mounting WIMs.");
1476         return WIMLIB_ERR_UNSUPPORTED;
1477 }
1478
1479 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1480 {
1481         return mount_unsupported_error();
1482 }
1483
1484 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir, 
1485                            int flags)
1486 {
1487         return mount_unsupported_error();
1488 }
1489
1490 #endif /* WITH_FUSE */