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 <ntfs-3g/volume.h>
27 #include <ntfs-3g/security.h>
28 #include <ntfs-3g/misc.h>
32 struct ntfs_apply_args {
33 struct SECURITY_API *scapi;
35 const struct wim_security_data *sd;
37 struct ntfs_inode *parent;
41 * Initializations before calling ntfs_get_file_security()
42 * ntfs_set_file_security() and ntfs_read_directory()
44 * Returns an (obscured) struct SECURITY_API* needed for further calls
45 * NULL if device is mounted (EBUSY)
48 static struct SECURITY_API *_ntfs_initialize_file_security(const char *device,
52 unsigned long mntflag;
54 struct SECURITY_API *scapi;
55 struct SECURITY_CONTEXT *scx;
57 scapi = (struct SECURITY_API*)NULL;
58 mnt = ntfs_check_if_mounted(device, &mntflag);
59 if (!mnt && !(mntflag & NTFS_MF_MOUNTED)) {
60 vol = ntfs_mount(device, flags);
62 scapi = (struct SECURITY_API*)
63 ntfs_malloc(sizeof(struct SECURITY_API));
64 if (!ntfs_volume_get_free_space(vol)
66 scapi->magic = MAGIC_API;
67 scapi->seccache = (struct PERMISSIONS_CACHE*)NULL;
68 scx = &scapi->security;
72 scx->pseccache = &scapi->seccache;
73 scx->vol->secure_flags = (1 << SECURITY_DEFAULT) |
75 ntfs_open_secure(vol);
76 ntfs_build_mapping(scx,(const char*)NULL,TRUE);
82 mnt = ntfs_umount(vol,FALSE);
83 scapi = (struct SECURITY_API*)NULL;
91 static int ntfs_apply_dentry(struct dentry *dentry, void *arg)
93 struct ntfs_apply_args *args = arg;
94 struct SECURITY_API *scapi = args->scapi;
95 ntfs_volume *vol = args->vol;
96 const struct wim_security_data *sd = args->sd;
97 int flags = args->flags;
99 ntfs_inode *dir_ni, *ni;
102 DEBUG("Applying `%s'", dentry->full_path_utf8);
104 if (dentry_is_root(dentry))
107 if (flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
108 wimlib_assert(dentry->full_path_utf8);
109 puts(dentry->full_path_utf8);
112 char *p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
119 const char *dir_name;
120 if (p == dentry->full_path_utf8)
123 dir_name = dentry->full_path_utf8;
125 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
127 ret = WIMLIB_ERR_NTFS_3G;
128 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
132 DEBUG("Found NTFS inode for `%s'", dir_name);
137 if (dentry_is_regular_file(dentry)) {
138 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
139 dentry->file_name_len / 2, S_IFREG);
140 } else if (dentry_is_directory(dentry)) {
141 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
142 dentry->file_name_len / 2, S_IFDIR);
149 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
150 dentry->full_path_utf8);
151 ret = WIMLIB_ERR_NTFS_3G;
154 if (ntfs_inode_close_in_dir(ni, dir_ni) != 0) {
155 ERROR_WITH_ERRNO("Failed to close new inode");
156 ret = WIMLIB_ERR_NTFS_3G;
159 DEBUG("Closed inode `%s'", dentry->full_path_utf8);
161 if (ntfs_inode_close(dir_ni) != 0) {
162 ret = WIMLIB_ERR_NTFS_3G;
163 ERROR_WITH_ERRNO("Failed to close directory inode");
166 DEBUG("Closed parent inode");
168 if (dentry->security_id != -1) {
169 wimlib_assert(dentry->security_id < sd->num_entries);
170 DEBUG("Applying security descriptor %d to `%s'",
171 dentry->security_id, dentry->full_path_utf8);
172 ret = ntfs_set_file_security(scapi, dentry->full_path_utf8,
174 sd->descriptors[dentry->security_id]);
176 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
177 dentry->full_path_utf8);
178 ret = WIMLIB_ERR_NTFS_3G;
182 DEBUG("Setting file attributes 0x%x on `%s'",
183 dentry->attributes, dentry->full_path_utf8);
184 if (!ntfs_set_file_attributes(scapi, dentry->full_path_utf8,
185 dentry->attributes)) {
186 ERROR_WITH_ERRNO("Failed to set NTFS file attributes on `%s'",
187 dentry->full_path_utf8);
188 ret = WIMLIB_ERR_NTFS_3G;
195 static int do_ntfs_apply(WIMStruct *w, const char *device, int flags)
197 struct SECURITY_API *scapi;
200 scapi = _ntfs_initialize_file_security(device, 0);
202 ERROR_WITH_ERRNO("Failed to initialize NTFS file security API "
203 "on NTFS volume `%s'", device);
204 return WIMLIB_ERR_NTFS_3G;
206 struct ntfs_apply_args args = {
208 .vol = scapi->security.vol,
209 .sd = wim_security_data(w),
212 ret = for_dentry_in_tree(wim_root_dentry(w), ntfs_apply_dentry,
215 if (!ntfs_leave_file_security(scapi)) {
216 ERROR_WITH_ERRNO("Failed to leave file security");
217 ret = WIMLIB_ERR_NTFS_3G;
222 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
223 const char *device, int flags)
228 return WIMLIB_ERR_INVALID_PARAM;
229 if (image == WIM_ALL_IMAGES) {
230 ERROR("Can only apply a single image when applying "
231 "directly to a NTFS volume");
232 return WIMLIB_ERR_INVALID_PARAM;
234 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
235 ERROR("Cannot specifcy symlink or hardlink flags when applying ");
236 ERROR("directly to a NTFS volume");
237 return WIMLIB_ERR_INVALID_PARAM;
239 ret = wimlib_select_image(w, image);
243 /*if (getuid() != 0) {*/
244 /*ERROR("We are not root, but NTFS-3g requires root privileges to set arbitrary");*/
245 /*ERROR("security data on the NTFS filesystem. Please run this program as root");*/
246 /*ERROR("if you want to extract a WIM image while preserving NTFS-specific");*/
247 /*ERROR("information.");*/
249 /*return WIMLIB_ERR_NOT_ROOT;*/
251 return do_ntfs_apply(w, device, flags);
254 #else /* WITH_NTFS_3G */
255 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
256 const char *device, int flags)
258 ERROR("wimlib was compiled without support for NTFS-3g, so");
259 ERROR("we cannot apply a WIM image directly to a NTFS volume");
260 return WIMLIB_ERR_UNSUPPORTED;
262 #endif /* WITH_NTFS_3G */