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.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
29 #include "wimlib_internal.h"
33 #include "lookup_table.h"
36 #include "timestamp.h"
40 #define FUSE_USE_VERSION 26
52 struct lookup_table_entry *lte;
53 struct dentry *dentry;
56 /* The WIMStruct for the mounted WIM. */
59 /* Working directory when `imagex mount' is run. */
60 static const char *working_directory;
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;
70 /* Flags passed to wimlib_mount(). */
71 static int mount_flags;
73 /* Name of the directory on which the WIM file is mounted. */
74 static const char *mount_dir;
76 /* Next hard link group ID to be assigned. These are also used as the inode
78 static u64 next_link_group_id;
80 /* List of lookup table entries in the staging directory */
81 static LIST_HEAD(staging_list);
83 static inline int get_lookup_flags()
85 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
86 return LOOKUP_FLAG_ADS_OK;
91 /* Returns nonzero if write permission is requested on the file open flags */
92 static inline int flags_writable(int open_flags)
94 return open_flags & (O_RDWR | O_WRONLY);
98 * Allocate a file descriptor for a lookup table entry
100 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
101 struct wimlib_fd **fd_ret)
103 static const u16 fds_per_alloc = 8;
104 static const u16 max_fds = 0xffff;
106 if (lte->num_opened_fds == lte->num_allocated_fds) {
107 struct wimlib_fd **fds;
110 if (lte->num_allocated_fds == max_fds)
112 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
114 fds = REALLOC(lte->fds, (lte->num_allocated_fds + num_new_fds) *
115 sizeof(lte->fds[0]));
118 memset(&fds[lte->num_allocated_fds], 0,
119 num_new_fds * sizeof(fds[0]));
121 lte->num_allocated_fds += num_new_fds;
123 for (u16 i = 0; ; i++) {
125 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
132 lte->num_opened_fds++;
139 static int close_wimlib_fd(struct wimlib_fd *fd)
141 struct lookup_table_entry *lte = fd->lte;
144 wimlib_assert(lte->num_opened_fds);
146 if (lte->staging_file_name) {
147 wimlib_assert(fd->staging_fd != -1);
148 if (close(fd->staging_fd) != 0)
151 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
152 if (lte->staging_file_name)
153 unlink(lte->staging_file_name);
154 free_lookup_table_entry(lte);
156 wimlib_assert(lte->fds[fd->idx] == fd);
157 lte->fds[fd->idx] = NULL;
162 /* Remove a dentry and all its alternate file streams */
163 static void remove_dentry(struct dentry *dentry,
164 struct lookup_table *lookup_table)
166 wimlib_assert(dentry);
167 wimlib_assert(dentry->resolved);
169 struct lookup_table_entry *lte = dentry->lte;
172 lte = lte_decrement_refcnt(lte, lookup_table);
173 if (lte && lte->num_opened_fds)
174 for (u16 i = 0; i < lte->num_allocated_fds; i++)
175 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
176 lte->fds[i]->dentry = NULL;
177 if (i == dentry->num_ads)
179 lte = dentry->ads_entries[i].lte;
183 unlink_dentry(dentry);
187 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
188 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf)
190 struct lookup_table_entry *lte;
192 if (dentry_is_symlink(dentry))
193 stbuf->st_mode = S_IFLNK | 0777;
194 else if (dentry_is_directory(dentry))
195 stbuf->st_mode = S_IFDIR | 0755;
197 stbuf->st_mode = S_IFREG | 0644;
199 stbuf->st_ino = (ino_t)dentry->hard_link;
201 stbuf->st_nlink = dentry_link_group_size(dentry);
202 stbuf->st_uid = getuid();
203 stbuf->st_gid = getgid();
205 /* Use the size of the unnamed (default) file stream. */
206 if ((lte = dentry_lte(dentry))) {
207 if (lte->staging_file_name) {
208 struct stat native_stat;
209 if (stat(lte->staging_file_name, &native_stat) != 0)
211 stbuf->st_size = native_stat.st_size;
213 stbuf->st_size = lte->resource_entry.original_size;
219 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
220 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
221 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
222 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
226 /* Creates a new staging file and returns its file descriptor opened for
229 * @name_ret: A location into which the a pointer to the newly allocated name of
230 * the staging file is stored.
231 * @return: The file descriptor for the new file. Returns -1 and sets errno on
232 * error, for any reason possible from the creat() function.
234 static int create_staging_file(char **name_ret, int open_flags)
242 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
243 name = MALLOC(name_len + 1);
251 memcpy(name, staging_dir_name, staging_dir_name_len);
252 name[staging_dir_name_len] = '/';
253 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
255 name[name_len] = '\0';
258 /* Just in case, verify that the randomly generated name doesn't name an
259 * existing file, and try again if so */
260 } while (stat(name, &stbuf) == 0);
266 /* doesn't exist--- ok */
268 DEBUG("Creating staging file `%s'", name);
270 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
282 * Removes open file descriptors from a lookup table entry @old_lte where the
283 * file descriptors have opened the corresponding file resource in the context
284 * of the hard link group @link_group; these file descriptors are extracted and
285 * placed in a new lookup table entry, which is returned.
287 static struct lookup_table_entry *
288 lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group)
291 u16 num_transferred_fds;
292 struct lookup_table_entry *new_lte;
294 new_lte = new_lookup_table_entry();
298 num_transferred_fds = 0;
299 for (u16 i = 0; i < old_lte->num_allocated_fds; i++)
300 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
301 old_lte->fds[i]->dentry->hard_link == link_group)
302 num_transferred_fds++;
303 DEBUG("Transferring %u file descriptors",
304 num_transferred_fds);
305 new_lte->fds = MALLOC(num_transferred_fds * sizeof(new_lte->fds[0]));
310 for (u16 i = 0, j = 0; ; i++) {
311 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
312 old_lte->fds[i]->dentry->hard_link == link_group) {
313 struct wimlib_fd *fd = old_lte->fds[i];
314 old_lte->fds[i] = NULL;
317 new_lte->fds[j] = fd;
318 if (++j == num_transferred_fds)
322 DEBUG("old_lte: %u fds open; new_lte: %u fds open",
323 old_lte->num_opened_fds, new_lte->num_opened_fds);
324 old_lte->num_opened_fds -= num_transferred_fds;
325 new_lte->num_opened_fds = num_transferred_fds;
326 new_lte->num_allocated_fds = num_transferred_fds;
331 * Transfers an alternate data stream entry to a new lookup table entry
333 static void lte_transfer_ads_entry(struct lookup_table_entry *new_lte,
334 struct ads_entry *ads_entry)
336 list_del(&ads_entry->lte_group_list.list);
337 list_add(&ads_entry->lte_group_list.list, &new_lte->lte_group_list);
338 ads_entry->lte = new_lte;
342 * Transfers a dentry to a new lookup table entry
344 static void lte_transfer_dentry(struct lookup_table_entry *new_lte,
345 struct dentry *dentry)
347 wimlib_assert(dentry->lte_group_list.list.next);
348 wimlib_assert(new_lte->lte_group_list.next);
349 list_del(&dentry->lte_group_list.list);
350 list_add(&dentry->lte_group_list.list, &new_lte->lte_group_list);
351 dentry->lte = new_lte;
354 static void lte_transfer_stream_entries(struct lookup_table_entry *new_lte,
355 struct dentry *dentry,
358 INIT_LIST_HEAD(&new_lte->lte_group_list);
359 if (stream_idx == 0) {
360 struct list_head *pos = &dentry->link_group_list;
363 d = container_of(pos, struct dentry, link_group_list);
364 wimlib_assert(d->hard_link == dentry->hard_link);
365 lte_transfer_dentry(new_lte, d);
367 } while (pos != &dentry->link_group_list);
369 struct ads_entry *ads_entry;
370 wimlib_assert(stream_idx <= dentry->num_ads);
371 ads_entry = &dentry->ads_entries[stream_idx - 1];
372 lte_transfer_ads_entry(new_lte, ads_entry);
377 * Extract a WIM resource to the staging directory.
379 * @dentry, @stream_idx: The stream on whose behalf we are modifying the lookup
380 * table entry (these may be more streams than this that reference the lookup
383 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
384 * extract, or NULL if there was no lookup table entry present for the stream
386 * @size: Number of bytes of the stream we want to extract (this supports the
387 * wimfs_truncate() function).
389 static int extract_resource_to_staging_dir(struct dentry *dentry,
391 struct lookup_table_entry **lte,
394 char *staging_file_name;
397 struct lookup_table_entry *old_lte, *new_lte;
398 size_t link_group_size;
402 * - Create a staging file for the WIM resource
403 * - Extract the resource to it
404 * - Create a new lte for the file resource
405 * - Transfer fds from the old lte to the new lte, but only if they share the
406 * same hard link group as this dentry. If there is no old lte, then this
407 * step does not need to be done
408 * - Transfer stream entries from the old lte's list to the new lte's list. If
409 * there is no old lte, we instead transfer entries for the hard link group.
411 * Note: *lte is permitted to be NULL, in which case there is no old
412 * lookup table entry.
415 DEBUG("Extracting resource `%s' to staging directory", dentry->full_path_utf8);
418 fd = create_staging_file(&staging_file_name, O_WRONLY);
423 ret = extract_resource_to_fd(w, &old_lte->resource_entry, fd,
427 if (ret != 0 || close(fd) != 0) {
433 goto out_delete_staging_file;
436 link_group_size = dentry_link_group_size(dentry);
439 if (link_group_size == old_lte->refcnt) {
440 /* This hard link group is the only user of the lookup
441 * table entry, so we can re-use it. */
442 DEBUG("Re-using lookup table entry");
443 lookup_table_unlink(w->lookup_table, old_lte);
446 DEBUG("Splitting lookup table entry "
447 "(link_group_size = %u, lte refcnt = %u)",
448 link_group_size, old_lte->refcnt);
449 /* Split a hard link group away from the "lookup table
450 * entry" hard link group (i.e. we had two hard link
451 * groups that were identical, but now we are changing
455 * The ADS really complicate things here and not
456 * everything is going to work correctly yet. For
457 * example it could be the same that a file contains two
458 * file streams that are identical and therefore share
459 * the same lookup table entry despite the fact that the
460 * streams themselves are not hardlinked.
462 wimlib_assert(old_lte->refcnt > link_group_size);
464 new_lte = lte_extract_fds(old_lte, dentry->hard_link);
467 goto out_delete_staging_file;
470 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
471 old_lte->refcnt -= link_group_size;
474 /* No old_lte was supplied, so the resource had no lookup table
475 * entry before (it must be an empty resource) */
476 new_lte = new_lookup_table_entry();
479 goto out_delete_staging_file;
481 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
483 new_lte->resource_entry.original_size = size;
484 new_lte->refcnt = link_group_size;
485 randomize_byte_array(new_lte->hash, WIM_HASH_SIZE);
486 new_lte->staging_file_name = staging_file_name;
488 lookup_table_insert(w->lookup_table, new_lte);
489 list_add(&new_lte->staging_list, &staging_list);
492 out_delete_staging_file:
493 unlink(staging_file_name);
494 FREE(staging_file_name);
499 * Creates a randomly named staging directory and returns its name into the
500 * static variable staging_dir_name.
502 * If the staging directory cannot be created, staging_dir_name is set to NULL.
504 static void make_staging_dir()
506 /* XXX Give the user an option of where to stage files */
508 static char prefix[] = "wimlib-staging-";
509 static const size_t prefix_len = 15;
510 static const size_t suffix_len = 10;
512 size_t pwd_len = strlen(working_directory);
514 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
516 staging_dir_name = MALLOC(staging_dir_name_len + 1);
517 if (!staging_dir_name) {
518 ERROR("Out of memory");
522 memcpy(staging_dir_name, working_directory, pwd_len);
523 staging_dir_name[pwd_len] = '/';
524 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
525 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
527 staging_dir_name[staging_dir_name_len] = '\0';
529 if (mkdir(staging_dir_name, 0700) != 0) {
530 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
532 FREE(staging_dir_name);
533 staging_dir_name = NULL;
537 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
538 int typeflag, struct FTW *ftwbuf)
540 if (remove(fpath) == 0)
543 return WIMLIB_ERR_DELETE_STAGING_DIR;
548 * Deletes the staging directory and all the files contained in it.
550 static inline int delete_staging_dir()
554 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
555 staging_dir_name = NULL;
559 /* Name and message queue descriptors for message queues between the filesystem
560 * daemon process and the unmount process. These are used when the filesystem
561 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
562 * unmount' command) needs to communicate with the filesystem daemon running
563 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
565 static char *unmount_to_daemon_mq_name;
566 static char *daemon_to_unmount_mq_name;
567 static int unmount_to_daemon_mq;
568 static int daemon_to_unmount_mq;
570 /* Simple function that returns the concatenation of 4 strings. */
571 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
574 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
575 char *p = MALLOC(len);
586 /* Removes trailing forward slashes in a string. */
587 static void remove_trailing_slashes(char *s)
589 long len = strlen(s);
590 for (long i = len - 1; i >= 1; i--) {
598 /* Changes forward slashes to underscores in a string. */
599 static void s_slashes_underscores_g(char *s)
609 * Opens two POSIX message queue: one for sending messages from the unmount
610 * process to the daemon process, and one to go the other way. The names of the
611 * message queues, which must be system-wide unique, are be based on the mount
612 * point. (There of course is still a possibility of a collision if one were to
613 * unmount two identically named directories simultaneously...)
615 * @daemon specifies whether the calling process is the filesystem daemon or the
618 static int open_message_queues(bool daemon)
620 static const char *slash = "/";
621 static const char *prefix = "wimlib-";
622 static const char *u2d_suffix = "unmount-to-daemon-mq";
623 static const char *d2u_suffix = "daemon-to-unmount-mq";
625 const char *mount_dir_basename = path_basename(mount_dir);
629 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
631 if (!unmount_to_daemon_mq_name) {
632 ERROR("Out of memory");
633 return WIMLIB_ERR_NOMEM;
635 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
637 if (!daemon_to_unmount_mq_name) {
638 ERROR("Out of memory");
639 ret = WIMLIB_ERR_NOMEM;
643 remove_trailing_slashes(unmount_to_daemon_mq_name);
644 remove_trailing_slashes(daemon_to_unmount_mq_name);
645 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
646 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
649 flags = O_RDONLY | O_CREAT;
651 flags = O_WRONLY | O_CREAT;
653 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
656 if (unmount_to_daemon_mq == -1) {
657 ERROR_WITH_ERRNO("mq_open()");
658 ret = WIMLIB_ERR_MQUEUE;
663 flags = O_WRONLY | O_CREAT;
665 flags = O_RDONLY | O_CREAT;
667 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
670 if (daemon_to_unmount_mq == -1) {
671 ERROR_WITH_ERRNO("mq_open()");
672 ret = WIMLIB_ERR_MQUEUE;
677 mq_close(unmount_to_daemon_mq);
678 mq_unlink(unmount_to_daemon_mq_name);
680 FREE(daemon_to_unmount_mq_name);
682 FREE(unmount_to_daemon_mq_name);
686 static int mq_get_msgsize(mqd_t mq)
688 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
693 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
694 msgsize = attr.mq_msgsize;
696 ERROR_WITH_ERRNO("mq_getattr()");
697 ERROR("Attempting to read %s", msgsize_max_file);
698 fp = fopen(msgsize_max_file, "rb");
700 if (fscanf(fp, "%d", &msgsize) != 1) {
701 ERROR("Assuming message size of 8192");
706 ERROR_WITH_ERRNO("Failed to open the file `%s'",
708 ERROR("Assuming message size of 8192");
715 /* Closes the message queues, which are allocated in static variables */
716 static void close_message_queues()
718 mq_close(unmount_to_daemon_mq);
719 mq_close(daemon_to_unmount_mq);
720 mq_unlink(unmount_to_daemon_mq_name);
721 mq_unlink(daemon_to_unmount_mq_name);
724 static int wimfs_access(const char *path, int mask)
726 /* XXX Permissions not implemented */
730 /* Closes the staging file descriptor associated with the lookup table entry, if
732 static int close_lte_fds(struct lookup_table_entry *lte)
734 for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
735 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
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;
747 static void lte_list_change_lte_ptr(struct lookup_table_entry *lte,
748 struct lookup_table_entry *newptr)
750 struct list_head *pos;
751 struct stream_list_head *head;
752 list_for_each(pos, <e->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);
758 ads_entry->lte = newptr;
760 wimlib_assert(head->type == STREAM_TYPE_NORMAL);
762 struct dentry *dentry;
763 dentry = container_of(head, struct dentry, lte_group_list);
765 dentry->lte = newptr;
771 static int calculate_sha1sum_of_staging_file(struct lookup_table_entry *lte,
772 struct lookup_table *table)
774 struct lookup_table_entry *duplicate_lte;
776 u8 hash[WIM_HASH_SIZE];
778 ret = sha1sum(lte->staging_file_name, hash);
782 lookup_table_unlink(table, lte);
783 memcpy(lte->hash, hash, WIM_HASH_SIZE);
785 duplicate_lte = __lookup_resource(table, hash);
788 /* Merge duplicate lookup table entries */
790 lte_list_change_lte_ptr(lte, duplicate_lte);
791 duplicate_lte->refcnt += lte->refcnt;
792 list_splice(&duplicate_lte->lte_group_list,
793 <e->lte_group_list);
795 free_lookup_table_entry(lte);
797 lookup_table_insert(table, lte);
803 /* Overwrites the WIM file, with changes saved. */
804 static int rebuild_wim(WIMStruct *w, bool check_integrity)
807 struct lookup_table_entry *lte, *tmp;
809 /* Close all the staging file descriptors. */
810 DEBUG("Closing all staging file descriptors.");
811 list_for_each_entry(lte, &staging_list, staging_list) {
812 ret = close_lte_fds(lte);
817 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
818 * lookup table entries. */
819 DEBUG("Calculating SHA1 checksums for all new staging files.");
820 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
821 ret = calculate_sha1sum_of_staging_file(lte, w->lookup_table);
828 xml_update_image_info(w, w->current_image);
830 ret = wimlib_overwrite(w, check_integrity);
832 ERROR("Failed to commit changes");
838 /* Called when the filesystem is unmounted. */
839 static void wimfs_destroy(void *p)
841 /* For read-write mounts, the `imagex unmount' command, which is
842 * running in a separate process and is executing the
843 * wimlib_unmount() function, will send this process a byte
844 * through a message queue that indicates whether the --commit
845 * option was specified or not. */
848 struct timespec timeout;
850 ssize_t bytes_received;
853 char check_integrity;
856 ret = open_message_queues(true);
860 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
865 /* Wait at most 3 seconds before giving up and discarding changes. */
866 gettimeofday(&now, NULL);
867 timeout.tv_sec = now.tv_sec + 3;
868 timeout.tv_nsec = now.tv_usec * 1000;
869 DEBUG("Waiting for message telling us whether to commit or not, and "
870 "whether to include integrity checks.");
872 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
873 msgsize, NULL, &timeout);
875 check_integrity = msg[1];
876 if (bytes_received == -1) {
877 if (errno == ETIMEDOUT) {
880 ERROR_WITH_ERRNO("mq_timedreceive()");
882 ERROR("Not committing.");
884 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
888 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
890 status = chdir(working_directory);
892 ERROR_WITH_ERRNO("chdir()");
893 status = WIMLIB_ERR_NOTDIR;
896 status = rebuild_wim(w, (check_integrity != 0));
898 ret = delete_staging_dir();
900 ERROR_WITH_ERRNO("Failed to delete the staging "
906 DEBUG("Read-only mount");
909 DEBUG("Sending status %u", status);
910 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
912 ERROR_WITH_ERRNO("Failed to send status to unmount process");
913 close_message_queues();
916 static int wimfs_fallocate(const char *path, int mode,
917 off_t offset, off_t len, struct fuse_file_info *fi)
919 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
920 wimlib_assert(fd->staging_fd != -1);
921 return fallocate(fd->staging_fd, mode, offset, len);
924 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
925 struct fuse_file_info *fi)
927 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
928 return dentry_to_stbuf(fd->dentry, stbuf);
931 static int wimfs_ftruncate(const char *path, off_t size,
932 struct fuse_file_info *fi)
934 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
935 int ret = ftruncate(fd->staging_fd, size);
938 fd->lte->resource_entry.original_size = size;
943 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
945 static int wimfs_getattr(const char *path, struct stat *stbuf)
947 struct dentry *dentry = get_dentry(w, path);
950 return dentry_to_stbuf(dentry, stbuf);
953 static int wimfs_getxattr(const char *path, const char *name, char *value,
960 /* Create a hard link */
961 static int wimfs_link(const char *to, const char *from)
963 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
964 const char *link_name;
966 to_dentry = get_dentry(w, to);
969 if (!dentry_is_regular_file(to_dentry))
972 from_dentry_parent = get_parent_dentry(w, from);
973 if (!from_dentry_parent)
975 if (!dentry_is_directory(from_dentry_parent))
978 link_name = path_basename(from);
979 if (get_dentry_child_with_name(from_dentry_parent, link_name))
981 from_dentry = clone_dentry(to_dentry);
984 if (change_dentry_name(from_dentry, link_name) != 0) {
989 /* Add the new dentry to the dentry list for the link group */
990 list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
992 /* Increment reference counts for the unnamed file stream and all
993 * alternate data streams. */
994 if (from_dentry->lte) {
995 list_add(&from_dentry->lte_group_list.list,
996 &to_dentry->lte_group_list.list);
997 from_dentry->lte->refcnt++;
999 for (u16 i = 0; i < from_dentry->num_ads; i++) {
1000 struct ads_entry *ads_entry = &from_dentry->ads_entries[i];
1002 ads_entry->lte->refcnt++;
1005 /* The ADS entries are owned by another dentry. */
1006 from_dentry->ads_entries_status = ADS_ENTRIES_USER;
1008 link_dentry(from_dentry, from_dentry_parent);
1012 static int wimfs_listxattr(const char *path, char *list, size_t size)
1019 * Create a directory in the WIM.
1020 * @mode is currently ignored.
1022 static int wimfs_mkdir(const char *path, mode_t mode)
1024 struct dentry *parent;
1025 struct dentry *newdir;
1026 const char *basename;
1028 parent = get_parent_dentry(w, path);
1032 if (!dentry_is_directory(parent))
1035 basename = path_basename(path);
1036 if (get_dentry_child_with_name(parent, basename))
1039 newdir = new_dentry(basename);
1040 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1041 newdir->resolved = true;
1042 link_dentry(newdir, parent);
1047 /* Creates a regular file. */
1048 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1050 const char *stream_name;
1051 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1052 && (stream_name = path_stream_name(path))) {
1053 /* Make an alternate data stream */
1054 struct ads_entry *new_entry;
1055 struct dentry *dentry;
1057 dentry = get_dentry(w, path);
1058 if (!dentry || !dentry_is_regular_file(dentry))
1060 if (dentry_get_ads_entry(dentry, stream_name))
1062 new_entry = dentry_add_ads(dentry, stream_name);
1066 struct dentry *dentry, *parent;
1067 const char *basename;
1069 /* Make a normal file (not an alternate data stream) */
1071 /* Make sure that the parent of @path exists and is a directory, and
1072 * that the dentry named by @path does not already exist. */
1073 parent = get_parent_dentry(w, path);
1076 if (!dentry_is_directory(parent))
1079 basename = path_basename(path);
1080 if (get_dentry_child_with_name(parent, path))
1083 dentry = new_dentry(basename);
1086 dentry->resolved = true;
1087 dentry->hard_link = next_link_group_id++;
1088 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
1089 INIT_LIST_HEAD(&dentry->lte_group_list.list);
1090 link_dentry(dentry, parent);
1097 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1099 struct dentry *dentry;
1100 struct lookup_table_entry *lte;
1103 struct wimlib_fd *fd;
1104 unsigned stream_idx;
1106 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1112 /* Empty file with no lookup-table entry. This is fine if it's
1113 * a read-only filesystem. Otherwise we need to create a lookup
1114 * table entry so that we can keep track of the file descriptors
1115 * (this is important in case someone opens the file for
1117 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1122 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1128 ret = alloc_wimlib_fd(lte, &fd);
1132 fd->dentry = dentry;
1134 /* The file resource may be in the staging directory (read-write
1135 * mounts only) or in the WIM. If it's in the staging
1136 * directory, we need to open a native file descriptor for the
1137 * corresponding file. Otherwise, we can read the file resource
1138 * directly from the WIM file if we are opening it read-only,
1139 * but we need to extract the resource to the staging directory
1140 * if we are opening it writable. */
1141 if (flags_writable(fi->flags) && !lte->staging_file_name) {
1142 ret = extract_resource_to_staging_dir(dentry, stream_idx, <e,
1143 lte->resource_entry.original_size);
1147 if (lte->staging_file_name) {
1148 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1149 if (fd->staging_fd == -1) {
1150 close_wimlib_fd(fd);
1154 fi->fh = (uint64_t)fd;
1158 /* Opens a directory. */
1159 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1161 struct dentry *dentry;
1163 dentry = get_dentry(w, path);
1166 if (!dentry_is_directory(dentry))
1168 dentry->num_times_opened++;
1169 fi->fh = (uint64_t)dentry;
1175 * Read data from a file in the WIM or in the staging directory.
1177 static int wimfs_read(const char *path, char *buf, size_t size,
1178 off_t offset, struct fuse_file_info *fi)
1180 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1183 /* Empty file with no lookup table entry on read-only mounted
1185 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1189 wimlib_assert(fd->lte);
1191 if (fd->lte->staging_file_name) {
1192 /* Read from staging file */
1194 wimlib_assert(fd->staging_fd != -1);
1197 DEBUG("Seek to offset %zu", offset);
1199 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1201 ret = read(fd->staging_fd, buf, size);
1208 struct resource_entry *res_entry;
1211 res_entry = &fd->lte->resource_entry;
1213 ctype = wim_resource_compression_type(w, res_entry);
1215 if (offset > res_entry->original_size)
1218 size = min(size, res_entry->original_size - offset);
1220 if (read_resource(w->fp, res_entry->size,
1221 res_entry->original_size,
1222 res_entry->offset, ctype, size,
1229 /* Fills in the entries of the directory specified by @path using the
1230 * FUSE-provided function @filler. */
1231 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1232 off_t offset, struct fuse_file_info *fi)
1234 struct dentry *parent, *child;
1236 parent = (struct dentry*)fi->fh;
1237 wimlib_assert(parent);
1238 child = parent->children;
1240 filler(buf, ".", NULL, 0);
1241 filler(buf, "..", NULL, 0);
1247 if (filler(buf, child->file_name_utf8, NULL, 0))
1249 child = child->next;
1250 } while (child != parent->children);
1255 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1257 struct dentry *dentry = get_dentry(w, path);
1261 if (!dentry_is_symlink(dentry))
1264 ret = dentry_readlink(dentry, buf, buf_len, w);
1271 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1274 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1277 /* Empty file with no lookup table entry on read-only mounted
1279 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1283 if (flags_writable(fi->flags) && fd->dentry) {
1284 u64 now = get_timestamp();
1285 fd->dentry->last_access_time = now;
1286 fd->dentry->last_write_time = now;
1289 return close_wimlib_fd(fd);
1292 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1294 struct dentry *dentry = (struct dentry *)fi->fh;
1296 wimlib_assert(dentry);
1297 wimlib_assert(dentry->num_times_opened);
1298 if (--dentry->num_times_opened == 0)
1299 free_dentry(dentry);
1303 static int wimfs_removexattr(const char *path, const char *name)
1309 /* Renames a file or directory. See rename (3) */
1310 static int wimfs_rename(const char *from, const char *to)
1314 struct dentry *parent_of_dst;
1315 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1316 u16 file_name_utf16_len, file_name_utf8_len;
1319 /* This rename() implementation currently only supports actual files
1320 * (not alternate data streams) */
1322 src = get_dentry(w, from);
1326 dst = get_dentry(w, to);
1329 ret = get_names(&file_name_utf16, &file_name_utf8,
1330 &file_name_utf16_len, &file_name_utf8_len,
1336 /* Destination file exists */
1338 if (src == dst) /* Same file */
1341 if (!dentry_is_directory(src)) {
1342 /* Cannot rename non-directory to directory. */
1343 if (dentry_is_directory(dst))
1346 /* Cannot rename directory to a non-directory or a non-empty
1348 if (!dentry_is_directory(dst))
1350 if (dst->children != NULL)
1353 parent_of_dst = dst->parent;
1354 remove_dentry(dst, w->lookup_table);
1356 /* Destination does not exist */
1357 parent_of_dst = get_parent_dentry(w, to);
1361 if (!dentry_is_directory(parent_of_dst))
1365 FREE(src->file_name);
1366 FREE(src->file_name_utf8);
1367 src->file_name = file_name_utf16;
1368 src->file_name_utf8 = file_name_utf8;
1369 src->file_name_len = file_name_utf16_len;
1370 src->file_name_utf8_len = file_name_utf8_len;
1373 link_dentry(src, parent_of_dst);
1377 /* Remove a directory */
1378 static int wimfs_rmdir(const char *path)
1380 struct dentry *dentry;
1382 dentry = get_dentry(w, path);
1386 if (!dentry_is_empty_directory(dentry))
1389 unlink_dentry(dentry);
1390 if (dentry->num_times_opened == 0)
1391 free_dentry(dentry);
1395 static int wimfs_setxattr(const char *path, const char *name,
1396 const char *value, size_t size, int flags)
1402 static int wimfs_symlink(const char *to, const char *from)
1404 struct dentry *dentry_parent, *dentry;
1405 const char *link_name;
1406 struct lookup_table_entry *lte;
1408 dentry_parent = get_parent_dentry(w, from);
1411 if (!dentry_is_directory(dentry_parent))
1414 link_name = path_basename(from);
1416 if (get_dentry_child_with_name(dentry_parent, link_name))
1418 dentry = new_dentry(link_name);
1422 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1423 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1425 if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
1426 goto out_free_dentry;
1428 dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1429 list_add(&dentry->ads_entries[1].lte_group_list.list,
1430 <e->lte_group_list);
1431 dentry->ads_entries[1].lte = lte;
1432 dentry->resolved = true;
1434 link_dentry(dentry, dentry_parent);
1437 free_dentry(dentry);
1442 /* Reduce the size of a file */
1443 static int wimfs_truncate(const char *path, off_t size)
1445 struct dentry *dentry;
1446 struct lookup_table_entry *lte;
1448 unsigned stream_idx;
1450 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1456 if (!lte) /* Already a zero-length file */
1459 if (lte->staging_file_name) {
1460 ret = truncate(lte->staging_file_name, size);
1463 lte->resource_entry.original_size = size;
1465 /* File in WIM. Extract it to the staging directory, but only
1466 * the first @size bytes of it. */
1467 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1470 dentry_update_all_timestamps(dentry);
1474 /* Remove a regular file */
1475 static int wimfs_unlink(const char *path)
1477 struct dentry *dentry;
1478 struct lookup_table_entry *lte;
1481 unsigned stream_idx;
1483 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1489 if (stream_idx == 0) {
1490 /* We are removing the full dentry including all alternate data
1492 remove_dentry(dentry, w->lookup_table);
1494 /* We are removing an alternate data stream. */
1495 struct ads_entry *ads_entry;
1497 ads_entry = &dentry->ads_entries[stream_idx - 1];
1498 lte = lte_decrement_refcnt(lte, w->lookup_table);
1500 list_del(&ads_entry->lte_group_list.list);
1501 dentry_remove_ads(dentry, ads_entry);
1503 /* Beware: The lookup table entry(s) may still be referenced by users
1504 * that have opened the corresponding streams. They are freed later in
1505 * wimfs_release() when the last file user has closed the stream. */
1509 /* Change the timestamp on a file dentry.
1511 * There is no distinction between a file and its alternate data streams here. */
1512 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1514 struct dentry *dentry = get_dentry(w, path);
1517 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1518 time(NULL) : tv[0].tv_sec;
1519 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1520 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1521 time(NULL) : tv[1].tv_sec;
1522 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1526 /* Writes to a file in the WIM filesystem.
1527 * It may be an alternate data stream, but here we don't even notice because we
1528 * just get a lookup table entry. */
1529 static int wimfs_write(const char *path, const char *buf, size_t size,
1530 off_t offset, struct fuse_file_info *fi)
1532 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1536 wimlib_assert(fd->lte);
1537 wimlib_assert(fd->lte->staging_file_name);
1538 wimlib_assert(fd->staging_fd != -1);
1540 /* Seek to the requested position */
1541 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1544 /* Write the data. */
1545 ret = write(fd->staging_fd, buf, size);
1552 static struct fuse_operations wimfs_operations = {
1553 .access = wimfs_access,
1554 .destroy = wimfs_destroy,
1555 .fallocate = wimfs_fallocate,
1556 .fgetattr = wimfs_fgetattr,
1557 .ftruncate = wimfs_ftruncate,
1558 .getattr = wimfs_getattr,
1559 .getxattr = wimfs_getxattr,
1561 .listxattr = wimfs_listxattr,
1562 .mkdir = wimfs_mkdir,
1563 .mknod = wimfs_mknod,
1565 .opendir = wimfs_opendir,
1567 .readdir = wimfs_readdir,
1568 .readlink = wimfs_readlink,
1569 .release = wimfs_release,
1570 .releasedir = wimfs_releasedir,
1571 .removexattr = wimfs_removexattr,
1572 .rename = wimfs_rename,
1573 .rmdir = wimfs_rmdir,
1574 .setxattr = wimfs_setxattr,
1575 .symlink = wimfs_symlink,
1576 .truncate = wimfs_truncate,
1577 .unlink = wimfs_unlink,
1578 .utimens = wimfs_utimens,
1579 .write = wimfs_write,
1583 /* Mounts a WIM file. */
1584 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1592 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1593 wim, image, dir, flags);
1596 return WIMLIB_ERR_INVALID_PARAM;
1598 ret = wimlib_select_image(wim, image);
1603 DEBUG("Selected image %d", image);
1605 next_link_group_id = assign_link_groups(wim->image_metadata[image - 1].lgt);
1607 /* Resolve all the lookup table entries of the dentry tree */
1608 for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
1611 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1612 wim_get_current_image_metadata(wim)->modified = true;
1614 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1615 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1616 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1617 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1620 working_directory = getcwd(NULL, 0);
1621 if (!working_directory) {
1622 ERROR_WITH_ERRNO("Could not determine current directory");
1623 return WIMLIB_ERR_NOTDIR;
1628 return WIMLIB_ERR_NOMEM;
1630 argv[argc++] = "mount";
1632 argv[argc++] = "-s"; /* disable multi-threaded operation */
1634 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1635 argv[argc++] = "-d";
1638 * We provide the use_ino option because we are going to assign inode
1639 * numbers oursides. We've already numbered the hard link groups with
1640 * unique numbers with the assign_link_groups() function, and the static
1641 * variable next_link_group_id is set to the next available link group
1642 * ID that we will assign to new dentries.
1644 char optstring[256] = "use_ino";
1645 argv[argc++] = "-o";
1646 argv[argc++] = optstring;
1647 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1648 /* Read-write mount. Make the staging directory */
1650 if (!staging_dir_name) {
1652 return WIMLIB_ERR_MKDIR;
1655 /* Read-only mount */
1656 strcat(optstring, ",ro");
1663 DEBUG("FUSE command line (argc = %d): ", argc);
1664 for (i = 0; i < argc; i++) {
1665 fputs(argv[i], stdout);
1673 /* Set static variables. */
1675 mount_flags = flags;
1677 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1679 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1684 * Unmounts the WIM file that was previously mounted on @dir by using
1687 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1694 struct timespec timeout;
1698 /* Execute `fusermount -u', which is installed setuid root, to unmount
1701 * FUSE does not yet implement synchronous unmounts. This means that
1702 * fusermount -u will return before the filesystem daemon returns from
1703 * wimfs_destroy(). This is partly what we want, because we need to
1704 * send a message from this process to the filesystem daemon telling
1705 * whether --commit was specified or not. However, after that, the
1706 * unmount process must wait for the filesystem daemon to finish writing
1713 ERROR_WITH_ERRNO("Failed to fork()");
1714 return WIMLIB_ERR_FORK;
1717 execlp("fusermount", "fusermount", "-u", dir, NULL);
1718 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1719 return WIMLIB_ERR_FUSERMOUNT;
1722 ret = waitpid(pid, &status, 0);
1724 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1726 return WIMLIB_ERR_FUSERMOUNT;
1730 ERROR("fusermount exited with status %d", status);
1731 return WIMLIB_ERR_FUSERMOUNT;
1734 /* Open message queues between the unmount process and the
1735 * filesystem daemon. */
1736 ret = open_message_queues(false);
1740 /* Send a message to the filesystem saying whether to commit or
1742 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1743 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1745 DEBUG("Sending message: %s, %s",
1746 (msg[0] == 0) ? "don't commit" : "commit",
1747 (msg[1] == 0) ? "don't check" : "check");
1748 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1750 ERROR("Failed to notify filesystem daemon whether we want to "
1751 "commit changes or not");
1752 close_message_queues();
1753 return WIMLIB_ERR_MQUEUE;
1756 /* Wait for a message from the filesytem daemon indicating whether the
1757 * filesystem was unmounted successfully (0) or an error occurred (1).
1758 * This may take a long time if a big WIM file needs to be rewritten. */
1760 /* Wait at most 600??? seconds before giving up and returning false.
1761 * Either it's a really big WIM file, or (more likely) the
1762 * filesystem daemon has crashed or failed for some reason.
1764 * XXX come up with some method to determine if the filesystem
1765 * daemon has really crashed or not. */
1767 gettimeofday(&now, NULL);
1768 timeout.tv_sec = now.tv_sec + 600;
1769 timeout.tv_nsec = now.tv_usec * 1000;
1771 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1772 char mailbox[msgsize];
1775 DEBUG("Waiting for message telling us whether the unmount was "
1776 "successful or not.");
1777 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1780 close_message_queues();
1782 if (errno_save == ETIMEDOUT) {
1783 ERROR("Timed out- probably the filesystem daemon "
1784 "crashed and the WIM was not written "
1786 return WIMLIB_ERR_TIMEOUT;
1788 ERROR("mq_receive(): %s", strerror(errno_save));
1789 return WIMLIB_ERR_MQUEUE;
1793 DEBUG("Received message: %s",
1794 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1795 if (mailbox[0] != 0)
1796 ERROR("Unmount failed");
1800 #else /* WITH_FUSE */
1803 static inline int mount_unsupported_error()
1805 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1806 "for mounting WIMs.");
1807 return WIMLIB_ERR_UNSUPPORTED;
1810 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1812 return mount_unsupported_error();
1815 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1818 return mount_unsupported_error();
1821 #endif /* WITH_FUSE */