Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * dir.c - Directory handling code. Originated from the Linux-NTFS project. |
| 3 | * |
| 4 | * Copyright (c) 2002-2005 Anton Altaparmakov |
| 5 | * Copyright (c) 2004-2005 Richard Russon |
| 6 | * Copyright (c) 2004-2008 Szabolcs Szakacsits |
| 7 | * Copyright (c) 2005-2007 Yura Pakhuchiy |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 8 | * Copyright (c) 2008-2014 Jean-Pierre Andre |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 9 | * |
| 10 | * This program/include file is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as published |
| 12 | * by the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program/include file is distributed in the hope that it will be |
| 16 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program (in the main directory of the NTFS-3G |
| 22 | * distribution in the file COPYING); if not, write to the Free Software |
| 23 | * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #ifdef HAVE_CONFIG_H |
| 27 | #include "config.h" |
| 28 | #endif |
| 29 | |
| 30 | #ifdef HAVE_STDLIB_H |
| 31 | #include <stdlib.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_ERRNO_H |
| 34 | #include <errno.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_STRING_H |
| 37 | #include <string.h> |
| 38 | #endif |
| 39 | #ifdef HAVE_SYS_STAT_H |
| 40 | #include <sys/stat.h> |
| 41 | #endif |
| 42 | |
| 43 | #ifdef HAVE_SYS_SYSMACROS_H |
| 44 | #include <sys/sysmacros.h> |
| 45 | #endif |
| 46 | |
| 47 | #include "param.h" |
| 48 | #include "types.h" |
| 49 | #include "debug.h" |
| 50 | #include "attrib.h" |
| 51 | #include "inode.h" |
| 52 | #include "dir.h" |
| 53 | #include "volume.h" |
| 54 | #include "mft.h" |
| 55 | #include "index.h" |
| 56 | #include "ntfstime.h" |
| 57 | #include "lcnalloc.h" |
| 58 | #include "logging.h" |
| 59 | #include "cache.h" |
| 60 | #include "misc.h" |
| 61 | #include "security.h" |
| 62 | #include "reparse.h" |
| 63 | #include "object_id.h" |
| 64 | |
| 65 | #ifdef HAVE_SETXATTR |
| 66 | #include <sys/xattr.h> |
| 67 | #endif |
| 68 | |
| 69 | /* |
| 70 | * The little endian Unicode strings "$I30", "$SII", "$SDH", "$O" |
| 71 | * and "$Q" as global constants. |
| 72 | */ |
| 73 | ntfschar NTFS_INDEX_I30[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('I'), |
| 74 | const_cpu_to_le16('3'), const_cpu_to_le16('0'), |
| 75 | const_cpu_to_le16('\0') }; |
| 76 | ntfschar NTFS_INDEX_SII[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('S'), |
| 77 | const_cpu_to_le16('I'), const_cpu_to_le16('I'), |
| 78 | const_cpu_to_le16('\0') }; |
| 79 | ntfschar NTFS_INDEX_SDH[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('S'), |
| 80 | const_cpu_to_le16('D'), const_cpu_to_le16('H'), |
| 81 | const_cpu_to_le16('\0') }; |
| 82 | ntfschar NTFS_INDEX_O[3] = { const_cpu_to_le16('$'), const_cpu_to_le16('O'), |
| 83 | const_cpu_to_le16('\0') }; |
| 84 | ntfschar NTFS_INDEX_Q[3] = { const_cpu_to_le16('$'), const_cpu_to_le16('Q'), |
| 85 | const_cpu_to_le16('\0') }; |
| 86 | ntfschar NTFS_INDEX_R[3] = { const_cpu_to_le16('$'), const_cpu_to_le16('R'), |
| 87 | const_cpu_to_le16('\0') }; |
| 88 | |
| 89 | #if CACHE_INODE_SIZE |
| 90 | |
| 91 | /* |
| 92 | * Pathname hashing |
| 93 | * |
| 94 | * Based on first char and second char (which may be '\0') |
| 95 | */ |
| 96 | |
| 97 | int ntfs_dir_inode_hash(const struct CACHED_GENERIC *cached) |
| 98 | { |
| 99 | const char *path; |
| 100 | const unsigned char *name; |
| 101 | |
| 102 | path = (const char*)cached->variable; |
| 103 | if (!path) { |
| 104 | ntfs_log_error("Bad inode cache entry\n"); |
| 105 | return (-1); |
| 106 | } |
| 107 | name = (const unsigned char*)strrchr(path,'/'); |
| 108 | if (!name) |
| 109 | name = (const unsigned char*)path; |
| 110 | return (((name[0] << 1) + name[1] + strlen((const char*)name)) |
| 111 | % (2*CACHE_INODE_SIZE)); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Pathname comparing for entering/fetching from cache |
| 116 | */ |
| 117 | |
| 118 | static int inode_cache_compare(const struct CACHED_GENERIC *cached, |
| 119 | const struct CACHED_GENERIC *wanted) |
| 120 | { |
| 121 | return (!cached->variable |
| 122 | || strcmp(cached->variable, wanted->variable)); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Pathname comparing for invalidating entries in cache |
| 127 | * |
| 128 | * A partial path is compared in order to invalidate all paths |
| 129 | * related to a renamed directory |
| 130 | * inode numbers are also checked, as deleting a long name may |
| 131 | * imply deleting a short name and conversely |
| 132 | * |
| 133 | * Only use associated with a CACHE_NOHASH flag |
| 134 | */ |
| 135 | |
| 136 | static int inode_cache_inv_compare(const struct CACHED_GENERIC *cached, |
| 137 | const struct CACHED_GENERIC *wanted) |
| 138 | { |
| 139 | int len; |
| 140 | BOOL different; |
| 141 | const struct CACHED_INODE *w; |
| 142 | const struct CACHED_INODE *c; |
| 143 | |
| 144 | w = (const struct CACHED_INODE*)wanted; |
| 145 | c = (const struct CACHED_INODE*)cached; |
| 146 | if (w->pathname) { |
| 147 | len = strlen(w->pathname); |
| 148 | different = !cached->variable |
| 149 | || ((w->inum != MREF(c->inum)) |
| 150 | && (strncmp(c->pathname, w->pathname, len) |
| 151 | || ((c->pathname[len] != '\0') |
| 152 | && (c->pathname[len] != '/')))); |
| 153 | } else |
| 154 | different = !c->pathname |
| 155 | || (w->inum != MREF(c->inum)); |
| 156 | return (different); |
| 157 | } |
| 158 | |
| 159 | #endif |
| 160 | |
| 161 | #if CACHE_LOOKUP_SIZE |
| 162 | |
| 163 | /* |
| 164 | * File name comparing for entering/fetching from lookup cache |
| 165 | */ |
| 166 | |
| 167 | static int lookup_cache_compare(const struct CACHED_GENERIC *cached, |
| 168 | const struct CACHED_GENERIC *wanted) |
| 169 | { |
| 170 | const struct CACHED_LOOKUP *c = (const struct CACHED_LOOKUP*) cached; |
| 171 | const struct CACHED_LOOKUP *w = (const struct CACHED_LOOKUP*) wanted; |
| 172 | return (!c->name |
| 173 | || (c->parent != w->parent) |
| 174 | || (c->namesize != w->namesize) |
| 175 | || memcmp(c->name, w->name, c->namesize)); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Inode number comparing for invalidating lookup cache |
| 180 | * |
| 181 | * All entries with designated inode number are invalidated |
| 182 | * |
| 183 | * Only use associated with a CACHE_NOHASH flag |
| 184 | */ |
| 185 | |
| 186 | static int lookup_cache_inv_compare(const struct CACHED_GENERIC *cached, |
| 187 | const struct CACHED_GENERIC *wanted) |
| 188 | { |
| 189 | const struct CACHED_LOOKUP *c = (const struct CACHED_LOOKUP*) cached; |
| 190 | const struct CACHED_LOOKUP *w = (const struct CACHED_LOOKUP*) wanted; |
| 191 | return (!c->name |
| 192 | || (c->parent != w->parent) |
| 193 | || (MREF(c->inum) != MREF(w->inum))); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Lookup hashing |
| 198 | * |
| 199 | * Based on first, second and and last char |
| 200 | */ |
| 201 | |
| 202 | int ntfs_dir_lookup_hash(const struct CACHED_GENERIC *cached) |
| 203 | { |
| 204 | const unsigned char *name; |
| 205 | int count; |
| 206 | unsigned int val; |
| 207 | |
| 208 | name = (const unsigned char*)cached->variable; |
| 209 | count = cached->varsize; |
| 210 | if (!name || !count) { |
| 211 | ntfs_log_error("Bad lookup cache entry\n"); |
| 212 | return (-1); |
| 213 | } |
| 214 | val = (name[0] << 2) + (name[1] << 1) + name[count - 1] + count; |
| 215 | return (val % (2*CACHE_LOOKUP_SIZE)); |
| 216 | } |
| 217 | |
| 218 | #endif |
| 219 | |
| 220 | /** |
| 221 | * ntfs_inode_lookup_by_name - find an inode in a directory given its name |
| 222 | * @dir_ni: ntfs inode of the directory in which to search for the name |
| 223 | * @uname: Unicode name for which to search in the directory |
| 224 | * @uname_len: length of the name @uname in Unicode characters |
| 225 | * |
| 226 | * Look for an inode with name @uname in the directory with inode @dir_ni. |
| 227 | * ntfs_inode_lookup_by_name() walks the contents of the directory looking for |
| 228 | * the Unicode name. If the name is found in the directory, the corresponding |
| 229 | * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it |
| 230 | * is a 64-bit number containing the sequence number. |
| 231 | * |
| 232 | * On error, return -1 with errno set to the error code. If the inode is is not |
| 233 | * found errno is ENOENT. |
| 234 | * |
| 235 | * Note, @uname_len does not include the (optional) terminating NULL character. |
| 236 | * |
| 237 | * Note, we look for a case sensitive match first but we also look for a case |
| 238 | * insensitive match at the same time. If we find a case insensitive match, we |
| 239 | * save that for the case that we don't find an exact match, where we return |
| 240 | * the mft reference of the case insensitive match. |
| 241 | * |
| 242 | * If the volume is mounted with the case sensitive flag set, then we only |
| 243 | * allow exact matches. |
| 244 | */ |
| 245 | u64 ntfs_inode_lookup_by_name(ntfs_inode *dir_ni, |
| 246 | const ntfschar *uname, const int uname_len) |
| 247 | { |
| 248 | VCN vcn; |
| 249 | u64 mref = 0; |
| 250 | s64 br; |
| 251 | ntfs_volume *vol = dir_ni->vol; |
| 252 | ntfs_attr_search_ctx *ctx; |
| 253 | INDEX_ROOT *ir; |
| 254 | INDEX_ENTRY *ie; |
| 255 | INDEX_ALLOCATION *ia; |
| 256 | IGNORE_CASE_BOOL case_sensitivity; |
| 257 | u8 *index_end; |
| 258 | ntfs_attr *ia_na; |
| 259 | int eo, rc; |
| 260 | u32 index_block_size; |
| 261 | u8 index_vcn_size_bits; |
| 262 | |
| 263 | ntfs_log_trace("Entering\n"); |
| 264 | |
| 265 | if (!dir_ni || !dir_ni->mrec || !uname || uname_len <= 0) { |
| 266 | errno = EINVAL; |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | ctx = ntfs_attr_get_search_ctx(dir_ni, NULL); |
| 271 | if (!ctx) |
| 272 | return -1; |
| 273 | |
| 274 | /* Find the index root attribute in the mft record. */ |
| 275 | if (ntfs_attr_lookup(AT_INDEX_ROOT, NTFS_INDEX_I30, 4, CASE_SENSITIVE, 0, NULL, |
| 276 | 0, ctx)) { |
| 277 | ntfs_log_perror("Index root attribute missing in directory inode " |
| 278 | "%lld", (unsigned long long)dir_ni->mft_no); |
| 279 | goto put_err_out; |
| 280 | } |
| 281 | case_sensitivity = (NVolCaseSensitive(vol) ? CASE_SENSITIVE : IGNORE_CASE); |
| 282 | /* Get to the index root value. */ |
| 283 | ir = (INDEX_ROOT*)((u8*)ctx->attr + |
| 284 | le16_to_cpu(ctx->attr->value_offset)); |
| 285 | index_block_size = le32_to_cpu(ir->index_block_size); |
| 286 | if (index_block_size < NTFS_BLOCK_SIZE || |
| 287 | index_block_size & (index_block_size - 1)) { |
| 288 | ntfs_log_error("Index block size %u is invalid.\n", |
| 289 | (unsigned)index_block_size); |
| 290 | goto put_err_out; |
| 291 | } |
| 292 | index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); |
| 293 | /* The first index entry. */ |
| 294 | ie = (INDEX_ENTRY*)((u8*)&ir->index + |
| 295 | le32_to_cpu(ir->index.entries_offset)); |
| 296 | /* |
| 297 | * Loop until we exceed valid memory (corruption case) or until we |
| 298 | * reach the last entry. |
| 299 | */ |
| 300 | for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { |
| 301 | /* Bounds checks. */ |
| 302 | if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie + |
| 303 | sizeof(INDEX_ENTRY_HEADER) > index_end || |
| 304 | (u8*)ie + le16_to_cpu(ie->key_length) > |
| 305 | index_end) { |
| 306 | ntfs_log_error("Index entry out of bounds in inode %lld" |
| 307 | "\n", (unsigned long long)dir_ni->mft_no); |
| 308 | goto put_err_out; |
| 309 | } |
| 310 | /* |
| 311 | * The last entry cannot contain a name. It can however contain |
| 312 | * a pointer to a child node in the B+tree so we just break out. |
| 313 | */ |
| 314 | if (ie->ie_flags & INDEX_ENTRY_END) |
| 315 | break; |
| 316 | |
| 317 | if (!le16_to_cpu(ie->length)) { |
| 318 | ntfs_log_error("Zero length index entry in inode %lld" |
| 319 | "\n", (unsigned long long)dir_ni->mft_no); |
| 320 | goto put_err_out; |
| 321 | } |
| 322 | /* |
| 323 | * Not a perfect match, need to do full blown collation so we |
| 324 | * know which way in the B+tree we have to go. |
| 325 | */ |
| 326 | rc = ntfs_names_full_collate(uname, uname_len, |
| 327 | (ntfschar*)&ie->key.file_name.file_name, |
| 328 | ie->key.file_name.file_name_length, |
| 329 | case_sensitivity, vol->upcase, vol->upcase_len); |
| 330 | /* |
| 331 | * If uname collates before the name of the current entry, there |
| 332 | * is definitely no such name in this index but we might need to |
| 333 | * descend into the B+tree so we just break out of the loop. |
| 334 | */ |
| 335 | if (rc == -1) |
| 336 | break; |
| 337 | /* The names are not equal, continue the search. */ |
| 338 | if (rc) |
| 339 | continue; |
| 340 | /* |
| 341 | * Perfect match, this will never happen as the |
| 342 | * ntfs_are_names_equal() call will have gotten a match but we |
| 343 | * still treat it correctly. |
| 344 | */ |
| 345 | mref = le64_to_cpu(ie->indexed_file); |
| 346 | ntfs_attr_put_search_ctx(ctx); |
| 347 | return mref; |
| 348 | } |
| 349 | /* |
| 350 | * We have finished with this index without success. Check for the |
| 351 | * presence of a child node and if not present return error code |
| 352 | * ENOENT, unless we have got the mft reference of a matching name |
| 353 | * cached in mref in which case return mref. |
| 354 | */ |
| 355 | if (!(ie->ie_flags & INDEX_ENTRY_NODE)) { |
| 356 | ntfs_attr_put_search_ctx(ctx); |
| 357 | if (mref) |
| 358 | return mref; |
| 359 | ntfs_log_debug("Entry not found - between root entries.\n"); |
| 360 | errno = ENOENT; |
| 361 | return -1; |
| 362 | } /* Child node present, descend into it. */ |
| 363 | |
| 364 | /* Open the index allocation attribute. */ |
| 365 | ia_na = ntfs_attr_open(dir_ni, AT_INDEX_ALLOCATION, NTFS_INDEX_I30, 4); |
| 366 | if (!ia_na) { |
| 367 | ntfs_log_perror("Failed to open index allocation (inode %lld)", |
| 368 | (unsigned long long)dir_ni->mft_no); |
| 369 | goto put_err_out; |
| 370 | } |
| 371 | |
| 372 | /* Allocate a buffer for the current index block. */ |
| 373 | ia = ntfs_malloc(index_block_size); |
| 374 | if (!ia) { |
| 375 | ntfs_attr_close(ia_na); |
| 376 | goto put_err_out; |
| 377 | } |
| 378 | |
| 379 | /* Determine the size of a vcn in the directory index. */ |
| 380 | if (vol->cluster_size <= index_block_size) { |
| 381 | index_vcn_size_bits = vol->cluster_size_bits; |
| 382 | } else { |
| 383 | index_vcn_size_bits = NTFS_BLOCK_SIZE_BITS; |
| 384 | } |
| 385 | |
| 386 | /* Get the starting vcn of the index_block holding the child node. */ |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 387 | vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 388 | |
| 389 | descend_into_child_node: |
| 390 | |
| 391 | /* Read the index block starting at vcn. */ |
| 392 | br = ntfs_attr_mst_pread(ia_na, vcn << index_vcn_size_bits, 1, |
| 393 | index_block_size, ia); |
| 394 | if (br != 1) { |
| 395 | if (br != -1) |
| 396 | errno = EIO; |
| 397 | ntfs_log_perror("Failed to read vcn 0x%llx", |
| 398 | (unsigned long long)vcn); |
| 399 | goto close_err_out; |
| 400 | } |
| 401 | |
| 402 | if (sle64_to_cpu(ia->index_block_vcn) != vcn) { |
| 403 | ntfs_log_error("Actual VCN (0x%llx) of index buffer is different " |
| 404 | "from expected VCN (0x%llx).\n", |
| 405 | (long long)sle64_to_cpu(ia->index_block_vcn), |
| 406 | (long long)vcn); |
| 407 | errno = EIO; |
| 408 | goto close_err_out; |
| 409 | } |
| 410 | if (le32_to_cpu(ia->index.allocated_size) + 0x18 != index_block_size) { |
| 411 | ntfs_log_error("Index buffer (VCN 0x%llx) of directory inode 0x%llx " |
| 412 | "has a size (%u) differing from the directory " |
| 413 | "specified size (%u).\n", (long long)vcn, |
| 414 | (unsigned long long)dir_ni->mft_no, |
| 415 | (unsigned) le32_to_cpu(ia->index.allocated_size) + 0x18, |
| 416 | (unsigned)index_block_size); |
| 417 | errno = EIO; |
| 418 | goto close_err_out; |
| 419 | } |
| 420 | index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); |
| 421 | if (index_end > (u8*)ia + index_block_size) { |
| 422 | ntfs_log_error("Size of index buffer (VCN 0x%llx) of directory inode " |
| 423 | "0x%llx exceeds maximum size.\n", |
| 424 | (long long)vcn, (unsigned long long)dir_ni->mft_no); |
| 425 | errno = EIO; |
| 426 | goto close_err_out; |
| 427 | } |
| 428 | |
| 429 | /* The first index entry. */ |
| 430 | ie = (INDEX_ENTRY*)((u8*)&ia->index + |
| 431 | le32_to_cpu(ia->index.entries_offset)); |
| 432 | /* |
| 433 | * Iterate similar to above big loop but applied to index buffer, thus |
| 434 | * loop until we exceed valid memory (corruption case) or until we |
| 435 | * reach the last entry. |
| 436 | */ |
| 437 | for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { |
| 438 | /* Bounds check. */ |
| 439 | if ((u8*)ie < (u8*)ia || (u8*)ie + |
| 440 | sizeof(INDEX_ENTRY_HEADER) > index_end || |
| 441 | (u8*)ie + le16_to_cpu(ie->key_length) > |
| 442 | index_end) { |
| 443 | ntfs_log_error("Index entry out of bounds in directory " |
| 444 | "inode %lld.\n", |
| 445 | (unsigned long long)dir_ni->mft_no); |
| 446 | errno = EIO; |
| 447 | goto close_err_out; |
| 448 | } |
| 449 | /* |
| 450 | * The last entry cannot contain a name. It can however contain |
| 451 | * a pointer to a child node in the B+tree so we just break out. |
| 452 | */ |
| 453 | if (ie->ie_flags & INDEX_ENTRY_END) |
| 454 | break; |
| 455 | |
| 456 | if (!le16_to_cpu(ie->length)) { |
| 457 | errno = EIO; |
| 458 | ntfs_log_error("Zero length index entry in inode %lld" |
| 459 | "\n", (unsigned long long)dir_ni->mft_no); |
| 460 | goto close_err_out; |
| 461 | } |
| 462 | /* |
| 463 | * Not a perfect match, need to do full blown collation so we |
| 464 | * know which way in the B+tree we have to go. |
| 465 | */ |
| 466 | rc = ntfs_names_full_collate(uname, uname_len, |
| 467 | (ntfschar*)&ie->key.file_name.file_name, |
| 468 | ie->key.file_name.file_name_length, |
| 469 | case_sensitivity, vol->upcase, vol->upcase_len); |
| 470 | /* |
| 471 | * If uname collates before the name of the current entry, there |
| 472 | * is definitely no such name in this index but we might need to |
| 473 | * descend into the B+tree so we just break out of the loop. |
| 474 | */ |
| 475 | if (rc == -1) |
| 476 | break; |
| 477 | /* The names are not equal, continue the search. */ |
| 478 | if (rc) |
| 479 | continue; |
| 480 | mref = le64_to_cpu(ie->indexed_file); |
| 481 | free(ia); |
| 482 | ntfs_attr_close(ia_na); |
| 483 | ntfs_attr_put_search_ctx(ctx); |
| 484 | return mref; |
| 485 | } |
| 486 | /* |
| 487 | * We have finished with this index buffer without success. Check for |
| 488 | * the presence of a child node. |
| 489 | */ |
| 490 | if (ie->ie_flags & INDEX_ENTRY_NODE) { |
| 491 | if ((ia->index.ih_flags & NODE_MASK) == LEAF_NODE) { |
| 492 | ntfs_log_error("Index entry with child node found in a leaf " |
| 493 | "node in directory inode %lld.\n", |
| 494 | (unsigned long long)dir_ni->mft_no); |
| 495 | errno = EIO; |
| 496 | goto close_err_out; |
| 497 | } |
| 498 | /* Child node present, descend into it. */ |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 499 | vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 500 | if (vcn >= 0) |
| 501 | goto descend_into_child_node; |
| 502 | ntfs_log_error("Negative child node vcn in directory inode " |
| 503 | "0x%llx.\n", (unsigned long long)dir_ni->mft_no); |
| 504 | errno = EIO; |
| 505 | goto close_err_out; |
| 506 | } |
| 507 | free(ia); |
| 508 | ntfs_attr_close(ia_na); |
| 509 | ntfs_attr_put_search_ctx(ctx); |
| 510 | /* |
| 511 | * No child node present, return error code ENOENT, unless we have got |
| 512 | * the mft reference of a matching name cached in mref in which case |
| 513 | * return mref. |
| 514 | */ |
| 515 | if (mref) |
| 516 | return mref; |
| 517 | ntfs_log_debug("Entry not found.\n"); |
| 518 | errno = ENOENT; |
| 519 | return -1; |
| 520 | put_err_out: |
| 521 | eo = EIO; |
| 522 | ntfs_log_debug("Corrupt directory. Aborting lookup.\n"); |
| 523 | eo_put_err_out: |
| 524 | ntfs_attr_put_search_ctx(ctx); |
| 525 | errno = eo; |
| 526 | return -1; |
| 527 | close_err_out: |
| 528 | eo = errno; |
| 529 | free(ia); |
| 530 | ntfs_attr_close(ia_na); |
| 531 | goto eo_put_err_out; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Lookup a file in a directory from its UTF-8 name |
| 536 | * |
| 537 | * The name is first fetched from cache if one is defined |
| 538 | * |
| 539 | * Returns the inode number |
| 540 | * or -1 if not possible (errno tells why) |
| 541 | */ |
| 542 | |
| 543 | u64 ntfs_inode_lookup_by_mbsname(ntfs_inode *dir_ni, const char *name) |
| 544 | { |
| 545 | int uname_len; |
| 546 | ntfschar *uname = (ntfschar*)NULL; |
| 547 | u64 inum; |
| 548 | char *cached_name; |
| 549 | const char *const_name; |
| 550 | |
| 551 | if (!NVolCaseSensitive(dir_ni->vol)) { |
| 552 | cached_name = ntfs_uppercase_mbs(name, |
| 553 | dir_ni->vol->upcase, dir_ni->vol->upcase_len); |
| 554 | const_name = cached_name; |
| 555 | } else { |
| 556 | cached_name = (char*)NULL; |
| 557 | const_name = name; |
| 558 | } |
| 559 | if (const_name) { |
| 560 | #if CACHE_LOOKUP_SIZE |
| 561 | |
| 562 | /* |
| 563 | * fetch inode from cache |
| 564 | */ |
| 565 | |
| 566 | if (dir_ni->vol->lookup_cache) { |
| 567 | struct CACHED_LOOKUP item; |
| 568 | struct CACHED_LOOKUP *cached; |
| 569 | |
| 570 | item.name = const_name; |
| 571 | item.namesize = strlen(const_name) + 1; |
| 572 | item.parent = dir_ni->mft_no; |
| 573 | cached = (struct CACHED_LOOKUP*)ntfs_fetch_cache( |
| 574 | dir_ni->vol->lookup_cache, |
| 575 | GENERIC(&item), lookup_cache_compare); |
| 576 | if (cached) { |
| 577 | inum = cached->inum; |
| 578 | if (inum == (u64)-1) |
| 579 | errno = ENOENT; |
| 580 | } else { |
| 581 | /* Generate unicode name. */ |
| 582 | uname_len = ntfs_mbstoucs(name, &uname); |
| 583 | if (uname_len >= 0) { |
| 584 | inum = ntfs_inode_lookup_by_name(dir_ni, |
| 585 | uname, uname_len); |
| 586 | item.inum = inum; |
| 587 | /* enter into cache, even if not found */ |
| 588 | ntfs_enter_cache(dir_ni->vol->lookup_cache, |
| 589 | GENERIC(&item), |
| 590 | lookup_cache_compare); |
| 591 | free(uname); |
| 592 | } else |
| 593 | inum = (s64)-1; |
| 594 | } |
| 595 | } else |
| 596 | #endif |
| 597 | { |
| 598 | /* Generate unicode name. */ |
| 599 | uname_len = ntfs_mbstoucs(cached_name, &uname); |
| 600 | if (uname_len >= 0) |
| 601 | inum = ntfs_inode_lookup_by_name(dir_ni, |
| 602 | uname, uname_len); |
| 603 | else |
| 604 | inum = (s64)-1; |
| 605 | } |
| 606 | if (cached_name) |
| 607 | free(cached_name); |
| 608 | } else |
| 609 | inum = (s64)-1; |
| 610 | return (inum); |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Update a cache lookup record when a name has been defined |
| 615 | * |
| 616 | * The UTF-8 name is required |
| 617 | */ |
| 618 | |
| 619 | void ntfs_inode_update_mbsname(ntfs_inode *dir_ni, const char *name, u64 inum) |
| 620 | { |
| 621 | #if CACHE_LOOKUP_SIZE |
| 622 | struct CACHED_LOOKUP item; |
| 623 | struct CACHED_LOOKUP *cached; |
| 624 | char *cached_name; |
| 625 | |
| 626 | if (dir_ni->vol->lookup_cache) { |
| 627 | if (!NVolCaseSensitive(dir_ni->vol)) { |
| 628 | cached_name = ntfs_uppercase_mbs(name, |
| 629 | dir_ni->vol->upcase, dir_ni->vol->upcase_len); |
| 630 | item.name = cached_name; |
| 631 | } else { |
| 632 | cached_name = (char*)NULL; |
| 633 | item.name = name; |
| 634 | } |
| 635 | if (item.name) { |
| 636 | item.namesize = strlen(item.name) + 1; |
| 637 | item.parent = dir_ni->mft_no; |
| 638 | item.inum = inum; |
| 639 | cached = (struct CACHED_LOOKUP*)ntfs_enter_cache( |
| 640 | dir_ni->vol->lookup_cache, |
| 641 | GENERIC(&item), lookup_cache_compare); |
| 642 | if (cached) |
| 643 | cached->inum = inum; |
| 644 | if (cached_name) |
| 645 | free(cached_name); |
| 646 | } |
| 647 | } |
| 648 | #endif |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * ntfs_pathname_to_inode - Find the inode which represents the given pathname |
| 653 | * @vol: An ntfs volume obtained from ntfs_mount |
| 654 | * @parent: A directory inode to begin the search (may be NULL) |
| 655 | * @pathname: Pathname to be located |
| 656 | * |
| 657 | * Take an ASCII pathname and find the inode that represents it. The function |
| 658 | * splits the path and then descends the directory tree. If @parent is NULL, |
| 659 | * then the root directory '.' will be used as the base for the search. |
| 660 | * |
| 661 | * Return: inode Success, the pathname was valid |
| 662 | * NULL Error, the pathname was invalid, or some other error occurred |
| 663 | */ |
| 664 | ntfs_inode *ntfs_pathname_to_inode(ntfs_volume *vol, ntfs_inode *parent, |
| 665 | const char *pathname) |
| 666 | { |
| 667 | u64 inum; |
| 668 | int len, err = 0; |
| 669 | char *p, *q; |
| 670 | ntfs_inode *ni; |
| 671 | ntfs_inode *result = NULL; |
| 672 | ntfschar *unicode = NULL; |
| 673 | char *ascii = NULL; |
| 674 | #if CACHE_INODE_SIZE |
| 675 | struct CACHED_INODE item; |
| 676 | struct CACHED_INODE *cached; |
| 677 | char *fullname; |
| 678 | #endif |
| 679 | |
| 680 | if (!vol || !pathname) { |
| 681 | errno = EINVAL; |
| 682 | return NULL; |
| 683 | } |
| 684 | |
| 685 | ntfs_log_trace("path: '%s'\n", pathname); |
| 686 | |
| 687 | ascii = strdup(pathname); |
| 688 | if (!ascii) { |
| 689 | ntfs_log_error("Out of memory.\n"); |
| 690 | err = ENOMEM; |
| 691 | goto out; |
| 692 | } |
| 693 | |
| 694 | p = ascii; |
| 695 | /* Remove leading /'s. */ |
| 696 | while (p && *p && *p == PATH_SEP) |
| 697 | p++; |
| 698 | #if CACHE_INODE_SIZE |
| 699 | fullname = p; |
| 700 | if (p[0] && (p[strlen(p)-1] == PATH_SEP)) |
| 701 | ntfs_log_error("Unnormalized path %s\n",ascii); |
| 702 | #endif |
| 703 | if (parent) { |
| 704 | ni = parent; |
| 705 | } else { |
| 706 | #if CACHE_INODE_SIZE |
| 707 | /* |
| 708 | * fetch inode for full path from cache |
| 709 | */ |
| 710 | if (*fullname) { |
| 711 | item.pathname = fullname; |
| 712 | item.varsize = strlen(fullname) + 1; |
| 713 | cached = (struct CACHED_INODE*)ntfs_fetch_cache( |
| 714 | vol->xinode_cache, GENERIC(&item), |
| 715 | inode_cache_compare); |
| 716 | } else |
| 717 | cached = (struct CACHED_INODE*)NULL; |
| 718 | if (cached) { |
| 719 | /* |
| 720 | * return opened inode if found in cache |
| 721 | */ |
| 722 | inum = MREF(cached->inum); |
| 723 | ni = ntfs_inode_open(vol, inum); |
| 724 | if (!ni) { |
| 725 | ntfs_log_debug("Cannot open inode %llu: %s.\n", |
| 726 | (unsigned long long)inum, p); |
| 727 | err = EIO; |
| 728 | } |
| 729 | result = ni; |
| 730 | goto out; |
| 731 | } |
| 732 | #endif |
| 733 | ni = ntfs_inode_open(vol, FILE_root); |
| 734 | if (!ni) { |
| 735 | ntfs_log_debug("Couldn't open the inode of the root " |
| 736 | "directory.\n"); |
| 737 | err = EIO; |
| 738 | result = (ntfs_inode*)NULL; |
| 739 | goto out; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | while (p && *p) { |
| 744 | /* Find the end of the first token. */ |
| 745 | q = strchr(p, PATH_SEP); |
| 746 | if (q != NULL) { |
| 747 | *q = '\0'; |
| 748 | } |
| 749 | #if CACHE_INODE_SIZE |
| 750 | /* |
| 751 | * fetch inode for partial path from cache |
| 752 | */ |
| 753 | cached = (struct CACHED_INODE*)NULL; |
| 754 | if (!parent) { |
| 755 | item.pathname = fullname; |
| 756 | item.varsize = strlen(fullname) + 1; |
| 757 | cached = (struct CACHED_INODE*)ntfs_fetch_cache( |
| 758 | vol->xinode_cache, GENERIC(&item), |
| 759 | inode_cache_compare); |
| 760 | if (cached) { |
| 761 | inum = cached->inum; |
| 762 | } |
| 763 | } |
| 764 | /* |
| 765 | * if not in cache, translate, search, then |
| 766 | * insert into cache if found |
| 767 | */ |
| 768 | if (!cached) { |
| 769 | len = ntfs_mbstoucs(p, &unicode); |
| 770 | if (len < 0) { |
| 771 | ntfs_log_perror("Could not convert filename to Unicode:" |
| 772 | " '%s'", p); |
| 773 | err = errno; |
| 774 | goto close; |
| 775 | } else if (len > NTFS_MAX_NAME_LEN) { |
| 776 | err = ENAMETOOLONG; |
| 777 | goto close; |
| 778 | } |
| 779 | inum = ntfs_inode_lookup_by_name(ni, unicode, len); |
| 780 | if (!parent && (inum != (u64) -1)) { |
| 781 | item.inum = inum; |
| 782 | ntfs_enter_cache(vol->xinode_cache, |
| 783 | GENERIC(&item), |
| 784 | inode_cache_compare); |
| 785 | } |
| 786 | } |
| 787 | #else |
| 788 | len = ntfs_mbstoucs(p, &unicode); |
| 789 | if (len < 0) { |
| 790 | ntfs_log_perror("Could not convert filename to Unicode:" |
| 791 | " '%s'", p); |
| 792 | err = errno; |
| 793 | goto close; |
| 794 | } else if (len > NTFS_MAX_NAME_LEN) { |
| 795 | err = ENAMETOOLONG; |
| 796 | goto close; |
| 797 | } |
| 798 | inum = ntfs_inode_lookup_by_name(ni, unicode, len); |
| 799 | #endif |
| 800 | if (inum == (u64) -1) { |
| 801 | ntfs_log_debug("Couldn't find name '%s' in pathname " |
| 802 | "'%s'.\n", p, pathname); |
| 803 | err = ENOENT; |
| 804 | goto close; |
| 805 | } |
| 806 | |
| 807 | if (ni != parent) |
| 808 | if (ntfs_inode_close(ni)) { |
| 809 | err = errno; |
| 810 | goto out; |
| 811 | } |
| 812 | |
| 813 | inum = MREF(inum); |
| 814 | ni = ntfs_inode_open(vol, inum); |
| 815 | if (!ni) { |
| 816 | ntfs_log_debug("Cannot open inode %llu: %s.\n", |
| 817 | (unsigned long long)inum, p); |
| 818 | err = EIO; |
| 819 | goto close; |
| 820 | } |
| 821 | |
| 822 | free(unicode); |
| 823 | unicode = NULL; |
| 824 | |
| 825 | if (q) *q++ = PATH_SEP; /* JPA */ |
| 826 | p = q; |
| 827 | while (p && *p && *p == PATH_SEP) |
| 828 | p++; |
| 829 | } |
| 830 | |
| 831 | result = ni; |
| 832 | ni = NULL; |
| 833 | close: |
| 834 | if (ni && (ni != parent)) |
| 835 | if (ntfs_inode_close(ni) && !err) |
| 836 | err = errno; |
| 837 | out: |
| 838 | free(ascii); |
| 839 | free(unicode); |
| 840 | if (err) |
| 841 | errno = err; |
| 842 | return result; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * The little endian Unicode string ".." for ntfs_readdir(). |
| 847 | */ |
| 848 | static const ntfschar dotdot[3] = { const_cpu_to_le16('.'), |
| 849 | const_cpu_to_le16('.'), |
| 850 | const_cpu_to_le16('\0') }; |
| 851 | |
| 852 | /* |
| 853 | * union index_union - |
| 854 | * More helpers for ntfs_readdir(). |
| 855 | */ |
| 856 | typedef union { |
| 857 | INDEX_ROOT *ir; |
| 858 | INDEX_ALLOCATION *ia; |
| 859 | } index_union __attribute__((__transparent_union__)); |
| 860 | |
| 861 | /** |
| 862 | * enum INDEX_TYPE - |
| 863 | * More helpers for ntfs_readdir(). |
| 864 | */ |
| 865 | typedef enum { |
| 866 | INDEX_TYPE_ROOT, /* index root */ |
| 867 | INDEX_TYPE_ALLOCATION, /* index allocation */ |
| 868 | } INDEX_TYPE; |
| 869 | |
| 870 | /* |
| 871 | * Decode Interix file types |
| 872 | * |
| 873 | * Non-Interix types are returned as plain files, because a |
| 874 | * Windows user may force patterns very similar to Interix, |
| 875 | * and most metadata files have such similar patters. |
| 876 | */ |
| 877 | |
| 878 | static u32 ntfs_interix_types(ntfs_inode *ni) |
| 879 | { |
| 880 | ntfs_attr *na; |
| 881 | u32 dt_type; |
| 882 | le64 magic; |
| 883 | |
| 884 | dt_type = NTFS_DT_UNKNOWN; |
| 885 | na = ntfs_attr_open(ni, AT_DATA, NULL, 0); |
| 886 | if (na) { |
| 887 | /* Unrecognized patterns (eg HID + SYST) are plain files */ |
| 888 | dt_type = NTFS_DT_REG; |
| 889 | if (na->data_size <= 1) { |
| 890 | if (!(ni->flags & FILE_ATTR_HIDDEN)) |
| 891 | dt_type = (na->data_size ? |
| 892 | NTFS_DT_SOCK : NTFS_DT_FIFO); |
| 893 | } else { |
| 894 | if ((na->data_size >= (s64)sizeof(magic)) |
| 895 | && (ntfs_attr_pread(na, 0, sizeof(magic), &magic) |
| 896 | == sizeof(magic))) { |
| 897 | if (magic == INTX_SYMBOLIC_LINK) |
| 898 | dt_type = NTFS_DT_LNK; |
| 899 | else if (magic == INTX_BLOCK_DEVICE) |
| 900 | dt_type = NTFS_DT_BLK; |
| 901 | else if (magic == INTX_CHARACTER_DEVICE) |
| 902 | dt_type = NTFS_DT_CHR; |
| 903 | } |
| 904 | } |
| 905 | ntfs_attr_close(na); |
| 906 | } |
| 907 | return (dt_type); |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * Decode file types |
| 912 | * |
| 913 | * Better only use for Interix types and junctions, |
| 914 | * unneeded complexity when used for plain files or directories |
| 915 | * |
| 916 | * Error cases are logged and returned as unknown. |
| 917 | */ |
| 918 | |
| 919 | static u32 ntfs_dir_entry_type(ntfs_inode *dir_ni, MFT_REF mref, |
| 920 | FILE_ATTR_FLAGS attributes) |
| 921 | { |
| 922 | ntfs_inode *ni; |
| 923 | u32 dt_type; |
| 924 | |
| 925 | dt_type = NTFS_DT_UNKNOWN; |
| 926 | ni = ntfs_inode_open(dir_ni->vol, mref); |
| 927 | if (ni) { |
| 928 | if ((attributes & FILE_ATTR_REPARSE_POINT) |
| 929 | && ntfs_possible_symlink(ni)) |
| 930 | dt_type = NTFS_DT_LNK; |
| 931 | else |
| 932 | if ((attributes & FILE_ATTR_SYSTEM) |
| 933 | && !(attributes & FILE_ATTR_I30_INDEX_PRESENT)) |
| 934 | dt_type = ntfs_interix_types(ni); |
| 935 | else |
| 936 | dt_type = (attributes |
| 937 | & FILE_ATTR_I30_INDEX_PRESENT |
| 938 | ? NTFS_DT_DIR : NTFS_DT_REG); |
| 939 | if (ntfs_inode_close(ni)) { |
| 940 | /* anything special worth doing ? */ |
| 941 | ntfs_log_error("Failed to close inode %lld\n", |
| 942 | (long long)MREF(mref)); |
| 943 | } |
| 944 | } |
| 945 | if (dt_type == NTFS_DT_UNKNOWN) |
| 946 | ntfs_log_error("Could not decode the type of inode %lld\n", |
| 947 | (long long)MREF(mref)); |
| 948 | return (dt_type); |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * ntfs_filldir - ntfs specific filldir method |
| 953 | * @dir_ni: ntfs inode of current directory |
| 954 | * @pos: current position in directory |
| 955 | * @ivcn_bits: log(2) of index vcn size |
| 956 | * @index_type: specifies whether @iu is an index root or an index allocation |
| 957 | * @iu: index root or index block to which @ie belongs |
| 958 | * @ie: current index entry |
| 959 | * @dirent: context for filldir callback supplied by the caller |
| 960 | * @filldir: filldir callback supplied by the caller |
| 961 | * |
| 962 | * Pass information specifying the current directory entry @ie to the @filldir |
| 963 | * callback. |
| 964 | */ |
| 965 | static int ntfs_filldir(ntfs_inode *dir_ni, s64 *pos, u8 ivcn_bits, |
| 966 | const INDEX_TYPE index_type, index_union iu, INDEX_ENTRY *ie, |
| 967 | void *dirent, ntfs_filldir_t filldir) |
| 968 | { |
| 969 | FILE_NAME_ATTR *fn = &ie->key.file_name; |
| 970 | unsigned dt_type; |
| 971 | BOOL metadata; |
| 972 | ntfschar *loname; |
| 973 | int res; |
| 974 | MFT_REF mref; |
| 975 | |
| 976 | ntfs_log_trace("Entering.\n"); |
| 977 | |
| 978 | /* Advance the position even if going to skip the entry. */ |
| 979 | if (index_type == INDEX_TYPE_ALLOCATION) |
| 980 | *pos = (u8*)ie - (u8*)iu.ia + (sle64_to_cpu( |
| 981 | iu.ia->index_block_vcn) << ivcn_bits) + |
| 982 | dir_ni->vol->mft_record_size; |
| 983 | else /* if (index_type == INDEX_TYPE_ROOT) */ |
| 984 | *pos = (u8*)ie - (u8*)iu.ir; |
| 985 | mref = le64_to_cpu(ie->indexed_file); |
| 986 | metadata = (MREF(mref) != FILE_root) && (MREF(mref) < FILE_first_user); |
| 987 | /* Skip root directory self reference entry. */ |
| 988 | if (MREF_LE(ie->indexed_file) == FILE_root) |
| 989 | return 0; |
| 990 | if ((ie->key.file_name.file_attributes |
| 991 | & (FILE_ATTR_REPARSE_POINT | FILE_ATTR_SYSTEM)) |
| 992 | && !metadata) |
| 993 | dt_type = ntfs_dir_entry_type(dir_ni, mref, |
| 994 | ie->key.file_name.file_attributes); |
| 995 | else if (ie->key.file_name.file_attributes |
| 996 | & FILE_ATTR_I30_INDEX_PRESENT) |
| 997 | dt_type = NTFS_DT_DIR; |
| 998 | else |
| 999 | dt_type = NTFS_DT_REG; |
| 1000 | |
| 1001 | /* return metadata files and hidden files if requested */ |
| 1002 | if ((!metadata && (NVolShowHidFiles(dir_ni->vol) |
| 1003 | || !(fn->file_attributes & FILE_ATTR_HIDDEN))) |
| 1004 | || (NVolShowSysFiles(dir_ni->vol) && (NVolShowHidFiles(dir_ni->vol) |
| 1005 | || metadata))) { |
| 1006 | if (NVolCaseSensitive(dir_ni->vol)) { |
| 1007 | res = filldir(dirent, fn->file_name, |
| 1008 | fn->file_name_length, |
| 1009 | fn->file_name_type, *pos, |
| 1010 | mref, dt_type); |
| 1011 | } else { |
| 1012 | loname = (ntfschar*)ntfs_malloc(2*fn->file_name_length); |
| 1013 | if (loname) { |
| 1014 | memcpy(loname, fn->file_name, |
| 1015 | 2*fn->file_name_length); |
| 1016 | ntfs_name_locase(loname, fn->file_name_length, |
| 1017 | dir_ni->vol->locase, |
| 1018 | dir_ni->vol->upcase_len); |
| 1019 | res = filldir(dirent, loname, |
| 1020 | fn->file_name_length, |
| 1021 | fn->file_name_type, *pos, |
| 1022 | mref, dt_type); |
| 1023 | free(loname); |
| 1024 | } else |
| 1025 | res = -1; |
| 1026 | } |
| 1027 | } else |
| 1028 | res = 0; |
| 1029 | return (res); |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * ntfs_mft_get_parent_ref - find mft reference of parent directory of an inode |
| 1034 | * @ni: ntfs inode whose parent directory to find |
| 1035 | * |
| 1036 | * Find the parent directory of the ntfs inode @ni. To do this, find the first |
| 1037 | * file name attribute in the mft record of @ni and return the parent mft |
| 1038 | * reference from that. |
| 1039 | * |
| 1040 | * Note this only makes sense for directories, since files can be hard linked |
| 1041 | * from multiple directories and there is no way for us to tell which one is |
| 1042 | * being looked for. |
| 1043 | * |
| 1044 | * Technically directories can have hard links, too, but we consider that as |
| 1045 | * illegal as Linux/UNIX do not support directory hard links. |
| 1046 | * |
| 1047 | * Return the mft reference of the parent directory on success or -1 on error |
| 1048 | * with errno set to the error code. |
| 1049 | */ |
| 1050 | static MFT_REF ntfs_mft_get_parent_ref(ntfs_inode *ni) |
| 1051 | { |
| 1052 | MFT_REF mref; |
| 1053 | ntfs_attr_search_ctx *ctx; |
| 1054 | FILE_NAME_ATTR *fn; |
| 1055 | int eo; |
| 1056 | |
| 1057 | ntfs_log_trace("Entering.\n"); |
| 1058 | |
| 1059 | if (!ni) { |
| 1060 | errno = EINVAL; |
| 1061 | return ERR_MREF(-1); |
| 1062 | } |
| 1063 | |
| 1064 | ctx = ntfs_attr_get_search_ctx(ni, NULL); |
| 1065 | if (!ctx) |
| 1066 | return ERR_MREF(-1); |
| 1067 | if (ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx)) { |
| 1068 | ntfs_log_error("No file name found in inode %lld\n", |
| 1069 | (unsigned long long)ni->mft_no); |
| 1070 | goto err_out; |
| 1071 | } |
| 1072 | if (ctx->attr->non_resident) { |
| 1073 | ntfs_log_error("File name attribute must be resident (inode " |
| 1074 | "%lld)\n", (unsigned long long)ni->mft_no); |
| 1075 | goto io_err_out; |
| 1076 | } |
| 1077 | fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + |
| 1078 | le16_to_cpu(ctx->attr->value_offset)); |
| 1079 | if ((u8*)fn + le32_to_cpu(ctx->attr->value_length) > |
| 1080 | (u8*)ctx->attr + le32_to_cpu(ctx->attr->length)) { |
| 1081 | ntfs_log_error("Corrupt file name attribute in inode %lld.\n", |
| 1082 | (unsigned long long)ni->mft_no); |
| 1083 | goto io_err_out; |
| 1084 | } |
| 1085 | mref = le64_to_cpu(fn->parent_directory); |
| 1086 | ntfs_attr_put_search_ctx(ctx); |
| 1087 | return mref; |
| 1088 | io_err_out: |
| 1089 | errno = EIO; |
| 1090 | err_out: |
| 1091 | eo = errno; |
| 1092 | ntfs_attr_put_search_ctx(ctx); |
| 1093 | errno = eo; |
| 1094 | return ERR_MREF(-1); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * ntfs_readdir - read the contents of an ntfs directory |
| 1099 | * @dir_ni: ntfs inode of current directory |
| 1100 | * @pos: current position in directory |
| 1101 | * @dirent: context for filldir callback supplied by the caller |
| 1102 | * @filldir: filldir callback supplied by the caller |
| 1103 | * |
| 1104 | * Parse the index root and the index blocks that are marked in use in the |
| 1105 | * index bitmap and hand each found directory entry to the @filldir callback |
| 1106 | * supplied by the caller. |
| 1107 | * |
| 1108 | * Return 0 on success or -1 on error with errno set to the error code. |
| 1109 | * |
| 1110 | * Note: Index blocks are parsed in ascending vcn order, from which follows |
| 1111 | * that the directory entries are not returned sorted. |
| 1112 | */ |
| 1113 | int ntfs_readdir(ntfs_inode *dir_ni, s64 *pos, |
| 1114 | void *dirent, ntfs_filldir_t filldir) |
| 1115 | { |
| 1116 | s64 i_size, br, ia_pos, bmp_pos, ia_start; |
| 1117 | ntfs_volume *vol; |
| 1118 | ntfs_attr *ia_na, *bmp_na = NULL; |
| 1119 | ntfs_attr_search_ctx *ctx = NULL; |
| 1120 | u8 *index_end, *bmp = NULL; |
| 1121 | INDEX_ROOT *ir; |
| 1122 | INDEX_ENTRY *ie; |
| 1123 | INDEX_ALLOCATION *ia = NULL; |
| 1124 | int rc, ir_pos, bmp_buf_size, bmp_buf_pos, eo; |
| 1125 | u32 index_block_size; |
| 1126 | u8 index_block_size_bits, index_vcn_size_bits; |
| 1127 | |
| 1128 | ntfs_log_trace("Entering.\n"); |
| 1129 | |
| 1130 | if (!dir_ni || !pos || !filldir) { |
| 1131 | errno = EINVAL; |
| 1132 | return -1; |
| 1133 | } |
| 1134 | |
| 1135 | if (!(dir_ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)) { |
| 1136 | errno = ENOTDIR; |
| 1137 | return -1; |
| 1138 | } |
| 1139 | |
| 1140 | vol = dir_ni->vol; |
| 1141 | |
| 1142 | ntfs_log_trace("Entering for inode %lld, *pos 0x%llx.\n", |
| 1143 | (unsigned long long)dir_ni->mft_no, (long long)*pos); |
| 1144 | |
| 1145 | /* Open the index allocation attribute. */ |
| 1146 | ia_na = ntfs_attr_open(dir_ni, AT_INDEX_ALLOCATION, NTFS_INDEX_I30, 4); |
| 1147 | if (!ia_na) { |
| 1148 | if (errno != ENOENT) { |
| 1149 | ntfs_log_perror("Failed to open index allocation attribute. " |
| 1150 | "Directory inode %lld is corrupt or bug", |
| 1151 | (unsigned long long)dir_ni->mft_no); |
| 1152 | return -1; |
| 1153 | } |
| 1154 | i_size = 0; |
| 1155 | } else |
| 1156 | i_size = ia_na->data_size; |
| 1157 | |
| 1158 | rc = 0; |
| 1159 | |
| 1160 | /* Are we at end of dir yet? */ |
| 1161 | if (*pos >= i_size + vol->mft_record_size) |
| 1162 | goto done; |
| 1163 | |
| 1164 | /* Emulate . and .. for all directories. */ |
| 1165 | if (!*pos) { |
| 1166 | rc = filldir(dirent, dotdot, 1, FILE_NAME_POSIX, *pos, |
| 1167 | MK_MREF(dir_ni->mft_no, |
| 1168 | le16_to_cpu(dir_ni->mrec->sequence_number)), |
| 1169 | NTFS_DT_DIR); |
| 1170 | if (rc) |
| 1171 | goto err_out; |
| 1172 | ++*pos; |
| 1173 | } |
| 1174 | if (*pos == 1) { |
| 1175 | MFT_REF parent_mref; |
| 1176 | |
| 1177 | parent_mref = ntfs_mft_get_parent_ref(dir_ni); |
| 1178 | if (parent_mref == ERR_MREF(-1)) { |
| 1179 | ntfs_log_perror("Parent directory not found"); |
| 1180 | goto dir_err_out; |
| 1181 | } |
| 1182 | |
| 1183 | rc = filldir(dirent, dotdot, 2, FILE_NAME_POSIX, *pos, |
| 1184 | parent_mref, NTFS_DT_DIR); |
| 1185 | if (rc) |
| 1186 | goto err_out; |
| 1187 | ++*pos; |
| 1188 | } |
| 1189 | |
| 1190 | ctx = ntfs_attr_get_search_ctx(dir_ni, NULL); |
| 1191 | if (!ctx) |
| 1192 | goto err_out; |
| 1193 | |
| 1194 | /* Get the offset into the index root attribute. */ |
| 1195 | ir_pos = (int)*pos; |
| 1196 | /* Find the index root attribute in the mft record. */ |
| 1197 | if (ntfs_attr_lookup(AT_INDEX_ROOT, NTFS_INDEX_I30, 4, CASE_SENSITIVE, 0, NULL, |
| 1198 | 0, ctx)) { |
| 1199 | ntfs_log_perror("Index root attribute missing in directory inode " |
| 1200 | "%lld", (unsigned long long)dir_ni->mft_no); |
| 1201 | goto dir_err_out; |
| 1202 | } |
| 1203 | /* Get to the index root value. */ |
| 1204 | ir = (INDEX_ROOT*)((u8*)ctx->attr + |
| 1205 | le16_to_cpu(ctx->attr->value_offset)); |
| 1206 | |
| 1207 | /* Determine the size of a vcn in the directory index. */ |
| 1208 | index_block_size = le32_to_cpu(ir->index_block_size); |
| 1209 | if (index_block_size < NTFS_BLOCK_SIZE || |
| 1210 | index_block_size & (index_block_size - 1)) { |
| 1211 | ntfs_log_error("Index block size %u is invalid.\n", |
| 1212 | (unsigned)index_block_size); |
| 1213 | goto dir_err_out; |
| 1214 | } |
| 1215 | index_block_size_bits = ffs(index_block_size) - 1; |
| 1216 | if (vol->cluster_size <= index_block_size) { |
| 1217 | index_vcn_size_bits = vol->cluster_size_bits; |
| 1218 | } else { |
| 1219 | index_vcn_size_bits = NTFS_BLOCK_SIZE_BITS; |
| 1220 | } |
| 1221 | |
| 1222 | /* Are we jumping straight into the index allocation attribute? */ |
| 1223 | if (*pos >= vol->mft_record_size) { |
| 1224 | ntfs_attr_put_search_ctx(ctx); |
| 1225 | ctx = NULL; |
| 1226 | goto skip_index_root; |
| 1227 | } |
| 1228 | |
| 1229 | index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); |
| 1230 | /* The first index entry. */ |
| 1231 | ie = (INDEX_ENTRY*)((u8*)&ir->index + |
| 1232 | le32_to_cpu(ir->index.entries_offset)); |
| 1233 | /* |
| 1234 | * Loop until we exceed valid memory (corruption case) or until we |
| 1235 | * reach the last entry or until filldir tells us it has had enough |
| 1236 | * or signals an error (both covered by the rc test). |
| 1237 | */ |
| 1238 | for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { |
| 1239 | ntfs_log_debug("In index root, offset %d.\n", (int)((u8*)ie - (u8*)ir)); |
| 1240 | /* Bounds checks. */ |
| 1241 | if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie + |
| 1242 | sizeof(INDEX_ENTRY_HEADER) > index_end || |
| 1243 | (u8*)ie + le16_to_cpu(ie->key_length) > |
| 1244 | index_end) |
| 1245 | goto dir_err_out; |
| 1246 | /* The last entry cannot contain a name. */ |
| 1247 | if (ie->ie_flags & INDEX_ENTRY_END) |
| 1248 | break; |
| 1249 | |
| 1250 | if (!le16_to_cpu(ie->length)) |
| 1251 | goto dir_err_out; |
| 1252 | |
| 1253 | /* Skip index root entry if continuing previous readdir. */ |
| 1254 | if (ir_pos > (u8*)ie - (u8*)ir) |
| 1255 | continue; |
| 1256 | /* |
| 1257 | * Submit the directory entry to ntfs_filldir(), which will |
| 1258 | * invoke the filldir() callback as appropriate. |
| 1259 | */ |
| 1260 | rc = ntfs_filldir(dir_ni, pos, index_vcn_size_bits, |
| 1261 | INDEX_TYPE_ROOT, ir, ie, dirent, filldir); |
| 1262 | if (rc) { |
| 1263 | ntfs_attr_put_search_ctx(ctx); |
| 1264 | ctx = NULL; |
| 1265 | goto err_out; |
| 1266 | } |
| 1267 | } |
| 1268 | ntfs_attr_put_search_ctx(ctx); |
| 1269 | ctx = NULL; |
| 1270 | |
| 1271 | /* If there is no index allocation attribute we are finished. */ |
| 1272 | if (!ia_na) |
| 1273 | goto EOD; |
| 1274 | |
| 1275 | /* Advance *pos to the beginning of the index allocation. */ |
| 1276 | *pos = vol->mft_record_size; |
| 1277 | |
| 1278 | skip_index_root: |
| 1279 | |
| 1280 | if (!ia_na) |
| 1281 | goto done; |
| 1282 | |
| 1283 | /* Allocate a buffer for the current index block. */ |
| 1284 | ia = ntfs_malloc(index_block_size); |
| 1285 | if (!ia) |
| 1286 | goto err_out; |
| 1287 | |
| 1288 | bmp_na = ntfs_attr_open(dir_ni, AT_BITMAP, NTFS_INDEX_I30, 4); |
| 1289 | if (!bmp_na) { |
| 1290 | ntfs_log_perror("Failed to open index bitmap attribute"); |
| 1291 | goto dir_err_out; |
| 1292 | } |
| 1293 | |
| 1294 | /* Get the offset into the index allocation attribute. */ |
| 1295 | ia_pos = *pos - vol->mft_record_size; |
| 1296 | |
| 1297 | bmp_pos = ia_pos >> index_block_size_bits; |
| 1298 | if (bmp_pos >> 3 >= bmp_na->data_size) { |
| 1299 | ntfs_log_error("Current index position exceeds index bitmap " |
| 1300 | "size.\n"); |
| 1301 | goto dir_err_out; |
| 1302 | } |
| 1303 | |
| 1304 | bmp_buf_size = min(bmp_na->data_size - (bmp_pos >> 3), 4096); |
| 1305 | bmp = ntfs_malloc(bmp_buf_size); |
| 1306 | if (!bmp) |
| 1307 | goto err_out; |
| 1308 | |
| 1309 | br = ntfs_attr_pread(bmp_na, bmp_pos >> 3, bmp_buf_size, bmp); |
| 1310 | if (br != bmp_buf_size) { |
| 1311 | if (br != -1) |
| 1312 | errno = EIO; |
| 1313 | ntfs_log_perror("Failed to read from index bitmap attribute"); |
| 1314 | goto err_out; |
| 1315 | } |
| 1316 | |
| 1317 | bmp_buf_pos = 0; |
| 1318 | /* If the index block is not in use find the next one that is. */ |
| 1319 | while (!(bmp[bmp_buf_pos >> 3] & (1 << (bmp_buf_pos & 7)))) { |
| 1320 | find_next_index_buffer: |
| 1321 | bmp_pos++; |
| 1322 | bmp_buf_pos++; |
| 1323 | /* If we have reached the end of the bitmap, we are done. */ |
| 1324 | if (bmp_pos >> 3 >= bmp_na->data_size) |
| 1325 | goto EOD; |
| 1326 | ia_pos = bmp_pos << index_block_size_bits; |
| 1327 | if (bmp_buf_pos >> 3 < bmp_buf_size) |
| 1328 | continue; |
| 1329 | /* Read next chunk from the index bitmap. */ |
| 1330 | bmp_buf_pos = 0; |
| 1331 | if ((bmp_pos >> 3) + bmp_buf_size > bmp_na->data_size) |
| 1332 | bmp_buf_size = bmp_na->data_size - (bmp_pos >> 3); |
| 1333 | br = ntfs_attr_pread(bmp_na, bmp_pos >> 3, bmp_buf_size, bmp); |
| 1334 | if (br != bmp_buf_size) { |
| 1335 | if (br != -1) |
| 1336 | errno = EIO; |
| 1337 | ntfs_log_perror("Failed to read from index bitmap attribute"); |
| 1338 | goto err_out; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | ntfs_log_debug("Handling index block 0x%llx.\n", (long long)bmp_pos); |
| 1343 | |
| 1344 | /* Read the index block starting at bmp_pos. */ |
| 1345 | br = ntfs_attr_mst_pread(ia_na, bmp_pos << index_block_size_bits, 1, |
| 1346 | index_block_size, ia); |
| 1347 | if (br != 1) { |
| 1348 | if (br != -1) |
| 1349 | errno = EIO; |
| 1350 | ntfs_log_perror("Failed to read index block"); |
| 1351 | goto err_out; |
| 1352 | } |
| 1353 | |
| 1354 | ia_start = ia_pos & ~(s64)(index_block_size - 1); |
| 1355 | if (sle64_to_cpu(ia->index_block_vcn) != ia_start >> |
| 1356 | index_vcn_size_bits) { |
| 1357 | ntfs_log_error("Actual VCN (0x%llx) of index buffer is different " |
| 1358 | "from expected VCN (0x%llx) in inode 0x%llx.\n", |
| 1359 | (long long)sle64_to_cpu(ia->index_block_vcn), |
| 1360 | (long long)ia_start >> index_vcn_size_bits, |
| 1361 | (unsigned long long)dir_ni->mft_no); |
| 1362 | goto dir_err_out; |
| 1363 | } |
| 1364 | if (le32_to_cpu(ia->index.allocated_size) + 0x18 != index_block_size) { |
| 1365 | ntfs_log_error("Index buffer (VCN 0x%llx) of directory inode %lld " |
| 1366 | "has a size (%u) differing from the directory " |
| 1367 | "specified size (%u).\n", (long long)ia_start >> |
| 1368 | index_vcn_size_bits, |
| 1369 | (unsigned long long)dir_ni->mft_no, |
| 1370 | (unsigned) le32_to_cpu(ia->index.allocated_size) |
| 1371 | + 0x18, (unsigned)index_block_size); |
| 1372 | goto dir_err_out; |
| 1373 | } |
| 1374 | index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); |
| 1375 | if (index_end > (u8*)ia + index_block_size) { |
| 1376 | ntfs_log_error("Size of index buffer (VCN 0x%llx) of directory inode " |
| 1377 | "%lld exceeds maximum size.\n", |
| 1378 | (long long)ia_start >> index_vcn_size_bits, |
| 1379 | (unsigned long long)dir_ni->mft_no); |
| 1380 | goto dir_err_out; |
| 1381 | } |
| 1382 | /* The first index entry. */ |
| 1383 | ie = (INDEX_ENTRY*)((u8*)&ia->index + |
| 1384 | le32_to_cpu(ia->index.entries_offset)); |
| 1385 | /* |
| 1386 | * Loop until we exceed valid memory (corruption case) or until we |
| 1387 | * reach the last entry or until ntfs_filldir tells us it has had |
| 1388 | * enough or signals an error (both covered by the rc test). |
| 1389 | */ |
| 1390 | for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { |
| 1391 | ntfs_log_debug("In index allocation, offset 0x%llx.\n", |
| 1392 | (long long)ia_start + ((u8*)ie - (u8*)ia)); |
| 1393 | /* Bounds checks. */ |
| 1394 | if ((u8*)ie < (u8*)ia || (u8*)ie + |
| 1395 | sizeof(INDEX_ENTRY_HEADER) > index_end || |
| 1396 | (u8*)ie + le16_to_cpu(ie->key_length) > |
| 1397 | index_end) { |
| 1398 | ntfs_log_error("Index entry out of bounds in directory inode " |
| 1399 | "%lld.\n", (unsigned long long)dir_ni->mft_no); |
| 1400 | goto dir_err_out; |
| 1401 | } |
| 1402 | /* The last entry cannot contain a name. */ |
| 1403 | if (ie->ie_flags & INDEX_ENTRY_END) |
| 1404 | break; |
| 1405 | |
| 1406 | if (!le16_to_cpu(ie->length)) |
| 1407 | goto dir_err_out; |
| 1408 | |
| 1409 | /* Skip index entry if continuing previous readdir. */ |
| 1410 | if (ia_pos - ia_start > (u8*)ie - (u8*)ia) |
| 1411 | continue; |
| 1412 | /* |
| 1413 | * Submit the directory entry to ntfs_filldir(), which will |
| 1414 | * invoke the filldir() callback as appropriate. |
| 1415 | */ |
| 1416 | rc = ntfs_filldir(dir_ni, pos, index_vcn_size_bits, |
| 1417 | INDEX_TYPE_ALLOCATION, ia, ie, dirent, filldir); |
| 1418 | if (rc) |
| 1419 | goto err_out; |
| 1420 | } |
| 1421 | goto find_next_index_buffer; |
| 1422 | EOD: |
| 1423 | /* We are finished, set *pos to EOD. */ |
| 1424 | *pos = i_size + vol->mft_record_size; |
| 1425 | done: |
| 1426 | free(ia); |
| 1427 | free(bmp); |
| 1428 | if (bmp_na) |
| 1429 | ntfs_attr_close(bmp_na); |
| 1430 | if (ia_na) |
| 1431 | ntfs_attr_close(ia_na); |
| 1432 | ntfs_log_debug("EOD, *pos 0x%llx, returning 0.\n", (long long)*pos); |
| 1433 | return 0; |
| 1434 | dir_err_out: |
| 1435 | errno = EIO; |
| 1436 | err_out: |
| 1437 | eo = errno; |
| 1438 | ntfs_log_trace("failed.\n"); |
| 1439 | if (ctx) |
| 1440 | ntfs_attr_put_search_ctx(ctx); |
| 1441 | free(ia); |
| 1442 | free(bmp); |
| 1443 | if (bmp_na) |
| 1444 | ntfs_attr_close(bmp_na); |
| 1445 | if (ia_na) |
| 1446 | ntfs_attr_close(ia_na); |
| 1447 | errno = eo; |
| 1448 | return -1; |
| 1449 | } |
| 1450 | |
| 1451 | |
| 1452 | /** |
| 1453 | * __ntfs_create - create object on ntfs volume |
| 1454 | * @dir_ni: ntfs inode for directory in which create new object |
| 1455 | * @securid: id of inheritable security descriptor, 0 if none |
| 1456 | * @name: unicode name of new object |
| 1457 | * @name_len: length of the name in unicode characters |
| 1458 | * @type: type of the object to create |
| 1459 | * @dev: major and minor device numbers (obtained from makedev()) |
| 1460 | * @target: target in unicode (only for symlinks) |
| 1461 | * @target_len: length of target in unicode characters |
| 1462 | * |
| 1463 | * Internal, use ntfs_create{,_device,_symlink} wrappers instead. |
| 1464 | * |
| 1465 | * @type can be: |
| 1466 | * S_IFREG to create regular file |
| 1467 | * S_IFDIR to create directory |
| 1468 | * S_IFBLK to create block device |
| 1469 | * S_IFCHR to create character device |
| 1470 | * S_IFLNK to create symbolic link |
| 1471 | * S_IFIFO to create FIFO |
| 1472 | * S_IFSOCK to create socket |
| 1473 | * other values are invalid. |
| 1474 | * |
| 1475 | * @dev is used only if @type is S_IFBLK or S_IFCHR, in other cases its value |
| 1476 | * ignored. |
| 1477 | * |
| 1478 | * @target and @target_len are used only if @type is S_IFLNK, in other cases |
| 1479 | * their value ignored. |
| 1480 | * |
| 1481 | * Return opened ntfs inode that describes created object on success or NULL |
| 1482 | * on error with errno set to the error code. |
| 1483 | */ |
| 1484 | static ntfs_inode *__ntfs_create(ntfs_inode *dir_ni, le32 securid, |
| 1485 | const ntfschar *name, u8 name_len, mode_t type, dev_t dev, |
| 1486 | const ntfschar *target, int target_len) |
| 1487 | { |
| 1488 | ntfs_inode *ni; |
| 1489 | int rollback_data = 0, rollback_sd = 0; |
| 1490 | FILE_NAME_ATTR *fn = NULL; |
| 1491 | STANDARD_INFORMATION *si = NULL; |
| 1492 | int err, fn_len, si_len; |
| 1493 | |
| 1494 | ntfs_log_trace("Entering.\n"); |
| 1495 | |
| 1496 | /* Sanity checks. */ |
| 1497 | if (!dir_ni || !name || !name_len) { |
| 1498 | ntfs_log_error("Invalid arguments.\n"); |
| 1499 | errno = EINVAL; |
| 1500 | return NULL; |
| 1501 | } |
| 1502 | |
| 1503 | if (dir_ni->flags & FILE_ATTR_REPARSE_POINT) { |
| 1504 | errno = EOPNOTSUPP; |
| 1505 | return NULL; |
| 1506 | } |
| 1507 | |
| 1508 | ni = ntfs_mft_record_alloc(dir_ni->vol, NULL); |
| 1509 | if (!ni) |
| 1510 | return NULL; |
| 1511 | #if CACHE_NIDATA_SIZE |
| 1512 | ntfs_inode_invalidate(dir_ni->vol, ni->mft_no); |
| 1513 | #endif |
| 1514 | /* |
| 1515 | * Create STANDARD_INFORMATION attribute. |
| 1516 | * JPA Depending on available inherited security descriptor, |
| 1517 | * Write STANDARD_INFORMATION v1.2 (no inheritance) or v3 |
| 1518 | */ |
| 1519 | if (securid) |
| 1520 | si_len = sizeof(STANDARD_INFORMATION); |
| 1521 | else |
| 1522 | si_len = offsetof(STANDARD_INFORMATION, v1_end); |
| 1523 | si = ntfs_calloc(si_len); |
| 1524 | if (!si) { |
| 1525 | err = errno; |
| 1526 | goto err_out; |
| 1527 | } |
| 1528 | si->creation_time = ni->creation_time; |
| 1529 | si->last_data_change_time = ni->last_data_change_time; |
| 1530 | si->last_mft_change_time = ni->last_mft_change_time; |
| 1531 | si->last_access_time = ni->last_access_time; |
| 1532 | if (securid) { |
| 1533 | set_nino_flag(ni, v3_Extensions); |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1534 | ni->owner_id = si->owner_id = const_cpu_to_le32(0); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1535 | ni->security_id = si->security_id = securid; |
| 1536 | ni->quota_charged = si->quota_charged = const_cpu_to_le64(0); |
| 1537 | ni->usn = si->usn = const_cpu_to_le64(0); |
| 1538 | } else |
| 1539 | clear_nino_flag(ni, v3_Extensions); |
| 1540 | if (!S_ISREG(type) && !S_ISDIR(type)) { |
| 1541 | si->file_attributes = FILE_ATTR_SYSTEM; |
| 1542 | ni->flags = FILE_ATTR_SYSTEM; |
| 1543 | } |
| 1544 | ni->flags |= FILE_ATTR_ARCHIVE; |
| 1545 | if (NVolHideDotFiles(dir_ni->vol) |
| 1546 | && (name_len > 1) |
| 1547 | && (name[0] == const_cpu_to_le16('.')) |
| 1548 | && (name[1] != const_cpu_to_le16('.'))) |
| 1549 | ni->flags |= FILE_ATTR_HIDDEN; |
| 1550 | /* |
| 1551 | * Set compression flag according to parent directory |
| 1552 | * unless NTFS version < 3.0 or cluster size > 4K |
| 1553 | * or compression has been disabled |
| 1554 | */ |
| 1555 | if ((dir_ni->flags & FILE_ATTR_COMPRESSED) |
| 1556 | && (dir_ni->vol->major_ver >= 3) |
| 1557 | && NVolCompression(dir_ni->vol) |
| 1558 | && (dir_ni->vol->cluster_size <= MAX_COMPRESSION_CLUSTER_SIZE) |
| 1559 | && (S_ISREG(type) || S_ISDIR(type))) |
| 1560 | ni->flags |= FILE_ATTR_COMPRESSED; |
| 1561 | /* Add STANDARD_INFORMATION to inode. */ |
| 1562 | if (ntfs_attr_add(ni, AT_STANDARD_INFORMATION, AT_UNNAMED, 0, |
| 1563 | (u8*)si, si_len)) { |
| 1564 | err = errno; |
| 1565 | ntfs_log_error("Failed to add STANDARD_INFORMATION " |
| 1566 | "attribute.\n"); |
| 1567 | goto err_out; |
| 1568 | } |
| 1569 | |
| 1570 | if (!securid) { |
| 1571 | if (ntfs_sd_add_everyone(ni)) { |
| 1572 | err = errno; |
| 1573 | goto err_out; |
| 1574 | } |
| 1575 | } |
| 1576 | rollback_sd = 1; |
| 1577 | |
| 1578 | if (S_ISDIR(type)) { |
| 1579 | INDEX_ROOT *ir = NULL; |
| 1580 | INDEX_ENTRY *ie; |
| 1581 | int ir_len, index_len; |
| 1582 | |
| 1583 | /* Create INDEX_ROOT attribute. */ |
| 1584 | index_len = sizeof(INDEX_HEADER) + sizeof(INDEX_ENTRY_HEADER); |
| 1585 | ir_len = offsetof(INDEX_ROOT, index) + index_len; |
| 1586 | ir = ntfs_calloc(ir_len); |
| 1587 | if (!ir) { |
| 1588 | err = errno; |
| 1589 | goto err_out; |
| 1590 | } |
| 1591 | ir->type = AT_FILE_NAME; |
| 1592 | ir->collation_rule = COLLATION_FILE_NAME; |
| 1593 | ir->index_block_size = cpu_to_le32(ni->vol->indx_record_size); |
| 1594 | if (ni->vol->cluster_size <= ni->vol->indx_record_size) |
| 1595 | ir->clusters_per_index_block = |
| 1596 | ni->vol->indx_record_size >> |
| 1597 | ni->vol->cluster_size_bits; |
| 1598 | else |
| 1599 | ir->clusters_per_index_block = |
| 1600 | ni->vol->indx_record_size >> |
| 1601 | NTFS_BLOCK_SIZE_BITS; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1602 | ir->index.entries_offset = const_cpu_to_le32(sizeof(INDEX_HEADER)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1603 | ir->index.index_length = cpu_to_le32(index_len); |
| 1604 | ir->index.allocated_size = cpu_to_le32(index_len); |
| 1605 | ie = (INDEX_ENTRY*)((u8*)ir + sizeof(INDEX_ROOT)); |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1606 | ie->length = const_cpu_to_le16(sizeof(INDEX_ENTRY_HEADER)); |
| 1607 | ie->key_length = const_cpu_to_le16(0); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1608 | ie->ie_flags = INDEX_ENTRY_END; |
| 1609 | /* Add INDEX_ROOT attribute to inode. */ |
| 1610 | if (ntfs_attr_add(ni, AT_INDEX_ROOT, NTFS_INDEX_I30, 4, |
| 1611 | (u8*)ir, ir_len)) { |
| 1612 | err = errno; |
| 1613 | free(ir); |
| 1614 | ntfs_log_error("Failed to add INDEX_ROOT attribute.\n"); |
| 1615 | goto err_out; |
| 1616 | } |
| 1617 | free(ir); |
| 1618 | } else { |
| 1619 | INTX_FILE *data; |
| 1620 | int data_len; |
| 1621 | |
| 1622 | switch (type) { |
| 1623 | case S_IFBLK: |
| 1624 | case S_IFCHR: |
| 1625 | data_len = offsetof(INTX_FILE, device_end); |
| 1626 | data = ntfs_malloc(data_len); |
| 1627 | if (!data) { |
| 1628 | err = errno; |
| 1629 | goto err_out; |
| 1630 | } |
| 1631 | data->major = cpu_to_le64(major(dev)); |
| 1632 | data->minor = cpu_to_le64(minor(dev)); |
| 1633 | if (type == S_IFBLK) |
| 1634 | data->magic = INTX_BLOCK_DEVICE; |
| 1635 | if (type == S_IFCHR) |
| 1636 | data->magic = INTX_CHARACTER_DEVICE; |
| 1637 | break; |
| 1638 | case S_IFLNK: |
| 1639 | data_len = sizeof(INTX_FILE_TYPES) + |
| 1640 | target_len * sizeof(ntfschar); |
| 1641 | data = ntfs_malloc(data_len); |
| 1642 | if (!data) { |
| 1643 | err = errno; |
| 1644 | goto err_out; |
| 1645 | } |
| 1646 | data->magic = INTX_SYMBOLIC_LINK; |
| 1647 | memcpy(data->target, target, |
| 1648 | target_len * sizeof(ntfschar)); |
| 1649 | break; |
| 1650 | case S_IFSOCK: |
| 1651 | data = NULL; |
| 1652 | data_len = 1; |
| 1653 | break; |
| 1654 | default: /* FIFO or regular file. */ |
| 1655 | data = NULL; |
| 1656 | data_len = 0; |
| 1657 | break; |
| 1658 | } |
| 1659 | /* Add DATA attribute to inode. */ |
| 1660 | if (ntfs_attr_add(ni, AT_DATA, AT_UNNAMED, 0, (u8*)data, |
| 1661 | data_len)) { |
| 1662 | err = errno; |
| 1663 | ntfs_log_error("Failed to add DATA attribute.\n"); |
| 1664 | free(data); |
| 1665 | goto err_out; |
| 1666 | } |
| 1667 | rollback_data = 1; |
| 1668 | free(data); |
| 1669 | } |
| 1670 | /* Create FILE_NAME attribute. */ |
| 1671 | fn_len = sizeof(FILE_NAME_ATTR) + name_len * sizeof(ntfschar); |
| 1672 | fn = ntfs_calloc(fn_len); |
| 1673 | if (!fn) { |
| 1674 | err = errno; |
| 1675 | goto err_out; |
| 1676 | } |
| 1677 | fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, |
| 1678 | le16_to_cpu(dir_ni->mrec->sequence_number)); |
| 1679 | fn->file_name_length = name_len; |
| 1680 | fn->file_name_type = FILE_NAME_POSIX; |
| 1681 | if (S_ISDIR(type)) |
| 1682 | fn->file_attributes = FILE_ATTR_I30_INDEX_PRESENT; |
| 1683 | if (!S_ISREG(type) && !S_ISDIR(type)) |
| 1684 | fn->file_attributes = FILE_ATTR_SYSTEM; |
| 1685 | else |
| 1686 | fn->file_attributes |= ni->flags & FILE_ATTR_COMPRESSED; |
| 1687 | fn->file_attributes |= FILE_ATTR_ARCHIVE; |
| 1688 | fn->file_attributes |= ni->flags & FILE_ATTR_HIDDEN; |
| 1689 | fn->creation_time = ni->creation_time; |
| 1690 | fn->last_data_change_time = ni->last_data_change_time; |
| 1691 | fn->last_mft_change_time = ni->last_mft_change_time; |
| 1692 | fn->last_access_time = ni->last_access_time; |
| 1693 | if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1694 | fn->data_size = fn->allocated_size = const_cpu_to_sle64(0); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1695 | else { |
| 1696 | fn->data_size = cpu_to_sle64(ni->data_size); |
| 1697 | fn->allocated_size = cpu_to_sle64(ni->allocated_size); |
| 1698 | } |
| 1699 | memcpy(fn->file_name, name, name_len * sizeof(ntfschar)); |
| 1700 | /* Add FILE_NAME attribute to inode. */ |
| 1701 | if (ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8*)fn, fn_len)) { |
| 1702 | err = errno; |
| 1703 | ntfs_log_error("Failed to add FILE_NAME attribute.\n"); |
| 1704 | goto err_out; |
| 1705 | } |
| 1706 | /* Add FILE_NAME attribute to index. */ |
| 1707 | if (ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no, |
| 1708 | le16_to_cpu(ni->mrec->sequence_number)))) { |
| 1709 | err = errno; |
| 1710 | ntfs_log_perror("Failed to add entry to the index"); |
| 1711 | goto err_out; |
| 1712 | } |
| 1713 | /* Set hard links count and directory flag. */ |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1714 | ni->mrec->link_count = const_cpu_to_le16(1); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1715 | if (S_ISDIR(type)) |
| 1716 | ni->mrec->flags |= MFT_RECORD_IS_DIRECTORY; |
| 1717 | ntfs_inode_mark_dirty(ni); |
| 1718 | /* Done! */ |
| 1719 | free(fn); |
| 1720 | free(si); |
| 1721 | ntfs_log_trace("Done.\n"); |
| 1722 | return ni; |
| 1723 | err_out: |
| 1724 | ntfs_log_trace("Failed.\n"); |
| 1725 | |
| 1726 | if (rollback_sd) |
| 1727 | ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0); |
| 1728 | |
| 1729 | if (rollback_data) |
| 1730 | ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0); |
| 1731 | /* |
| 1732 | * Free extent MFT records (should not exist any with current |
| 1733 | * ntfs_create implementation, but for any case if something will be |
| 1734 | * changed in the future). |
| 1735 | */ |
| 1736 | while (ni->nr_extents) |
| 1737 | if (ntfs_mft_record_free(ni->vol, *(ni->extent_nis))) { |
| 1738 | err = errno; |
| 1739 | ntfs_log_error("Failed to free extent MFT record. " |
| 1740 | "Leaving inconsistent metadata.\n"); |
| 1741 | } |
| 1742 | if (ntfs_mft_record_free(ni->vol, ni)) |
| 1743 | ntfs_log_error("Failed to free MFT record. " |
| 1744 | "Leaving inconsistent metadata. Run chkdsk.\n"); |
| 1745 | free(fn); |
| 1746 | free(si); |
| 1747 | errno = err; |
| 1748 | return NULL; |
| 1749 | } |
| 1750 | |
| 1751 | /** |
| 1752 | * Some wrappers around __ntfs_create() ... |
| 1753 | */ |
| 1754 | |
| 1755 | ntfs_inode *ntfs_create(ntfs_inode *dir_ni, le32 securid, const ntfschar *name, |
| 1756 | u8 name_len, mode_t type) |
| 1757 | { |
| 1758 | if (type != S_IFREG && type != S_IFDIR && type != S_IFIFO && |
| 1759 | type != S_IFSOCK) { |
| 1760 | ntfs_log_error("Invalid arguments.\n"); |
| 1761 | return NULL; |
| 1762 | } |
| 1763 | return __ntfs_create(dir_ni, securid, name, name_len, type, 0, NULL, 0); |
| 1764 | } |
| 1765 | |
| 1766 | ntfs_inode *ntfs_create_device(ntfs_inode *dir_ni, le32 securid, |
| 1767 | const ntfschar *name, u8 name_len, mode_t type, dev_t dev) |
| 1768 | { |
| 1769 | if (type != S_IFCHR && type != S_IFBLK) { |
| 1770 | ntfs_log_error("Invalid arguments.\n"); |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | return __ntfs_create(dir_ni, securid, name, name_len, type, dev, NULL, 0); |
| 1774 | } |
| 1775 | |
| 1776 | ntfs_inode *ntfs_create_symlink(ntfs_inode *dir_ni, le32 securid, |
| 1777 | const ntfschar *name, u8 name_len, const ntfschar *target, |
| 1778 | int target_len) |
| 1779 | { |
| 1780 | if (!target || !target_len) { |
| 1781 | ntfs_log_error("%s: Invalid argument (%p, %d)\n", __FUNCTION__, |
| 1782 | target, target_len); |
| 1783 | return NULL; |
| 1784 | } |
| 1785 | return __ntfs_create(dir_ni, securid, name, name_len, S_IFLNK, 0, |
| 1786 | target, target_len); |
| 1787 | } |
| 1788 | |
| 1789 | int ntfs_check_empty_dir(ntfs_inode *ni) |
| 1790 | { |
| 1791 | ntfs_attr *na; |
| 1792 | int ret = 0; |
| 1793 | |
| 1794 | if (!(ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)) |
| 1795 | return 0; |
| 1796 | |
| 1797 | na = ntfs_attr_open(ni, AT_INDEX_ROOT, NTFS_INDEX_I30, 4); |
| 1798 | if (!na) { |
| 1799 | errno = EIO; |
| 1800 | ntfs_log_perror("Failed to open directory"); |
| 1801 | return -1; |
| 1802 | } |
| 1803 | |
| 1804 | /* Non-empty directory? */ |
| 1805 | if ((na->data_size != sizeof(INDEX_ROOT) + sizeof(INDEX_ENTRY_HEADER))){ |
| 1806 | /* Both ENOTEMPTY and EEXIST are ok. We use the more common. */ |
| 1807 | errno = ENOTEMPTY; |
| 1808 | ntfs_log_debug("Directory is not empty\n"); |
| 1809 | ret = -1; |
| 1810 | } |
| 1811 | |
| 1812 | ntfs_attr_close(na); |
| 1813 | return ret; |
| 1814 | } |
| 1815 | |
| 1816 | static int ntfs_check_unlinkable_dir(ntfs_inode *ni, FILE_NAME_ATTR *fn) |
| 1817 | { |
| 1818 | int link_count = le16_to_cpu(ni->mrec->link_count); |
| 1819 | int ret; |
| 1820 | |
| 1821 | ret = ntfs_check_empty_dir(ni); |
| 1822 | if (!ret || errno != ENOTEMPTY) |
| 1823 | return ret; |
| 1824 | /* |
| 1825 | * Directory is non-empty, so we can unlink only if there is more than |
| 1826 | * one "real" hard link, i.e. links aren't different DOS and WIN32 names |
| 1827 | */ |
| 1828 | if ((link_count == 1) || |
| 1829 | (link_count == 2 && fn->file_name_type == FILE_NAME_DOS)) { |
| 1830 | errno = ENOTEMPTY; |
| 1831 | ntfs_log_debug("Non-empty directory without hard links\n"); |
| 1832 | goto no_hardlink; |
| 1833 | } |
| 1834 | |
| 1835 | ret = 0; |
| 1836 | no_hardlink: |
| 1837 | return ret; |
| 1838 | } |
| 1839 | |
| 1840 | /** |
| 1841 | * ntfs_delete - delete file or directory from ntfs volume |
| 1842 | * @ni: ntfs inode for object to delte |
| 1843 | * @dir_ni: ntfs inode for directory in which delete object |
| 1844 | * @name: unicode name of the object to delete |
| 1845 | * @name_len: length of the name in unicode characters |
| 1846 | * |
| 1847 | * @ni is always closed after the call to this function (even if it failed), |
| 1848 | * user does not need to call ntfs_inode_close himself. |
| 1849 | * |
| 1850 | * Return 0 on success or -1 on error with errno set to the error code. |
| 1851 | */ |
| 1852 | int ntfs_delete(ntfs_volume *vol, const char *pathname, |
| 1853 | ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, |
| 1854 | u8 name_len) |
| 1855 | { |
| 1856 | ntfs_attr_search_ctx *actx = NULL; |
| 1857 | FILE_NAME_ATTR *fn = NULL; |
| 1858 | BOOL looking_for_dos_name = FALSE, looking_for_win32_name = FALSE; |
| 1859 | BOOL case_sensitive_match = TRUE; |
| 1860 | int err = 0; |
| 1861 | #if CACHE_NIDATA_SIZE |
| 1862 | int i; |
| 1863 | #endif |
| 1864 | #if CACHE_INODE_SIZE |
| 1865 | struct CACHED_INODE item; |
| 1866 | const char *p; |
| 1867 | u64 inum = (u64)-1; |
| 1868 | int count; |
| 1869 | #endif |
| 1870 | #if CACHE_LOOKUP_SIZE |
| 1871 | struct CACHED_LOOKUP lkitem; |
| 1872 | #endif |
| 1873 | |
| 1874 | ntfs_log_trace("Entering.\n"); |
| 1875 | |
| 1876 | if (!ni || !dir_ni || !name || !name_len) { |
| 1877 | ntfs_log_error("Invalid arguments.\n"); |
| 1878 | errno = EINVAL; |
| 1879 | goto err_out; |
| 1880 | } |
| 1881 | if (ni->nr_extents == -1) |
| 1882 | ni = ni->base_ni; |
| 1883 | if (dir_ni->nr_extents == -1) |
| 1884 | dir_ni = dir_ni->base_ni; |
| 1885 | /* |
| 1886 | * Search for FILE_NAME attribute with such name. If it's in POSIX or |
| 1887 | * WIN32_AND_DOS namespace, then simply remove it from index and inode. |
| 1888 | * If filename in DOS or in WIN32 namespace, then remove DOS name first, |
| 1889 | * only then remove WIN32 name. |
| 1890 | */ |
| 1891 | actx = ntfs_attr_get_search_ctx(ni, NULL); |
| 1892 | if (!actx) |
| 1893 | goto err_out; |
| 1894 | search: |
| 1895 | while (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, |
| 1896 | 0, NULL, 0, actx)) { |
| 1897 | char *s; |
| 1898 | IGNORE_CASE_BOOL case_sensitive = IGNORE_CASE; |
| 1899 | |
| 1900 | errno = 0; |
| 1901 | fn = (FILE_NAME_ATTR*)((u8*)actx->attr + |
| 1902 | le16_to_cpu(actx->attr->value_offset)); |
| 1903 | s = ntfs_attr_name_get(fn->file_name, fn->file_name_length); |
| 1904 | ntfs_log_trace("name: '%s' type: %d dos: %d win32: %d " |
| 1905 | "case: %d\n", s, fn->file_name_type, |
| 1906 | looking_for_dos_name, looking_for_win32_name, |
| 1907 | case_sensitive_match); |
| 1908 | ntfs_attr_name_free(&s); |
| 1909 | if (looking_for_dos_name) { |
| 1910 | if (fn->file_name_type == FILE_NAME_DOS) |
| 1911 | break; |
| 1912 | else |
| 1913 | continue; |
| 1914 | } |
| 1915 | if (looking_for_win32_name) { |
| 1916 | if (fn->file_name_type == FILE_NAME_WIN32) |
| 1917 | break; |
| 1918 | else |
| 1919 | continue; |
| 1920 | } |
| 1921 | |
| 1922 | /* Ignore hard links from other directories */ |
| 1923 | if (dir_ni->mft_no != MREF_LE(fn->parent_directory)) { |
| 1924 | ntfs_log_debug("MFT record numbers don't match " |
| 1925 | "(%llu != %llu)\n", |
| 1926 | (long long unsigned)dir_ni->mft_no, |
| 1927 | (long long unsigned)MREF_LE(fn->parent_directory)); |
| 1928 | continue; |
| 1929 | } |
| 1930 | if (case_sensitive_match |
| 1931 | || ((fn->file_name_type == FILE_NAME_POSIX) |
| 1932 | && NVolCaseSensitive(ni->vol))) |
| 1933 | case_sensitive = CASE_SENSITIVE; |
| 1934 | |
| 1935 | if (ntfs_names_are_equal(fn->file_name, fn->file_name_length, |
| 1936 | name, name_len, case_sensitive, |
| 1937 | ni->vol->upcase, ni->vol->upcase_len)){ |
| 1938 | |
| 1939 | if (fn->file_name_type == FILE_NAME_WIN32) { |
| 1940 | looking_for_dos_name = TRUE; |
| 1941 | ntfs_attr_reinit_search_ctx(actx); |
| 1942 | continue; |
| 1943 | } |
| 1944 | if (fn->file_name_type == FILE_NAME_DOS) |
| 1945 | looking_for_dos_name = TRUE; |
| 1946 | break; |
| 1947 | } |
| 1948 | } |
| 1949 | if (errno) { |
| 1950 | /* |
| 1951 | * If case sensitive search failed, then try once again |
| 1952 | * ignoring case. |
| 1953 | */ |
| 1954 | if (errno == ENOENT && case_sensitive_match) { |
| 1955 | case_sensitive_match = FALSE; |
| 1956 | ntfs_attr_reinit_search_ctx(actx); |
| 1957 | goto search; |
| 1958 | } |
| 1959 | goto err_out; |
| 1960 | } |
| 1961 | |
| 1962 | if (ntfs_check_unlinkable_dir(ni, fn) < 0) |
| 1963 | goto err_out; |
| 1964 | |
| 1965 | if (ntfs_index_remove(dir_ni, ni, fn, le32_to_cpu(actx->attr->value_length))) |
| 1966 | goto err_out; |
| 1967 | |
| 1968 | /* |
| 1969 | * Keep the last name in place, this is useful for undeletion |
| 1970 | * (Windows also does so), however delete the name if it were |
| 1971 | * in an extent, to avoid leaving an attribute list. |
| 1972 | */ |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1973 | if ((ni->mrec->link_count == const_cpu_to_le16(1)) && !actx->base_ntfs_ino) { |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1974 | /* make sure to not loop to another search */ |
| 1975 | looking_for_dos_name = FALSE; |
| 1976 | } else { |
| 1977 | if (ntfs_attr_record_rm(actx)) |
| 1978 | goto err_out; |
| 1979 | } |
| 1980 | |
| 1981 | ni->mrec->link_count = cpu_to_le16(le16_to_cpu( |
| 1982 | ni->mrec->link_count) - 1); |
| 1983 | |
| 1984 | ntfs_inode_mark_dirty(ni); |
| 1985 | if (looking_for_dos_name) { |
| 1986 | looking_for_dos_name = FALSE; |
| 1987 | looking_for_win32_name = TRUE; |
| 1988 | ntfs_attr_reinit_search_ctx(actx); |
| 1989 | goto search; |
| 1990 | } |
| 1991 | /* TODO: Update object id, quota and securiry indexes if required. */ |
| 1992 | /* |
| 1993 | * If hard link count is not equal to zero then we are done. In other |
| 1994 | * case there are no reference to this inode left, so we should free all |
| 1995 | * non-resident attributes and mark all MFT record as not in use. |
| 1996 | */ |
| 1997 | #if CACHE_LOOKUP_SIZE |
| 1998 | /* invalidate entry in lookup cache */ |
| 1999 | lkitem.name = (const char*)NULL; |
| 2000 | lkitem.namesize = 0; |
| 2001 | lkitem.inum = ni->mft_no; |
| 2002 | lkitem.parent = dir_ni->mft_no; |
| 2003 | ntfs_invalidate_cache(vol->lookup_cache, GENERIC(&lkitem), |
| 2004 | lookup_cache_inv_compare, CACHE_NOHASH); |
| 2005 | #endif |
| 2006 | #if CACHE_INODE_SIZE |
| 2007 | inum = ni->mft_no; |
| 2008 | if (pathname) { |
| 2009 | /* invalide cache entry, even if there was an error */ |
| 2010 | /* Remove leading /'s. */ |
| 2011 | p = pathname; |
| 2012 | while (*p == PATH_SEP) |
| 2013 | p++; |
| 2014 | if (p[0] && (p[strlen(p)-1] == PATH_SEP)) |
| 2015 | ntfs_log_error("Unnormalized path %s\n",pathname); |
| 2016 | item.pathname = p; |
| 2017 | item.varsize = strlen(p); |
| 2018 | } else { |
| 2019 | item.pathname = (const char*)NULL; |
| 2020 | item.varsize = 0; |
| 2021 | } |
| 2022 | item.inum = inum; |
| 2023 | count = ntfs_invalidate_cache(vol->xinode_cache, GENERIC(&item), |
| 2024 | inode_cache_inv_compare, CACHE_NOHASH); |
| 2025 | if (pathname && !count) |
| 2026 | ntfs_log_error("Could not delete inode cache entry for %s\n", |
| 2027 | pathname); |
| 2028 | #endif |
| 2029 | if (ni->mrec->link_count) { |
| 2030 | ntfs_inode_update_times(ni, NTFS_UPDATE_CTIME); |
| 2031 | goto ok; |
| 2032 | } |
| 2033 | if (ntfs_delete_reparse_index(ni)) { |
| 2034 | /* |
| 2035 | * Failed to remove the reparse index : proceed anyway |
| 2036 | * This is not a critical error, the entry is useless |
| 2037 | * because of sequence_number, and stopping file deletion |
| 2038 | * would be much worse as the file is not referenced now. |
| 2039 | */ |
| 2040 | err = errno; |
| 2041 | } |
| 2042 | if (ntfs_delete_object_id_index(ni)) { |
| 2043 | /* |
| 2044 | * Failed to remove the object id index : proceed anyway |
| 2045 | * This is not a critical error. |
| 2046 | */ |
| 2047 | err = errno; |
| 2048 | } |
| 2049 | ntfs_attr_reinit_search_ctx(actx); |
| 2050 | while (!ntfs_attrs_walk(actx)) { |
| 2051 | if (actx->attr->non_resident) { |
| 2052 | runlist *rl; |
| 2053 | |
| 2054 | rl = ntfs_mapping_pairs_decompress(ni->vol, actx->attr, |
| 2055 | NULL); |
| 2056 | if (!rl) { |
| 2057 | err = errno; |
| 2058 | ntfs_log_error("Failed to decompress runlist. " |
| 2059 | "Leaving inconsistent metadata.\n"); |
| 2060 | continue; |
| 2061 | } |
| 2062 | if (ntfs_cluster_free_from_rl(ni->vol, rl)) { |
| 2063 | err = errno; |
| 2064 | ntfs_log_error("Failed to free clusters. " |
| 2065 | "Leaving inconsistent metadata.\n"); |
| 2066 | continue; |
| 2067 | } |
| 2068 | free(rl); |
| 2069 | } |
| 2070 | } |
| 2071 | if (errno != ENOENT) { |
| 2072 | err = errno; |
| 2073 | ntfs_log_error("Attribute enumeration failed. " |
| 2074 | "Probably leaving inconsistent metadata.\n"); |
| 2075 | } |
| 2076 | /* All extents should be attached after attribute walk. */ |
| 2077 | #if CACHE_NIDATA_SIZE |
| 2078 | /* |
| 2079 | * Disconnect extents before deleting them, so they are |
| 2080 | * not wrongly moved to cache through the chainings |
| 2081 | */ |
| 2082 | for (i=ni->nr_extents-1; i>=0; i--) { |
| 2083 | ni->extent_nis[i]->base_ni = (ntfs_inode*)NULL; |
| 2084 | ni->extent_nis[i]->nr_extents = 0; |
| 2085 | if (ntfs_mft_record_free(ni->vol, ni->extent_nis[i])) { |
| 2086 | err = errno; |
| 2087 | ntfs_log_error("Failed to free extent MFT record. " |
| 2088 | "Leaving inconsistent metadata.\n"); |
| 2089 | } |
| 2090 | } |
| 2091 | free(ni->extent_nis); |
| 2092 | ni->nr_extents = 0; |
| 2093 | ni->extent_nis = (ntfs_inode**)NULL; |
| 2094 | #else |
| 2095 | while (ni->nr_extents) |
| 2096 | if (ntfs_mft_record_free(ni->vol, *(ni->extent_nis))) { |
| 2097 | err = errno; |
| 2098 | ntfs_log_error("Failed to free extent MFT record. " |
| 2099 | "Leaving inconsistent metadata.\n"); |
| 2100 | } |
| 2101 | #endif |
| 2102 | debug_double_inode(ni->mft_no,0); |
| 2103 | if (ntfs_mft_record_free(ni->vol, ni)) { |
| 2104 | err = errno; |
| 2105 | ntfs_log_error("Failed to free base MFT record. " |
| 2106 | "Leaving inconsistent metadata.\n"); |
| 2107 | } |
| 2108 | ni = NULL; |
| 2109 | ok: |
| 2110 | ntfs_inode_update_times(dir_ni, NTFS_UPDATE_MCTIME); |
| 2111 | out: |
| 2112 | if (actx) |
| 2113 | ntfs_attr_put_search_ctx(actx); |
| 2114 | if (ntfs_inode_close(dir_ni) && !err) |
| 2115 | err = errno; |
| 2116 | if (ntfs_inode_close(ni) && !err) |
| 2117 | err = errno; |
| 2118 | if (err) { |
| 2119 | errno = err; |
| 2120 | ntfs_log_debug("Could not delete file: %s\n", strerror(errno)); |
| 2121 | return -1; |
| 2122 | } |
| 2123 | ntfs_log_trace("Done.\n"); |
| 2124 | return 0; |
| 2125 | err_out: |
| 2126 | err = errno; |
| 2127 | goto out; |
| 2128 | } |
| 2129 | |
| 2130 | /** |
| 2131 | * ntfs_link - create hard link for file or directory |
| 2132 | * @ni: ntfs inode for object to create hard link |
| 2133 | * @dir_ni: ntfs inode for directory in which new link should be placed |
| 2134 | * @name: unicode name of the new link |
| 2135 | * @name_len: length of the name in unicode characters |
| 2136 | * |
| 2137 | * NOTE: At present we allow creating hardlinks to directories, we use them |
| 2138 | * in a temporary state during rename. But it's defenitely bad idea to have |
| 2139 | * hard links to directories as a result of operation. |
| 2140 | * FIXME: Create internal __ntfs_link that allows hard links to a directories |
| 2141 | * and external ntfs_link that do not. Write ntfs_rename that uses __ntfs_link. |
| 2142 | * |
| 2143 | * Return 0 on success or -1 on error with errno set to the error code. |
| 2144 | */ |
| 2145 | static int ntfs_link_i(ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, |
| 2146 | u8 name_len, FILE_NAME_TYPE_FLAGS nametype) |
| 2147 | { |
| 2148 | FILE_NAME_ATTR *fn = NULL; |
| 2149 | int fn_len, err; |
| 2150 | |
| 2151 | ntfs_log_trace("Entering.\n"); |
| 2152 | |
| 2153 | if (!ni || !dir_ni || !name || !name_len || |
| 2154 | ni->mft_no == dir_ni->mft_no) { |
| 2155 | err = EINVAL; |
| 2156 | ntfs_log_perror("ntfs_link wrong arguments"); |
| 2157 | goto err_out; |
| 2158 | } |
| 2159 | |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 2160 | if (NVolHideDotFiles(dir_ni->vol)) { |
| 2161 | /* Set hidden flag according to the latest name */ |
| 2162 | if ((name_len > 1) |
| 2163 | && (name[0] == const_cpu_to_le16('.')) |
| 2164 | && (name[1] != const_cpu_to_le16('.'))) |
| 2165 | ni->flags |= FILE_ATTR_HIDDEN; |
| 2166 | else |
| 2167 | ni->flags &= ~FILE_ATTR_HIDDEN; |
| 2168 | } |
| 2169 | |
| 2170 | /* Create FILE_NAME attribute. */ |
| 2171 | fn_len = sizeof(FILE_NAME_ATTR) + name_len * sizeof(ntfschar); |
| 2172 | fn = ntfs_calloc(fn_len); |
| 2173 | if (!fn) { |
| 2174 | err = errno; |
| 2175 | goto err_out; |
| 2176 | } |
| 2177 | fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, |
| 2178 | le16_to_cpu(dir_ni->mrec->sequence_number)); |
| 2179 | fn->file_name_length = name_len; |
| 2180 | fn->file_name_type = nametype; |
| 2181 | fn->file_attributes = ni->flags; |
| 2182 | if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { |
| 2183 | fn->file_attributes |= FILE_ATTR_I30_INDEX_PRESENT; |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 2184 | fn->data_size = fn->allocated_size = const_cpu_to_sle64(0); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 2185 | } else { |
| 2186 | fn->allocated_size = cpu_to_sle64(ni->allocated_size); |
| 2187 | fn->data_size = cpu_to_sle64(ni->data_size); |
| 2188 | } |
| 2189 | fn->creation_time = ni->creation_time; |
| 2190 | fn->last_data_change_time = ni->last_data_change_time; |
| 2191 | fn->last_mft_change_time = ni->last_mft_change_time; |
| 2192 | fn->last_access_time = ni->last_access_time; |
| 2193 | memcpy(fn->file_name, name, name_len * sizeof(ntfschar)); |
| 2194 | /* Add FILE_NAME attribute to index. */ |
| 2195 | if (ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no, |
| 2196 | le16_to_cpu(ni->mrec->sequence_number)))) { |
| 2197 | err = errno; |
| 2198 | ntfs_log_perror("Failed to add filename to the index"); |
| 2199 | goto err_out; |
| 2200 | } |
| 2201 | /* Add FILE_NAME attribute to inode. */ |
| 2202 | if (ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8*)fn, fn_len)) { |
| 2203 | ntfs_log_error("Failed to add FILE_NAME attribute.\n"); |
| 2204 | err = errno; |
| 2205 | /* Try to remove just added attribute from index. */ |
| 2206 | if (ntfs_index_remove(dir_ni, ni, fn, fn_len)) |
| 2207 | goto rollback_failed; |
| 2208 | goto err_out; |
| 2209 | } |
| 2210 | /* Increment hard links count. */ |
| 2211 | ni->mrec->link_count = cpu_to_le16(le16_to_cpu( |
| 2212 | ni->mrec->link_count) + 1); |
| 2213 | /* Done! */ |
| 2214 | ntfs_inode_mark_dirty(ni); |
| 2215 | free(fn); |
| 2216 | ntfs_log_trace("Done.\n"); |
| 2217 | return 0; |
| 2218 | rollback_failed: |
| 2219 | ntfs_log_error("Rollback failed. Leaving inconsistent metadata.\n"); |
| 2220 | err_out: |
| 2221 | free(fn); |
| 2222 | errno = err; |
| 2223 | return -1; |
| 2224 | } |
| 2225 | |
| 2226 | int ntfs_link(ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, |
| 2227 | u8 name_len) |
| 2228 | { |
| 2229 | return (ntfs_link_i(ni, dir_ni, name, name_len, FILE_NAME_POSIX)); |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * Get a parent directory from an inode entry |
| 2234 | * |
| 2235 | * This is only used in situations where the path used to access |
| 2236 | * the current file is not known for sure. The result may be different |
| 2237 | * from the path when the file is linked in several parent directories. |
| 2238 | * |
| 2239 | * Currently this is only used for translating ".." in the target |
| 2240 | * of a Vista relative symbolic link |
| 2241 | */ |
| 2242 | |
| 2243 | ntfs_inode *ntfs_dir_parent_inode(ntfs_inode *ni) |
| 2244 | { |
| 2245 | ntfs_inode *dir_ni = (ntfs_inode*)NULL; |
| 2246 | u64 inum; |
| 2247 | FILE_NAME_ATTR *fn; |
| 2248 | ntfs_attr_search_ctx *ctx; |
| 2249 | |
| 2250 | if (ni->mft_no != FILE_root) { |
| 2251 | /* find the name in the attributes */ |
| 2252 | ctx = ntfs_attr_get_search_ctx(ni, NULL); |
| 2253 | if (!ctx) |
| 2254 | return ((ntfs_inode*)NULL); |
| 2255 | |
| 2256 | if (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, |
| 2257 | CASE_SENSITIVE, 0, NULL, 0, ctx)) { |
| 2258 | /* We know this will always be resident. */ |
| 2259 | fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + |
| 2260 | le16_to_cpu(ctx->attr->value_offset)); |
| 2261 | inum = le64_to_cpu(fn->parent_directory); |
| 2262 | if (inum != (u64)-1) { |
| 2263 | dir_ni = ntfs_inode_open(ni->vol, MREF(inum)); |
| 2264 | } |
| 2265 | } |
| 2266 | ntfs_attr_put_search_ctx(ctx); |
| 2267 | } |
| 2268 | return (dir_ni); |
| 2269 | } |
| 2270 | |
| 2271 | #ifdef HAVE_SETXATTR |
| 2272 | |
| 2273 | #define MAX_DOS_NAME_LENGTH 12 |
| 2274 | |
| 2275 | /* |
| 2276 | * Get a DOS name for a file in designated directory |
| 2277 | * |
| 2278 | * Not allowed if there are several non-dos names (EMLINK) |
| 2279 | * |
| 2280 | * Returns size if found |
| 2281 | * 0 if not found |
| 2282 | * -1 if there was an error (described by errno) |
| 2283 | */ |
| 2284 | |
| 2285 | static int get_dos_name(ntfs_inode *ni, u64 dnum, ntfschar *dosname) |
| 2286 | { |
| 2287 | size_t outsize = 0; |
| 2288 | int namecount = 0; |
| 2289 | FILE_NAME_ATTR *fn; |
| 2290 | ntfs_attr_search_ctx *ctx; |
| 2291 | |
| 2292 | /* find the name in the attributes */ |
| 2293 | ctx = ntfs_attr_get_search_ctx(ni, NULL); |
| 2294 | if (!ctx) |
| 2295 | return -1; |
| 2296 | |
| 2297 | while (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, |
| 2298 | 0, NULL, 0, ctx)) { |
| 2299 | /* We know this will always be resident. */ |
| 2300 | fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + |
| 2301 | le16_to_cpu(ctx->attr->value_offset)); |
| 2302 | |
| 2303 | if (fn->file_name_type != FILE_NAME_DOS) |
| 2304 | namecount++; |
| 2305 | if ((fn->file_name_type & FILE_NAME_DOS) |
| 2306 | && (MREF_LE(fn->parent_directory) == dnum)) { |
| 2307 | /* |
| 2308 | * Found a DOS or WIN32+DOS name for the entry |
| 2309 | * copy name, after truncation for safety |
| 2310 | */ |
| 2311 | outsize = fn->file_name_length; |
| 2312 | /* TODO : reject if name is too long ? */ |
| 2313 | if (outsize > MAX_DOS_NAME_LENGTH) |
| 2314 | outsize = MAX_DOS_NAME_LENGTH; |
| 2315 | memcpy(dosname,fn->file_name,outsize*sizeof(ntfschar)); |
| 2316 | } |
| 2317 | } |
| 2318 | ntfs_attr_put_search_ctx(ctx); |
| 2319 | if ((outsize > 0) && (namecount > 1)) { |
| 2320 | outsize = -1; |
| 2321 | errno = EMLINK; /* this error implies there is a dos name */ |
| 2322 | } |
| 2323 | return (outsize); |
| 2324 | } |
| 2325 | |
| 2326 | |
| 2327 | /* |
| 2328 | * Get a long name for a file in designated directory |
| 2329 | * |
| 2330 | * Not allowed if there are several non-dos names (EMLINK) |
| 2331 | * |
| 2332 | * Returns size if found |
| 2333 | * 0 if not found |
| 2334 | * -1 if there was an error (described by errno) |
| 2335 | */ |
| 2336 | |
| 2337 | static int get_long_name(ntfs_inode *ni, u64 dnum, ntfschar *longname) |
| 2338 | { |
| 2339 | size_t outsize = 0; |
| 2340 | int namecount = 0; |
| 2341 | FILE_NAME_ATTR *fn; |
| 2342 | ntfs_attr_search_ctx *ctx; |
| 2343 | |
| 2344 | /* find the name in the attributes */ |
| 2345 | ctx = ntfs_attr_get_search_ctx(ni, NULL); |
| 2346 | if (!ctx) |
| 2347 | return -1; |
| 2348 | |
| 2349 | /* first search for WIN32 or DOS+WIN32 names */ |
| 2350 | while (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, |
| 2351 | 0, NULL, 0, ctx)) { |
| 2352 | /* We know this will always be resident. */ |
| 2353 | fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + |
| 2354 | le16_to_cpu(ctx->attr->value_offset)); |
| 2355 | |
| 2356 | if (fn->file_name_type != FILE_NAME_DOS) |
| 2357 | namecount++; |
| 2358 | if ((fn->file_name_type & FILE_NAME_WIN32) |
| 2359 | && (MREF_LE(fn->parent_directory) == dnum)) { |
| 2360 | /* |
| 2361 | * Found a WIN32 or WIN32+DOS name for the entry |
| 2362 | * copy name |
| 2363 | */ |
| 2364 | outsize = fn->file_name_length; |
| 2365 | memcpy(longname,fn->file_name,outsize*sizeof(ntfschar)); |
| 2366 | } |
| 2367 | } |
| 2368 | if (namecount > 1) { |
| 2369 | ntfs_attr_put_search_ctx(ctx); |
| 2370 | errno = EMLINK; |
| 2371 | return -1; |
| 2372 | } |
| 2373 | /* if not found search for POSIX names */ |
| 2374 | if (!outsize) { |
| 2375 | ntfs_attr_reinit_search_ctx(ctx); |
| 2376 | while (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, |
| 2377 | 0, NULL, 0, ctx)) { |
| 2378 | /* We know this will always be resident. */ |
| 2379 | fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + |
| 2380 | le16_to_cpu(ctx->attr->value_offset)); |
| 2381 | |
| 2382 | if ((fn->file_name_type == FILE_NAME_POSIX) |
| 2383 | && (MREF_LE(fn->parent_directory) == dnum)) { |
| 2384 | /* |
| 2385 | * Found a POSIX name for the entry |
| 2386 | * copy name |
| 2387 | */ |
| 2388 | outsize = fn->file_name_length; |
| 2389 | memcpy(longname,fn->file_name,outsize*sizeof(ntfschar)); |
| 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | ntfs_attr_put_search_ctx(ctx); |
| 2394 | return (outsize); |
| 2395 | } |
| 2396 | |
| 2397 | |
| 2398 | /* |
| 2399 | * Get the ntfs DOS name into an extended attribute |
| 2400 | */ |
| 2401 | |
| 2402 | int ntfs_get_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni, |
| 2403 | char *value, size_t size) |
| 2404 | { |
| 2405 | int outsize = 0; |
| 2406 | char *outname = (char*)NULL; |
| 2407 | u64 dnum; |
| 2408 | int doslen; |
| 2409 | ntfschar dosname[MAX_DOS_NAME_LENGTH]; |
| 2410 | |
| 2411 | dnum = dir_ni->mft_no; |
| 2412 | doslen = get_dos_name(ni, dnum, dosname); |
| 2413 | if (doslen > 0) { |
| 2414 | /* |
| 2415 | * Found a DOS name for the entry, make |
| 2416 | * uppercase and encode into the buffer |
| 2417 | * if there is enough space |
| 2418 | */ |
| 2419 | ntfs_name_upcase(dosname, doslen, |
| 2420 | ni->vol->upcase, ni->vol->upcase_len); |
| 2421 | if (ntfs_ucstombs(dosname, doslen, &outname, size) < 0) { |
| 2422 | ntfs_log_error("Cannot represent dosname in current locale.\n"); |
| 2423 | outsize = -errno; |
| 2424 | } else { |
| 2425 | outsize = strlen(outname); |
| 2426 | if (value && (outsize <= (int)size)) |
| 2427 | memcpy(value, outname, outsize); |
| 2428 | else |
| 2429 | if (size && (outsize > (int)size)) |
| 2430 | outsize = -ERANGE; |
| 2431 | free(outname); |
| 2432 | } |
| 2433 | } else { |
| 2434 | if (doslen == 0) |
| 2435 | errno = ENODATA; |
| 2436 | outsize = -errno; |
| 2437 | } |
| 2438 | return (outsize); |
| 2439 | } |
| 2440 | |
| 2441 | /* |
| 2442 | * Change the name space of an existing file or directory |
| 2443 | * |
| 2444 | * Returns the old namespace if successful |
| 2445 | * -1 if an error occurred (described by errno) |
| 2446 | */ |
| 2447 | |
| 2448 | static int set_namespace(ntfs_inode *ni, ntfs_inode *dir_ni, |
| 2449 | const ntfschar *name, int len, |
| 2450 | FILE_NAME_TYPE_FLAGS nametype) |
| 2451 | { |
| 2452 | ntfs_attr_search_ctx *actx; |
| 2453 | ntfs_index_context *icx; |
| 2454 | FILE_NAME_ATTR *fnx; |
| 2455 | FILE_NAME_ATTR *fn = NULL; |
| 2456 | BOOL found; |
| 2457 | int lkup; |
| 2458 | int ret; |
| 2459 | |
| 2460 | ret = -1; |
| 2461 | actx = ntfs_attr_get_search_ctx(ni, NULL); |
| 2462 | if (actx) { |
| 2463 | found = FALSE; |
| 2464 | do { |
| 2465 | lkup = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, |
| 2466 | CASE_SENSITIVE, 0, NULL, 0, actx); |
| 2467 | if (!lkup) { |
| 2468 | fn = (FILE_NAME_ATTR*)((u8*)actx->attr + |
| 2469 | le16_to_cpu(actx->attr->value_offset)); |
| 2470 | found = (MREF_LE(fn->parent_directory) |
| 2471 | == dir_ni->mft_no) |
| 2472 | && !memcmp(fn->file_name, name, |
| 2473 | len*sizeof(ntfschar)); |
| 2474 | } |
| 2475 | } while (!lkup && !found); |
| 2476 | if (found) { |
| 2477 | icx = ntfs_index_ctx_get(dir_ni, NTFS_INDEX_I30, 4); |
| 2478 | if (icx) { |
| 2479 | lkup = ntfs_index_lookup((char*)fn, len, icx); |
| 2480 | if (!lkup && icx->data && icx->data_len) { |
| 2481 | fnx = (FILE_NAME_ATTR*)icx->data; |
| 2482 | ret = fn->file_name_type; |
| 2483 | fn->file_name_type = nametype; |
| 2484 | fnx->file_name_type = nametype; |
| 2485 | ntfs_inode_mark_dirty(ni); |
| 2486 | ntfs_index_entry_mark_dirty(icx); |
| 2487 | } |
| 2488 | ntfs_index_ctx_put(icx); |
| 2489 | } |
| 2490 | } |
| 2491 | ntfs_attr_put_search_ctx(actx); |
| 2492 | } |
| 2493 | return (ret); |
| 2494 | } |
| 2495 | |
| 2496 | /* |
| 2497 | * Set a DOS name to a file and adjust name spaces |
| 2498 | * |
| 2499 | * If the new names are collapsible (same uppercased chars) : |
| 2500 | * |
| 2501 | * - the existing DOS name or DOS+Win32 name is made Posix |
| 2502 | * - if it was a real DOS name, the existing long name is made DOS+Win32 |
| 2503 | * and the existing DOS name is deleted |
| 2504 | * - finally the existing long name is made DOS+Win32 unless already done |
| 2505 | * |
| 2506 | * If the new names are not collapsible : |
| 2507 | * |
| 2508 | * - insert the short name as a DOS name |
| 2509 | * - delete the old long name or existing short name |
| 2510 | * - insert the new long name (as a Win32 or DOS+Win32 name) |
| 2511 | * |
| 2512 | * Deleting the old long name will not delete the file |
| 2513 | * provided the old name was in the Posix name space, |
| 2514 | * because the alternate name has been set before. |
| 2515 | * |
| 2516 | * The inodes of file and parent directory are always closed |
| 2517 | * |
| 2518 | * Returns 0 if successful |
| 2519 | * -1 if failed |
| 2520 | */ |
| 2521 | |
| 2522 | static int set_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni, |
| 2523 | const ntfschar *shortname, int shortlen, |
| 2524 | const ntfschar *longname, int longlen, |
| 2525 | const ntfschar *deletename, int deletelen, BOOL existed) |
| 2526 | { |
| 2527 | unsigned int linkcount; |
| 2528 | ntfs_volume *vol; |
| 2529 | BOOL collapsible; |
| 2530 | BOOL deleted; |
| 2531 | BOOL done; |
| 2532 | FILE_NAME_TYPE_FLAGS oldnametype; |
| 2533 | u64 dnum; |
| 2534 | u64 fnum; |
| 2535 | int res; |
| 2536 | |
| 2537 | res = -1; |
| 2538 | vol = ni->vol; |
| 2539 | dnum = dir_ni->mft_no; |
| 2540 | fnum = ni->mft_no; |
| 2541 | /* save initial link count */ |
| 2542 | linkcount = le16_to_cpu(ni->mrec->link_count); |
| 2543 | |
| 2544 | /* check whether the same name may be used as DOS and WIN32 */ |
| 2545 | collapsible = ntfs_collapsible_chars(ni->vol, shortname, shortlen, |
| 2546 | longname, longlen); |
| 2547 | if (collapsible) { |
| 2548 | deleted = FALSE; |
| 2549 | done = FALSE; |
| 2550 | if (existed) { |
| 2551 | oldnametype = set_namespace(ni, dir_ni, deletename, |
| 2552 | deletelen, FILE_NAME_POSIX); |
| 2553 | if (oldnametype == FILE_NAME_DOS) { |
| 2554 | if (set_namespace(ni, dir_ni, longname, longlen, |
| 2555 | FILE_NAME_WIN32_AND_DOS) >= 0) { |
| 2556 | if (!ntfs_delete(vol, |
| 2557 | (const char*)NULL, ni, dir_ni, |
| 2558 | deletename, deletelen)) |
| 2559 | res = 0; |
| 2560 | deleted = TRUE; |
| 2561 | } else |
| 2562 | done = TRUE; |
| 2563 | } |
| 2564 | } |
| 2565 | if (!deleted) { |
| 2566 | if (!done && (set_namespace(ni, dir_ni, |
| 2567 | longname, longlen, |
| 2568 | FILE_NAME_WIN32_AND_DOS) >= 0)) |
| 2569 | res = 0; |
| 2570 | ntfs_inode_update_times(ni, NTFS_UPDATE_CTIME); |
| 2571 | ntfs_inode_update_times(dir_ni, NTFS_UPDATE_MCTIME); |
| 2572 | if (ntfs_inode_close_in_dir(ni,dir_ni) && !res) |
| 2573 | res = -1; |
| 2574 | if (ntfs_inode_close(dir_ni) && !res) |
| 2575 | res = -1; |
| 2576 | } |
| 2577 | } else { |
| 2578 | if (!ntfs_link_i(ni, dir_ni, shortname, shortlen, |
| 2579 | FILE_NAME_DOS) |
| 2580 | /* make sure a new link was recorded */ |
| 2581 | && (le16_to_cpu(ni->mrec->link_count) > linkcount)) { |
| 2582 | /* delete the existing long name or short name */ |
| 2583 | // is it ok to not provide the path ? |
| 2584 | if (!ntfs_delete(vol, (char*)NULL, ni, dir_ni, |
| 2585 | deletename, deletelen)) { |
| 2586 | /* delete closes the inodes, so have to open again */ |
| 2587 | dir_ni = ntfs_inode_open(vol, dnum); |
| 2588 | if (dir_ni) { |
| 2589 | ni = ntfs_inode_open(vol, fnum); |
| 2590 | if (ni) { |
| 2591 | if (!ntfs_link_i(ni, dir_ni, |
| 2592 | longname, longlen, |
| 2593 | FILE_NAME_WIN32)) |
| 2594 | res = 0; |
| 2595 | if (ntfs_inode_close_in_dir(ni, |
| 2596 | dir_ni) |
| 2597 | && !res) |
| 2598 | res = -1; |
| 2599 | } |
| 2600 | if (ntfs_inode_close(dir_ni) && !res) |
| 2601 | res = -1; |
| 2602 | } |
| 2603 | } |
| 2604 | } else { |
| 2605 | ntfs_inode_close_in_dir(ni,dir_ni); |
| 2606 | ntfs_inode_close(dir_ni); |
| 2607 | } |
| 2608 | } |
| 2609 | return (res); |
| 2610 | } |
| 2611 | |
| 2612 | |
| 2613 | /* |
| 2614 | * Set the ntfs DOS name into an extended attribute |
| 2615 | * |
| 2616 | * The DOS name will be added as another file name attribute |
| 2617 | * using the existing file name information from the original |
| 2618 | * name or overwriting the DOS Name if one exists. |
| 2619 | * |
| 2620 | * The inode of the file is always closed |
| 2621 | */ |
| 2622 | |
| 2623 | int ntfs_set_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni, |
| 2624 | const char *value, size_t size, int flags) |
| 2625 | { |
| 2626 | int res = 0; |
| 2627 | int longlen = 0; |
| 2628 | int shortlen = 0; |
| 2629 | char newname[3*MAX_DOS_NAME_LENGTH + 1]; |
| 2630 | ntfschar oldname[MAX_DOS_NAME_LENGTH]; |
| 2631 | int oldlen; |
| 2632 | u64 dnum; |
| 2633 | BOOL closed = FALSE; |
| 2634 | ntfschar *shortname = NULL; |
| 2635 | ntfschar longname[NTFS_MAX_NAME_LEN]; |
| 2636 | |
| 2637 | /* copy the string to insert a null char, and truncate */ |
| 2638 | if (size > 3*MAX_DOS_NAME_LENGTH) |
| 2639 | size = 3*MAX_DOS_NAME_LENGTH; |
| 2640 | strncpy(newname, value, size); |
| 2641 | /* a long name may be truncated badly and be untranslatable */ |
| 2642 | newname[size] = 0; |
| 2643 | /* convert the string to the NTFS wide chars, and truncate */ |
| 2644 | shortlen = ntfs_mbstoucs(newname, &shortname); |
| 2645 | if (shortlen > MAX_DOS_NAME_LENGTH) |
| 2646 | shortlen = MAX_DOS_NAME_LENGTH; |
| 2647 | /* make sure the short name has valid chars */ |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 2648 | if ((shortlen < 0) |
| 2649 | || ntfs_forbidden_names(ni->vol,shortname,shortlen)) { |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 2650 | ntfs_inode_close_in_dir(ni,dir_ni); |
| 2651 | ntfs_inode_close(dir_ni); |
| 2652 | res = -errno; |
| 2653 | return res; |
| 2654 | } |
| 2655 | dnum = dir_ni->mft_no; |
| 2656 | longlen = get_long_name(ni, dnum, longname); |
| 2657 | if (longlen > 0) { |
| 2658 | oldlen = get_dos_name(ni, dnum, oldname); |
| 2659 | if ((oldlen >= 0) |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 2660 | && !ntfs_forbidden_names(ni->vol, longname, longlen)) { |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 2661 | if (oldlen > 0) { |
| 2662 | if (flags & XATTR_CREATE) { |
| 2663 | res = -1; |
| 2664 | errno = EEXIST; |
| 2665 | } else |
| 2666 | if ((shortlen == oldlen) |
| 2667 | && !memcmp(shortname,oldname, |
| 2668 | oldlen*sizeof(ntfschar))) |
| 2669 | /* already set, done */ |
| 2670 | res = 0; |
| 2671 | else { |
| 2672 | res = set_dos_name(ni, dir_ni, |
| 2673 | shortname, shortlen, |
| 2674 | longname, longlen, |
| 2675 | oldname, oldlen, TRUE); |
| 2676 | closed = TRUE; |
| 2677 | } |
| 2678 | } else { |
| 2679 | if (flags & XATTR_REPLACE) { |
| 2680 | res = -1; |
| 2681 | errno = ENODATA; |
| 2682 | } else { |
| 2683 | res = set_dos_name(ni, dir_ni, |
| 2684 | shortname, shortlen, |
| 2685 | longname, longlen, |
| 2686 | longname, longlen, FALSE); |
| 2687 | closed = TRUE; |
| 2688 | } |
| 2689 | } |
| 2690 | } else |
| 2691 | res = -1; |
| 2692 | } else { |
| 2693 | res = -1; |
| 2694 | if (!longlen) |
| 2695 | errno = ENOENT; |
| 2696 | } |
| 2697 | free(shortname); |
| 2698 | if (!closed) { |
| 2699 | ntfs_inode_close_in_dir(ni,dir_ni); |
| 2700 | ntfs_inode_close(dir_ni); |
| 2701 | } |
| 2702 | return (res ? -1 : 0); |
| 2703 | } |
| 2704 | |
| 2705 | /* |
| 2706 | * Delete the ntfs DOS name |
| 2707 | */ |
| 2708 | |
| 2709 | int ntfs_remove_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni) |
| 2710 | { |
| 2711 | int res; |
| 2712 | int oldnametype; |
| 2713 | int longlen = 0; |
| 2714 | int shortlen; |
| 2715 | u64 dnum; |
| 2716 | ntfs_volume *vol; |
| 2717 | BOOL deleted = FALSE; |
| 2718 | ntfschar shortname[MAX_DOS_NAME_LENGTH]; |
| 2719 | ntfschar longname[NTFS_MAX_NAME_LEN]; |
| 2720 | |
| 2721 | res = -1; |
| 2722 | vol = ni->vol; |
| 2723 | dnum = dir_ni->mft_no; |
| 2724 | longlen = get_long_name(ni, dnum, longname); |
| 2725 | if (longlen > 0) { |
| 2726 | shortlen = get_dos_name(ni, dnum, shortname); |
| 2727 | if (shortlen >= 0) { |
| 2728 | /* migrate the long name as Posix */ |
| 2729 | oldnametype = set_namespace(ni,dir_ni,longname,longlen, |
| 2730 | FILE_NAME_POSIX); |
| 2731 | switch (oldnametype) { |
| 2732 | case FILE_NAME_WIN32_AND_DOS : |
| 2733 | /* name was Win32+DOS : done */ |
| 2734 | res = 0; |
| 2735 | break; |
| 2736 | case FILE_NAME_DOS : |
| 2737 | /* name was DOS, make it back to DOS */ |
| 2738 | set_namespace(ni,dir_ni,longname,longlen, |
| 2739 | FILE_NAME_DOS); |
| 2740 | errno = ENOENT; |
| 2741 | break; |
| 2742 | case FILE_NAME_WIN32 : |
| 2743 | /* name was Win32, make it Posix and delete */ |
| 2744 | if (set_namespace(ni,dir_ni,shortname,shortlen, |
| 2745 | FILE_NAME_POSIX) >= 0) { |
| 2746 | if (!ntfs_delete(vol, |
| 2747 | (const char*)NULL, ni, |
| 2748 | dir_ni, shortname, |
| 2749 | shortlen)) |
| 2750 | res = 0; |
| 2751 | deleted = TRUE; |
| 2752 | } else { |
| 2753 | /* |
| 2754 | * DOS name has been found, but cannot |
| 2755 | * migrate to Posix : something bad |
| 2756 | * has happened |
| 2757 | */ |
| 2758 | errno = EIO; |
| 2759 | ntfs_log_error("Could not change" |
| 2760 | " DOS name of inode %lld to Posix\n", |
| 2761 | (long long)ni->mft_no); |
| 2762 | } |
| 2763 | break; |
| 2764 | default : |
| 2765 | /* name was Posix or not found : error */ |
| 2766 | errno = ENOENT; |
| 2767 | break; |
| 2768 | } |
| 2769 | } |
| 2770 | } else { |
| 2771 | if (!longlen) |
| 2772 | errno = ENOENT; |
| 2773 | res = -1; |
| 2774 | } |
| 2775 | if (!deleted) { |
| 2776 | ntfs_inode_close_in_dir(ni,dir_ni); |
| 2777 | ntfs_inode_close(dir_ni); |
| 2778 | } |
| 2779 | return (res); |
| 2780 | } |
| 2781 | |
| 2782 | #endif |