]> wimlib.net Git - wimlib/blob - src/ntfs.c
fa3607c0ad415656994160c577521733cfe7c86b
[wimlib] / src / ntfs.c
1 /*
2  * Copyright (C) 2012 Eric Biggers
3  *
4  * This file is part of wimlib, a library for working with WIM files.
5  *
6  * wimlib is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.
10  *
11  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with wimlib; if not, see http://www.gnu.org/licenses/.
18  */
19
20 #include "config.h"
21 #include "wimlib_internal.h"
22
23 #ifdef WITH_NTFS_3G
24 #include "dentry.h"
25 #include <ntfs-3g/volume.h>
26 #include <ntfs-3g/security.h>
27 #include <unistd.h>
28
29 struct ntfs_apply_args {
30         struct SECURITY_API *scapi;
31         ntfs_volume *vol;
32         const struct wim_security_data *sd;
33         int flags;
34         struct ntfs_inode *parent;
35 };
36
37 static int ntfs_apply_dentry(struct dentry *dentry, void *arg)
38 {
39         struct ntfs_apply_args *args       = arg;
40         struct SECURITY_API *scapi         = args->scapi;
41         ntfs_volume *vol                   = args->vol;
42         const struct wim_security_data *sd = args->sd;
43         int flags                          = args->flags;
44         int ret = 0;
45         ntfs_inode *dir_ni, *ni;
46
47         if (dentry_is_root(dentry))
48                 return 0;
49
50         if (flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
51                 wimlib_assert(dentry->full_path_utf8);
52                 puts(dentry->full_path_utf8);
53         }
54
55         char *p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
56         do {
57                 p--;
58         } while (*p != '/');
59         char orig = *p;
60         *p = '\0';
61         const char *dir_name = dentry->full_path_utf8;
62
63         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
64         if (!dir_ni) {
65                 ret = WIMLIB_ERR_NTFS_3G;
66                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
67                                  dir_name);
68                 goto out;
69         }
70
71         ret = 0;
72         if (dentry_is_regular_file(dentry)) {
73                 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
74                                  dentry->file_name_len, S_IFREG);
75         } else if (dentry_is_directory(dentry)) {
76                 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
77                                  dentry->file_name_len, S_IFDIR);
78         } else {
79                 goto out;
80         }
81         if (!ni) {
82                 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
83                                  dentry->full_path_utf8);
84                 ret = WIMLIB_ERR_NTFS_3G;
85                 goto out;
86         }
87 out:
88         *p = orig;
89         return ret;
90 }
91
92 static int do_ntfs_apply(WIMStruct *w, const char *device, int flags)
93 {
94         struct SECURITY_API *scapi;
95         
96         scapi = ntfs_initialize_file_security(device, 0);
97         if (!scapi) {
98                 ERROR_WITH_ERRNO("Failed to initialize NTFS file security API "
99                                  "on NTFS volume `%s'", device);
100         }
101         struct ntfs_apply_args args = {
102                 .scapi = scapi,
103                 .vol = scapi->security.vol,
104                 .sd = wim_security_data(w),
105                 .flags = flags,
106         };
107         return for_dentry_in_tree(wim_root_dentry(w), ntfs_apply_dentry,
108                                   &args);
109 }
110
111 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
112                                                 const char *device, int flags)
113 {
114         int ret;
115
116         if (!device)
117                 return WIMLIB_ERR_INVALID_PARAM;
118         if (image == WIM_ALL_IMAGES) {
119                 ERROR("Can only apply a single image when applying "
120                       "directly to a NTFS volume");
121                 return WIMLIB_ERR_INVALID_PARAM;
122         }
123         if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
124                 ERROR("Cannot specifcy symlink or hardlink flags when applying ");
125                 ERROR("directly to a NTFS volume");
126                 return WIMLIB_ERR_INVALID_PARAM;
127         }
128         ret = wimlib_select_image(w, image);
129         if (ret != 0)
130                 return ret;
131
132         if (getuid() != 0) {
133                 ERROR("We are not root, but NTFS-3g requires root privileges to set arbitrary");
134                 ERROR("security data on the NTFS filesystem.  Please run this program as root");
135                 ERROR("if you want to extract a WIM image while preserving NTFS-specific");
136                 ERROR("information.");
137
138                 return WIMLIB_ERR_NOT_ROOT;
139         }
140         return do_ntfs_apply(w, device, flags);
141 }
142
143 #else /* WITH_NTFS_3G */
144 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
145                                                 const char *device, int flags)
146 {
147         ERROR("wimlib was compiled without support for NTFS-3g, so");
148         ERROR("we cannot apply a WIM image directly to a NTFS volume");
149         return WIMLIB_ERR_UNSUPPORTED;
150 }
151 #endif /* WITH_NTFS_3G */