partition_utils.c revision 12717169a51335f4b20b7c78a63b9ab66a81a366
1/*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <fcntl.h>
18#include <sys/ioctl.h>
19#include <sys/mount.h> /* for BLKGETSIZE */
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24#include <cutils/properties.h>
25
26static int only_one_char(char *buf, int len, char c)
27{
28    int i, ret;
29
30    ret = 1;
31    for (i=0; i<len; i++) {
32        if (buf[i] != c) {
33            ret = 0;
34            break;
35        }
36    }
37    return ret;
38}
39
40int partition_wiped(char *source)
41{
42    char buf[4096];
43    int fd, ret;
44
45    if ((fd = open(source, O_RDONLY)) < 0) {
46        return 0;
47    }
48
49    ret = read(fd, buf, sizeof(buf));
50    close(fd);
51
52    if (ret != sizeof(buf)) {
53        return 0;
54    }
55
56    /* Check for all zeros */
57    if (only_one_char(buf, sizeof(buf), 0)) {
58       return 1;
59    }
60
61    /* Check for all ones */
62    if (only_one_char(buf, sizeof(buf), 0xff)) {
63       return 1;
64    }
65
66    return 0;
67}
68
69