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