blob: 5ec573ee9027051e794418147a54d0033f6f8702 [file] [log] [blame]
Andreas Huber413f5232009-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 Huber16293992010-06-23 11:31:17 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "MediaScanner"
19#include <utils/Log.h>
20
Andreas Huber413f5232009-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(
Mike Lockwoodc59ad082010-09-10 14:47:36 -040051 const char *path, MediaScannerClient &client,
Andreas Huber413f5232009-12-03 11:31:19 -080052 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 Root3e42b442010-03-15 21:17:08 -070064 if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') {
Andreas Huber413f5232009-12-03 11:31:19 -080065 pathBuffer[pathLength] = '/';
66 pathBuffer[pathLength + 1] = 0;
67 --pathRemaining;
68 }
69
70 client.setLocale(locale());
71
72 status_t result =
73 doProcessDirectory(
Mike Lockwoodc59ad082010-09-10 14:47:36 -040074 pathBuffer, pathRemaining, client, exceptionCheck, exceptionEnv);
Andreas Huber413f5232009-12-03 11:31:19 -080075
76 free(pathBuffer);
77
78 return result;
79}
80
Andreas Huber413f5232009-12-03 11:31:19 -080081status_t MediaScanner::doProcessDirectory(
Mike Lockwoodc59ad082010-09-10 14:47:36 -040082 char *path, int pathRemaining, MediaScannerClient &client,
83 ExceptionCheck exceptionCheck, void *exceptionEnv) {
Andreas Huber413f5232009-12-03 11:31:19 -080084 // place to copy file or directory name
85 char* fileSpot = path + strlen(path);
86 struct dirent* entry;
Mike Lockwood3e9f9f12010-12-16 12:54:24 -080087 struct stat statbuf;
Andreas Huber413f5232009-12-03 11:31:19 -080088
89 // ignore directories that contain a ".nomedia" file
90 if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {
91 strcpy(fileSpot, ".nomedia");
92 if (access(path, F_OK) == 0) {
93 LOGD("found .nomedia, skipping directory\n");
94 fileSpot[0] = 0;
95 client.addNoMediaFolder(path);
96 return OK;
97 }
98
99 // restore path
100 fileSpot[0] = 0;
101 }
102
103 DIR* dir = opendir(path);
104 if (!dir) {
105 LOGD("opendir %s failed, errno: %d", path, errno);
106 return UNKNOWN_ERROR;
107 }
108
109 while ((entry = readdir(dir))) {
110 const char* name = entry->d_name;
111
112 // ignore "." and ".."
113 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
114 continue;
115 }
116
Mike Lockwoodef04e8f2010-06-14 22:57:22 -0700117 int nameLength = strlen(name);
118 if (nameLength + 1 > pathRemaining) {
119 // path too long!
120 continue;
121 }
122 strcpy(fileSpot, name);
123
Andreas Huber413f5232009-12-03 11:31:19 -0800124 int type = entry->d_type;
125 if (type == DT_UNKNOWN) {
126 // If the type is unknown, stat() the file instead.
127 // This is sometimes necessary when accessing NFS mounted filesystems, but
128 // could be needed in other cases well.
Andreas Huber413f5232009-12-03 11:31:19 -0800129 if (stat(path, &statbuf) == 0) {
130 if (S_ISREG(statbuf.st_mode)) {
131 type = DT_REG;
132 } else if (S_ISDIR(statbuf.st_mode)) {
133 type = DT_DIR;
134 }
135 } else {
136 LOGD("stat() failed for %s: %s", path, strerror(errno) );
137 }
138 }
139 if (type == DT_REG || type == DT_DIR) {
Mike Lockwoodef04e8f2010-06-14 22:57:22 -0700140 if (type == DT_DIR) {
Andreas Huber413f5232009-12-03 11:31:19 -0800141 // ignore directories with a name that starts with '.'
142 // for example, the Mac ".Trashes" directory
143 if (name[0] == '.') continue;
144
Mike Lockwood3e9f9f12010-12-16 12:54:24 -0800145 // report the directory to the client
146 if (stat(path, &statbuf) == 0) {
147 client.scanFile(path, statbuf.st_mtime, 0, true);
148 }
149
150 // and now process its contents
Andreas Huber413f5232009-12-03 11:31:19 -0800151 strcat(fileSpot, "/");
Mike Lockwood3e9f9f12010-12-16 12:54:24 -0800152 int err = doProcessDirectory(path, pathRemaining - nameLength - 1, client,
153 exceptionCheck, exceptionEnv);
Andreas Huber413f5232009-12-03 11:31:19 -0800154 if (err) {
155 // pass exceptions up - ignore other errors
156 if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
157 LOGE("Error processing '%s' - skipping\n", path);
158 continue;
159 }
Mike Lockwoodc59ad082010-09-10 14:47:36 -0400160 } else {
Andreas Huber413f5232009-12-03 11:31:19 -0800161 stat(path, &statbuf);
Mike Lockwood3e9f9f12010-12-16 12:54:24 -0800162 client.scanFile(path, statbuf.st_mtime, statbuf.st_size, false);
Andreas Huber413f5232009-12-03 11:31:19 -0800163 if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
164 }
165 }
166 }
167
168 closedir(dir);
169 return OK;
170failure:
171 closedir(dir);
172 return -1;
173}
174
175} // namespace android