]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfscommon/Catalog/FileIDsServices.c
xnu-2782.40.9.tar.gz
[apple/xnu.git] / bsd / hfs / hfscommon / Catalog / FileIDsServices.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b
A
28
29#include "../../hfs_macos_defs.h"
30#include "../../hfs_format.h"
31
32#include "../headers/FileMgrInternal.h"
33#include "../headers/HFSUnicodeWrappers.h"
34#include "../headers/CatalogPrivate.h"
316670eb
A
35#include <sys/kernel.h>
36#include <sys/malloc.h>
37#include <libkern/libkern.h>
1c79356b
A
38
39
40struct ExtentsRecBuffer {
9bccf70c 41 ExtentKey extentKey;
1c79356b
A
42 ExtentRecord extentData;
43};
44typedef struct ExtentsRecBuffer ExtentsRecBuffer;
45
46
2d21ac55 47static u_int32_t CheckExtents( void *extents, u_int32_t blocks, Boolean isHFSPlus );
6d2010ae
A
48static OSErr DeleteExtents( ExtendedVCB *vcb, u_int32_t fileNumber, int quitEarly, u_int8_t forkType, Boolean isHFSPlus );
49static OSErr MoveExtents( ExtendedVCB *vcb, u_int32_t srcFileID, u_int32_t destFileID, int quitEarly, u_int8_t forkType, Boolean isHFSPlus );
39236c6e
A
50
51#if CONFIG_HFS_STD
91447636 52static void CopyCatalogNodeInfo( CatalogRecord *src, CatalogRecord *dest );
39236c6e
A
53#endif
54
91447636 55static void CopyBigCatalogNodeInfo( CatalogRecord *src, CatalogRecord *dest );
2d21ac55 56static void CopyExtentInfo( ExtentKey *key, ExtentRecord *data, ExtentsRecBuffer *buffer, u_int16_t bufferCount );
1c79356b 57
6d2010ae
A
58/*
59 * This function moves the overflow extents associated with srcID into the file associated with dstID.
60 * We should have already verified that 'srcID' has overflow extents. So now we move all of the overflow
61 * extent records.
62 */
63OSErr MoveData( ExtendedVCB *vcb, HFSCatalogNodeID srcID, HFSCatalogNodeID destID, int rsrc) {
64
65 OSErr err;
66
67 /*
68 * Only the source file should have extents, so we just track those.
69 * We operate on the fork represented by the open FD that was used to call into this
70 * function
71 */
72 if (rsrc) {
73 /* Copy the extent overflow blocks. */
74 err = MoveExtents( vcb, srcID, destID, 1, (u_int8_t)0xff, 1);
75 if ( err != noErr ) {
76 if ( err != dskFulErr ) {
77 return( err );
78 }
79 /*
80 * In case of error, we would have probably run into problems
81 * growing the extents b-tree. Since the move is actually a copy + delete
82 * just delete the new entries. Same for below.
83 */
84 err = DeleteExtents( vcb, destID, 1, (u_int8_t)0xff, 1);
85 ReturnIfError( err ); // we are doomed. Just QUIT!
86 goto FlushAndReturn;
87 }
88 }
89 else {
90 /* Copy the extent overflow blocks. */
91 err = MoveExtents( vcb, srcID, destID, 1, 0, 1);
92 if ( err != noErr ) {
93 if ( err != dskFulErr ) {
94 return( err );
95 }
96 err = DeleteExtents( vcb, destID, 1, 0, 1);
97 ReturnIfError( err ); // we are doomed. Just QUIT!
98 goto FlushAndReturn;
99 }
100 }
101
102FlushAndReturn:
103 /* Write out the catalog and extent overflow B-Tree changes */
104 err = FlushCatalog( vcb );
105 err = FlushExtentFile( vcb );
106
107 return( err );
108}
1c79356b
A
109
110
2d21ac55 111OSErr ExchangeFileIDs( ExtendedVCB *vcb, ConstUTF8Param srcName, ConstUTF8Param destName, HFSCatalogNodeID srcID, HFSCatalogNodeID destID, u_int32_t srcHint, u_int32_t destHint )
1c79356b 112{
9bccf70c
A
113 CatalogKey srcKey; // 518 bytes
114 CatalogKey destKey; // 518 bytes
1c79356b 115 CatalogRecord srcData; // 520 bytes
1c79356b
A
116 CatalogRecord destData; // 520 bytes
117 CatalogRecord swapData; // 520 bytes
2d21ac55
A
118 int16_t numSrcExtentBlocks;
119 int16_t numDestExtentBlocks;
9bccf70c
A
120 OSErr err;
121 Boolean isHFSPlus = ( vcb->vcbSigWord == kHFSPlusSigWord );
316670eb 122
0b4e3aa0 123 err = BuildCatalogKeyUTF8(vcb, srcID, srcName, kUndefinedStrLen, &srcKey, NULL);
1c79356b 124 ReturnIfError(err);
316670eb 125
0b4e3aa0 126 err = BuildCatalogKeyUTF8(vcb, destID, destName, kUndefinedStrLen, &destKey, NULL);
1c79356b 127 ReturnIfError(err);
316670eb 128
1c79356b
A
129 if ( isHFSPlus )
130 {
131 //-- Step 1: Check the catalog nodes for extents
132
133 //-- locate the source file, test for extents in extent file, and copy the cat record for later
134 err = LocateCatalogNodeByKey( vcb, srcHint, &srcKey, &srcData, &srcHint );
135 ReturnIfError( err );
316670eb 136
1c79356b
A
137 if ( srcData.recordType != kHFSPlusFileRecord )
138 return( cmFThdDirErr ); // Error "cmFThdDirErr = it is a directory"
316670eb 139
1c79356b
A
140 //-- Check if there are any extents in the source file
141