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 newdir->hard_link = next_link_group_id++;
1043 link_dentry(newdir, parent);
1048 /* Creates a regular file. */
1049 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1051 const char *stream_name;
1052 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1053 && (stream_name = path_stream_name(path))) {
1054 /* Make an alternate data stream */
1055 struct ads_entry *new_entry;
1056 struct dentry *dentry;
1058 dentry = get_dentry(w, path);
1059 if (!dentry || !dentry_is_regular_file(dentry))
1061 if (dentry_get_ads_entry(dentry, stream_name))
1063 new_entry = dentry_add_ads(dentry, stream_name);
1067 struct dentry *dentry, *parent;
1068 const char *basename;
1070 /* Make a normal file (not an alternate data stream) */
1072 /* Make sure that the parent of @path exists and is a directory, and
1073 * that the dentry named by @path does not already exist. */
1074 parent = get_parent_dentry(w, path);
1077 if (!dentry_is_directory(parent))
1080 basename = path_basename(path);
1081 if (get_dentry_child_with_name(parent, path))
1084 dentry = new_dentry(basename);
1087 dentry->resolved = true;
1088 dentry->hard_link = next_link_group_id++;
1089 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
1090 INIT_LIST_HEAD(&dentry->lte_group_list.list);
1091 link_dentry(dentry, parent);
1098 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1100 struct dentry *dentry;
1101 struct lookup_table_entry *lte;
1104 struct wimlib_fd *fd;
1105 unsigned stream_idx;
1107 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1113 /* Empty file with no lookup-table entry. This is fine if it's
1114 * a read-only filesystem. Otherwise we need to create a lookup
1115 * table entry so that we can keep track of the file descriptors
1116 * (this is important in case someone opens the file for
1118 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1123 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1129 ret = alloc_wimlib_fd(lte, &fd);
1133 fd->dentry = dentry;
1135 /* The file resource may be in the staging directory (read-write
1136 * mounts only) or in the WIM. If it's in the staging
1137 * directory, we need to open a native file descriptor for the
1138 * corresponding file. Otherwise, we can read the file resource
1139 * directly from the WIM file if we are opening it read-only,
1140 * but we need to extract the resource to the staging directory
1141 * if we are opening it writable. */
1142 if (flags_writable(fi->flags) && !lte->staging_file_name) {
1143 ret = extract_resource_to_staging_dir(dentry, stream_idx, <e,
1144 lte->resource_entry.original_size);
1148 if (lte->staging_file_name) {
1149 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1150 if (fd->staging_fd == -1) {
1151 close_wimlib_fd(fd);
1155 fi->fh = (uint64_t)fd;
1159 /* Opens a directory. */
1160 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1162 struct dentry *dentry;
1164 dentry = get_dentry(w, path);
1167 if (!dentry_is_directory(dentry))
1169 dentry->num_times_opened++;
1170 fi->fh = (uint64_t)dentry;
1176 * Read data from a file in the WIM or in the staging directory.
1178 static int wimfs_read(const char *path, char *buf, size_t size,
1179 off_t offset, struct fuse_file_info *fi)
1181 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1184 /* Empty file with no lookup table entry on read-only mounted
1186 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1190 wimlib_assert(fd->lte);
1192 if (fd->lte->staging_file_name) {
1193 /* Read from staging file */
1195 wimlib_assert(fd->staging_fd != -1);
1198 DEBUG("Seek to offset %zu", offset);
1200 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1202 ret = read(fd->staging_fd, buf, size);
1209 struct resource_entry *res_entry;
1212 res_entry = &fd->lte->resource_entry;
1214 ctype = wim_resource_compression_type(w, res_entry);
1216 if (offset > res_entry->original_size)
1219 size = min(size, res_entry->original_size - offset);
1221 if (read_resource(w->fp, res_entry->size,
1222 res_entry->original_size,
1223 res_entry->offset, ctype, size,
1230 /* Fills in the entries of the directory specified by @path using the
1231 * FUSE-provided function @filler. */
1232 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1233 off_t offset, struct fuse_file_info *fi)
1235 struct dentry *parent, *child;
1237 parent = (struct dentry*)fi->fh;
1238 wimlib_assert(parent);
1239 child = parent->children;
1241 filler(buf, ".", NULL, 0);
1242 filler(buf, "..", NULL, 0);
1248 if (filler(buf, child->file_name_utf8, NULL, 0))
1250 child = child->next;
1251 } while (child != parent->children);
1256 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1258 struct dentry *dentry = get_dentry(w, path);
1262 if (!dentry_is_symlink(dentry))
1265 ret = dentry_readlink(dentry, buf, buf_len, w);
1272 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1275 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1278 /* Empty file with no lookup table entry on read-only mounted
1280 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1284 if (flags_writable(fi->flags) && fd->dentry) {
1285 u64 now = get_timestamp();
1286 fd->dentry->last_access_time = now;
1287 fd->dentry->last_write_time = now;
1290 return close_wimlib_fd(fd);
1293 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1295 struct dentry *dentry = (struct dentry *)fi->fh;
1297 wimlib_assert(dentry);
1298 wimlib_assert(dentry->num_times_opened);
1299 if (--dentry->num_times_opened == 0)
1300 free_dentry(dentry);
1304 static int wimfs_removexattr(const char *path, const char *name)
1310 /* Renames a file or directory. See rename (3) */
1311 static int wimfs_rename(const char *from, const char *to)
1315 struct dentry *parent_of_dst;
1316 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1317 u16 file_name_utf16_len, file_name_utf8_len;
1320 /* This rename() implementation currently only supports actual files
1321 * (not alternate data streams) */
1323 src = get_dentry(w, from);
1327 dst = get_dentry(w, to);
1330 ret = get_names(&file_name_utf16, &file_name_utf8,
1331 &file_name_utf16_len, &file_name_utf8_len,
1337 /* Destination file exists */
1339 if (src == dst) /* Same file */
1342 if (!dentry_is_directory(src)) {
1343 /* Cannot rename non-directory to directory. */
1344 if (dentry_is_directory(dst))
1347 /* Cannot rename directory to a non-directory or a non-empty
1349 if (!dentry_is_directory(dst))
1351 if (dst->children != NULL)
1354 parent_of_dst = dst->parent;
1355 remove_dentry(dst, w->lookup_table);
1357 /* Destination does not exist */
1358 parent_of_dst = get_parent_dentry(w, to);
1362 if (!dentry_is_directory(parent_of_dst))
1366 FREE(src->file_name);
1367 FREE(src->file_name_utf8);
1368 src->file_name = file_name_utf16;
1369 src->file_name_utf8 = file_name_utf8;
1370 src->file_name_len = file_name_utf16_len;
1371 src->file_name_utf8_len = file_name_utf8_len;
1374 link_dentry(src, parent_of_dst);
1378 /* Remove a directory */
1379 static int wimfs_rmdir(const char *path)
1381 struct dentry *dentry;
1383 dentry = get_dentry(w, path);
1387 if (!dentry_is_empty_directory(dentry))
1390 unlink_dentry(dentry);
1391 if (dentry->num_times_opened == 0)
1392 free_dentry(dentry);
1396 static int wimfs_setxattr(const char *path, const char *name,
1397 const char *value, size_t size, int flags)
1403 static int wimfs_symlink(const char *to, const char *from)
1405 struct dentry *dentry_parent, *dentry;
1406 const char *link_name;
1407 struct lookup_table_entry *lte;
1409 dentry_parent = get_parent_dentry(w, from);
1412 if (!dentry_is_directory(dentry_parent))
1415 link_name = path_basename(from);
1417 if (get_dentry_child_with_name(dentry_parent, link_name))
1419 dentry = new_dentry(link_name);
1423 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1424 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1426 if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
1427 goto out_free_dentry;
1431 dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1432 list_add(&dentry->ads_entries[1].lte_group_list.list,
1433 <e->lte_group_list);
1434 dentry->ads_entries[1].lte = lte;
1435 dentry->resolved = true;
1437 link_dentry(dentry, dentry_parent);
1440 free_dentry(dentry);
1445 /* Reduce the size of a file */
1446 static int wimfs_truncate(const char *path, off_t size)
1448 struct dentry *dentry;
1449 struct lookup_table_entry *lte;
1451 unsigned stream_idx;
1453 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1459 if (!lte) /* Already a zero-length file */
1462 if (lte->staging_file_name) {
1463 ret = truncate(lte->staging_file_name, size);
1466 lte->resource_entry.original_size = size;
1468 /* File in WIM. Extract it to the staging directory, but only
1469 * the first @size bytes of it. */
1470 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1473 dentry_update_all_timestamps(dentry);
1477 /* Remove a regular file */
1478 static int wimfs_unlink(const char *path)
1480 struct dentry *dentry;
1481 struct lookup_table_entry *lte;
1484 unsigned stream_idx;
1486 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1492 if (stream_idx == 0) {
1493 /* We are removing the full dentry including all alternate data
1495 remove_dentry(dentry, w->lookup_table);
1497 /* We are removing an alternate data stream. */
1498 struct ads_entry *ads_entry;
1500 ads_entry = &dentry->ads_entries[stream_idx - 1];
1501 lte = lte_decrement_refcnt(lte, w->lookup_table);
1503 list_del(&ads_entry->lte_group_list.list);
1504 dentry_remove_ads(dentry, ads_entry);
1506 /* Beware: The lookup table entry(s) may still be referenced by users
1507 * that have opened the corresponding streams. They are freed later in
1508 * wimfs_release() when the last file user has closed the stream. */
1512 /* Change the timestamp on a file dentry.
1514 * There is no distinction between a file and its alternate data streams here. */
1515 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1517 struct dentry *dentry = get_dentry(w, path);
1520 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1521 time(NULL) : tv[0].tv_sec;
1522 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1523 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1524 time(NULL) : tv[1].tv_sec;
1525 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1529 /* Writes to a file in the WIM filesystem.
1530 * It may be an alternate data stream, but here we don't even notice because we
1531 * just get a lookup table entry. */
1532 static int wimfs_write(const char *path, const char *buf, size_t size,
1533 off_t offset, struct fuse_file_info *fi)
1535 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1539 wimlib_assert(fd->lte);
1540 wimlib_assert(fd->lte->staging_file_name);
1541 wimlib_assert(fd->staging_fd != -1);
1543 /* Seek to the requested position */
1544 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1547 /* Write the data. */
1548 ret = write(fd->staging_fd, buf, size);
1555 static struct fuse_operations wimfs_operations = {
1556 .access = wimfs_access,
1557 .destroy = wimfs_destroy,
1558 .fallocate = wimfs_fallocate,
1559 .fgetattr = wimfs_fgetattr,
1560 .ftruncate = wimfs_ftruncate,
1561 .getattr = wimfs_getattr,
1562 .getxattr = wimfs_getxattr,
1564 .listxattr = wimfs_listxattr,
1565 .mkdir = wimfs_mkdir,
1566 .mknod = wimfs_mknod,
1568 .opendir = wimfs_opendir,
1570 .readdir = wimfs_readdir,
1571 .readlink = wimfs_readlink,
1572 .release = wimfs_release,
1573 .releasedir = wimfs_releasedir,
1574 .removexattr = wimfs_removexattr,
1575 .rename = wimfs_rename,
1576 .rmdir = wimfs_rmdir,
1577 .setxattr = wimfs_setxattr,
1578 .symlink = wimfs_symlink,
1579 .truncate = wimfs_truncate,
1580 .unlink = wimfs_unlink,
1581 .utimens = wimfs_utimens,
1582 .write = wimfs_write,
1586 /* Mounts a WIM file. */
1587 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1595 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1596 wim, image, dir, flags);
1599 return WIMLIB_ERR_INVALID_PARAM;
1601 ret = wimlib_select_image(wim, image);
1606 DEBUG("Selected image %d", image);
1608 next_link_group_id = assign_link_groups(wim->image_metadata[image - 1].lgt);
1610 /* Resolve all the lookup table entries of the dentry tree */
1611 for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
1614 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1615 wim_get_current_image_metadata(wim)->modified = true;
1617 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1618 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1619 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1620 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1623 working_directory = getcwd(NULL, 0);
1624 if (!working_directory) {
1625 ERROR_WITH_ERRNO("Could not determine current directory");
1626 return WIMLIB_ERR_NOTDIR;
1631 return WIMLIB_ERR_NOMEM;
1633 argv[argc++] = "mount";
1635 argv[argc++] = "-s"; /* disable multi-threaded operation */
1637 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1638 argv[argc++] = "-d";
1641 * We provide the use_ino option because we are going to assign inode
1642 * numbers oursides. We've already numbered the hard link groups with
1643 * unique numbers with the assign_link_groups() function, and the static
1644 * variable next_link_group_id is set to the next available link group
1645 * ID that we will assign to new dentries.
1647 char optstring[256] = "use_ino";
1648 argv[argc++] = "-o";
1649 argv[argc++] = optstring;
1650 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1651 /* Read-write mount. Make the staging directory */
1653 if (!staging_dir_name) {
1655 return WIMLIB_ERR_MKDIR;
1658 /* Read-only mount */
1659 strcat(optstring, ",ro");
1666 DEBUG("FUSE command line (argc = %d): ", argc);
1667 for (i = 0; i < argc; i++) {
1668 fputs(argv[i], stdout);
1676 /* Set static variables. */
1678 mount_flags = flags;
1680 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1682 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1687 * Unmounts the WIM file that was previously mounted on @dir by using
1690 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1697 struct timespec timeout;
1701 /* Execute `fusermount -u', which is installed setuid root, to unmount
1704 * FUSE does not yet implement synchronous unmounts. This means that
1705 * fusermount -u will return before the filesystem daemon returns from
1706 * wimfs_destroy(). This is partly what we want, because we need to
1707 * send a message from this process to the filesystem daemon telling
1708 * whether --commit was specified or not. However, after that, the
1709 * unmount process must wait for the filesystem daemon to finish writing
1716 ERROR_WITH_ERRNO("Failed to fork()");
1717 return WIMLIB_ERR_FORK;
1720 execlp("fusermount", "fusermount", "-u", dir, NULL);
1721 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1722 return WIMLIB_ERR_FUSERMOUNT;
1725 ret = waitpid(pid, &status, 0);
1727 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1729 return WIMLIB_ERR_FUSERMOUNT;
1733 ERROR("fusermount exited with status %d", status);
1734 return WIMLIB_ERR_FUSERMOUNT;
1737 /* Open message queues between the unmount process and the
1738 * filesystem daemon. */
1739 ret = open_message_queues(false);
1743 /* Send a message to the filesystem saying whether to commit or
1745 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1746 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1748 DEBUG("Sending message: %s, %s",
1749 (msg[0] == 0) ? "don't commit" : "commit",
1750 (msg[1] == 0) ? "don't check" : "check");
1751 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1753 ERROR("Failed to notify filesystem daemon whether we want to "
1754 "commit changes or not");
1755 close_message_queues();
1756 return WIMLIB_ERR_MQUEUE;
1759 /* Wait for a message from the filesytem daemon indicating whether the
1760 * filesystem was unmounted successfully (0) or an error occurred (1).
1761 * This may take a long time if a big WIM file needs to be rewritten. */
1763 /* Wait at most 600??? seconds before giving up and returning false.
1764 * Either it's a really big WIM file, or (more likely) the
1765 * filesystem daemon has crashed or failed for some reason.
1767 * XXX come up with some method to determine if the filesystem
1768 * daemon has really crashed or not. */
1770 gettimeofday(&now, NULL);
1771 timeout.tv_sec = now.tv_sec + 600;
1772 timeout.tv_nsec = now.tv_usec * 1000;
1774 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1775 char mailbox[msgsize];
1778 DEBUG("Waiting for message telling us whether the unmount was "
1779 "successful or not.");
1780 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1783 close_message_queues();
1785 if (errno_save == ETIMEDOUT) {
1786 ERROR("Timed out- probably the filesystem daemon "
1787 "crashed and the WIM was not written "
1789 return WIMLIB_ERR_TIMEOUT;
1791 ERROR("mq_receive(): %s", strerror(errno_save));
1792 return WIMLIB_ERR_MQUEUE;
1796 DEBUG("Received message: %s",
1797 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1798 if (mailbox[0] != 0)
1799 ERROR("Unmount failed");
1803 #else /* WITH_FUSE */
1806 static inline int mount_unsupported_error()
1808 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1809 "for mounting WIMs.");
1810 return WIMLIB_ERR_UNSUPPORTED;
1813 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1815 return mount_unsupported_error();
1818 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1821 return mount_unsupported_error();
1824 #endif /* WITH_FUSE */