2 * Copyright (C) 2012 Eric Biggers
4 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
21 #include "wimlib_internal.h"
26 #include "lookup_table.h"
27 #include <ntfs-3g/attrib.h>
28 #include <ntfs-3g/misc.h>
29 #include <ntfs-3g/reparse.h>
30 #include <ntfs-3g/security.h>
31 #include <ntfs-3g/volume.h>
35 struct ntfs_apply_args {
36 struct SECURITY_API *scapi;
42 * Initializations before calling ntfs_get_file_security()
43 * ntfs_set_file_security() and ntfs_read_directory()
45 * Returns an (obscured) struct SECURITY_API* needed for further calls
46 * NULL if device is mounted (EBUSY)
49 static struct SECURITY_API *_ntfs_initialize_file_security(const char *device,
53 unsigned long mntflag;
55 struct SECURITY_API *scapi;
56 struct SECURITY_CONTEXT *scx;
58 scapi = (struct SECURITY_API*)NULL;
59 mnt = ntfs_check_if_mounted(device, &mntflag);
60 if (!mnt && !(mntflag & NTFS_MF_MOUNTED)) {
61 vol = ntfs_mount(device, flags);
63 scapi = (struct SECURITY_API*)
64 ntfs_malloc(sizeof(struct SECURITY_API));
65 if (!ntfs_volume_get_free_space(vol)
67 scapi->magic = MAGIC_API;
68 scapi->seccache = (struct PERMISSIONS_CACHE*)NULL;
69 scx = &scapi->security;
73 scx->pseccache = &scapi->seccache;
74 scx->vol->secure_flags = (1 << SECURITY_DEFAULT) |
76 ntfs_open_secure(vol);
77 ntfs_build_mapping(scx,(const char*)NULL,TRUE);
83 mnt = ntfs_umount(vol,FALSE);
84 scapi = (struct SECURITY_API*)NULL;
92 static int extract_resource_to_ntfs_attr(WIMStruct *w, const struct resource_entry *entry,
95 u8 buf[min(entry->original_size, WIM_CHUNK_SIZE)];
96 u64 num_chunks = (entry->original_size + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
97 u64 n = WIM_CHUNK_SIZE;
98 int res_ctype = wim_resource_compression_type(w, entry);
100 for (u64 i = 0; i < num_chunks; i++) {
101 DEBUG("Write chunk %u of %u", i + 1, num_chunks);
103 if (i == num_chunks - 1) {
104 n = entry->original_size % WIM_CHUNK_SIZE;
110 ret = read_resource(w->fp, entry->size, entry->original_size,
111 entry->offset, res_ctype, n, offset, buf);
115 if (ntfs_attr_pwrite(na, offset, n, buf) != n) {
116 ERROR("Failed to write to NTFS data stream");
117 return WIMLIB_ERR_WRITE;
124 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
128 struct lookup_table_entry *lte;
132 if (dentry_is_directory(dentry)) {
133 wimlib_assert(!dentry_first_lte(dentry, w->lookup_table));
137 DEBUG("Writing NTFS data streams for `%s'", dentry->full_path_utf8);
139 wimlib_assert(dentry->num_ads == 0);
141 lte = dentry_stream_lte(dentry, 0, w->lookup_table);
142 if (lte && lte->resource_entry.original_size != 0) {
144 na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
146 ERROR_WITH_ERRNO("Failed to open unnamed data stream of "
147 "extracted file `%s'",
148 dentry->full_path_utf8);
149 return WIMLIB_ERR_NTFS_3G;
151 ret = extract_resource_to_ntfs_attr(w, <e->resource_entry, na);
160 static int ntfs_apply_dentry(struct dentry *dentry, void *arg)
162 struct ntfs_apply_args *args = arg;
163 struct SECURITY_API *scapi = args->scapi;
164 ntfs_volume *vol = scapi->security.vol;
165 int flags = args->flags;
166 WIMStruct *w = args->w;
168 ntfs_inode *dir_ni, *ni;
171 DEBUG("Applying `%s'", dentry->full_path_utf8);
173 if (dentry_is_root(dentry))
176 if (flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
177 wimlib_assert(dentry->full_path_utf8);
178 puts(dentry->full_path_utf8);
181 char *p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
188 const char *dir_name;
189 if (p == dentry->full_path_utf8)
192 dir_name = dentry->full_path_utf8;
194 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
196 ret = WIMLIB_ERR_NTFS_3G;
197 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
201 DEBUG("Found NTFS inode for `%s'", dir_name);
206 if (dentry_is_directory(dentry)) {
207 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
208 dentry->file_name_len / 2, S_IFDIR);
210 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
211 dentry->file_name_len / 2, S_IFREG);
215 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
216 dentry->full_path_utf8);
217 ret = WIMLIB_ERR_NTFS_3G;
221 ret = write_ntfs_data_streams(ni, dentry, w);
224 if (ntfs_inode_close_in_dir(ni, dir_ni) != 0) {
225 ERROR_WITH_ERRNO("Failed to close new inode");
226 ret = WIMLIB_ERR_NTFS_3G;
229 DEBUG("Closed inode `%s'", dentry->full_path_utf8);
231 if (ntfs_inode_close(dir_ni) != 0) {
232 ret = WIMLIB_ERR_NTFS_3G;
233 ERROR_WITH_ERRNO("Failed to close directory inode");
236 DEBUG("Closed parent inode");
238 if (dentry->security_id != -1) {
239 const struct wim_security_data *sd = wim_security_data(w);
240 wimlib_assert(dentry->security_id < sd->num_entries);
241 DEBUG("Applying security descriptor %d to `%s'",
242 dentry->security_id, dentry->full_path_utf8);
243 ret = ntfs_set_file_security(scapi, dentry->full_path_utf8,
245 sd->descriptors[dentry->security_id]);
247 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
248 dentry->full_path_utf8);
249 ret = WIMLIB_ERR_NTFS_3G;
253 DEBUG("Setting file attributes 0x%x on `%s'",
254 dentry->attributes, dentry->full_path_utf8);
255 if (!ntfs_set_file_attributes(scapi, dentry->full_path_utf8,
256 dentry->attributes)) {
257 ERROR_WITH_ERRNO("Failed to set NTFS file attributes on `%s'",
258 dentry->full_path_utf8);
259 ret = WIMLIB_ERR_NTFS_3G;
262 if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
263 struct lookup_table_entry *lte;
265 lte = dentry_first_lte(dentry, w->lookup_table);
267 ERROR("Could not find reparse data for `%s'",
268 dentry->full_path_utf8);
269 ret = WIMLIB_ERR_INVALID_DENTRY;
273 if (!(ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8))
274 || (ret = ntfs_set_ntfs_reparse_data(ni, lte->symlink_buf,
275 lte->resource_entry.original_size,
278 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on "
279 "`%s'", dentry->full_path_utf8);
280 ret = WIMLIB_ERR_NTFS_3G;
288 static int do_ntfs_apply(WIMStruct *w, const char *device, int flags)
290 struct SECURITY_API *scapi;
293 scapi = _ntfs_initialize_file_security(device, 0);
295 ERROR_WITH_ERRNO("Failed to initialize NTFS file security API "
296 "on NTFS volume `%s'", device);
297 return WIMLIB_ERR_NTFS_3G;
299 struct ntfs_apply_args args = {
304 ret = for_dentry_in_tree(wim_root_dentry(w), ntfs_apply_dentry,
307 if (!ntfs_leave_file_security(scapi)) {
308 ERROR_WITH_ERRNO("Failed to leave file security");
309 ret = WIMLIB_ERR_NTFS_3G;
314 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
315 const char *device, int flags)
320 return WIMLIB_ERR_INVALID_PARAM;
321 if (image == WIM_ALL_IMAGES) {
322 ERROR("Can only apply a single image when applying "
323 "directly to a NTFS volume");
324 return WIMLIB_ERR_INVALID_PARAM;
326 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
327 ERROR("Cannot specifcy symlink or hardlink flags when applying ");
328 ERROR("directly to a NTFS volume");
329 return WIMLIB_ERR_INVALID_PARAM;
331 ret = wimlib_select_image(w, image);
335 /*if (getuid() != 0) {*/
336 /*ERROR("We are not root, but NTFS-3g requires root privileges to set arbitrary");*/
337 /*ERROR("security data on the NTFS filesystem. Please run this program as root");*/
338 /*ERROR("if you want to extract a WIM image while preserving NTFS-specific");*/
339 /*ERROR("information.");*/
341 /*return WIMLIB_ERR_NOT_ROOT;*/
343 return do_ntfs_apply(w, device, flags);
346 #else /* WITH_NTFS_3G */
347 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
348 const char *device, int flags)
350 ERROR("wimlib was compiled without support for NTFS-3g, so");
351 ERROR("we cannot apply a WIM image directly to a NTFS volume");
352 return WIMLIB_ERR_UNSUPPORTED;
354 #endif /* WITH_NTFS_3G */