1 /* 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14 * its contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "config.h" 30 #include "FileSystem.h" 31 32 #include "CString.h" 33 #include "PlatformString.h" 34 35 #include <sys/stat.h> 36 #ifdef ANDROID_PLUGINS 37 #include <sys/types.h> 38 #include <dirent.h> 39 #include <fnmatch.h> 40 #endif 41 #include <libgen.h> 42 #include <unistd.h> 43 44 namespace WebCore { 45 46 bool fileExists(const String& path) 47 { 48 if (path.isNull()) 49 return false; 50 51 CString fsRep = fileSystemRepresentation(path); 52 53 if (!fsRep.data() || fsRep.data()[0] == '\0') 54 return false; 55 56 struct stat fileInfo; 57 58 // stat(...) returns 0 on successful stat'ing of the file, and non-zero in any case where the file doesn't exist or cannot be accessed 59 return !stat(fsRep.data(), &fileInfo); 60 } 61 62 bool deleteFile(const String& path) 63 { 64 CString fsRep = fileSystemRepresentation(path); 65 66 if (!fsRep.data() || fsRep.data()[0] == '\0') 67 return false; 68 69 // unlink(...) returns 0 on successful deletion of the path and non-zero in any other case (including invalid permissions or non-existent file) 70 return !unlink(fsRep.data()); 71 } 72 73 bool deleteEmptyDirectory(const String& path) 74 { 75 CString fsRep = fileSystemRepresentation(path); 76 77 if (!fsRep.data() || fsRep.data()[0] == '\0') 78 return false; 79 80 // rmdir(...) returns 0 on successful deletion of the path and non-zero in any other case (including invalid permissions or non-existent file) 81 return !rmdir(fsRep.data()); 82 } 83 84 bool getFileSize(const String& path, long long& result) 85 { 86 CString fsRep = fileSystemRepresentation(path); 87 88 if (!fsRep.data() || fsRep.data()[0] == '\0') 89 return false; 90 91 struct stat fileInfo; 92 93 if (stat(fsRep.data(), &fileInfo)) 94 return false; 95 96 result = fileInfo.st_size; 97 return true; 98 } 99 100 bool getFileModificationTime(const String& path, time_t& result) 101 { 102 CString fsRep = fileSystemRepresentation(path); 103 104 if (!fsRep.data() || fsRep.data()[0] == '\0') 105 return false; 106 107 struct stat fileInfo; 108 109 if (stat(fsRep.data(), &fileInfo)) 110 return false; 111 112 result = fileInfo.st_mtime; 113 return true; 114 } 115 116 String pathByAppendingComponent(const String& path, const String& component) 117 { 118 if (path.endsWith("/")) 119 return path + component; 120 else 121 return path + "/" + component; 122 } 123 124 bool makeAllDirectories(const String& path) 125 { 126 CString fullPath = fileSystemRepresentation(path); 127 if (!access(fullPath.data(), F_OK)) 128 return true; 129 130 char* p = fullPath.mutableData() + 1; 131 int length = fullPath.length(); 132 133 if(p[length - 1] == '/') 134 p[length - 1] = '\0'; 135 for (; *p; ++p) 136 if (*p == '/') { 137 *p = '\0'; 138 if (access(fullPath.data(), F_OK)) 139 if (mkdir(fullPath.data(), S_IRWXU)) 140 return false; 141 *p = '/'; 142 } 143 if (access(fullPath.data(), F_OK)) 144 if (mkdir(fullPath.data(), S_IRWXU)) 145 return false; 146 147 return true; 148 } 149 150 String pathGetFileName(const String& path) 151 { 152 return path.substring(path.reverseFind('/') + 1); 153 } 154 155 String directoryName(const String& path) 156 { 157 CString fsRep = fileSystemRepresentation(path); 158 159 if (!fsRep.data() || fsRep.data()[0] == '\0') 160 return String(); 161 162 return dirname(fsRep.mutableData()); 163 } 164 165 // OK to not implement listDirectory at the moment, because it's only used for plug-ins, and 166 // all platforms that use the shared plug-in implementation have implementations. We'd need 167 // to implement it if we wanted to use PluginDatabase.cpp on the Mac. Better to not implement 168 // at all and get a link error in case this arises, rather than having a stub here, because 169 // with a stub you learn about the problem at runtime instead of link time. 170 171 } // namespace WebCore 172