~easypeasy-maintainers/easypeasy-project/ubiquity

« back to all changes in this revision

Viewing changes to d-i/source/partconf/util.c

  • Committer: Jon Ramvi
  • Date: 2009-07-10 16:08:35 UTC
  • Revision ID: jon@geteasypeasy.com-20090710160835-ltb4mkc3qg31p0mo
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _GNU_SOURCE
 
2
#include <stdlib.h>
 
3
#include <stdio.h>
 
4
#include <string.h>
 
5
#include <stdarg.h>
 
6
#include <sys/mount.h>
 
7
 
 
8
char *
 
9
size_desc(long long bytes)
 
10
{
 
11
    static char ret[256];
 
12
    double kib, mib, gib;
 
13
 
 
14
    kib = bytes / 1024.0;
 
15
    mib = kib / 1024.0;
 
16
    gib = mib / 1024.0;
 
17
    if (gib > 10.0)
 
18
        sprintf(ret, "%.0f GiB", gib);
 
19
    else if (gib >= 1.0)
 
20
        sprintf(ret, "%.1f GiB", gib);
 
21
    else if (mib >= 100.0)
 
22
        sprintf(ret, "%.0f MiB", mib);
 
23
    else if (mib >= 1.0)
 
24
        sprintf(ret, "%.1f MiB", mib);
 
25
    else if (kib >= 100.0)
 
26
        sprintf(ret, "%.0f kiB", kib);
 
27
    else if (kib >= 1.0)
 
28
        sprintf(ret, "%.1f kiB", kib);
 
29
    else
 
30
        sprintf(ret, "%lld B", bytes);
 
31
    return ret;
 
32
}
 
33
 
 
34
void
 
35
modprobe(const char *mod)
 
36
{
 
37
    FILE *fp;
 
38
    char *cmd;
 
39
    char printk[1024] = "";
 
40
 
 
41
    if ((fp = fopen("/proc/sys/kernel/printk", "r")) != NULL) {
 
42
        fgets(printk, sizeof(printk), fp);
 
43
        fclose(fp);
 
44
    }
 
45
    if ((fp = fopen("/proc/sys/kernel/printk", "w")) != NULL) {
 
46
        fputs("0\n", fp);
 
47
        fclose(fp);
 
48
    }
 
49
    asprintf(&cmd, "modprobe %s >>/var/log/messages 2>&1", mod);
 
50
    system(cmd);
 
51
    free(cmd);
 
52
    if ((fp = fopen("/proc/sys/kernel/printk", "w")) != NULL) {
 
53
        fputs(printk, fp);
 
54
        fclose(fp);
 
55
    }
 
56
}
 
57
 
 
58
/*
 
59
 * Check if something's already mounted on /target/mntpoint
 
60
 */
 
61
int
 
62
check_proc_mounts(const char *mntpoint)
 
63
{
 
64
    FILE *fp;
 
65
    char buf[1024], mnt[1024];
 
66
    char *tmp;
 
67
 
 
68
    if ((fp = fopen("/proc/mounts", "r")) == NULL)
 
69
        return 0;
 
70
    asprintf(&tmp, "/target%s", mntpoint);
 
71
    while (fgets(buf, sizeof(buf), fp) != NULL) {
 
72
        sscanf(buf, "%*s %s", mnt);
 
73
        if (strcmp(tmp, mnt) == 0) {
 
74
            free(tmp);
 
75
            fclose(fp);
 
76
            return 1;
 
77
        }
 
78
    }
 
79
    free(tmp);
 
80
    fclose(fp);
 
81
    return 0;
 
82
}
 
83
 
 
84
/*
 
85
 * Check if the given device is already activated as a swap
 
86
 */
 
87
int
 
88
check_proc_swaps(const char *dev)
 
89
{
 
90
    FILE *fp;
 
91
    char buf[1024];
 
92
 
 
93
    if ((fp = fopen("/proc/swaps", "r")) == NULL)
 
94
        return 0;
 
95
    fgets(buf, sizeof(buf), fp);
 
96
    while (fgets(buf, sizeof(buf), fp) != NULL) {
 
97
        if (strstr(buf, dev) == buf) {
 
98
            fclose(fp);
 
99
            return 1;
 
100
        }
 
101
    }
 
102
    fclose(fp);
 
103
    return 0;
 
104
}
 
105
 
 
106
void
 
107
append_message(const char *fmt, ...)
 
108
{
 
109
    FILE *fp;
 
110
    va_list ap;
 
111
 
 
112
    if ((fp = fopen("/var/log/messages", "a")) == NULL)
 
113
        return;
 
114
    va_start(ap, fmt);
 
115
    vfprintf(fp, fmt, ap);
 
116
    fclose(fp);
 
117
    va_end(ap);
 
118
}
 
119
 
 
120
/*
 
121
 * Counts the number of occurrences of c in s
 
122
 */
 
123
int
 
124
strcount(const char *s, int c)
 
125
{
 
126
    const char *p;
 
127
    int ret = 0;
 
128
 
 
129
    p = s;
 
130
    while ((p = index(p, c)) != NULL) {
 
131
        ret++;
 
132
        p++;
 
133
    }
 
134
    return ret;
 
135
}
 
136
 
 
137
/*
 
138
 * 
 
139
 */
 
140
int
 
141
umount_target(void)
 
142
{
 
143
    FILE *fp;
 
144
    char buf[1024], mnt[1024];
 
145
    char *mounts[1024];
 
146
    int i, m_count = 0;
 
147
    int sort_func(const void *v1, const void *v2)
 
148
    {
 
149
        char *m1, *m2;
 
150
 
 
151
        m1 = *(char **)v1;
 
152
        m2 = *(char **)v2;
 
153
        if (m1 == NULL && m2 == NULL)
 
154
            return 0;
 
155
        else if (m1 == NULL)
 
156
            return -1;
 
157
        else if (m2 == NULL)
 
158
            return 1;
 
159
        if (strstr(m1, m2) == m1)
 
160
            return -1;
 
161
        else if (strstr(m2, m1) == m2)
 
162
            return 1;
 
163
        else
 
164
            return strcmp(m2, m1);
 
165
    }
 
166
 
 
167
    if ((fp = fopen("/proc/mounts", "r")) == NULL)
 
168
        return 0;
 
169
    while (fgets(buf, sizeof(buf), fp) != NULL) {
 
170
        sscanf(buf, "%*s %s", mnt);
 
171
        if (strstr(mnt, "/target") != mnt)
 
172
            continue;
 
173
        mounts[m_count++] = strdup(mnt);
 
174
    }
 
175
    fclose(fp);
 
176
    if (m_count == 0)
 
177
        return 1;
 
178
    qsort(mounts, m_count, sizeof(char *), sort_func);
 
179
    for (i = 0; i < m_count; i++) {
 
180
        if (umount(mounts[i]) < 0)
 
181
            return 0;
 
182
    }
 
183
    return 1;
 
184
}
 
185