2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
34 #include <unordered_map>
35 #include <unordered_set>
36 #include <dispatch/dispatch.h>
38 #include "DyldSharedCache.h"
42 #if BUILDING_CACHE_BUILDER
45 std::pair
<uint8_t*, struct stat
> cacheLoad(Diagnostics
& diags
, const std::string path
);
48 std::pair
<uint8_t*, struct stat
> fill(Diagnostics
& diags
, const std::string
& path
);
50 std::unordered_map
<std::string
, std::pair
<uint8_t*, struct stat
>> entries
;
51 dispatch_queue_t cache_queue
;
54 extern FileCache fileCache
;
58 // recursively walk all files in a directory tree
59 // symlinks are ignored
60 // dirFilter should return true on directories which should not be recursed into
61 // callback is called on each regular file found with stat() info about the file
63 void iterateDirectoryTree(const std::string
& pathPrefix
, const std::string
& path
, bool (^dirFilter
)(const std::string
& dirPath
),
64 void (^callback
)(const std::string
& path
, const struct stat
& statBuf
), bool processFiles
=true, bool recurse
=true);
68 // writes the buffer to a temp file, then renames the file to the final path
69 // returns true on success
71 bool safeSave(const void* buffer
, size_t bufferLen
, const std::string
& path
);
74 const void* mapFileReadOnly(const char* path
, size_t& mappedSize
);
76 bool isProtectedBySIP(const std::string
& path
);
77 bool isProtectedBySIPExceptDyld(const std::string
& path
);
78 bool isProtectedBySIP(int fd
);
80 bool fileExists(const std::string
& path
);
82 std::unordered_map
<std::string
, uint32_t> parseOrderFile(const std::string
& orderFileData
);
83 std::string
loadOrderFile(const std::string
& orderFilePath
);
85 std::string
normalize_absolute_file_path(std::string path
);
86 std::string
basePath(const std::string
& path
);
87 std::string
dirPath(const std::string
& path
);
88 std::string
realPath(const std::string
& path
);
89 std::string
realFilePath(const std::string
& path
);
91 std::string
toolDir();
93 #if BUILDING_CACHE_BUILDER
94 class SymlinkResolver
{
98 void addFile(Diagnostics
& diags
, std::string path
);
100 void addSymlink(Diagnostics
& diags
, std::string fromPath
, std::string toPath
);
102 std::string
realPath(Diagnostics
& diags
, const std::string
& path
) const;
104 std::vector
<DyldSharedCache::FileAlias
> getResolvedSymlinks(Diagnostics
& diags
);
107 std::set
<std::string
> filePaths
;
108 std::map
<std::string
, std::string
> symlinks
;
110 #endif // BUILDING_CACHE_BUILDER
113 #endif // FileUtils_h