Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Andreas Huber | 1629399 | 2010-06-23 11:31:17 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MediaScanner" |
| 19 | #include <utils/Log.h> |
| 20 | |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 21 | #include <media/mediascanner.h> |
| 22 | |
| 23 | #include <sys/stat.h> |
| 24 | #include <dirent.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | MediaScanner::MediaScanner() |
| 29 | : mLocale(NULL) { |
| 30 | } |
| 31 | |
| 32 | MediaScanner::~MediaScanner() { |
| 33 | setLocale(NULL); |
| 34 | } |
| 35 | |
| 36 | void MediaScanner::setLocale(const char *locale) { |
| 37 | if (mLocale) { |
| 38 | free(mLocale); |
| 39 | mLocale = NULL; |
| 40 | } |
| 41 | if (locale) { |
| 42 | mLocale = strdup(locale); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | const char *MediaScanner::locale() const { |
| 47 | return mLocale; |
| 48 | } |
| 49 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 50 | MediaScanResult MediaScanner::processDirectory( |
| 51 | const char *path, MediaScannerClient &client) { |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 52 | int pathLength = strlen(path); |
| 53 | if (pathLength >= PATH_MAX) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 54 | return MEDIA_SCAN_RESULT_SKIPPED; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 55 | } |
| 56 | char* pathBuffer = (char *)malloc(PATH_MAX + 1); |
| 57 | if (!pathBuffer) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 58 | return MEDIA_SCAN_RESULT_ERROR; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | int pathRemaining = PATH_MAX - pathLength; |
| 62 | strcpy(pathBuffer, path); |
Kenny Root | 3e42b44 | 2010-03-15 21:17:08 -0700 | [diff] [blame] | 63 | if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') { |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 64 | pathBuffer[pathLength] = '/'; |
| 65 | pathBuffer[pathLength + 1] = 0; |
| 66 | --pathRemaining; |
| 67 | } |
| 68 | |
| 69 | client.setLocale(locale()); |
| 70 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 71 | MediaScanResult result = doProcessDirectory(pathBuffer, pathRemaining, client, false); |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 72 | |
| 73 | free(pathBuffer); |
| 74 | |
| 75 | return result; |
| 76 | } |
| 77 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 78 | MediaScanResult MediaScanner::doProcessDirectory( |
| 79 | char *path, int pathRemaining, MediaScannerClient &client, bool noMedia) { |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 80 | // place to copy file or directory name |
| 81 | char* fileSpot = path + strlen(path); |
| 82 | struct dirent* entry; |
| 83 | |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 84 | // Treat all files as non-media in directories that contain a ".nomedia" file |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 85 | if (pathRemaining >= 8 /* strlen(".nomedia") */ ) { |
| 86 | strcpy(fileSpot, ".nomedia"); |
| 87 | if (access(path, F_OK) == 0) { |
Dianne Hackborn | 767ad02 | 2011-06-09 17:27:05 -0700 | [diff] [blame] | 88 | LOGV("found .nomedia, setting noMedia flag\n"); |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame] | 89 | noMedia = true; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // restore path |
| 93 | fileSpot[0] = 0; |
| 94 | } |
| 95 | |
| 96 | DIR* dir = opendir(path); |
| 97 | if (!dir) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 98 | LOGW("Error opening directory '%s', skipping: %s.", path, strerror(errno)); |
| 99 | return MEDIA_SCAN_RESULT_SKIPPED; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 102 | MediaScanResult result = MEDIA_SCAN_RESULT_OK; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 103 | while ((entry = readdir(dir))) { |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 104 | if (doProcessDirectoryEntry(path, pathRemaining, client, noMedia, entry, fileSpot) |
| 105 | == MEDIA_SCAN_RESULT_ERROR) { |
| 106 | result = MEDIA_SCAN_RESULT_ERROR; |
| 107 | break; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 108 | } |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 109 | } |
| 110 | closedir(dir); |
| 111 | return result; |
| 112 | } |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 113 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 114 | MediaScanResult MediaScanner::doProcessDirectoryEntry( |
| 115 | char *path, int pathRemaining, MediaScannerClient &client, bool noMedia, |
| 116 | struct dirent* entry, char* fileSpot) { |
| 117 | struct stat statbuf; |
| 118 | const char* name = entry->d_name; |
| 119 | |
| 120 | // ignore "." and ".." |
| 121 | if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) { |
| 122 | return MEDIA_SCAN_RESULT_SKIPPED; |
| 123 | } |
| 124 | |
| 125 | int nameLength = strlen(name); |
| 126 | if (nameLength + 1 > pathRemaining) { |
| 127 | // path too long! |
| 128 | return MEDIA_SCAN_RESULT_SKIPPED; |
| 129 | } |
| 130 | strcpy(fileSpot, name); |
| 131 | |
| 132 | int type = entry->d_type; |
| 133 | if (type == DT_UNKNOWN) { |
| 134 | // If the type is unknown, stat() the file instead. |
| 135 | // This is sometimes necessary when accessing NFS mounted filesystems, but |
| 136 | // could be needed in other cases well. |
| 137 | if (stat(path, &statbuf) == 0) { |
| 138 | if (S_ISREG(statbuf.st_mode)) { |
| 139 | type = DT_REG; |
| 140 | } else if (S_ISDIR(statbuf.st_mode)) { |
| 141 | type = DT_DIR; |
| 142 | } |
| 143 | } else { |
| 144 | LOGD("stat() failed for %s: %s", path, strerror(errno) ); |
Mike Lockwood | ef04e8f | 2010-06-14 22:57:22 -0700 | [diff] [blame] | 145 | } |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 146 | } |
| 147 | if (type == DT_DIR) { |
| 148 | bool childNoMedia = noMedia; |
| 149 | // set noMedia flag on directories with a name that starts with '.' |
| 150 | // for example, the Mac ".Trashes" directory |
| 151 | if (name[0] == '.') |
| 152 | childNoMedia = true; |
Mike Lockwood | ef04e8f | 2010-06-14 22:57:22 -0700 | [diff] [blame] | 153 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 154 | // report the directory to the client |
| 155 | if (stat(path, &statbuf) == 0) { |
| 156 | status_t status = client.scanFile(path, statbuf.st_mtime, 0, |
| 157 | true /*isDirectory*/, childNoMedia); |
| 158 | if (status) { |
| 159 | return MEDIA_SCAN_RESULT_ERROR; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 162 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 163 | // and now process its contents |
| 164 | strcat(fileSpot, "/"); |
| 165 | MediaScanResult result = doProcessDirectory(path, pathRemaining - nameLength - 1, |
| 166 | client, childNoMedia); |
| 167 | if (result == MEDIA_SCAN_RESULT_ERROR) { |
| 168 | return MEDIA_SCAN_RESULT_ERROR; |
| 169 | } |
| 170 | } else if (type == DT_REG) { |
| 171 | stat(path, &statbuf); |
| 172 | status_t status = client.scanFile(path, statbuf.st_mtime, statbuf.st_size, |
| 173 | false /*isDirectory*/, noMedia); |
| 174 | if (status) { |
| 175 | return MEDIA_SCAN_RESULT_ERROR; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Jeff Brown | 7188e55 | 2011-07-20 16:38:43 -0700 | [diff] [blame] | 179 | return MEDIA_SCAN_RESULT_OK; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | } // namespace android |