Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Mike Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 17 | #define LOG_TAG "MtpUtils" |
| 18 | |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/unique_fd.h> |
| 21 | #include <dirent.h> |
| 22 | #include <fcntl.h> |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 23 | #include <string> |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 24 | #include <sys/sendfile.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
Mike Lockwood | 335dd2b | 2010-05-19 10:33:39 -0400 | [diff] [blame] | 27 | #include <stdio.h> |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 28 | #include <time.h> |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 29 | #include <unistd.h> |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 30 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 31 | #include "MtpUtils.h" |
| 32 | |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 33 | using namespace std; |
| 34 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 35 | namespace android { |
| 36 | |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 37 | constexpr unsigned long FILE_COPY_SIZE = 262144; |
| 38 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 39 | /* |
| 40 | DateTime strings follow a compatible subset of the definition found in ISO 8601, and |
| 41 | take the form of a Unicode string formatted as: "YYYYMMDDThhmmss.s". In this |
| 42 | representation, YYYY shall be replaced by the year, MM replaced by the month (01-12), |
| 43 | DD replaced by the day (01-31), T is a constant character 'T' delimiting time from date, |
| 44 | hh is replaced by the hour (00-23), mm is replaced by the minute (00-59), and ss by the |
| 45 | second (00-59). The ".s" is optional, and represents tenths of a second. |
Elliott Hughes | d4b4738 | 2016-01-05 17:05:47 -0800 | [diff] [blame] | 46 | This is followed by a UTC offset given as "[+-]zzzz" or the literal "Z", meaning UTC. |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 47 | */ |
| 48 | |
| 49 | bool parseDateTime(const char* dateTime, time_t& outSeconds) { |
| 50 | int year, month, day, hour, minute, second; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 51 | if (sscanf(dateTime, "%04d%02d%02dT%02d%02d%02d", |
Elliott Hughes | d4b4738 | 2016-01-05 17:05:47 -0800 | [diff] [blame] | 52 | &year, &month, &day, &hour, &minute, &second) != 6) |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 53 | return false; |
Elliott Hughes | d4b4738 | 2016-01-05 17:05:47 -0800 | [diff] [blame] | 54 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 55 | // skip optional tenth of second |
Elliott Hughes | d4b4738 | 2016-01-05 17:05:47 -0800 | [diff] [blame] | 56 | const char* tail = dateTime + 15; |
| 57 | if (tail[0] == '.' && tail[1]) tail += 2; |
| 58 | |
| 59 | // FIXME: "Z" means UTC, but non-"Z" doesn't mean local time. |
| 60 | // It might be that you're in Asia/Seoul on vacation and your Android |
| 61 | // device has noticed this via the network, but your camera was set to |
| 62 | // America/Los_Angeles once when you bought it and doesn't know where |
| 63 | // it is right now, so the camera says "20160106T081700-0800" but we |
| 64 | // just ignore the "-0800" and assume local time which is actually "+0900". |
| 65 | // I think to support this (without switching to Java or using icu4c) |
| 66 | // you'd want to always use timegm(3) and then manually add/subtract |
| 67 | // the UTC offset parsed from the string (taking care of wrapping). |
| 68 | // mktime(3) ignores the tm_gmtoff field, so you can't let it do the work. |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 69 | bool useUTC = (tail[0] == 'Z'); |
| 70 | |
Elliott Hughes | d4b4738 | 2016-01-05 17:05:47 -0800 | [diff] [blame] | 71 | struct tm tm = {}; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 72 | tm.tm_sec = second; |
| 73 | tm.tm_min = minute; |
| 74 | tm.tm_hour = hour; |
| 75 | tm.tm_mday = day; |
Mike Lockwood | ea1db0a | 2011-01-26 14:41:45 -0800 | [diff] [blame] | 76 | tm.tm_mon = month - 1; // mktime uses months in 0 - 11 range |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 77 | tm.tm_year = year - 1900; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 78 | tm.tm_isdst = -1; |
Elliott Hughes | d4b4738 | 2016-01-05 17:05:47 -0800 | [diff] [blame] | 79 | outSeconds = useUTC ? timegm(&tm) : mktime(&tm); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | void formatDateTime(time_t seconds, char* buffer, int bufferLength) { |
| 85 | struct tm tm; |
| 86 | |
| 87 | localtime_r(&seconds, &tm); |
| 88 | snprintf(buffer, bufferLength, "%04d%02d%02dT%02d%02d%02d", |
Elliott Hughes | d4b4738 | 2016-01-05 17:05:47 -0800 | [diff] [blame] | 89 | tm.tm_year + 1900, |
Mike Lockwood | ea1db0a | 2011-01-26 14:41:45 -0800 | [diff] [blame] | 90 | tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range |
| 91 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 94 | int makeFolder(const char *path) { |
| 95 | mode_t mask = umask(0); |
| 96 | int ret = mkdir((const char *)path, DIR_PERM); |
| 97 | umask(mask); |
| 98 | if (ret && ret != -EEXIST) { |
| 99 | PLOG(ERROR) << "Failed to create folder " << path; |
| 100 | ret = -1; |
| 101 | } else { |
| 102 | chown((const char *)path, getuid(), FILE_GROUP); |
| 103 | } |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Copies target path and all children to destination path. |
| 109 | * |
| 110 | * Returns 0 on success or a negative value indicating number of failures |
| 111 | */ |
| 112 | int copyRecursive(const char *fromPath, const char *toPath) { |
| 113 | int ret = 0; |
| 114 | string fromPathStr(fromPath); |
| 115 | string toPathStr(toPath); |
| 116 | |
| 117 | DIR* dir = opendir(fromPath); |
| 118 | if (!dir) { |
| 119 | PLOG(ERROR) << "opendir " << fromPath << " failed"; |
| 120 | return -1; |
| 121 | } |
| 122 | if (fromPathStr[fromPathStr.size()-1] != '/') |
| 123 | fromPathStr += '/'; |
| 124 | if (toPathStr[toPathStr.size()-1] != '/') |
| 125 | toPathStr += '/'; |
| 126 | |
| 127 | struct dirent* entry; |
| 128 | while ((entry = readdir(dir))) { |
| 129 | const char* name = entry->d_name; |
| 130 | |
| 131 | // ignore "." and ".." |
| 132 | if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) { |
| 133 | continue; |
| 134 | } |
| 135 | string oldFile = fromPathStr + name; |
| 136 | string newFile = toPathStr + name; |
| 137 | |
| 138 | if (entry->d_type == DT_DIR) { |
| 139 | ret += makeFolder(newFile.c_str()); |
| 140 | ret += copyRecursive(oldFile.c_str(), newFile.c_str()); |
| 141 | } else { |
| 142 | ret += copyFile(oldFile.c_str(), newFile.c_str()); |
| 143 | } |
| 144 | } |
| 145 | return ret; |
| 146 | } |
| 147 | |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 148 | int copyFile(const char *fromPath, const char *toPath) { |
| 149 | auto start = std::chrono::steady_clock::now(); |
| 150 | |
| 151 | android::base::unique_fd fromFd(open(fromPath, O_RDONLY)); |
| 152 | if (fromFd == -1) { |
| 153 | PLOG(ERROR) << "Failed to open copy from " << fromPath; |
| 154 | return -1; |
| 155 | } |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 156 | android::base::unique_fd toFd(open(toPath, O_CREAT | O_WRONLY, FILE_PERM)); |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 157 | if (toFd == -1) { |
| 158 | PLOG(ERROR) << "Failed to open copy to " << toPath; |
| 159 | return -1; |
| 160 | } |
| 161 | off_t offset = 0; |
| 162 | |
| 163 | struct stat sstat = {}; |
| 164 | if (stat(fromPath, &sstat) == -1) |
| 165 | return -1; |
| 166 | |
| 167 | off_t length = sstat.st_size; |
| 168 | int ret = 0; |
| 169 | |
| 170 | while (offset < length) { |
| 171 | ssize_t transfer_length = std::min(length - offset, (off_t) FILE_COPY_SIZE); |
| 172 | ret = sendfile(toFd, fromFd, &offset, transfer_length); |
| 173 | if (ret != transfer_length) { |
| 174 | ret = -1; |
| 175 | PLOG(ERROR) << "Copying failed!"; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | auto end = std::chrono::steady_clock::now(); |
| 180 | std::chrono::duration<double> diff = end - start; |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 181 | LOG(DEBUG) << "Copied a file with MTP. Time: " << diff.count() << " s, Size: " << length << |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 182 | ", Rate: " << ((double) length) / diff.count() << " bytes/s"; |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 183 | chown(toPath, getuid(), FILE_GROUP); |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 184 | return ret == -1 ? -1 : 0; |
| 185 | } |
| 186 | |
| 187 | void deleteRecursive(const char* path) { |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 188 | string pathStr(path); |
| 189 | if (pathStr[pathStr.size()-1] != '/') { |
| 190 | pathStr += '/'; |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 191 | } |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 192 | |
| 193 | DIR* dir = opendir(path); |
| 194 | if (!dir) { |
| 195 | PLOG(ERROR) << "opendir " << path << " failed"; |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | struct dirent* entry; |
| 200 | while ((entry = readdir(dir))) { |
| 201 | const char* name = entry->d_name; |
| 202 | |
| 203 | // ignore "." and ".." |
| 204 | if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) { |
| 205 | continue; |
| 206 | } |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 207 | pathStr.append(name); |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 208 | if (entry->d_type == DT_DIR) { |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 209 | deleteRecursive(pathStr.c_str()); |
| 210 | rmdir(pathStr.c_str()); |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 211 | } else { |
Jerry Zhang | e242f12 | 2017-10-16 14:54:08 -0700 | [diff] [blame] | 212 | unlink(pathStr.c_str()); |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | closedir(dir); |
| 216 | } |
| 217 | |
| 218 | void deletePath(const char* path) { |
| 219 | struct stat statbuf; |
| 220 | if (stat(path, &statbuf) == 0) { |
| 221 | if (S_ISDIR(statbuf.st_mode)) { |
| 222 | deleteRecursive(path); |
| 223 | rmdir(path); |
| 224 | } else { |
| 225 | unlink(path); |
| 226 | } |
| 227 | } else { |
| 228 | PLOG(ERROR) << "deletePath stat failed for " << path;; |
| 229 | } |
| 230 | } |
| 231 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 232 | } // namespace android |