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 | |
| 50 | status_t MediaScanner::processDirectory( |
Mike Lockwood | c59ad08 | 2010-09-10 14:47:36 -0400 | [diff] [blame] | 51 | const char *path, MediaScannerClient &client, |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 52 | ExceptionCheck exceptionCheck, void *exceptionEnv) { |
| 53 | int pathLength = strlen(path); |
| 54 | if (pathLength >= PATH_MAX) { |
| 55 | return UNKNOWN_ERROR; |
| 56 | } |
| 57 | char* pathBuffer = (char *)malloc(PATH_MAX + 1); |
| 58 | if (!pathBuffer) { |
| 59 | return UNKNOWN_ERROR; |
| 60 | } |
| 61 | |
| 62 | int pathRemaining = PATH_MAX - pathLength; |
| 63 | strcpy(pathBuffer, path); |
Kenny Root | 3e42b44 | 2010-03-15 21:17:08 -0700 | [diff] [blame] | 64 | if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') { |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 65 | pathBuffer[pathLength] = '/'; |
| 66 | pathBuffer[pathLength + 1] = 0; |
| 67 | --pathRemaining; |
| 68 | } |
| 69 | |
| 70 | client.setLocale(locale()); |
| 71 | |
| 72 | status_t result = |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 73 | doProcessDirectory(pathBuffer, pathRemaining, client, false, exceptionCheck, exceptionEnv); |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 74 | |
| 75 | free(pathBuffer); |
| 76 | |
| 77 | return result; |
| 78 | } |
| 79 | |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 80 | status_t MediaScanner::doProcessDirectory( |
Mike Lockwood | c59ad08 | 2010-09-10 14:47:36 -0400 | [diff] [blame] | 81 | char *path, int pathRemaining, MediaScannerClient &client, |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 82 | bool noMedia, ExceptionCheck exceptionCheck, void *exceptionEnv) { |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 83 | // place to copy file or directory name |
| 84 | char* fileSpot = path + strlen(path); |
| 85 | struct dirent* entry; |
Mike Lockwood | 3e9f9f1 | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 86 | struct stat statbuf; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 87 | |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 88 | // 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] | 89 | if (pathRemaining >= 8 /* strlen(".nomedia") */ ) { |
| 90 | strcpy(fileSpot, ".nomedia"); |
| 91 | if (access(path, F_OK) == 0) { |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 92 | LOGD("found .nomedia, setting noMedia flag\n"); |
| 93 | noMedia = true; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // restore path |
| 97 | fileSpot[0] = 0; |
| 98 | } |
| 99 | |
| 100 | DIR* dir = opendir(path); |
| 101 | if (!dir) { |
| 102 | LOGD("opendir %s failed, errno: %d", path, errno); |
| 103 | return UNKNOWN_ERROR; |
| 104 | } |
| 105 | |
| 106 | while ((entry = readdir(dir))) { |
| 107 | const char* name = entry->d_name; |
| 108 | |
| 109 | // ignore "." and ".." |
| 110 | if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) { |
| 111 | continue; |
| 112 | } |
| 113 | |
Mike Lockwood | ef04e8f | 2010-06-14 22:57:22 -0700 | [diff] [blame] | 114 | int nameLength = strlen(name); |
| 115 | if (nameLength + 1 > pathRemaining) { |
| 116 | // path too long! |
| 117 | continue; |
| 118 | } |
| 119 | strcpy(fileSpot, name); |
| 120 | |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 121 | int type = entry->d_type; |
| 122 | if (type == DT_UNKNOWN) { |
| 123 | // If the type is unknown, stat() the file instead. |
| 124 | // This is sometimes necessary when accessing NFS mounted filesystems, but |
| 125 | // could be needed in other cases well. |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 126 | if (stat(path, &statbuf) == 0) { |
| 127 | if (S_ISREG(statbuf.st_mode)) { |
| 128 | type = DT_REG; |
| 129 | } else if (S_ISDIR(statbuf.st_mode)) { |
| 130 | type = DT_DIR; |
| 131 | } |
| 132 | } else { |
| 133 | LOGD("stat() failed for %s: %s", path, strerror(errno) ); |
| 134 | } |
| 135 | } |
| 136 | if (type == DT_REG || type == DT_DIR) { |
Mike Lockwood | ef04e8f | 2010-06-14 22:57:22 -0700 | [diff] [blame] | 137 | if (type == DT_DIR) { |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 138 | // set noMedia flag on directories with a name that starts with '.' |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 139 | // for example, the Mac ".Trashes" directory |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 140 | if (name[0] == '.') |
| 141 | noMedia = true; |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 142 | |
Mike Lockwood | 3e9f9f1 | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 143 | // report the directory to the client |
| 144 | if (stat(path, &statbuf) == 0) { |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 145 | client.scanFile(path, statbuf.st_mtime, 0, true, noMedia); |
Mike Lockwood | 3e9f9f1 | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // and now process its contents |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 149 | strcat(fileSpot, "/"); |
Mike Lockwood | 3e9f9f1 | 2010-12-16 12:54:24 -0800 | [diff] [blame] | 150 | int err = doProcessDirectory(path, pathRemaining - nameLength - 1, client, |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 151 | noMedia, exceptionCheck, exceptionEnv); |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 152 | if (err) { |
| 153 | // pass exceptions up - ignore other errors |
| 154 | if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure; |
| 155 | LOGE("Error processing '%s' - skipping\n", path); |
| 156 | continue; |
| 157 | } |
Mike Lockwood | c59ad08 | 2010-09-10 14:47:36 -0400 | [diff] [blame] | 158 | } else { |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 159 | stat(path, &statbuf); |
Mike Lockwood | 462acca | 2011-04-24 11:15:09 -0700 | [diff] [blame^] | 160 | client.scanFile(path, statbuf.st_mtime, statbuf.st_size, false, noMedia); |
Andreas Huber | 413f523 | 2009-12-03 11:31:19 -0800 | [diff] [blame] | 161 | if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | closedir(dir); |
| 167 | return OK; |
| 168 | failure: |
| 169 | closedir(dir); |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | } // namespace android |