您的位置:首页 > 编程语言 > C语言/C++

MediaScanner分析 - MediaScanner.cpp

2011-11-19 17:35 330 查看
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 name
105     char* fileSpot = path + strlen(path);
106     struct dirent* entry;
107 
108     // ignore directories that contain a  ".nomedia" file
109     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 path
119         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, but
140             // 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" directory
165                 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 errors
178                     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也不处理U盘和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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: