summaryrefslogtreecommitdiffstats
path: root/vold/media.c
blob: 40637ff9eead5d7e1c1697225d5d6c9be5ef6d0e (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

/*
 * Copyright (C) 2008 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.
 */

#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>

#include <sys/types.h>

#include "vold.h"
#include "media.h"

static media_list_t *list_root = NULL;

media_t *media_create(char *devpath, char *name, char *serial, media_type_t type)
{
    media_list_t *list_entry;
    media_t *new;

    if (!(new = malloc(sizeof(media_t))))
        return NULL;

    memset(new, 0, sizeof(media_t));

    if (!(list_entry = malloc(sizeof(media_list_t)))) {
        free(new);
        return NULL;
    }
    list_entry->media = new;
    list_entry->next = NULL;

    if (!list_root)
        list_root = list_entry;
    else {
        media_list_t *list_scan = list_root;
        while(list_scan->next)
            list_scan = list_scan->next;
        list_scan->next = list_entry;
    }
     
    new->devpath = strdup(devpath);
    new->name = strdup(name);
    if (!serial)
        new->serial = 0;
    else
        new->serial = strtoul(serial, NULL, 0);

    new->media_type = type;

    return new;
}

void media_destroy(media_t *media)
{
    media_list_t *list_next;

    if (list_root->media == media) {
        list_next = list_root->next;
        free(list_root);
        list_root = list_next;
    } else {
        media_list_t *list_scan = list_root;
        while (list_scan->next->media != media)
            list_scan = list_scan -> next;
        list_next = list_scan->next->next;
        free(list_scan->next);
        list_scan->next = list_next;
    }

    free(media->devpath);
    free(media->name);

    while(media->devs)
        media_remove_blkdev(media, media->devs->dev);
    free(media);
}

media_t *media_lookup_by_path(char *devpath, boolean fuzzy_match)
{
    media_list_t *list_scan = list_root;

    while (list_scan) {
        if (fuzzy_match) {
            if (!strncmp(list_scan->media->devpath, devpath, strlen(devpath)))
                return list_scan->media;
        } else {
            if (!strcmp(list_scan->media->devpath, devpath))
                return list_scan->media;
        }
        list_scan = list_scan->next;
    }
#if DEBUG_MEDIA
    LOG_VOL("media_lookup_by_path(): No media found @ %s", devpath);
#endif
    return NULL;
}

int media_add_blkdev(media_t *card, blkdev_t *dev)
{
    blkdev_list_t *list_entry;

    if (!(list_entry = malloc(sizeof(blkdev_list_t)))) {
        LOGE("Out of memory");
        return -ENOMEM;
    }
    
    list_entry->next = NULL;
    list_entry->dev = dev;
    if (!card->devs)
        card->devs = list_entry;
    else {
        blkdev_list_t *scan = card->devs;

        while(scan->next)
            scan = scan->next;

        scan->next = list_entry;
    }
    return 0;
}

void media_remove_blkdev(media_t *card, blkdev_t *dev)
{
    if (card->devs->dev == dev)
        card->devs = card->devs->next;
    else {
        blkdev_list_t *scan = card->devs;
        while (scan->next->dev != dev)
            scan = scan -> next;
        blkdev_list_t *next = scan->next->next;
        free(scan->next);
        scan->next = next;
    }
}

media_t *media_lookup_by_dev(blkdev_t *dev)
{
    media_list_t *media_scan = list_root;

    while (media_scan) {
        blkdev_list_t *blk_scan = media_scan->media->devs;
        while (blk_scan) {
            if (blk_scan->dev == dev)
                return media_scan->media;
            blk_scan = blk_scan->next;
        }
        media_scan = media_scan->next;
    }
    return NULL;
}