MediaScanner.cpp revision c59ad085c9737e8d56328732be6864de302acae9
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//#define LOG_NDEBUG 0
18#define LOG_TAG "MediaScanner"
19#include <utils/Log.h>
20
21#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, MediaScannerClient &client,
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);
64    if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') {
65        pathBuffer[pathLength] = '/';
66        pathBuffer[pathLength + 1] = 0;
67        --pathRemaining;
68    }
69
70    client.setLocale(locale());
71
72    status_t result =
73        doProcessDirectory(
74                pathBuffer, pathRemaining, client, exceptionCheck, exceptionEnv);
75
76    free(pathBuffer);
77
78    return result;
79}
80
81status_t MediaScanner::doProcessDirectory(
82        char *path, int pathRemaining, MediaScannerClient &client,
83        ExceptionCheck exceptionCheck, void *exceptionEnv) {
84    // place to copy file or directory name
85    char* fileSpot = path + strlen(path);
86    struct dirent* entry;
87
88    // ignore directories that contain a  ".nomedia" file
89    if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {
90        strcpy(fileSpot, ".nomedia");
91        if (access(path, F_OK) == 0) {
92            LOGD("found .nomedia, skipping directory\n");
93            fileSpot[0] = 0;
94            client.addNoMediaFolder(path);
95            return OK;
96        }
97
98        // restore path
99        fileSpot[0] = 0;
100    }
101
102    DIR* dir = opendir(path);
103    if (!dir) {
104        LOGD("opendir %s failed, errno: %d", path, errno);
105        return UNKNOWN_ERROR;
106    }
107
108    while ((entry = readdir(dir))) {
109        const char* name = entry->d_name;
110
111        // ignore "." and ".."
112        if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
113            continue;
114        }
115
116        int nameLength = strlen(name);
117        if (nameLength + 1 > pathRemaining) {
118            // path too long!
119            continue;
120        }
121        strcpy(fileSpot, name);
122
123        int type = entry->d_type;
124        if (type == DT_UNKNOWN) {
125            // If the type is unknown, stat() the file instead.
126            // This is sometimes necessary when accessing NFS mounted filesystems, but
127            // could be needed in other cases well.
128            struct stat statbuf;
129            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) {
140            if (type == DT_DIR) {
141                // ignore directories with a name that starts with '.'
142                // for example, the Mac ".Trashes" directory
143                if (name[0] == '.') continue;
144
145                strcat(fileSpot, "/");
146                int err = doProcessDirectory(path, pathRemaining - nameLength - 1, client, exceptionCheck, exceptionEnv);
147                if (err) {
148                    // pass exceptions up - ignore other errors
149                    if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
150                    LOGE("Error processing '%s' - skipping\n", path);
151                    continue;
152                }
153            } else {
154                struct stat statbuf;
155                stat(path, &statbuf);
156                if (statbuf.st_size > 0) {
157                    client.scanFile(path, statbuf.st_mtime, statbuf.st_size);
158                }
159                if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
160            }
161        }
162    }
163
164    closedir(dir);
165    return OK;
166failure:
167    closedir(dir);
168    return -1;
169}
170
171}  // namespace android
172