]> git.saurik.com Git - apple/dyld.git/blob - dyld3/shared-cache/FileUtils.h
dyld-519.2.2.tar.gz
[apple/dyld.git] / dyld3 / shared-cache / FileUtils.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #ifndef FileUtils_h
26 #define FileUtils_h
27
28 #include <stdint.h>
29
30 #include <string>
31 #include <vector>
32 #include <unordered_map>
33 #include <unordered_set>
34 #include <dispatch/dispatch.h>
35
36 class Diagnostics;
37
38 #if BUILDING_CACHE_BUILDER
39 struct FileCache {
40 FileCache(void);
41 std::pair<uint8_t*, struct stat> cacheLoad(Diagnostics& diags, const std::string path);
42 void preflightCache(Diagnostics& diags, const std::string& path);
43 void preflightCache(Diagnostics& diags, const std::unordered_set<std::string>& paths);
44
45 private:
46 std::pair<uint8_t*, struct stat> fill(Diagnostics& diags, const std::string& path);
47
48 std::unordered_map<std::string, std::pair<uint8_t*, struct stat>> entries;
49 dispatch_queue_t cache_queue;
50 };
51
52 extern FileCache fileCache;
53 #endif
54
55 //
56 // recursively walk all files in a directory tree
57 // symlinks are ignored
58 // dirFilter should return true on directories which should not be recursed into
59 // callback is called on each regular file found with stat() info about the file
60 //
61 void iterateDirectoryTree(const std::string& pathPrefix, const std::string& path, bool (^dirFilter)(const std::string& dirPath),
62 void (^callback)(const std::string& path, const struct stat& statBuf), bool processFiles=true);
63
64
65 //
66 // writes the buffer to a temp file, then renames the file to the final path
67 // returns true on success
68 //
69 bool safeSave(const void* buffer, size_t bufferLen, const std::string& path);
70
71
72 const void* mapFileReadOnly(const std::string& path, size_t& mappedSize);
73
74 bool isProtectedBySIP(const std::string& path);
75 bool isProtectedBySIP(int fd);
76
77 bool fileExists(const std::string& path);
78
79 std::unordered_map<std::string, uint32_t> loadOrderFile(const std::string& orderFile);
80
81 std::string normalize_absolute_file_path(std::string path);
82 std::string basePath(const std::string& path);
83 std::string dirPath(const std::string& path);
84 std::string realPath(const std::string& path);
85 std::string realFilePath(const std::string& path);
86
87 std::string toolDir();
88
89
90
91
92 #endif // FileUtils_h