MediaScanner.cpp分析
檔案路徑 frameworks/base/media/libmedia/MediaScanner.cpp
status_t MediaScanner::processDirectory( const char *path, const char *extensions, MediaScannerClient &client, ExceptionCheck exceptionCheck, void *exceptionEnv) { int pathLength = strlen(path); if (pathLength >= PATH_MAX) { return UNKNOWN_ERROR; } char* pathBuffer = (char *)malloc(PATH_MAX + 1); if (!pathBuffer) { return UNKNOWN_ERROR; } int pathRemaining = PATH_MAX - pathLength; strcpy(pathBuffer, path); if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') { pathBuffer[pathLength] = '/'; pathBuffer[pathLength + 1] = 0; --pathRemaining; } client.setLocale(locale()); status_t result = doProcessDirectory( pathBuffer, pathRemaining, extensions, client, exceptionCheck, exceptionEnv); free(pathBuffer); return result;}
很簡單,對路徑參數做了些處理,調用doProcessDirectory
@extensions可能包含多個副檔名,副檔名之間用comma分隔開
client.setLocale(locale()) 目的未知
static bool fileMatchesExtension(const char* path, const char* extensions) { char* extension = strrchr(path, '.'); if (!extension) return false; ++extension; // skip the dot if (extension[0] == 0) return false; while (extensions[0]) { char* comma = strchr(extensions, ','); size_t length = (comma ? comma - extensions : strlen(extensions)); if (length == strlen(extension) && strncasecmp(extension, extensions, length) == 0) return true; extensions += length; if (extensions[0] == ',') ++extensions; } return false;}
參數@extensions可能包含多個extensions,用","分割開,這個函數檢查給定路徑@path的副檔名是否包含在@extensions中
100 status_t MediaScanner::doProcessDirectory(101 char *path, int pathRemaining, const char *extensions,102 MediaScannerClient &client, ExceptionCheck exceptionCheck,103 void *exceptionEnv) {104 // place to copy file or directory name105 char* fileSpot = path + strlen(path);106 struct dirent* entry;107 108 // ignore directories that contain a ".nomedia" file109 if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {110 strcpy(fileSpot, ".nomedia");111 if (access(path, F_OK) == 0) {112 LOGD("found .nomedia, skipping directory\n");113 fileSpot[0] = 0;114 client.addNoMediaFolder(path);115 return OK;116 }117 118 // restore path119 fileSpot[0] = 0;120 }121 122 DIR* dir = opendir(path);123 if (!dir) {124 LOGD("opendir %s failed, errno: %d", path, errno);125 return UNKNOWN_ERROR;126 }127 128 while ((entry = readdir(dir))) {129 const char* name = entry->d_name;130 131 // ignore "." and ".."132 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {133 continue;134 }135 136 int type = entry->d_type;137 if (type == DT_UNKNOWN) {138 // If the type is unknown, stat() the file instead.139 // This is sometimes necessary when accessing NFS mounted filesystems, but140 // could be needed in other cases well.141 struct stat statbuf;142 if (stat(path, &statbuf) == 0) {143 if (S_ISREG(statbuf.st_mode)) {144 type = DT_REG;145 } else if (S_ISDIR(statbuf.st_mode)) {146 type = DT_DIR;147 }148 } else {149 LOGD("stat() failed for %s: %s", path, strerror(errno) );150 }151 }152 if (type == DT_REG || type == DT_DIR) {153 int nameLength = strlen(name);154 bool isDirectory = (type == DT_DIR);155 156 if (nameLength > pathRemaining || (isDirectory && nameLength + 1 > pathRemaining)) {157 // path too long!158 continue;159 }160 161 strcpy(fileSpot, name);162 if (isDirectory) {163 // ignore directories with a name that starts with '.'164 // for example, the Mac ".Trashes" directory165 if (name[0] == '.') continue;166 167 //When path is "/mnt/sdcard", ignore "/mnt/sdcard/extsd" and "/mnt/sdcard/udisk"168 if ((!strcmp(path, "/mnt/sdcard/extsd") && !strcmp(name, "extsd"))169 || (!strcmp(path, "/mnt/sdcard/udisk") && !strcmp(name, "udisk"))) {170 SLOGI("Ignore %s:%s.", path, name);171 continue;172 }173 174 strcat(fileSpot, "/");175 int err = doProcessDirectory(path, pathRemaining - nameLength - 1, extensions, client, exceptionCheck, exceptionEnv);176 if (err) {177 // pass exceptions up - ignore other errors178 if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;179 LOGE("Error processing '%s' - skipping\n", path);180 continue;181 }182 } else if (fileMatchesExtension(path, extensions)) {183 struct stat statbuf;184 stat(path, &statbuf);185 if (statbuf.st_size > 0) {186 client.scanFile(path, statbuf.st_mtime, statbuf.st_size);187 }188 if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;189 }190 }191 }192 193 closedir(dir);194 return OK;195 failure:196 closedir(dir);197 return -1;198 }199 200 } // namespace android
136~151 readdir讀取出來的dirent 有可能沒法擷取目錄項類型,需要使用stat系統調用來擷取,這樣既可以在某些情況下提升效率,又能解決相容性問題。
152 檔案是普通檔案或者目錄
156 ~158 看來MediaScanner並不想處理路徑太長的檔案,長路徑的檔案和子目錄都不做處理了
162 ~ 181 對於子目錄,首先忽略掉.開始的目錄,這也符合Linux/Unix的慣例,忽略掉/mnt/sdcard/extsd /mnt/sdcard/udisk udisk, 看來MediaScanner也不處理隨身碟和sd卡。最後遞迴處理子目錄
182 ~ 189 如果是檔案,並且檔案的副檔名是否在掃描的副檔名集合@extensions中,那麼調用client.scanFile來處理這個檔案,注意這裡會忽略掉檔案尺寸為0的檔案。
Ok, 到這裡MediaScanner.cpp結束,現在我大概對MediaScanner.cpp有了一個印象,這個MediaScanner.cpp估計也是一個Service,負責處理Media檔案掃描,具體到每一個檔案的處理過程交給一個MediaScannerClient來處理。
MediaScanner的含義就是對某一個路徑進行掃描,找到那些符合副檔名集合的檔案進行處理,至於怎麼處理,這個要分析java層的frameworks/base/media/java/android/media/MediaScanner.java
MediaScanner.cpp的幾個規則:
1. 不處理.開始的目錄,但是處理.開始的檔案, 哈哈
2. 不處理u盤,SD卡
3. 不處理0尺寸檔案
4. 當然也不處理副檔名不匹配的檔案
MediaScannerClient.cpp分析
MediaScannerClient.cpp存在的意義是由於MediaScanner.java無法處理locale字串,至於為什麼不能放在MediaScanner.java中做,而是很複雜的又實現了一個MediaScannerClient.cpp,還沒想明白
MediaScannerClient.cpp為MediaScanner.java提供了如下幾個方法;
setLocale, beginFile, addStringTag, convertValues, endFile
這幾個方法圍繞著MediaScanner.java擷取的metadata(用mNames, mValues表示),由於包含著本地化的字串,需要把native encoding 轉換為UTF-8