summaryrefslogtreecommitdiffstats
path: root/media/mtp/MtpMediaScanner.cpp
blob: a9adf01f51f4b93a255524a505db58c8968810f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "MtpMediaScanner"

#include "MtpDebug.h"
#include "MtpDatabase.h"
#include "MtpMediaScanner.h"
#include "mtp.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>

namespace android {

MtpMediaScanner::MtpMediaScanner(MtpStorageID id, const char* filePath, MtpDatabase* db)
    :   mStorageID(id),
        mFilePath(filePath),
        mDatabase(db),
        mFileList(NULL),
        mFileCount(0)
{
}

MtpMediaScanner::~MtpMediaScanner() {
}

bool MtpMediaScanner::scanFiles() {
    mDatabase->beginTransaction();
    mFileCount = 0;
    mFileList = mDatabase->getFileList(mFileCount);

    int ret = scanDirectory(mFilePath, MTP_PARENT_ROOT);

    for (int i = 0; i < mFileCount; i++) {
        MtpObjectHandle test = mFileList[i];
        if (! (test & kObjectHandleMarkBit)) {
            LOGV("delete missing file %08X", test);
            mDatabase->deleteFile(test);
        }
    }

    delete[] mFileList;
    mFileCount = 0;
    mDatabase->commitTransaction();
    return (ret == 0);
}


static const struct MediaFileTypeEntry
{
    const char*     extension;
    MtpObjectFormat format;
} sFileTypes[] =
{
    { "MP3",    MTP_FORMAT_MP3,             },
    { "M4A",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "WAV",    MTP_FORMAT_WAV,             },
    { "AMR",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "AWB",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "WMA",    MTP_FORMAT_WMA,             },
    { "OGG",    MTP_FORMAT_OGG,             },
    { "OGA",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "AAC",    MTP_FORMAT_AAC,             },
    { "MID",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "MIDI",   MTP_FORMAT_UNDEFINED_AUDIO, },
    { "XMF",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "RTTTL",  MTP_FORMAT_UNDEFINED_AUDIO, },
    { "SMF",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "IMY",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "RTX",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "OTA",    MTP_FORMAT_UNDEFINED_AUDIO, },
    { "MPEG",   MTP_FORMAT_UNDEFINED_VIDEO, },
    { "MP4",    MTP_FORMAT_UNDEFINED_VIDEO, },
    { "M4V",    MTP_FORMAT_UNDEFINED_VIDEO, },
    { "3GP",    MTP_FORMAT_UNDEFINED_VIDEO, },
    { "3GPP",   MTP_FORMAT_UNDEFINED_VIDEO, },
    { "3G2",    MTP_FORMAT_UNDEFINED_VIDEO, },
    { "3GPP2",  MTP_FORMAT_UNDEFINED_VIDEO, },
    { "WMV",    MTP_FORMAT_UNDEFINED_VIDEO, },
    { "ASF",    MTP_FORMAT_UNDEFINED_VIDEO, },
    { "JPG",    MTP_FORMAT_EXIF_JPEG,       },
    { "JPEG",   MTP_FORMAT_EXIF_JPEG,       },
    { "GIF",    MTP_FORMAT_GIF,             },
    { "PNG",    MTP_FORMAT_PNG,             },
    { "BMP",    MTP_FORMAT_BMP,             },
    { "WBMP",   MTP_FORMAT_BMP,             },
    { "M3U",    MTP_FORMAT_M3U_PLAYLIST,    },
    { "PLS",    MTP_FORMAT_PLS_PLAYLIST,    },
    { "WPL",    MTP_FORMAT_WPL_PLAYLIST,    },
};

MtpObjectFormat MtpMediaScanner::getFileFormat(const char* path)
{
    const char* extension = strrchr(path, '.');
    if (!extension)
        return MTP_FORMAT_UNDEFINED;
    extension++; // skip the dot

    for (unsigned i = 0; i < sizeof(sFileTypes) / sizeof(sFileTypes[0]); i++) {
        if (!strcasecmp(extension, sFileTypes[i].extension)) {
            return sFileTypes[i].format;
        }
    }
    return MTP_FORMAT_UNDEFINED;
}

int MtpMediaScanner::scanDirectory(const char* path, MtpObjectHandle parent)
{
    char buffer[PATH_MAX];
    struct dirent* entry;

    unsigned length = strlen(path);
    if (length > sizeof(buffer) + 2) {
        LOGE("path too long: %s", path);
    }

    DIR* dir = opendir(path);
    if (!dir) {
        LOGE("opendir %s failed, errno: %d", path, errno);
        return -1;
    }

    strncpy(buffer, path, sizeof(buffer));
    char* fileStart = buffer + length;
    // make sure we have a trailing slash
    if (fileStart[-1] != '/') {
        *(fileStart++) = '/';
    }
    int fileNameLength = sizeof(buffer) + fileStart - buffer;

    while ((entry = readdir(dir))) {
        const char* name = entry->d_name;

        // ignore "." and "..", as well as any files or directories staring with dot
        if (name[0] == '.') {
            continue;
        }
        if (strlen(name) + 1 > fileNameLength) {
            LOGE("path too long for %s", name);
            continue;
        }
        strcpy(fileStart, name);

        struct stat statbuf;
        memset(&statbuf, 0, sizeof(statbuf));
        stat(buffer, &statbuf);

        if (S_ISDIR(statbuf.st_mode)) {
            MtpObjectHandle handle = mDatabase->getObjectHandle(buffer);
            if (handle) {
                markFile(handle);
            } else {
                handle = mDatabase->addFile(buffer, MTP_FORMAT_ASSOCIATION,
                        parent, mStorageID, 0, statbuf.st_mtime);
            }
            scanDirectory(buffer, handle);
        } else if (S_ISREG(statbuf.st_mode)) {
            scanFile(buffer, parent, statbuf);
        }
    }

    closedir(dir);
    return 0;
}

void MtpMediaScanner::scanFile(const char* path, MtpObjectHandle parent, struct stat& statbuf) {
    MtpObjectFormat format = getFileFormat(path);
    // don't scan unknown file types
    if (format == MTP_FORMAT_UNDEFINED)
        return;
    MtpObjectHandle handle = mDatabase->getObjectHandle(path);
    // fixme - rescan if mod date changed
    if (handle) {
        markFile(handle);
    } else {
        mDatabase->beginTransaction();
        handle = mDatabase->addFile(path, format, parent, mStorageID,
                statbuf.st_size, statbuf.st_mtime);
        if (handle <= 0) {
            LOGE("addFile failed in MtpMediaScanner::scanFile()");
            mDatabase->rollbackTransaction();
            return;
        }
        mDatabase->commitTransaction();
    }
}

void MtpMediaScanner::markFile(MtpObjectHandle handle) {
    if (mFileList) {
        handle &= kObjectHandleIndexMask;
        // binary search for the file in mFileList
        int low = 0;
        int high = mFileCount;
        int index;

        while (low < high) {
            index = (low + high) >> 1;
            MtpObjectHandle test = (mFileList[index] & kObjectHandleIndexMask);
            if (handle < test)
                high = index;       // item is less than index
            else if (handle > test)
                low = index + 1;    // item is greater than index
            else {
                mFileList[index] |= kObjectHandleMarkBit;
                return;
            }
        }
        LOGE("file %d not found in mFileList", handle);
    }
}

}  // namespace android