Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

MediaScanner.cpp 原始碼解析:資料夾遞迴掃描的實作

編輯:關於 Android 程式設計

一、簡介

MediaScanner 負責對資料夾做遞迴掃描,找出其中的媒體檔案。

  • 編譯產物:libmedia → libmedia.so
  • 檔案位置:frameworks/av/media/libmedia/MediaScanner.cpp

二、掃描的整體結構

呼叫鏈是這樣的:由 JNI 進入 processDirectory,再由 doProcessDirectory 與 doProcessDirectoryEntry 兩支函式互相搭配,完成遞迴。

分工很乾脆:doProcessDirectory 負責「掃一個資料夾裡的所有項目」,doProcessDirectoryEntry 負責「判斷單一項目是什麼、該怎麼處理」。遇到子資料夾就再呼叫前者,遞迴於焉成立。

三、doProcessDirectory:遍歷目錄項

MediaScanResult MediaScanner::doProcessDirectory(
        char *path, int pathRemaining, MediaScannerClient &client, bool noMedia) {
    char* fileSpot = path + strlen(path);
    struct dirent* entry;

    // 含 .nomedia 檔的目錄,內部所有檔案都視為非媒體
    DIR* dir = opendir(path);
    MediaScanResult result = MEDIA_SCAN_RESULT_OK;
    while ((entry = readdir(dir))) {
        if (doProcessDirectoryEntry(path, pathRemaining, client, noMedia, entry, fileSpot)
                == MEDIA_SCAN_RESULT_ERROR) {
            result = MEDIA_SCAN_RESULT_ERROR;
            break;
        }
    }
    closedir(dir);
    return result;
}

這裡有兩個細節值得留意。第一,fileSpot 是個游標,指向路徑字串尾端的暫存位置,遞迴時可以在同一個 buffer 上反覆拼接與回退,避免層層配置新字串。第二,noMedia 這個旗標會一路往下傳——一旦某個目錄被標記為 noMedia,其所有子項目都跟著被跳過,這正是 .nomedia 檔能讓 Gallery 忽略整個目錄的原理。

四、doProcessDirectoryEntry:分類與分派

MediaScanResult MediaScanner::doProcessDirectoryEntry(
        char *path, int pathRemaining, MediaScannerClient &client, bool noMedia,
        struct dirent* entry, char* fileSpot) {
    struct stat statbuf;
    const char* name = entry->d_name;
    int type = entry->d_type;

    // 某些檔案系統不提供 d_type,需要靠 stat 補判
    if (type == DT_UNKNOWN) {
        if (stat(path, &statbuf) == 0) {
            if (S_ISREG(statbuf.st_mode)) {
                type = DT_REG;      // 檔案
            } else if (S_ISDIR(statbuf.st_mode)) {
                type = DT_DIR;      // 目錄
            }
        }
    }

    if (type == DT_DIR) {
        bool childNoMedia = noMedia;
        // 名稱以 '.' 開頭的目錄(如 macOS 的 .Trashes)直接視為非媒體
        if (name[0] == '.') childNoMedia = true;

        if (stat(path, &statbuf) == 0) {
            status_t status = client.scanFile(path, statbuf.st_mtime, 0,
                    true /*isDirectory*/, childNoMedia);
            if (status) return MEDIA_SCAN_RESULT_ERROR;
        }
        // 繼續往下遞迴
        strcat(fileSpot, "/");
        MediaScanResult result = doProcessDirectory(path, pathRemaining - nameLength - 1,
                client, childNoMedia);
        if (result == MEDIA_SCAN_RESULT_ERROR) return MEDIA_SCAN_RESULT_ERROR;

    } else if (type == DT_REG) {
        stat(path, &statbuf);
        status_t status = client.scanFile(path, statbuf.st_mtime, statbuf.st_size,
                false /*isDirectory*/, noMedia);
        if (status) return MEDIA_SCAN_RESULT_ERROR;
    }
    // 其他類型(連結、裝置檔等)直接跳過
    return MEDIA_SCAN_RESULT_OK;
}

五、幾個值得注意的設計

  • d_type 的相容處理:readdir() 回傳的 d_type 在部分檔案系統上是 DT_UNKNOWN,此時必須退回用 stat() 判斷型別。這是跨檔案系統寫 C 常踩的坑。
  • 隱藏目錄規則:以 . 開頭的目錄一律設 childNoMedia,同時也不掃描其內容。這讓系統天然忽略 macOS 留下的 .Trashes、.Spotlight-V100 等雜項。
  • 目錄也回報給 client:目錄型別的項目會以 size = 0、isDirectory = true 呼叫 scanFile(),讓上層能建立完整的目錄樹資訊。
  • 錯誤即中斷:任一項失敗就立刻向上拋 MEDIA_SCAN_RESULT_ERROR,避免半成品寫入媒體資料庫。
  • pathRemaining 的意義:這是路徑緩衝區的剩餘空間,遞迴時逐層遞減,防止 strcat 寫出界。C 字串操作最容易出事的正是這種邊界條件。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved