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/layout.h>
28 #include <ntfs-3g/acls.h>
29 #include <ntfs-3g/attrib.h>
30 #include <ntfs-3g/misc.h>
31 #include <ntfs-3g/reparse.h>
32 #include <ntfs-3g/security.h>
33 #include <ntfs-3g/volume.h>
37 struct ntfs_apply_args {
38 struct SECURITY_API *scapi;
43 extern int _ntfs_set_file_security(ntfs_volume *vol, ntfs_inode *ni,
44 u32 selection, const char *attr);
45 extern int _ntfs_set_file_attributes(ntfs_inode *ni, s32 attrib);
48 * Initializations before calling ntfs_get_file_security()
49 * ntfs_set_file_security() and ntfs_read_directory()
51 * Returns an (obscured) struct SECURITY_API* needed for further calls
52 * NULL if device is mounted (EBUSY)
55 static struct SECURITY_API *_ntfs_initialize_file_security(const char *device,
59 unsigned long mntflag;
61 struct SECURITY_API *scapi;
62 struct SECURITY_CONTEXT *scx;
64 scapi = (struct SECURITY_API*)NULL;
65 mnt = ntfs_check_if_mounted(device, &mntflag);
66 if (!mnt && !(mntflag & NTFS_MF_MOUNTED)) {
67 vol = ntfs_mount(device, flags);
69 scapi = (struct SECURITY_API*)
70 ntfs_malloc(sizeof(struct SECURITY_API));
71 if (!ntfs_volume_get_free_space(vol)
73 scapi->magic = MAGIC_API;
74 scapi->seccache = (struct PERMISSIONS_CACHE*)NULL;
75 scx = &scapi->security;
79 scx->pseccache = &scapi->seccache;
80 scx->vol->secure_flags = (1 << SECURITY_DEFAULT) |
82 ntfs_open_secure(vol);
83 ntfs_build_mapping(scx,(const char*)NULL,TRUE);
89 mnt = ntfs_umount(vol,FALSE);
90 scapi = (struct SECURITY_API*)NULL;
99 * JPA NTFS constants or structs
100 * should be moved to layout.h
103 #define ALIGN_SDS_BLOCK 0x40000 /* Alignment for a $SDS block */
104 #define ALIGN_SDS_ENTRY 16 /* Alignment for a $SDS entry */
105 #define STUFFSZ 0x4000 /* unitary stuffing size for $SDS */
106 #define FIRST_SECURITY_ID 0x100 /* Lowest security id */
107 /* Mask for attributes which can be forced */
108 #define FILE_ATTR_SETTABLE ( FILE_ATTR_READONLY \
111 | FILE_ATTR_ARCHIVE \
112 | FILE_ATTR_TEMPORARY \
113 | FILE_ATTR_OFFLINE \
114 | FILE_ATTR_NOT_CONTENT_INDEXED )
121 static int extract_resource_to_ntfs_attr(WIMStruct *w, const struct resource_entry *entry,
124 u8 buf[min(entry->original_size, WIM_CHUNK_SIZE)];
125 u64 num_chunks = (entry->original_size + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
126 u64 n = WIM_CHUNK_SIZE;
127 int res_ctype = wim_resource_compression_type(w, entry);
129 for (u64 i = 0; i < num_chunks; i++) {
130 DEBUG("Write chunk %u of %u", i + 1, num_chunks);
132 if (i == num_chunks - 1) {
133 n = entry->original_size % WIM_CHUNK_SIZE;
139 ret = read_resource(w->fp, entry->size, entry->original_size,
140 entry->offset, res_ctype, n, offset, buf);
144 if (ntfs_attr_pwrite(na, offset, n, buf) != n) {
145 ERROR("Failed to write to NTFS data stream");
146 return WIMLIB_ERR_WRITE;
153 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
157 struct lookup_table_entry *lte;
161 DEBUG("Writing NTFS data streams for `%s'", dentry->full_path_utf8);
163 wimlib_assert(dentry->num_ads == 0);
165 lte = dentry_stream_lte(dentry, 0, w->lookup_table);
166 if (lte && lte->resource_entry.original_size != 0) {
168 na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
170 ERROR_WITH_ERRNO("Failed to open unnamed data stream of "
171 "extracted file `%s'",
172 dentry->full_path_utf8);
173 return WIMLIB_ERR_NTFS_3G;
175 ret = extract_resource_to_ntfs_attr(w, <e->resource_entry, na);
184 static int __ntfs_apply_dentry(struct dentry *dentry, ntfs_inode *dir_ni,
191 print_dentry(dentry, w->lookup_table);
193 if (dentry_is_directory(dentry))
198 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
199 dentry->file_name_len / 2, type);
202 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
203 dentry->full_path_utf8);
204 ret = WIMLIB_ERR_NTFS_3G;
208 if (!dentry_is_directory(dentry) &&
209 !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
210 ret = write_ntfs_data_streams(ni, dentry, w);
215 DEBUG("Setting file attributes 0x%x on `%s'",
217 dentry->full_path_utf8);
219 if (!_ntfs_set_file_attributes(ni, dentry->attributes)) {
220 ERROR("Failed to set NTFS file attributes on `%s'",
221 dentry->full_path_utf8);
222 ret = WIMLIB_ERR_NTFS_3G;
226 if (dentry->security_id != -1) {
227 const struct wim_security_data *sd = wim_security_data(w);
228 wimlib_assert(dentry->security_id < sd->num_entries);
229 DEBUG("Applying security descriptor %d to `%s'",
230 dentry->security_id, dentry->full_path_utf8);
231 if (!_ntfs_set_file_security(ni->vol, ni, ~0,
232 sd->descriptors[dentry->security_id]))
234 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
235 dentry->full_path_utf8);
236 ret = WIMLIB_ERR_NTFS_3G;
241 if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
242 struct lookup_table_entry *lte;
244 lte = dentry_first_lte(dentry, w->lookup_table);
246 ERROR("Could not find reparse data for `%s'",
247 dentry->full_path_utf8);
248 ret = WIMLIB_ERR_INVALID_DENTRY;
252 ret = ntfs_set_ntfs_reparse_data(ni, lte->symlink_buf,
253 lte->resource_entry.original_size,
256 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on "
257 "`%s'", dentry->full_path_utf8);
258 ret = WIMLIB_ERR_NTFS_3G;
263 if (ntfs_inode_close_in_dir(ni, dir_ni) != 0) {
264 ERROR_WITH_ERRNO("Failed to close new inode");
265 ret = WIMLIB_ERR_NTFS_3G;
272 static int ntfs_apply_dentry(struct dentry *dentry, void *arg)
274 struct ntfs_apply_args *args = arg;
275 struct SECURITY_API *scapi = args->scapi;
276 ntfs_volume *vol = scapi->security.vol;
277 int extract_flags = args->extract_flags;
278 WIMStruct *w = args->w;
282 DEBUG("Applying `%s'", dentry->full_path_utf8);
284 if (dentry_is_root(dentry))
287 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
288 wimlib_assert(dentry->full_path_utf8);
289 puts(dentry->full_path_utf8);
292 char *p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
299 const char *dir_name = dentry->full_path_utf8;
301 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
304 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
306 return WIMLIB_ERR_NTFS_3G;
308 DEBUG("Found NTFS inode for `%s'", dir_name);
310 ret = __ntfs_apply_dentry(dentry, dir_ni, w);
312 if (ntfs_inode_close(dir_ni) != 0) {
314 ret = WIMLIB_ERR_NTFS_3G;
315 ERROR_WITH_ERRNO("Failed to close directory inode");
321 static int do_ntfs_apply(WIMStruct *w, const char *device, int extract_flags)
323 struct SECURITY_API *scapi;
326 scapi = _ntfs_initialize_file_security(device, 0);
328 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
329 return WIMLIB_ERR_NTFS_3G;
331 struct ntfs_apply_args args = {
333 .extract_flags = extract_flags,
336 ret = for_dentry_in_tree(wim_root_dentry(w), ntfs_apply_dentry,
338 if (!ntfs_leave_file_security(scapi)) {
339 ERROR_WITH_ERRNO("Failed to unmount NTFS volume");
340 ret = WIMLIB_ERR_NTFS_3G;
345 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
346 const char *device, int flags)
351 return WIMLIB_ERR_INVALID_PARAM;
352 if (image == WIM_ALL_IMAGES) {
353 ERROR("Can only apply a single image when applying "
354 "directly to a NTFS volume");
355 return WIMLIB_ERR_INVALID_PARAM;
357 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
358 ERROR("Cannot specifcy symlink or hardlink flags when applying ");
359 ERROR("directly to a NTFS volume");
360 return WIMLIB_ERR_INVALID_PARAM;
362 ret = wimlib_select_image(w, image);
366 /*if (getuid() != 0) {*/
367 /*ERROR("We are not root, but NTFS-3g requires root privileges to set arbitrary");*/
368 /*ERROR("security data on the NTFS filesystem. Please run this program as root");*/
369 /*ERROR("if you want to extract a WIM image while preserving NTFS-specific");*/
370 /*ERROR("information.");*/
372 /*return WIMLIB_ERR_NOT_ROOT;*/
374 return do_ntfs_apply(w, device, flags);
377 #else /* WITH_NTFS_3G */
378 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
379 const char *device, int flags)
381 ERROR("wimlib was compiled without support for NTFS-3g, so");
382 ERROR("we cannot apply a WIM image directly to a NTFS volume");
383 return WIMLIB_ERR_UNSUPPORTED;
385 #endif /* WITH_NTFS_3G */