blob: ba98f04e76bb45bbf61adf335cbdb382a29b9bf7 [file] [log] [blame]
Andreas Huberbfb9fb12009-12-03 11:31:19 -08001/*
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 Huberfb41d592010-06-23 11:31:17 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "MediaScanner"
19#include <utils/Log.h>
20
Andreas Huberbfb9fb12009-12-03 11:31:19 -080021#include <media/mediascanner.h>
22
23#include <sys/stat.h>
24#include <dirent.h>
25
26namespace android {
27
28MediaScanner::MediaScanner()
29 : mLocale(NULL) {
30}
31
32MediaScanner::~MediaScanner() {
33 setLocale(NULL);
34}
35
36void 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
46const char *MediaScanner::locale() const {
47 return mLocale;
48}
49
50status_t MediaScanner::processDirectory(
51 const char *path, const char *extensions,
52 MediaScannerClient &client,
53 ExceptionCheck exceptionCheck, void *exceptionEnv) {
54 int pathLength = strlen(path);
55 if (pathLength >= PATH_MAX) {
56 return UNKNOWN_ERROR;
57 }
58 char* pathBuffer = (char *)malloc(PATH_MAX + 1);
59 if (!pathBuffer) {
60 return UNKNOWN_ERROR;
61 }
62
63 int pathRemaining = PATH_MAX - pathLength;
64 strcpy(pathBuffer, path);
Kenny Roota8c02d72010-03-15 21:17:08 -070065 if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') {
Andreas Huberbfb9fb12009-12-03 11:31:19 -080066 pathBuffer[pathLength] = '/';
67 pathBuffer[pathLength + 1] = 0;
68 --pathRemaining;
69 }
70
71 client.setLocale(locale());
72
73 status_t result =
74 doProcessDirectory(
75 pathBuffer, pathRemaining, extensions, client,
76 exceptionCheck, exceptionEnv);
77
78 free(pathBuffer);
79
80 return result;
81}
82
83static bool fileMatchesExtension(const char* path, const char* extensions) {
Mike Lockwood5a23f8c2010-07-15 14:58:25 -040084 const char* extension = strrchr(path, '.');
Andreas Huberbfb9fb12009-12-03 11:31:19 -080085 if (!extension) return false;
86 ++extension; // skip the dot
87 if (extension[0] == 0) return false;
88
89 while (extensions[0]) {
Mike Lockwood5a23f8c2010-07-15 14:58:25 -040090 const char* comma = strchr(extensions, ',');
Andreas Huberbfb9fb12009-12-03 11:31:19 -080091 size_t length = (comma ? comma - extensions : strlen(extensions));
92 if (length == strlen(extension) && strncasecmp(extension, extensions, length) == 0) return true;
93 extensions += length;
94 if (extensions[0] == ',') ++extensions;
95 }
96
97 return false;
98}
99
100status_t MediaScanner::doProcessDirectory(
101 char *path, int pathRemaining, const char *extensions,
102 MediaScannerClient &client, ExceptionCheck exceptionCheck,
103 void *exceptionEnv) {
104 // place to copy file or directory name
105 char* fileSpot = path + strlen(path);
106 struct dirent* entry;
107
108 // ignore directories that contain a ".nomedia" file
109 if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {
110 strcpy(fileSpot, ".nomedia");
111 if (access(path, F_OK) == 0) {
112 LOGD("found .nomedia, skipping directory\n");
113 fileSpot[0] = 0;
114 client.addNoMediaFolder(path);
115 return OK;
116 }
117
118 // restore path
119 fileSpot[0] = 0;
120 }
121
122 DIR* dir = opendir(path);
123 if (!dir) {
124 LOGD("opendir %s failed, errno: %d", path, errno);
125 return UNKNOWN_ERROR;
126 }
127
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
Mike Lockwoodf30218b2010-06-14 22:57:22 -0700136 int nameLength = strlen(name);
137 if (nameLength + 1 > pathRemaining) {
138 // path too long!
139 continue;
140 }
141 strcpy(fileSpot, name);
142
Andreas Huberbfb9fb12009-12-03 11:31:19 -0800143 int type = entry->d_type;
144 if (type == DT_UNKNOWN) {
145 // If the type is unknown, stat() the file instead.
146 // This is sometimes necessary when accessing NFS mounted filesystems, but
147 // could be needed in other cases well.
148 struct stat statbuf;
149 if (stat(path, &statbuf) == 0) {
150 if (S_ISREG(statbuf.st_mode)) {
151 type = DT_REG;
152 } else if (S_ISDIR(statbuf.st_mode)) {
153 type = DT_DIR;
154 }
155 } else {
156 LOGD("stat() failed for %s: %s", path, strerror(errno) );
157 }
158 }
159 if (type == DT_REG || type == DT_DIR) {
Mike Lockwoodf30218b2010-06-14 22:57:22 -0700160 if (type == DT_DIR) {
Andreas Huberbfb9fb12009-12-03 11:31:19 -0800161 // ignore directories with a name that starts with '.'
162 // for example, the Mac ".Trashes" directory
163 if (name[0] == '.') continue;
164
165 strcat(fileSpot, "/");
166 int err = doProcessDirectory(path, pathRemaining - nameLength - 1, extensions, client, exceptionCheck, exceptionEnv);
167 if (err) {
168 // pass exceptions up - ignore other errors
169 if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
170 LOGE("Error processing '%s' - skipping\n", path);
171 continue;
172 }
173 } else if (fileMatchesExtension(path, extensions)) {
174 struct stat statbuf;
175 stat(path, &statbuf);
176 if (statbuf.st_size > 0) {
177 client.scanFile(path, statbuf.st_mtime, statbuf.st_size);
178 }
179 if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
180 }
181 }
182 }
183
184 closedir(dir);
185 return OK;
186failure:
187 closedir(dir);
188 return -1;
189}
190
191} // namespace android