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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
|
#include "fdiskP.h"
#include "strutils.h"
/**
* SECTION: script
* @title: Script
* @short_description: text based sfdisk compatible description of partition table
*
* The libfdisk scripts are based on original sfdisk script (dumps). Each
* script has two parts: script headers and partition table entries
* (partitions).
*
* For more details about script format see sfdisk man page.
*/
/* script header (e.g. unit: sectors) */
struct fdisk_scriptheader {
struct list_head headers;
char *name;
char *data;
};
/* script control struct */
struct fdisk_script {
struct fdisk_table *table;
struct list_head headers;
struct fdisk_context *cxt;
int refcount;
/* parser's state */
size_t nlines;
int fmt; /* input format */
struct fdisk_label *label;
};
static void fdisk_script_free_header(struct fdisk_script *dp, struct fdisk_scriptheader *fi)
{
if (!fi)
return;
DBG(SCRIPT, ul_debugobj(fi, "free header %s", fi->name));
free(fi->name);
free(fi->data);
list_del(&fi->headers);
free(fi);
}
/**
* fdisk_new_script:
* @cxt: context
*
* The script hold fdisk_table and additional information to read/write
* script to the file.
*
* Returns: newly allocated script struct.
*/
struct fdisk_script *fdisk_new_script(struct fdisk_context *cxt)
{
struct fdisk_script *dp = NULL;
dp = calloc(1, sizeof(*dp));
if (!dp)
return NULL;
DBG(SCRIPT, ul_debugobj(dp, "alloc"));
dp->refcount = 1;
dp->cxt = cxt;
fdisk_ref_context(cxt);
dp->table = fdisk_new_table();
if (!dp->table) {
fdisk_unref_script(dp);
return NULL;
}
INIT_LIST_HEAD(&dp->headers);
return dp;
}
/**
* fdisk_new_script_from_file:
* @cxt: context
* @filename: path to the script file
*
* Allocates a new script and reads script from @filename.
*
* Returns: new script instance or NULL in case of error (check errno for more details).
*/
struct fdisk_script *fdisk_new_script_from_file(struct fdisk_context *cxt,
const char *filename)
{
int rc;
FILE *f;
struct fdisk_script *dp, *res = NULL;
assert(cxt);
assert(filename);
DBG(SCRIPT, ul_debug("opening %s", filename));
f = fopen(filename, "r");
if (!f)
return NULL;
dp = fdisk_new_script(cxt);
if (!dp)
goto done;
rc = fdisk_script_read_file(dp, f);
if (rc) {
errno = -rc;
goto done;
}
res = dp;
done:
fclose(f);
if (!res)
fdisk_unref_script(dp);
else
errno = 0;
return res;
}
/**
* fdisk_ref_script:
* @dp: script pointer
*
* Incremparts reference counter.
*/
void fdisk_ref_script(struct fdisk_script *dp)
{
if (dp)
dp->refcount++;
}
static void fdisk_reset_script(struct fdisk_script *dp)
{
assert(dp);
DBG(SCRIPT, ul_debugobj(dp, "reset"));
fdisk_unref_table(dp->table);
dp->table = NULL;
while (!list_empty(&dp->headers)) {
struct fdisk_scriptheader *fi = list_entry(dp->headers.next,
struct fdisk_scriptheader, headers);
fdisk_script_free_header(dp, fi);
}
INIT_LIST_HEAD(&dp->headers);
}
/**
* fdisk_unref_script:
* @dp: script pointer
*
* De-incremparts reference counter, on zero the @dp is automatically
* deallocated.
*/
void fdisk_unref_script(struct fdisk_script *dp)
{
if (!dp)
return;
dp->refcount--;
if (dp->refcount <= 0) {
fdisk_reset_script(dp);
fdisk_unref_context(dp->cxt);
DBG(SCRIPT, ul_debugobj(dp, "free script"));
free(dp);
}
}
static struct fdisk_scriptheader *script_get_header(struct fdisk_script *dp,
const char *name)
{
struct list_head *p;
list_for_each(p, &dp->headers) {
struct fdisk_scriptheader *fi = list_entry(p, struct fdisk_scriptheader, headers);
if (strcasecmp(fi->name, name) == 0)
return fi;
}
return NULL;
}
/**
* fdisk_script_get_header:
* @dp: script instance
* @name: header name
*
* Returns: pointer to header data or NULL.
*/
const char *fdisk_script_get_header(struct fdisk_script *dp, const char *name)
{
struct fdisk_scriptheader *fi;
assert(dp);
assert(name);
fi = script_get_header(dp, name);
return fi ? fi->data : NULL;
}
/**
* fdisk_script_set_header:
* @dp: script instance
* @name: header name
* @data: header data (or NULL)
*
* The headers are used as global options (in script) for whole partition
* table, always one header per line.
*
* If no @data specified then the header is removed. If header does not exist
* and @data specified then a new header added.
*
* Note that libfdisk allows to specify arbitrary custom header, the default
* build-in headers are "unit" and "label", and some label specific headers
* (for example "uuid" and "name" for GPT).
*
* Returns: 0 on success, <0 on error
*/
int fdisk_script_set_header(struct fdisk_script *dp,
const char *name,
const char *data)
{
struct fdisk_scriptheader *fi;
assert(dp);
assert(name);
if (!dp || !name)
return -EINVAL;
fi = script_get_header(dp, name);
if (!fi && !data)
return 0; /* want to remove header that does not exist, success */
if (!data) {
/* no data, remove the header */
fdisk_script_free_header(dp, fi);
return 0;
}
if (!fi) {
/* new header */
fi = calloc(1, sizeof(*fi));
if (!fi)
return -ENOMEM;
INIT_LIST_HEAD(&fi->headers);
fi->name = strdup(name);
fi->data = strdup(data);
if (!fi->data || !fi->name) {
fdisk_script_free_header(dp, fi);
return -ENOMEM;
}
list_add_tail(&fi->headers, &dp->headers);
} else {
/* update existing */
char *x = strdup(data);
if (!x)
return -ENOMEM;
free(fi->data);
fi->data = x;
}
if (strcmp(name, "label") == 0)
dp->label = NULL;
return 0;
}
/**
* fdisk_script_get_table:
* @dp: script
*
* The table (container with partitions) is possible to create by
* fdisk_script_read_context() or fdisk_script_read_file(), otherwise
* this function returns NULL.
*
* Returns: NULL or script.
*/
struct fdisk_table *fdisk_script_get_table(struct fdisk_script *dp)
{
assert(dp);
return dp ? dp->table : NULL;
}
static struct fdisk_label *script_get_label(struct fdisk_script *dp)
{
assert(dp);
assert(dp->cxt);
if (!dp->label) {
dp->label = fdisk_get_label(dp->cxt,
fdisk_script_get_header(dp, "label"));
DBG(SCRIPT, ul_debugobj(dp, "label '%s'", dp->label ? dp->label->name : ""));
}
return dp->label;
}
/**
* fdisk_script_get_nlines:
* @dp: script
*
* Returns: number of parsed lines or <0 on error.
*/
int fdisk_script_get_nlines(struct fdisk_script *dp)
{
assert(dp);
return dp->nlines;
}
/**
* fdisk_script_read_context:
* @dp: script
* @cxt: context
*
* Reads data from the @cxt context (on disk partition table) into the script.
* If the context is no specified than defaults to context used for fdisk_new_script().
*
* Return: 0 on success, <0 on error.
*/
int fdisk_script_read_context(struct fdisk_script *dp, struct fdisk_context *cxt)
{
struct fdisk_label *lb;
int rc;
char *p = NULL;
assert(dp);
if (!cxt)
cxt = dp->cxt;
if (!dp || !cxt)
return -EINVAL;
DBG(SCRIPT, ul_debugobj(dp, "reading context into script"));
fdisk_reset_script(dp);
lb = fdisk_get_label(cxt, NULL);
if (!lb)
return -EINVAL;
/* allocate and fill new table */
rc = fdisk_get_partitions(cxt, &dp->table);
if (rc)
return rc;
/* generate headers */
rc = fdisk_script_set_header(dp, "label", fdisk_label_get_name(lb));
if (!rc && fdisk_get_disklabel_id(cxt, &p) == 0 && p) {
rc = fdisk_script_set_header(dp, "label-id", p);
free(p);
}
if (!rc && cxt->dev_path)
rc = fdisk_script_set_header(dp, "device", cxt->dev_path);
if (!rc)
rc = fdisk_script_set_header(dp, "unit", "sectors");
/* TODO: label specific headers (e.g. uuid for GPT) */
DBG(SCRIPT, ul_debugobj(dp, "read context done [rc=%d]", rc));
return rc;
}
/**
* fdisk_script_write_file:
* @dp: script
* @f: output file
*
* Writes script @dp to the ile @f.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_script_write_file(struct fdisk_script *dp, FILE *f)
{
struct list_head *h;
struct fdisk_partition *pa;
struct fdisk_iter itr;
const char *devname = NULL;
assert(dp);
assert(f);
DBG(SCRIPT, ul_debugobj(dp, "writing script to file"));
/* script headers */
list_for_each(h, &dp->headers) {
struct fdisk_scriptheader *fi = list_entry(h, struct fdisk_scriptheader, headers);
fprintf(f, "%s: %s\n", fi->name, fi->data);
if (strcmp(fi->name, "device") == 0)
devname = fi->data;
}
if (!dp->table) {
DBG(SCRIPT, ul_debugobj(dp, "script table empty"));
return 0;
}
DBG(SCRIPT, ul_debugobj(dp, "%zu entries", fdisk_table_get_nents(dp->table)));
fputc('\n', f);
fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
while (fdisk_table_next_partition(dp->table, &itr, &pa) == 0) {
char *p = NULL;
if (devname)
p = fdisk_partname(devname, pa->partno + 1);
if (p) {
DBG(SCRIPT, ul_debugobj(dp, "write %s entry", p));
fprintf(f, "%s :", p);
} else
fprintf(f, "%zu :", pa->partno + 1);
if (fdisk_partition_has_start(pa))
fprintf(f, " start=%12ju", pa->start);
if (fdisk_partition_has_size(pa))
fprintf(f, ", size=%12ju", pa->size);
if (pa->type && fdisk_parttype_get_string(pa->type))
fprintf(f, ", type=%s", fdisk_parttype_get_string(pa->type));
else if (pa->type)
fprintf(f, ", type=%x", fdisk_parttype_get_code(pa->type));
if (pa->uuid)
fprintf(f, ", uuid=%s", pa->uuid);
if (pa->name && *pa->name)
fprintf(f, ", name=\"%s\"", pa->name);
/* for MBR attr=80 means bootable */
if (pa->attrs) {
struct fdisk_label *lb = script_get_label(dp);
if (!lb || fdisk_label_get_type(lb) != FDISK_DISKLABEL_DOS)
fprintf(f, ", attrs=\"%s\"", pa->attrs);
}
if (fdisk_partition_is_bootable(pa))
fprintf(f, ", bootable");
fputc('\n', f);
}
DBG(SCRIPT, ul_debugobj(dp, "write script done"));
return 0;
}
static inline int is_header_line(const char *s)
{
const char *p = strchr(s, ':');
if (!p || p == s || !*(p + 1) || strchr(s, '='))
return 0;
return 1;
}
/* parses "<name>: value", note modifies @s*/
static int parse_header_line(struct fdisk_script *dp, char *s)
{
int rc = -EINVAL;
char *name, *value;
DBG(SCRIPT, ul_debugobj(dp, " parse header '%s'", s));
if (!s || !*s)
return -EINVAL;
name = s;
value = strchr(s, ':');
if (!value)
goto done;
*value = '\0';
value++;
ltrim_whitespace((unsigned char *) name);
rtrim_whitespace((unsigned char *) name);
ltrim_whitespace((unsigned char *) value);
rtrim_whitespace((unsigned char *) value);
if (strcmp(name, "label") == 0) {
if (dp->cxt && !fdisk_get_label(dp->cxt, value))
goto done; /* unknown label name */
} else if (strcmp(name, "unit") == 0) {
if (strcmp(value, "sectors") != 0)
goto done; /* only "sectors" supported */
} else if (strcmp(name, "label-id") == 0
|| strcmp(name, "device") == 0) {
; /* whatever is posssible */
} else
goto done; /* unknown header */
if (*name && *value)
rc = fdisk_script_set_header(dp, name, value);
done:
if (rc)
DBG(SCRIPT, ul_debugobj(dp, "header parse error: "
"[rc=%d, name='%s', value='%s']",
rc, name, value));
return rc;
}
/* returns zero terminated string with next token and @str is updated */
static char *next_token(char **str)
{
char *tk_begin = NULL,
*tk_end = NULL,
*end = NULL,
*p;
int open_quote = 0;
for (p = *str; p && *p; p++) {
if (!tk_begin) {
if (isblank(*p))
continue;
tk_begin = *p == '"' ? p + 1 : p;
}
if (*p == '"')
open_quote ^= 1;
if (open_quote)
continue;
if (isblank(*p) || *p == ',' || *p == ';' || *p == '"' )
tk_end = p;
else if (*(p + 1) == '\0')
tk_end = p + 1;
if (tk_begin && tk_end)
break;
}
if (!tk_end)
return NULL;
end = isblank(*tk_end) ? (char *) skip_blank(tk_end) : tk_end;
if (*end == ',' || *end == ';')
end++;
*tk_end = '\0';
*str = end;
return tk_begin;
}
static int next_number(char **s, uint64_t *num, int *power)
{
char *tk;
int rc = -EINVAL;
assert(num);
assert(s);
tk = next_token(s);
if (tk)
rc = parse_size(tk, (uintmax_t *) num, power);
return rc;
}
static int next_string(char **s, char **str)
{
char *tk;
int rc = -EINVAL;
assert(s);
assert(str);
tk = next_token(s);
if (tk) {
*str = strdup(tk);
rc = !*str ? -ENOMEM : 0;
}
return rc;
}
static int partno_from_devname(char *s)
{
int pno;
size_t sz;
char *end, *p;
sz = rtrim_whitespace((unsigned char *)s);
p = s + sz - 1;
while (p > s && isdigit(*(p - 1)))
p--;
errno = 0;
pno = strtol(p, &end, 10);
if (errno || !end || p == end)
return -1;
return pno - 1;
}
/* dump format
* <device>: start=<num>, size=<num>, type=<string>, ...
*/
static int parse_script_line(struct fdisk_script *dp, char *s)
{
char *p, *x;
struct fdisk_partition *pa;
int rc = 0;
uint64_t num;
int pno;
assert(dp);
assert(s);
DBG(SCRIPT, ul_debugobj(dp, " parse script line: '%s'", s));
pa = fdisk_new_partition();
if (!pa)
return -ENOMEM;
fdisk_partition_start_follow_default(pa, 1);
fdisk_partition_end_follow_default(pa, 1);
fdisk_partition_partno_follow_default(pa, 1);
/* set partno */
p = strchr(s, ':');
x = strchr(s, '=');
if (p && (!x || p < x)) {
*p = '\0';
p++;
pno = partno_from_devname(s);
if (pno >= 0) {
fdisk_partition_partno_follow_default(pa, 0);
fdisk_partition_set_partno(pa, pno);
}
} else
p = s;
while (rc == 0 && p && *p) {
DBG(SCRIPT, ul_debugobj(dp, " parsing '%s'", p));
p = (char *) skip_blank(p);
if (!strncasecmp(p, "start=", 6)) {
p += 6;
rc = next_number(&p, &num, NULL);
if (!rc) {
fdisk_partition_set_start(pa, num);
fdisk_partition_start_follow_default(pa, 0);
}
} else if (!strncasecmp(p, "size=", 5)) {
int pow = 0;
p += 5;
rc = next_number(&p, &num, &pow);
if (!rc) {
if (pow) /* specified as <num><suffix> */
num /= dp->cxt->sector_size;
else /* specified as number of sectors */
fdisk_partition_size_explicit(pa, 1);
fdisk_partition_set_size(pa, num);
fdisk_partition_end_follow_default(pa, 0);
}
} else if (!strncasecmp(p, "bootable", 8)) {
char *tk = next_token(&p);
if (strcmp(tk, "bootable") == 0)
pa->boot = 1;
else
rc = -EINVAL;
} else if (!strncasecmp(p, "attrs=", 6)) {
p += 6;
rc = next_string(&p, &pa->attrs);
} else if (!strncasecmp(p, "uuid=", 5)) {
p += 5;
rc = next_string(&p, &pa->uuid);
} else if (!strncasecmp(p, "name=", 5)) {
p += 5;
rc = next_string(&p, &pa->name);
} else if (!strncasecmp(p, "type=", 5) ||
!strncasecmp(p, "Id=", 3)) { /* backward compatiility */
char *type;
p += (*p == 'I' ? 3 : 5); /* "Id=" or "type=" */
rc = next_string(&p, &type);
if (rc)
break;
pa->type = fdisk_label_parse_parttype(
script_get_label(dp), type);
free(type);
if (!pa->type || fdisk_parttype_is_unknown(pa->type)) {
rc = -EINVAL;
fdisk_unref_parttype(pa->type);
pa->type = NULL;
break;
}
} else {
DBG(SCRIPT, ul_debugobj(dp, "script parse error: unknown field '%s'", p));
rc = -EINVAL;
break;
}
}
if (!rc)
rc = fdisk_table_add_partition(dp->table, pa);
if (rc)
DBG(SCRIPT, ul_debugobj(dp, "script parse error: [rc=%d]", rc));
fdisk_unref_partition(pa);
return rc;
}
/* original sfdisk supports partition types shortcuts like 'L' = Linux native
*/
static struct fdisk_parttype *translate_type_shortcuts(struct fdisk_script *dp, char *str)
{
struct fdisk_label *lb;
const char *type = NULL;
if (strlen(str) != 1)
return NULL;
lb = script_get_label(dp);
if (!lb)
return NULL;
if (lb->id == FDISK_DISKLABEL_DOS) {
switch (*str) {
case 'L': /* Linux */
type = "83";
break;
case 'S': /* Swap */
type = "82";
break;
case 'E': /* Dos extended */
type = "05";
break;
case 'X': /* Linux extended */
type = "85";
break;
}
} else if (lb->id == FDISK_DISKLABEL_GPT) {
switch (*str) {
case 'L': /* Linux */
type = "0FC63DAF-8483-4772-8E79-3D69D8477DE4";
break;
case 'S': /* Swap */
type = "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F";
break;
case 'H': /* Home */
type = "933AC7E1-2EB4-4F13-B844-0E14E2AEF915";
break;
}
}
return type ? fdisk_label_parse_parttype(lb, type) : NULL;
}
/* simple format:
* <start>, <size>, <type>, <bootable>, ...
*/
static int parse_commas_line(struct fdisk_script *dp, char *s)
{
int rc = 0;
char *p = s, *str;
struct fdisk_partition *pa;
enum { ITEM_START, ITEM_SIZE, ITEM_TYPE, ITEM_BOOTABLE };
int item = -1;
assert(dp);
assert(s);
pa = fdisk_new_partition();
if (!pa)
return -ENOMEM;
fdisk_partition_start_follow_default(pa, 1);
fdisk_partition_end_follow_default(pa, 1);
fdisk_partition_partno_follow_default(pa, 1);
while (rc == 0 && p && *p) {
uint64_t num;
char *begin;
p = (char *) skip_blank(p);
item++;
DBG(SCRIPT, ul_debugobj(dp, " parsing item %d ('%s')", item, p));
begin = p;
switch (item) {
case ITEM_START:
if (*p == ',' || *p == ';')
fdisk_partition_start_follow_default(pa, 1);
else {
rc = next_number(&p, &num, NULL);
if (!rc)
fdisk_partition_set_start(pa, num);
fdisk_partition_start_follow_default(pa, 0);
}
break;
case ITEM_SIZE:
if (*p == ',' || *p == ';' || *p == '+')
fdisk_partition_end_follow_default(pa, 1);
else {
int pow = 0;
rc = next_number(&p, &num, &pow);
if (!rc) {
if (pow) /* specified as <size><suffix> */
num /= dp->cxt->sector_size;
else /* specified as number of sectors */
fdisk_partition_size_explicit(pa, 1);
fdisk_partition_set_size(pa, num);
}
fdisk_partition_end_follow_default(pa, 0);
}
break;
case ITEM_TYPE:
if (*p == ',' || *p == ';')
break; /* use default type */
rc = next_string(&p, &str);
if (rc)
break;
pa->type = translate_type_shortcuts(dp, str);
if (!pa->type)
pa->type = fdisk_label_parse_parttype(
script_get_label(dp), str);
free(str);
if (!pa->type || fdisk_parttype_is_unknown(pa->type)) {
rc = -EINVAL;
fdisk_unref_parttype(pa->type);
pa->type = NULL;
break;
}
break;
case ITEM_BOOTABLE:
if (*p == ',' || *p == ';')
break;
else {
char *tk = next_token(&p);
if (tk && *tk == '*' && *(tk + 1) == '\0')
pa->boot = 1;
else if (tk && *tk == '-' && *(tk + 1) == '\0')
pa->boot = 0;
else
rc = -EINVAL;
}
break;
default:
break;
}
if (begin == p)
p++;
}
if (!rc)
rc = fdisk_table_add_partition(dp->table, pa);
if (rc)
DBG(SCRIPT, ul_debugobj(dp, "script parse error: [rc=%d]", rc));
fdisk_unref_partition(pa);
return rc;
}
/* modifies @s ! */
int fdisk_script_read_buffer(struct fdisk_script *dp, char *s)
{
int rc = 0;
assert(dp);
assert(s);
DBG(SCRIPT, ul_debugobj(dp, " parsing buffer"));
s = (char *) skip_blank(s);
if (!s || !*s)
return 0; /* nothing baby, ignore */
if (!dp->table) {
dp->table = fdisk_new_table();
if (!dp->table)
return -ENOMEM;
}
/* parse header lines only if no partition specified yet */
if (fdisk_table_is_empty(dp->table) && is_header_line(s))
rc = parse_header_line(dp, s);
/* parse script format */
else if (strchr(s, '='))
rc = parse_script_line(dp, s);
/* parse simple <value>, ... format */
else
rc = parse_commas_line(dp, s);
if (rc)
DBG(SCRIPT, ul_debugobj(dp, "%zu: parse error [rc=%d]",
dp->nlines, rc));
return rc;
}
/**
* fdisk_script_read_line:
* @dp: script
* @f: file
* @buf: buffer to store one line of the file
* @bufsz: buffer size
*
* Reads next line into dump.
*
* Returns: 0 on success, <0 on error, 1 when nothing to read.
*/
int fdisk_script_read_line(struct fdisk_script *dp, FILE *f, char *buf, size_t bufsz)
{
char *s;
assert(dp);
assert(f);
DBG(SCRIPT, ul_debugobj(dp, " parsing line %zu", dp->nlines));
/* read the next non-blank non-comment line */
do {
if (fgets(buf, bufsz, f) == NULL)
return 1;
dp->nlines++;
s = strchr(buf, '\n');
if (!s) {
/* Missing final newline? Otherwise an extremely */
/* long line - assume file was corrupted */
if (feof(f)) {
DBG(SCRIPT, ul_debugobj(dp, "no final newline"));
s = strchr(buf, '\0');
} else {
DBG(SCRIPT, ul_debugobj(dp,
"%zu: missing newline at line", dp->nlines));
return -EINVAL;
}
}
*s = '\0';
if (--s >= buf && *s == '\r')
*s = '\0';
s = (char *) skip_blank(buf);
} while (*s == '\0' || *s == '#');
return fdisk_script_read_buffer(dp, s);
}
/**
* fdisk_script_read_file:
* @dp: script
* @f: input file
*
* Reads file @f into script @dp.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_script_read_file(struct fdisk_script *dp, FILE *f)
{
char buf[BUFSIZ];
int rc;
assert(dp);
assert(f);
DBG(SCRIPT, ul_debugobj(dp, "parsing file"));
while (!feof(f)) {
rc = fdisk_script_read_line(dp, f, buf, sizeof(buf));
if (rc)
break;
}
if (rc == 1)
rc = 0; /* end of file */
DBG(SCRIPT, ul_debugobj(dp, "parsing file done [rc=%d]", rc));
return rc;
}
/**
* fdisk_set_script:
* @cxt: context
* @dp: script (or NULL to remove previous reference)
*
* Sets reference to the @dp script. The script headers might be used by label
* drivers to overwrite built-in defaults (for example disk label Id) and label
* driver might optimize the default semantic to be more usable for scripts
* (for example to not ask for primary/logical/extended partition type).
*
* Note that script also contains reference to the fdisk context (see
* fdisk_new_script()). This context may be completely independent on
* context used for fdisk_set_script().
*
* Returns: <0 on error, 0 on success.
*/
int fdisk_set_script(struct fdisk_context *cxt, struct fdisk_script *dp)
{
assert(cxt);
/* unref old */
if (cxt->script)
fdisk_unref_script(cxt->script);
/* ref new */
cxt->script = dp;
if (cxt->script) {
DBG(CXT, ul_debugobj(cxt, "setting reference to script %p", cxt->script));
fdisk_ref_script(cxt->script);
}
return 0;
}
/**
* fdisk_get_script:
* @cxt: context
*
* Returns: the current script or NULL.
*/
struct fdisk_script *fdisk_get_script(struct fdisk_context *cxt)
{
assert(cxt);
return cxt->script;
}
/**
* fdisk_apply_script_headers:
* @cxt: context
* @dp: script
*
* Associte context @cxt with script @dp and creates a new empty disklabel.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_apply_script_headers(struct fdisk_context *cxt, struct fdisk_script *dp)
{
const char *name;
assert(cxt);
assert(dp);
DBG(SCRIPT, ul_debugobj(dp, "applying script headers"));
fdisk_set_script(cxt, dp);
/* create empty label */
name = fdisk_script_get_header(dp, "label");
if (!name)
return -EINVAL;
return fdisk_create_disklabel(cxt, name);
}
/**
* fdisk_apply_script:
* @cxt: context
* @dp: script
*
* This function creates a new disklabel and partition within context @cxt. You
* have to call fdisk_write_disklabel() to apply changes to the device.
*
* Returns: 0 on error, <0 on error.
*/
int fdisk_apply_script(struct fdisk_context *cxt, struct fdisk_script *dp)
{
int rc;
struct fdisk_script *old;
assert(dp);
assert(cxt);
DBG(CXT, ul_debugobj(cxt, "applying script %p", dp));
old = fdisk_get_script(cxt);
/* create empty disk label */
rc = fdisk_apply_script_headers(cxt, dp);
/* create partitions */
if (!rc && dp->table)
rc = fdisk_apply_table(cxt, dp->table);
fdisk_set_script(cxt, old);
DBG(CXT, ul_debugobj(cxt, "script done [rc=%d]", rc));
return rc;
}
#ifdef TEST_PROGRAM
int test_dump(struct fdisk_test *ts, int argc, char *argv[])
{
char *devname = argv[1];
struct fdisk_context *cxt;
struct fdisk_script *dp;
cxt = fdisk_new_context();
fdisk_assign_device(cxt, devname, 1);
dp = fdisk_new_script(cxt);
fdisk_script_read_context(dp, NULL);
fdisk_script_write_file(dp, stdout);
fdisk_unref_script(dp);
fdisk_unref_context(cxt);
return 0;
}
int test_read(struct fdisk_test *ts, int argc, char *argv[])
{
char *filename = argv[1];
struct fdisk_script *dp;
struct fdisk_context *cxt;
FILE *f;
if (!(f = fopen(filename, "r")))
err(EXIT_FAILURE, "%s: cannot open", filename);
cxt = fdisk_new_context();
dp = fdisk_new_script(cxt);
fdisk_script_read_file(dp, f);
fclose(f);
fdisk_script_write_file(dp, stdout);
fdisk_unref_script(dp);
fdisk_unref_context(cxt);
return 0;
}
int test_stdin(struct fdisk_test *ts, int argc, char *argv[])
{
char buf[BUFSIZ];
struct fdisk_script *dp;
struct fdisk_context *cxt;
int rc = 0;
cxt = fdisk_new_context();
dp = fdisk_new_script(cxt);
fdisk_script_set_header(dp, "label", "dos");
printf("<start>, <size>, <type>, <bootable: *|->\n");
do {
struct fdisk_partition *pa;
size_t n = fdisk_table_get_nents(dp->table);
printf(" #%zu :\n", n + 1);
rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf));
if (rc == 0) {
pa = fdisk_table_get_partition(dp->table, n);
printf(" #%zu %12ju %12ju\n", n + 1,
fdisk_partition_get_start(pa),
fdisk_partition_get_size(pa));
}
} while (rc == 0);
if (!rc)
fdisk_script_write_file(dp, stdout);
fdisk_unref_script(dp);
fdisk_unref_context(cxt);
return rc;
}
int test_apply(struct fdisk_test *ts, int argc, char *argv[])
{
char *devname = argv[1], *scriptname = argv[2];
struct fdisk_context *cxt;
struct fdisk_script *dp = NULL;
struct fdisk_table *tb = NULL;
struct fdisk_iter *itr = NULL;
struct fdisk_partition *pa = NULL;
int rc;
cxt = fdisk_new_context();
fdisk_assign_device(cxt, devname, 0);
dp = fdisk_new_script_from_file(cxt, scriptname);
if (!dp)
return -errno;
rc = fdisk_apply_script(cxt, dp);
if (rc)
goto done;
fdisk_unref_script(dp);
/* list result */
fdisk_list_disklabel(cxt);
fdisk_get_partitions(cxt, &tb);
itr = fdisk_new_iter(FDISK_ITER_FORWARD);
while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
printf(" #%zu %12ju %12ju\n", fdisk_partition_get_partno(pa),
fdisk_partition_get_start(pa),
fdisk_partition_get_size(pa));
}
done:
fdisk_free_iter(itr);
fdisk_unref_table(tb);
/*fdisk_write_disklabel(cxt);*/
fdisk_unref_context(cxt);
return 0;
}
int main(int argc, char *argv[])
{
struct fdisk_test tss[] = {
{ "--dump", test_dump, "<device> dump PT as script" },
{ "--read", test_read, "<file> read PT script from file" },
{ "--apply", test_apply, "<device> <file> try apply script from file to device" },
{ "--stdin", test_stdin, " read input like sfdisk" },
{ NULL }
};
return fdisk_run_test(tss, argc, argv);
}
#endif
|