| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013, The Android Open Source Project |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 * you may not use this file except in compliance with the License. |
| 6 * You may obtain a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 * See the License for the specific language governing permissions and |
| 14 * limitations under the License. |
| 15 */ |
| 16 |
| 17 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/file_utils.
h" |
| 18 |
| 19 #include <cstdio> |
| 20 #include <cstring> |
| 21 #include <dirent.h> |
| 22 #include <fcntl.h> |
| 23 #include <libgen.h> |
| 24 #include <sys/types.h> |
| 25 #include <sys/stat.h> |
| 26 #include <unistd.h> |
| 27 |
| 28 namespace latinime { |
| 29 |
| 30 // Returns -1 on error. |
| 31 /* static */ int FileUtils::getFileSize(const char* const filePath) { |
| 32 const int fd = open(filePath, O_RDONLY); |
| 33 if (fd == -1) { |
| 34 return -1; |
| 35 } |
| 36 struct stat statBuf; |
| 37 if (fstat(fd, &statBuf) != 0) { |
| 38 close(fd); |
| 39 return -1; |
| 40 } |
| 41 close(fd); |
| 42 return static_cast<int>(statBuf.st_size); |
| 43 } |
| 44 |
| 45 /* static */ bool FileUtils::existsDir(const char* const dirPath) { |
| 46 DIR* const dir = opendir(dirPath); |
| 47 if (dir == NULL) { |
| 48 return false; |
| 49 } |
| 50 closedir(dir); |
| 51 return true; |
| 52 } |
| 53 |
| 54 // Remove a directory and all files in the directory. |
| 55 /* static */ bool FileUtils::removeDirAndFiles(const char* const dirPath) { |
| 56 return removeDirAndFiles(dirPath, 5 /* maxTries */); |
| 57 } |
| 58 |
| 59 // Remove a directory and all files in the directory, trying up to maxTimes. |
| 60 /* static */ bool FileUtils::removeDirAndFiles(const char* const dirPath, |
| 61 const int maxTries) { |
| 62 DIR* const dir = opendir(dirPath); |
| 63 if (dir == NULL) { |
| 64 AKLOGE("Cannot open dir %s.", dirPath); |
| 65 return true; |
| 66 } |
| 67 struct dirent* dirent; |
| 68 while ((dirent = readdir(dir)) != NULL) { |
| 69 if (dirent->d_type == DT_DIR) { |
| 70 continue; |
| 71 } |
| 72 if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0) { |
| 73 continue; |
| 74 } |
| 75 const int filePathBufSize = getFilePathBufSize(dirPath, dirent->d_name); |
| 76 char filePath[filePathBufSize]; |
| 77 getFilePath(dirPath, dirent->d_name, filePathBufSize, filePath); |
| 78 if (remove(filePath) != 0) { |
| 79 AKLOGE("Cannot remove file %s.", filePath); |
| 80 closedir(dir); |
| 81 return false; |
| 82 } |
| 83 } |
| 84 closedir(dir); |
| 85 if (remove(dirPath) != 0) { |
| 86 if (maxTries > 0) { |
| 87 // On NFS, deleting files sometimes creates new files. I'm not sure what |
| 88 // the |
| 89 // correct way of dealing with this is, but for the time being, this seems |
| 90 // to work. |
| 91 removeDirAndFiles(dirPath, maxTries - 1); |
| 92 } else { |
| 93 AKLOGE("Cannot remove directory %s.", dirPath); |
| 94 return false; |
| 95 } |
| 96 } |
| 97 return true; |
| 98 } |
| 99 |
| 100 /* static */ int FileUtils::getFilePathWithSuffixBufSize( |
| 101 const char* const filePath, |
| 102 const char* const suffix) { |
| 103 return strlen(filePath) + strlen(suffix) + 1 /* terminator */; |
| 104 } |
| 105 |
| 106 /* static */ void FileUtils::getFilePathWithSuffix(const char* const filePath, |
| 107 const char* const suffix, |
| 108 const int filePathBufSize, |
| 109 char* const outFilePath) { |
| 110 snprintf(outFilePath, filePathBufSize, "%s%s", filePath, suffix); |
| 111 } |
| 112 |
| 113 /* static */ int FileUtils::getFilePathBufSize(const char* const dirPath, |
| 114 const char* const fileName) { |
| 115 return strlen(dirPath) + 1 /* '/' */ + strlen(fileName) + 1 /* terminator */; |
| 116 } |
| 117 |
| 118 /* static */ void FileUtils::getFilePath(const char* const dirPath, |
| 119 const char* const fileName, |
| 120 const int filePathBufSize, |
| 121 char* const outFilePath) { |
| 122 snprintf(outFilePath, filePathBufSize, "%s/%s", dirPath, fileName); |
| 123 } |
| 124 |
| 125 /* static */ bool FileUtils::getFilePathWithoutSuffix( |
| 126 const char* const filePath, |
| 127 const char* const suffix, |
| 128 const int outDirPathBufSize, |
| 129 char* const outDirPath) { |
| 130 const int filePathLength = strlen(filePath); |
| 131 const int suffixLength = strlen(suffix); |
| 132 if (filePathLength <= suffixLength) { |
| 133 AKLOGE("File path length (%s:%d) is shorter that suffix length (%s:%d).", |
| 134 filePath, filePathLength, suffix, suffixLength); |
| 135 return false; |
| 136 } |
| 137 const int resultFilePathLength = filePathLength - suffixLength; |
| 138 if (outDirPathBufSize <= resultFilePathLength) { |
| 139 AKLOGE( |
| 140 "outDirPathBufSize is too small. filePath: %s, suffix: %s, " |
| 141 "outDirPathBufSize: %d", |
| 142 filePath, suffix, outDirPathBufSize); |
| 143 return false; |
| 144 } |
| 145 if (strncmp(filePath + resultFilePathLength, suffix, suffixLength) != 0) { |
| 146 AKLOGE("File Path %s does not have %s as a suffix", filePath, suffix); |
| 147 return false; |
| 148 } |
| 149 snprintf(outDirPath, resultFilePathLength + 1 /* terminator */, "%s", |
| 150 filePath); |
| 151 return true; |
| 152 } |
| 153 |
| 154 /* static */ void FileUtils::getDirPath(const char* const filePath, |
| 155 const int outDirPathBufSize, |
| 156 char* const outDirPath) { |
| 157 for (int i = strlen(filePath) - 1; i >= 0; --i) { |
| 158 if (filePath[i] == '/') { |
| 159 if (i >= outDirPathBufSize) { |
| 160 AKLOGE( |
| 161 "outDirPathBufSize is too small. filePath: %s, outDirPathBufSize: " |
| 162 "%d", |
| 163 filePath, outDirPathBufSize); |
| 164 ASSERT(false); |
| 165 return; |
| 166 } |
| 167 snprintf(outDirPath, i + 1 /* terminator */, "%s", filePath); |
| 168 return; |
| 169 } |
| 170 } |
| 171 } |
| 172 |
| 173 /* static */ void FileUtils::getBasename(const char* const filePath, |
| 174 const int outNameBufSize, |
| 175 char* const outName) { |
| 176 const int filePathBufSize = strlen(filePath) + 1 /* terminator */; |
| 177 char filePathBuf[filePathBufSize]; |
| 178 snprintf(filePathBuf, filePathBufSize, "%s", filePath); |
| 179 const char* const baseName = basename(filePathBuf); |
| 180 const int baseNameLength = strlen(baseName); |
| 181 if (baseNameLength >= outNameBufSize) { |
| 182 AKLOGE("outNameBufSize is too small. filePath: %s, outNameBufSize: %d", |
| 183 filePath, outNameBufSize); |
| 184 return; |
| 185 } |
| 186 snprintf(outName, baseNameLength + 1 /* terminator */, "%s", baseName); |
| 187 } |
| 188 |
| 189 } // namespace latinime |
| OLD | NEW |