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