-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtddevice_info.c
182 lines (156 loc) · 5.63 KB
/
mtddevice_info.c
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
/*
* sysinfo
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mtddevice_info.h"
char mtds[MAX_MTD_DEVICES][BUFFER_SIZE];
extern int correct_string();
int get_mtd(void)
{
FILE *file = NULL;
int mtd_count = 0;
memset(mtds, 0, sizeof(mtds));
if ((file = fopen("/proc/mtd", "r")) == NULL)
{
printf("cannot open /proc/mtd\n");
return -1;
}
while (mtd_count < MAX_MTD_DEVICES && fgets(mtds[mtd_count], BUFFER_SIZE, file))
{
if (strstr(mtds[mtd_count], "mtd") != NULL)
{
correct_string(mtds[mtd_count]);
mtd_count++;
}
}
fclose(file);
return mtd_count;
}
int parse_df(const char *mtd_dev, long* size, long* used, double* percent) {
char line[512];
int ret = -1;
*size = 0;
*used = 0;
FILE *fp = popen("df -k", "r");
if (fp == NULL) {
perror("error command df -k");
return -1;
}
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, mtd_dev) != NULL) {
long k_size, k_used;
if (sscanf(line, "%*s %ld %ld %*d %lf%%", &k_size, &k_used, percent) == 3) {
*size = k_size * 1024;
*used = k_used * 1024; // Umrechnung von Kilobyte in Byte
ret = 0;
break;
} else {
fprintf(stderr, "error parsing df: %s", line);
}
}
}
pclose(fp);
return ret;
}
int get_mtd_device_infos(MTDDeviceInfo *devices) {
long df_size, df_used;
double df_percent;
int num_mtd_devices = 0, i;
char dev[16] = "";
char name[32] = "";
char mtd_device_path[64] = "";
for (i = 0; i < MAX_MTD_DEVICES; i++) {
snprintf(mtd_device_path, sizeof(mtd_device_path), "%s/mtd%d", SYSFS_MTD_DEVICES_PATH, i);
FILE *size_file = fopen(strcat(mtd_device_path, "/size"), "r");
if (size_file == NULL) {
// We have reached the end of MTD devices
break;
}
snprintf(mtd_device_path, sizeof(mtd_device_path), "%s/mtd%d", SYSFS_MTD_DEVICES_PATH, i);
FILE *erase_size_file = fopen(strcat(mtd_device_path, "/erasesize"), "r");
if (erase_size_file == NULL) {
perror("fopen");
return 0;
}
snprintf(mtd_device_path, sizeof(mtd_device_path), "%s/mtd%d", SYSFS_MTD_DEVICES_PATH, i);
FILE *name_file = fopen(strcat(mtd_device_path, "/name"), "r");
if (name_file == NULL) {
perror("fopen");
return 0;
}
size_t total_size, erase_size;
fscanf(size_file, "%zu", &total_size);
fscanf(erase_size_file, "%zu", &erase_size);
fscanf(name_file, "%31s", name);
fclose(size_file);
fclose(erase_size_file);
fclose(name_file);
snprintf(devices[num_mtd_devices].dev, sizeof(devices[num_mtd_devices].dev), "mtd%d", i);
snprintf(devices[num_mtd_devices].name, sizeof(devices[num_mtd_devices].name), "%s", name);
size_t used_size = total_size - erase_size;
double percent = (double) used_size / total_size * 100.0;
// For CST with MTD Devices
if ((strstr(name, "systemFS") != NULL) || (strstr(name, "var") != NULL) ||
(strstr(name, "root0") != NULL) || (strstr(name, "root1") != NULL))
{
strcpy(dev, devices[num_mtd_devices].dev);
dev[strlen(dev)] = '\0';
if (strstr(name, "var") || strstr(name, "root")) {
char* ptr = strstr(devices[num_mtd_devices].dev, "mtd");
if (ptr != NULL) {
ptr += 3; // setzt pointer auf die Position nach dem "mtd" Substring
int num = atoi(ptr);
if (strstr(name, "root"))
snprintf(dev, sizeof(dev), "mtd:root%d", num);
else if (strstr(name, "var"))
snprintf(dev, sizeof(dev), "mtdblock%d", num);
}
}
df_size = df_used = df_percent = 0.0;
total_size = used_size = percent = 0.0;
if (parse_df(dev, &df_size, &df_used, &df_percent) == 0) {
//printf("parse df: DEV = %s %ld %ld %f\n", devices[num_mtd_devices].dev, df_size, df_used, df_percent);
total_size = df_size;
used_size = df_used;
percent = df_percent;
}
}
devices[num_mtd_devices].total_size = total_size;
devices[num_mtd_devices].used_size = used_size;
devices[num_mtd_devices].used_percentage = percent;
//printf("Sizes = %zu %zu %lf\n", total_size, used_size, percent);
if (total_size < 1024) {
snprintf(devices[num_mtd_devices].total_size_str, sizeof(devices[num_mtd_devices].total_size_str), "%.0f B", (double) total_size);
} else if (total_size < (1024 * 1024)) {
snprintf(devices[num_mtd_devices].total_size_str, sizeof(devices[num_mtd_devices].total_size_str), "%.0f K", (double) total_size / 1024.0);
} else {
snprintf(devices[num_mtd_devices].total_size_str, sizeof(devices[num_mtd_devices].total_size_str), "%.2f M", (double) total_size / (1024.0 * 1024.0));
}
if (used_size < 1024) {
snprintf(devices[num_mtd_devices].used_size_str, sizeof(devices[num_mtd_devices].used_size_str), "%.0f B", (double) used_size);
} else if (total_size < (1024 * 1024)) {
snprintf(devices[num_mtd_devices].used_size_str, sizeof(devices[num_mtd_devices].used_size_str), "%.0f K", (double) used_size / 1024.0);
} else {
snprintf(devices[num_mtd_devices].used_size_str, sizeof(devices[num_mtd_devices].used_size_str), "%.2f M", (double) used_size / (1024.0 * 1024.0));
}
num_mtd_devices++;
}
return num_mtd_devices;
}