• MediaScanner分析


    MediaScanner.cpp分析

    文件路径 frameworks/base/media/libmedia/MediaScanner.cpp

    1. status_t MediaScanner::processDirectory(  
    2.         const char *path, const char *extensions,  
    3.         MediaScannerClient &client,  
    4.         ExceptionCheck exceptionCheck, void *exceptionEnv) {  
    5.     int pathLength = strlen(path);  
    6.     if (pathLength >= PATH_MAX) {  
    7.         return UNKNOWN_ERROR;  
    8.     }  
    9.     char* pathBuffer = (char *)malloc(PATH_MAX + 1);  
    10.     if (!pathBuffer) {  
    11.         return UNKNOWN_ERROR;  
    12.     }  
    13.   
    14.     int pathRemaining = PATH_MAX - pathLength;  
    15.     strcpy(pathBuffer, path);  
    16.     if (pathLength > 0 && pathBuffer[pathLength - 1] != '/') {  
    17.         pathBuffer[pathLength] = '/';  
    18.         pathBuffer[pathLength + 1] = 0;  
    19.         --pathRemaining;  
    20.     }  
    21.   
    22.     client.setLocale(locale());  
    23.   
    24.     status_t result =  
    25.         doProcessDirectory(  
    26.                 pathBuffer, pathRemaining, extensions, client,  
    27.                 exceptionCheck, exceptionEnv);  
    28.   
    29.     free(pathBuffer);  
    30.   
    31.     return result;  
    32. }  


    很简单,对路径参数做了些处理,调用doProcessDirectory

    @extensions可能包含多个扩展名,扩展名之间用comma分隔开

    client.setLocale(locale()) 目的未知


    1. static bool fileMatchesExtension(const char* path, const char* extensions) {  
    2.     char* extension = strrchr(path, '.');  
    3.     if (!extension) return false;  
    4.     ++extension;    // skip the dot  
    5.     if (extension[0] == 0) return false;  
    6.   
    7.     while (extensions[0]) {  
    8.         char* comma = strchr(extensions, ',');  
    9.         size_t length = (comma ? comma - extensions : strlen(extensions));  
    10.         if (length == strlen(extension) && strncasecmp(extension, extensions, length) == 0) return true;  
    11.         extensions += length;  
    12.         if (extensions[0] == ',') ++extensions;  
    13.     }  
    14.   
    15.     return false;  
    16. }  

    参数@extensions可能包含多个extensions,用","分割开,这个函数检查给定路径@path的扩展名是否包含在@extensions中


    1. 100 status_t MediaScanner::doProcessDirectory(  
    2. 101         char *path, int pathRemaining, const char *extensions,  
    3. 102         MediaScannerClient &client, ExceptionCheck exceptionCheck,  
    4. 103         void *exceptionEnv) {  
    5. 104     // place to copy file or directory name  
    6. 105     char* fileSpot = path + strlen(path);  
    7. 106     struct dirent* entry;  
    8. 107   
    9. 108     // ignore directories that contain a  ".nomedia" file  
    10. 109     if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {  
    11. 110         strcpy(fileSpot, ".nomedia");  
    12. 111         if (access(path, F_OK) == 0) {  
    13. 112             LOGD("found .nomedia, skipping directory ");  
    14. 113             fileSpot[0] = 0;  
    15. 114             client.addNoMediaFolder(path);  
    16. 115             return OK;  
    17. 116         }  
    18. 117   
    19. 118         // restore path  
    20. 119         fileSpot[0] = 0;  
    21. 120     }  
    22. 121   
    23. 122     DIR* dir = opendir(path);  
    24. 123     if (!dir) {  
    25. 124         LOGD("opendir %s failed, errno: %d", path, errno);  
    26. 125         return UNKNOWN_ERROR;  
    27. 126     }  
    28. 127   
    29. 128     while ((entry = readdir(dir))) {  
    30. 129         const char* name = entry->d_name;  
    31. 130   
    32. 131         // ignore "." and ".."  
    33. 132         if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {  
    34. 133             continue;  
    35. 134         }  
    36. 135   
    37. 136         int type = entry->d_type;  
    38. 137         if (type == DT_UNKNOWN) {  
    39. 138             // If the type is unknown, stat() the file instead.  
    40. 139             // This is sometimes necessary when accessing NFS mounted filesystems, but  
    41. 140             // could be needed in other cases well.  
    42. 141             struct stat statbuf;  
    43. 142             if (stat(path, &statbuf) == 0) {  
    44. 143                 if (S_ISREG(statbuf.st_mode)) {  
    45. 144                     type = DT_REG;  
    46. 145                 } else if (S_ISDIR(statbuf.st_mode)) {  
    47. 146                     type = DT_DIR;  
    48. 147                 }  
    49. 148             } else {  
    50. 149                 LOGD("stat() failed for %s: %s", path, strerror(errno) );  
    51. 150             }  
    52. 151         }  
    53. 152         if (type == DT_REG || type == DT_DIR) {  
    54. 153             int nameLength = strlen(name);  
    55. 154             bool isDirectory = (type == DT_DIR);  
    56. 155   
    57. 156             if (nameLength > pathRemaining || (isDirectory && nameLength + 1 > pathRemaining)) {  
    58. 157                 // path too long!  
    59. 158                 continue;  
    60. 159             }  
    61. 160   
    62. 161             strcpy(fileSpot, name);  
    63. 162             if (isDirectory) {  
    64. 163                 // ignore directories with a name that starts with '.'  
    65. 164                 // for example, the Mac ".Trashes" directory  
    66. 165                 if (name[0] == '.') continue;  
    67. 166   
    68. 167                 //When path is "/mnt/sdcard", ignore "/mnt/sdcard/extsd" and "/mnt/sdcard/udisk"  
    69. 168                 if ((!strcmp(path, "/mnt/sdcard/extsd") && !strcmp(name, "extsd"))  
    70. 169                         || (!strcmp(path, "/mnt/sdcard/udisk") && !strcmp(name, "udisk"))) {  
    71. 170                     SLOGI("Ignore %s:%s.", path, name);  
    72. 171                     continue;  
    73. 172                 }  
    74. 173   
    75. 174                 strcat(fileSpot, "/");  
    76. 175                 int err = doProcessDirectory(path, pathRemaining - nameLength - 1, extensions, client, exceptionCheck, exceptionEnv);  
    77. 176                 if (err) {  
    78. 177                     // pass exceptions up - ignore other errors  
    79. 178                     if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;  
    80. 179                     LOGE("Error processing '%s' - skipping ", path);  
    81. 180                     continue;  
    82. 181                 }  
    83. 182             } else if (fileMatchesExtension(path, extensions)) {  
    84. 183                 struct stat statbuf;  
    85. 184                 stat(path, &statbuf);  
    86. 185                 if (statbuf.st_size > 0) {  
    87. 186                     client.scanFile(path, statbuf.st_mtime, statbuf.st_size);  
    88. 187                 }  
    89. 188                 if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;  
    90. 189             }  
    91. 190         }  
    92. 191     }  
    93. 192   
    94. 193     closedir(dir);  
    95. 194     return OK;  
    96. 195 failure:  
    97. 196     closedir(dir);  
    98. 197     return -1;  
    99. 198 }  
    100. 199   
    101. 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
  • 相关阅读:
    常用Linux命令总结
    mysql基础归纳
    第一次使用Ubuntu20.04系统-遇坑小记
    Linux操作系统常用命令
    单例模式
    MVC设计模式
    SpringMVC体系结构简要描述
    报错:数据库连接问题
    数据库增删改查语句
    JDBC关键步骤(简化版)
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744539.html
Copyright © 2020-2023  润新知