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