/
disk.c
335 lines (275 loc) · 7.2 KB
/
disk.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* disk.c
*
* Author: Michael Schwarz
* Copyright (C) 2011 Michael Schwarz
*
* Funtions for creating partitions, mounting, unmounting and copying
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <parted/parted.h>
#include <sys/mount.h>
#include <unistd.h>
#include "disk.h"
#define MAX_DRIVES 128
#define MAX_PARTITIONS 8
#define BOOT_SECTOR_SIZE (1024*1024*16)
#define FILE_SYSTEM "ext2"
/**
* Checks if the given partition is mounted
*
* */
int is_mounted(const char* partition) {
char buffer[256];
FILE* f;
int status = 0;
f = fopen("/proc/mounts", "r");
if(f == NULL) {
return -1;
}
while(!feof(f)) {
fgets(buffer, 256, f);
if(strncmp(buffer, partition, strlen(partition)) == 0 && buffer[strlen(partition)] == ' ') {
status = 1;
break;
}
}
return status;
}
char* get_mountpoint(const char* source) {
static char buffer[256];
FILE* f;
f = fopen("/proc/mounts", "r");
if(f == NULL) {
return NULL;
}
while(!feof(f)) {
fgets(buffer, 256, f);
if(strncmp(buffer, source, strlen(source)) == 0 && buffer[strlen(source)] == ' ') {
// get mount point
int end = strlen(source) + 1;
while(buffer[end] != ' ' && buffer[end] != 0) {
end++;
}
buffer[end] = 0;
return &buffer[strlen(source) + 1];
}
}
return NULL;
}
/**
* (internal) Timer for file system creation progress
*
*/
void create_ext2_timer(PedTimer* timer, void* context) {
//printf(".");
}
/**
* Creates the boot- and the linux partition and formats the linux
* partition with an ext2 filesystem
*
*/
int create_partitions(const char* dev, unsigned long bootsector_size) {
PedDisk* disk;
PedDevice* device;
PedPartition* boot_part;
PedPartition* linux_part;
PedFileSystemType* fs_type;
PedTimer* timer;
// get device from string e.g. "/dev/sdd"
device = ped_device_get(dev);
if(device == NULL) {
return 0;
}
// create new partition table
disk = ped_disk_new_fresh(device, ped_disk_type_get("msdos"));
if(disk == NULL) {
ped_device_destroy(device);
return 0;
}
// get file system type (ext2)
fs_type = ped_file_system_type_get(FILE_SYSTEM);
// create partitions
boot_part = ped_partition_new(disk, PED_PARTITION_NORMAL, fs_type, 0, bootsector_size / device->sector_size);
linux_part = ped_partition_new(disk, PED_PARTITION_NORMAL, fs_type, bootsector_size / device->sector_size, device->length - 1);
// add partitions to partition table
PedConstraint* constraint = ped_constraint_any(device);
ped_disk_add_partition(disk, linux_part, constraint);
ped_disk_add_partition(disk, boot_part, constraint);
ped_constraint_destroy(constraint);
// create timer
timer = ped_timer_new(create_ext2_timer, NULL);
// create filesystem
ped_file_system_create(&linux_part->geom, fs_type, timer);
// commit to hardware
ped_disk_commit_to_dev(disk);
ped_disk_commit_to_os(disk);
return 1;
}
/**
* Mounts an ext2 partition to the given directory
*
*/
int mount_partition(const char* partition, const char* mount_point) {
return mount(partition, mount_point, FILE_SYSTEM, 0, NULL);
}
/**
* Umounts the given directory
*
*/
int unmount_partition(const char* mount_point) {
if(mount_point == NULL) {
return 1;
}
return umount(mount_point);
}
/**
* Returns 1 if the user is root, otherwise 0
*
*/
int is_root() {
return geteuid() == 0;
}
/**
* Returns an array of partitions of the given device
*
*
*/
Partition** get_partitions(const char* dev) {
PedDevice* device;
PedDisk* disk;
PedPartition* part;
Partition** partitions = (Partition**)malloc(MAX_PARTITIONS * sizeof(Partition*));
int part_nr = 0;
device = ped_device_get(dev);
if(device == NULL) {
return NULL;
}
disk = ped_disk_new(device);
if(disk == NULL) {
ped_device_destroy(device);
return NULL;
}
for(part = ped_disk_next_partition(disk, NULL); part; part = ped_disk_next_partition(disk, part)) {
if(part->num < 0) {
continue;
}
partitions[part_nr] = (Partition*)malloc(sizeof(Partition));
partitions[part_nr]->num = part->num;
partitions[part_nr]->start = part->geom.start;
partitions[part_nr]->length = part->geom.length;
part_nr++;
}
partitions[part_nr] = NULL;
ped_disk_destroy(disk);
ped_device_destroy(device);
return partitions;
}
/**
* Returns an array of devices
*
*/
Drive** get_devices() {
PedDevice* device = NULL;
Drive** devs = (Drive**)malloc(MAX_DRIVES * sizeof(Drive*));
int drives = 0;
ped_device_probe_all();
while((device = ped_device_get_next(device))) {
devs[drives] = (Drive*)malloc(sizeof(Drive));
devs[drives]->length = device->length;
devs[drives]->sector_size = device->sector_size;
devs[drives]->name = (char*)malloc((strlen(device->path) + 1) * sizeof(char));
strcpy(devs[drives]->name, device->path);
devs[drives]->model = (char*)malloc((strlen(device->model) + 1) * sizeof(char));
strcpy(devs[drives]->model, device->model);
drives++;
}
devs[drives] = NULL;
return devs;
}
/**
* Returns the size of a device
*
*/
unsigned long long get_device_size(const char* dev) {
PedDevice* device = ped_device_get(dev);
if(device == NULL) {
return 0;
}
return device->length * device->sector_size;
}
/**
* Makes a size in bytes human-readable
*
*/
void format_size(char* buffer, unsigned long long size) {
int unit = 0;
const char* units[] = {"MB", "GB", "TB", "PB"};
size /= 1024 * 1024;
double s = size;
while(s >= 1024) {
s /= 1024;
unit++;
}
sprintf(buffer, "%.1f %s", s, units[unit]);
}
unsigned long dd(const char* input, const char* output, unsigned long blocksize) {
FILE* in = fopen(input, "rb");
if(in == NULL) {
return 0;
}
FILE* out = fopen(output, "wb");
if(out == NULL) {
return 0;
}
char* buffer = (char*)malloc(blocksize);
unsigned long read, total = 0;
while(!feof(in)) {
read = fread(buffer, blocksize, 1, in);
fwrite(buffer, blocksize, 1, out);
total += blocksize;
}
fclose(in);
fclose(out);
free(buffer);
return total;
}
unsigned long copy_file(const char* input, const char* output) {
FILE* in = fopen(input, "rb");
if(in == NULL) {
return 0;
}
FILE* out = fopen(output, "wb");
if(out == NULL) {
return 0;
}
char buffer[512];
unsigned long read, total = 0;
while(!feof(in)) {
read = fread(buffer, 1, 512, in);
fwrite(buffer, 1, read, out);
total += read;
}
fclose(in);
fclose(out);
return total;
}
void sync_card() {
sync();
}