blob: 41f8593a9033b998bb78a54da50af180f9f1c1cb [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
Jeff Brown7188e552011-07-20 16:38:43 -070050MediaScanResult MediaScanner::processDirectory(
51 const char *path, MediaScannerClient &client) {
Andreas Huber413f5232009-12-03 11:31:19 -080052 int pathLength = strlen(path);
53 if (pathLength >= PATH_MAX) {
Jeff Brown7188e552011-07-20 16:38:43 -070054 return MEDIA_SCAN_RESULT_SKIPPED;
Andreas Huber413f5232009-12-03 11:31:19 -080055 }
56 char* pathBuffer = (char *)malloc(PATH_MAX + 1);
57 if (!pathBuffer) {
Jeff Brown7188e552011-07-20 16:38:43 -070058 return MEDIA_SCAN_RESULT_ERROR;
Andreas Huber413f5232009-12-03 11:31:19 -080059 }
60
61 int pathRemaining = PATH_MAX - pathLength;
62 strcpy(pathBuffer, path);
Kenny Root3e42b442010-03-15 21:17:08 -070063 if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') {
Andreas Huber413f5232009-12-03 11:31:19 -080064 pathBuffer[pathLength] = '/';
65 pathBuffer[pathLength + 1] = 0;
66 --pathRemaining;
67 }
68
69 client.setLocale(locale());
70
Jeff Brown7188e552011-07-20 16:38:43 -070071 MediaScanResult result = doProcessDirectory(pathBuffer, pathRemaining, client, false);
Andreas Huber413f5232009-12-03 11:31:19 -080072
73 free(pathBuffer);
74
75 return result;
76}
77
Jeff Brown7188e552011-07-20 16:38:43 -070078MediaScanResult MediaScanner::doProcessDirectory(
79 char *path, int pathRemaining, MediaScannerClient &client, bool noMedia) {
Andreas Huber413f5232009-12-03 11:31:19 -080080 // place to copy file or directory name
81 char* fileSpot = path + strlen(path);
82 struct dirent* entry;
83
Mike Lockwood462acca2011-04-24 11:15:09 -070084 // Treat all files as non-media in directories that contain a ".nomedia" file
Andreas Huber413f5232009-12-03 11:31:19 -080085 if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {
86 strcpy(fileSpot, ".nomedia");
87 if (access(path, F_OK) == 0) {
Dianne Hackborn767ad022011-06-09 17:27:05 -070088 LOGV("found .nomedia, setting noMedia flag\n");
Mike Lockwood462acca2011-04-24 11:15:09 -070089 noMedia = true;
Andreas Huber413f5232009-12-03 11:31:19 -080090 }
91
92 // restore path
93 fileSpot[0] = 0;
94 }
95
96 DIR* dir = opendir(path);
97 if (!dir) {
Jeff Brown7188e552011-07-20 16:38:43 -070098 LOGW("Error opening directory '%s', skipping: %s.", path, strerror(errno));
99 return MEDIA_SCAN_RESULT_SKIPPED;
Andreas Huber413f5232009-12-03 11:31:19 -0800100 }
101
Jeff Brown7188e552011-07-20 16:38:43 -0700102 MediaScanResult result = MEDIA_SCAN_RESULT_OK;
Andreas Huber413f5232009-12-03 11:31:19 -0800103 while ((entry = readdir(dir))) {
Jeff Brown7188e552011-07-20 16:38:43 -0700104 if (doProcessDirectoryEntry(path, pathRemaining, client, noMedia, entry, fileSpot)
105 == MEDIA_SCAN_RESULT_ERROR) {
106 result = MEDIA_SCAN_RESULT_ERROR;
107 break;
Andreas Huber413f5232009-12-03 11:31:19 -0800108 }
Jeff Brown7188e552011-07-20 16:38:43 -0700109 }
110 closedir(dir);
111 return result;
112}
Andreas Huber413f5232009-12-03 11:31:19 -0800113
Jeff Brown7188e552011-07-20 16:38:43 -0700114MediaScanResult MediaScanner::doProcessDirectoryEntry(
115 char *path, int pathRemaining, MediaScannerClient &client, bool noMedia,
116 struct dirent* entry, char* fileSpot) {
117 struct stat statbuf;
118 const char* name = entry->d_name;
119
120 // ignore "." and ".."
121 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
122 return MEDIA_SCAN_RESULT_SKIPPED;
123 }
124
125 int nameLength = strlen(name);
126 if (nameLength + 1 > pathRemaining) {
127 // path too long!
128 return MEDIA_SCAN_RESULT_SKIPPED;
129 }
130 strcpy(fileSpot, name);
131
132 int type = entry->d_type;
133 if (type == DT_UNKNOWN) {
134 // If the type is unknown, stat() the file instead.
135 // This is sometimes necessary when accessing NFS mounted filesystems, but
136 // could be needed in other cases well.
137 if (stat(path, &statbuf) == 0) {
138 if (S_ISREG(statbuf.st_mode)) {
139 type = DT_REG;
140 } else if (S_ISDIR(statbuf.st_mode)) {
141 type = DT_DIR;
142 }
143 } else {
144 LOGD("stat() failed for %s: %s", path, strerror(errno) );
Mike Lockwoodef04e8f2010-06-14 22:57:22 -0700145 }
Jeff Brown7188e552011-07-20 16:38:43 -0700146 }
147 if (type == DT_DIR) {
148 bool childNoMedia = noMedia;
149 // set noMedia flag on directories with a name that starts with '.'
150 // for example, the Mac ".Trashes" directory
151 if (name[0] == '.')
152 childNoMedia = true;
Mike Lockwoodef04e8f2010-06-14 22:57:22 -0700153
Jeff Brown7188e552011-07-20 16:38:43 -0700154 // report the directory to the client
155 if (stat(path, &statbuf) == 0) {
156 status_t status = client.scanFile(path, statbuf.st_mtime, 0,
157 true /*isDirectory*/, childNoMedia);
158 if (status) {
159 return MEDIA_SCAN_RESULT_ERROR;
Andreas Huber413f5232009-12-03 11:31:19 -0800160 }
161 }
Andreas Huber413f5232009-12-03 11:31:19 -0800162
Jeff Brown7188e552011-07-20 16:38:43 -0700163 // and now process its contents
164 strcat(fileSpot, "/");
165 MediaScanResult result = doProcessDirectory(path, pathRemaining - nameLength - 1,
166 client, childNoMedia);
167 if (result == MEDIA_SCAN_RESULT_ERROR) {
168 return MEDIA_SCAN_RESULT_ERROR;
169 }
170 } else if (type == DT_REG) {
171 stat(path, &statbuf);
172 status_t status = client.scanFile(path, statbuf.st_mtime, statbuf.st_size,
173 false /*isDirectory*/, noMedia);
174 if (status) {
175 return MEDIA_SCAN_RESULT_ERROR;
Andreas Huber413f5232009-12-03 11:31:19 -0800176 }
177 }
178
Jeff Brown7188e552011-07-20 16:38:43 -0700179 return MEDIA_SCAN_RESULT_OK;
Andreas Huber413f5232009-12-03 11:31:19 -0800180}
181
182} // namespace android