| 1 | /* |
| 2 | * Copyright (C) 2007-2017 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2008 Collabora, Ltd. All rights reserved. |
| 4 | * Copyright (C) 2015 Canon Inc. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
| 16 | * its contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 20 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include <time.h> |
| 34 | #include <utility> |
| 35 | #include <wtf/Forward.h> |
| 36 | #include <wtf/OptionSet.h> |
| 37 | #include <wtf/Vector.h> |
| 38 | #include <wtf/WallTime.h> |
| 39 | #include <wtf/text/WTFString.h> |
| 40 | |
| 41 | #if USE(CF) |
| 42 | #include <wtf/RetainPtr.h> |
| 43 | #endif |
| 44 | |
| 45 | #if USE(CF) |
| 46 | typedef const struct __CFData* CFDataRef; |
| 47 | #endif |
| 48 | |
| 49 | OBJC_CLASS NSString; |
| 50 | |
| 51 | #if OS(WINDOWS) |
| 52 | typedef void *HANDLE; |
| 53 | #endif |
| 54 | |
| 55 | #if USE(GLIB) |
| 56 | typedef struct _GFileIOStream GFileIOStream; |
| 57 | #endif |
| 58 | |
| 59 | namespace WTF { |
| 60 | |
| 61 | struct FileMetadata; |
| 62 | |
| 63 | namespace FileSystemImpl { |
| 64 | |
| 65 | // PlatformFileHandle |
| 66 | #if USE(GLIB) && !OS(WINDOWS) |
| 67 | typedef GFileIOStream* PlatformFileHandle; |
| 68 | const PlatformFileHandle invalidPlatformFileHandle = 0; |
| 69 | #elif OS(WINDOWS) |
| 70 | typedef HANDLE PlatformFileHandle; |
| 71 | // FIXME: -1 is INVALID_HANDLE_VALUE, defined in <winbase.h>. Chromium tries to |
| 72 | // avoid using Windows headers in headers. We'd rather move this into the .cpp. |
| 73 | const PlatformFileHandle invalidPlatformFileHandle = reinterpret_cast<HANDLE>(-1); |
| 74 | #else |
| 75 | typedef int PlatformFileHandle; |
| 76 | const PlatformFileHandle invalidPlatformFileHandle = -1; |
| 77 | #endif |
| 78 | |
| 79 | enum class FileOpenMode { |
| 80 | Read, |
| 81 | Write, |
| 82 | #if OS(DARWIN) |
| 83 | EventsOnly, |
| 84 | #endif |
| 85 | }; |
| 86 | |
| 87 | enum class FileSeekOrigin { |
| 88 | Beginning, |
| 89 | Current, |
| 90 | End, |
| 91 | }; |
| 92 | |
| 93 | enum class FileLockMode { |
| 94 | Shared = 1 << 0, |
| 95 | Exclusive = 1 << 1, |
| 96 | Nonblocking = 1 << 2, |
| 97 | }; |
| 98 | |
| 99 | enum class ShouldFollowSymbolicLinks { No, Yes }; |
| 100 | |
| 101 | WTF_EXPORT_PRIVATE bool fileExists(const String&); |
| 102 | WTF_EXPORT_PRIVATE bool deleteFile(const String&); |
| 103 | WTF_EXPORT_PRIVATE bool deleteEmptyDirectory(const String&); |
| 104 | WTF_EXPORT_PRIVATE bool moveFile(const String& oldPath, const String& newPath); |
| 105 | WTF_EXPORT_PRIVATE bool getFileSize(const String&, long long& result); |
| 106 | WTF_EXPORT_PRIVATE bool getFileSize(PlatformFileHandle, long long& result); |
| 107 | WTF_EXPORT_PRIVATE Optional<WallTime> getFileModificationTime(const String&); |
| 108 | WTF_EXPORT_PRIVATE Optional<WallTime> getFileCreationTime(const String&); // Not all platforms store file creation time. |
| 109 | WTF_EXPORT_PRIVATE Optional<FileMetadata> fileMetadata(const String& path); |
| 110 | WTF_EXPORT_PRIVATE Optional<FileMetadata> fileMetadataFollowingSymlinks(const String& path); |
| 111 | WTF_EXPORT_PRIVATE bool fileIsDirectory(const String&, ShouldFollowSymbolicLinks); |
| 112 | WTF_EXPORT_PRIVATE String pathByAppendingComponent(const String& path, const String& component); |
| 113 | WTF_EXPORT_PRIVATE String pathByAppendingComponents(StringView path, const Vector<StringView>& components); |
| 114 | WTF_EXPORT_PRIVATE String lastComponentOfPathIgnoringTrailingSlash(const String& path); |
| 115 | WTF_EXPORT_PRIVATE bool makeAllDirectories(const String& path); |
| 116 | WTF_EXPORT_PRIVATE String homeDirectoryPath(); |
| 117 | WTF_EXPORT_PRIVATE String pathGetFileName(const String&); |
| 118 | WTF_EXPORT_PRIVATE String directoryName(const String&); |
| 119 | WTF_EXPORT_PRIVATE bool getVolumeFreeSpace(const String&, uint64_t&); |
| 120 | WTF_EXPORT_PRIVATE Optional<int32_t> getFileDeviceId(const CString&); |
| 121 | WTF_EXPORT_PRIVATE bool createSymbolicLink(const String& targetPath, const String& symbolicLinkPath); |
| 122 | |
| 123 | WTF_EXPORT_PRIVATE void setMetadataURL(const String& path, const String& urlString, const String& referrer = { }); |
| 124 | |
| 125 | bool canExcludeFromBackup(); // Returns true if any file can ever be excluded from backup. |
| 126 | bool excludeFromBackup(const String&); // Returns true if successful. |
| 127 | |
| 128 | WTF_EXPORT_PRIVATE Vector<String> listDirectory(const String& path, const String& filter = String()); |
| 129 | |
| 130 | WTF_EXPORT_PRIVATE CString fileSystemRepresentation(const String&); |
| 131 | String stringFromFileSystemRepresentation(const char*); |
| 132 | |
| 133 | inline bool isHandleValid(const PlatformFileHandle& handle) { return handle != invalidPlatformFileHandle; } |
| 134 | |
| 135 | // Prefix is what the filename should be prefixed with, not the full path. |
| 136 | WTF_EXPORT_PRIVATE String openTemporaryFile(const String& prefix, PlatformFileHandle&); |
| 137 | WTF_EXPORT_PRIVATE PlatformFileHandle openFile(const String& path, FileOpenMode); |
| 138 | WTF_EXPORT_PRIVATE void closeFile(PlatformFileHandle&); |
| 139 | // Returns the resulting offset from the beginning of the file if successful, -1 otherwise. |
| 140 | WTF_EXPORT_PRIVATE long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin); |
| 141 | bool truncateFile(PlatformFileHandle, long long offset); |
| 142 | // Returns number of bytes actually read if successful, -1 otherwise. |
| 143 | WTF_EXPORT_PRIVATE int writeToFile(PlatformFileHandle, const char* data, int length); |
| 144 | // Returns number of bytes actually written if successful, -1 otherwise. |
| 145 | WTF_EXPORT_PRIVATE int readFromFile(PlatformFileHandle, char* data, int length); |
| 146 | |
| 147 | WTF_EXPORT_PRIVATE PlatformFileHandle openAndLockFile(const String&, FileOpenMode, OptionSet<FileLockMode> = FileLockMode::Exclusive); |
| 148 | WTF_EXPORT_PRIVATE void unlockAndCloseFile(PlatformFileHandle); |
| 149 | |
| 150 | // Appends the contents of the file found at 'path' to the open PlatformFileHandle. |
| 151 | // Returns true if the write was successful, false if it was not. |
| 152 | WTF_EXPORT_PRIVATE bool appendFileContentsToFileHandle(const String& path, PlatformFileHandle&); |
| 153 | |
| 154 | WTF_EXPORT_PRIVATE bool hardLink(const String& source, const String& destination); |
| 155 | // Hard links a file if possible, copies it if not. |
| 156 | WTF_EXPORT_PRIVATE bool hardLinkOrCopyFile(const String& source, const String& destination); |
| 157 | |
| 158 | #if USE(FILE_LOCK) |
| 159 | WTF_EXPORT_PRIVATE bool lockFile(PlatformFileHandle, OptionSet<FileLockMode>); |
| 160 | WTF_EXPORT_PRIVATE bool unlockFile(PlatformFileHandle); |
| 161 | #endif |
| 162 | |
| 163 | // Encode a string for use within a file name. |
| 164 | WTF_EXPORT_PRIVATE String encodeForFileName(const String&); |
| 165 | WTF_EXPORT_PRIVATE String decodeFromFilename(const String&); |
| 166 | |
| 167 | WTF_EXPORT_PRIVATE bool filesHaveSameVolume(const String&, const String&); |
| 168 | |
| 169 | #if USE(CF) |
| 170 | WTF_EXPORT_PRIVATE RetainPtr<CFURLRef> pathAsURL(const String&); |
| 171 | #endif |
| 172 | |
| 173 | #if USE(GLIB) |
| 174 | String filenameForDisplay(const String&); |
| 175 | #endif |
| 176 | |
| 177 | #if OS(WINDOWS) |
| 178 | WTF_EXPORT_PRIVATE String localUserSpecificStorageDirectory(); |
| 179 | WTF_EXPORT_PRIVATE String roamingUserSpecificStorageDirectory(); |
| 180 | WTF_EXPORT_PRIVATE String createTemporaryDirectory(); |
| 181 | WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&); |
| 182 | #endif |
| 183 | |
| 184 | #if PLATFORM(COCOA) |
| 185 | WTF_EXPORT_PRIVATE NSString *createTemporaryDirectory(NSString *directoryPrefix); |
| 186 | WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&); |
| 187 | #endif |
| 188 | |
| 189 | WTF_EXPORT_PRIVATE String realPath(const String&); |
| 190 | |
| 191 | WTF_EXPORT_PRIVATE bool isSafeToUseMemoryMapForPath(const String&); |
| 192 | WTF_EXPORT_PRIVATE void makeSafeToUseMemoryMapForPath(const String&); |
| 193 | |
| 194 | class MappedFileData { |
| 195 | public: |
| 196 | MappedFileData() { } |
| 197 | MappedFileData(MappedFileData&&); |
| 198 | WTF_EXPORT_PRIVATE MappedFileData(const String& filePath, bool& success); |
| 199 | WTF_EXPORT_PRIVATE ~MappedFileData(); |
| 200 | MappedFileData& operator=(MappedFileData&&); |
| 201 | |
| 202 | explicit operator bool() const { return !!m_fileData; } |
| 203 | const void* data() const { return m_fileData; } |
| 204 | unsigned size() const { return m_fileSize; } |
| 205 | |
| 206 | private: |
| 207 | void* m_fileData { nullptr }; |
| 208 | unsigned m_fileSize { 0 }; |
| 209 | }; |
| 210 | |
| 211 | inline MappedFileData::MappedFileData(MappedFileData&& other) |
| 212 | : m_fileData(std::exchange(other.m_fileData, nullptr)) |
| 213 | , m_fileSize(std::exchange(other.m_fileSize, 0)) |
| 214 | { |
| 215 | } |
| 216 | |
| 217 | inline MappedFileData& MappedFileData::operator=(MappedFileData&& other) |
| 218 | { |
| 219 | m_fileData = std::exchange(other.m_fileData, nullptr); |
| 220 | m_fileSize = std::exchange(other.m_fileSize, 0); |
| 221 | return *this; |
| 222 | } |
| 223 | |
| 224 | } // namespace FileSystemImpl |
| 225 | } // namespace WTF |
| 226 | |
| 227 | namespace FileSystem = WTF::FileSystemImpl; |
| 228 | |