MediaScanner.cpp revision 7188e55f54a43c55fd6b96454720c447f1dc454e
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 50MediaScanResult MediaScanner::processDirectory( 51 const char *path, MediaScannerClient &client) { 52 int pathLength = strlen(path); 53 if (pathLength >= PATH_MAX) { 54 return MEDIA_SCAN_RESULT_SKIPPED; 55 } 56 char* pathBuffer = (char *)malloc(PATH_MAX + 1); 57 if (!pathBuffer) { 58 return MEDIA_SCAN_RESULT_ERROR; 59 } 60 61 int pathRemaining = PATH_MAX - pathLength; 62 strcpy(pathBuffer, path); 63 if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') { 64 pathBuffer[pathLength] = '/'; 65 pathBuffer[pathLength + 1] = 0; 66 --pathRemaining; 67 } 68 69 client.setLocale(locale()); 70 71 MediaScanResult result = doProcessDirectory(pathBuffer, pathRemaining, client, false); 72 73 free(pathBuffer); 74 75 return result; 76} 77 78MediaScanResult MediaScanner::doProcessDirectory( 79 char *path, int pathRemaining, MediaScannerClient &client, bool noMedia) { 80 // place to copy file or directory name 81 char* fileSpot = path + strlen(path); 82 struct dirent* entry; 83 84 // Treat all files as non-media in directories that contain a ".nomedia" file 85 if (pathRemaining >= 8 /* strlen(".nomedia") */ ) { 86 strcpy(fileSpot, ".nomedia"); 87 if (access(path, F_OK) == 0) { 88 LOGV("found .nomedia, setting noMedia flag\n"); 89 noMedia = true; 90 } 91 92 // restore path 93 fileSpot[0] = 0; 94 } 95 96 DIR* dir = opendir(path); 97 if (!dir) { 98 LOGW("Error opening directory '%s', skipping: %s.", path, strerror(errno)); 99 return MEDIA_SCAN_RESULT_SKIPPED; 100 } 101 102 MediaScanResult result = MEDIA_SCAN_RESULT_OK; 103 while ((entry = readdir(dir))) { 104 if (doProcessDirectoryEntry(path, pathRemaining, client, noMedia, entry, fileSpot) 105 == MEDIA_SCAN_RESULT_ERROR) { 106 result = MEDIA_SCAN_RESULT_ERROR; 107 break; 108 } 109 } 110 closedir(dir); 111 return result; 112} 113 114MediaScanResult 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) ); 145 } 146 } 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; 153 154 // 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; 160 } 161 } 162 163 // 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; 176 } 177 } 178 179 return MEDIA_SCAN_RESULT_OK; 180} 181 182} // namespace android 183