summaryrefslogtreecommitdiffstats
path: root/libfdisk/src/partition.c
blob: 12ac435fa9b791d5994dd030765d98a0ddff1d68 (plain) (blame)
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

#include "c.h"
#include "strutils.h"

#include "fdiskP.h"

struct fdisk_partition *fdisk_new_partition(void)
{
	struct fdisk_partition *pa = calloc(1, sizeof(*pa));

	DBG(PART, dbgprint("new %p", pa));
	return pa;
}

void fdisk_reset_partition(struct fdisk_partition *pa)
{
	if (!pa)
		return;
	fdisk_free_parttype(pa->type);
	free(pa->name);
	free(pa->uuid);
	free(pa->attrs);
	memset(pa, 0, sizeof(*pa));
}

void fdisk_free_partition(struct fdisk_partition *pa)
{
	if (!pa)
		return;
	fdisk_reset_partition(pa);
	DBG(PART, dbgprint("free %p", pa));
	free(pa);
}

int fdisk_partition_set_start(struct fdisk_partition *pa, uint64_t off)
{
	if (!pa)
		return -EINVAL;
	pa->start = off;
	return 0;
}

uint64_t fdisk_partition_get_start(struct fdisk_partition *pa)
{
	return pa ? pa->start : 0;
}

int fdisk_partition_set_end(struct fdisk_partition *pa, uint64_t off, int isrel)
{
	if (!pa)
		return -EINVAL;
	pa->end = off;
	pa->size = 0;
	pa->endrel = isrel ? 1 : 0;
	return 0;
}

uint64_t fdisk_partition_get_end(struct fdisk_partition *pa)
{
	return pa ? pa->start : 0;
}


int fdisk_partition_set_size(struct fdisk_partition *pa, uint64_t size)
{
	if (!pa)
		return -EINVAL;
	pa->size = size;
	pa->end = 0;
	return 0;
}

uint64_t fdisk_partition_get_size(struct fdisk_partition *pa)
{
	return pa ? pa->size : 0;
}

int fdisk_partition_set_partno(struct fdisk_partition *pa, size_t n)
{
	if (!pa)
		return -EINVAL;
	pa->partno = n;
	return 0;
}

size_t fdisk_partition_get_partno(struct fdisk_partition *pa)
{
	return pa ? pa->partno : (size_t) -1;
}

int fdisk_partition_set_type(struct fdisk_partition *pa, struct fdisk_parttype *type)
{
	if (!pa)
		return -EINVAL;
	pa->type = type;
	return 0;
}

const struct fdisk_parttype *fdisk_partition_get_type(struct fdisk_partition *pa)
{
	return pa ? pa->type : NULL;
}

int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name)
{
	char *p = NULL;

	if (!pa)
		return -EINVAL;
	if (name) {
	       p = strdup(name);
	       if (!p)
		       return -ENOMEM;
	}
	free(pa->name);
	pa->name = p;
	return 0;
}

const char *fdisk_partition_get_name(struct fdisk_partition *pa)
{
	return pa ? pa->name : NULL;
}

int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid)
{
	char *p = NULL;

	if (!pa)
		return -EINVAL;
	if (uuid) {
	       p = strdup(uuid);
	       if (!p)
		       return -ENOMEM;
	}
	free(pa->uuid);
	pa->uuid = p;
	return 0;
}

const char *fdisk_partition_get_uuid(struct fdisk_partition *pa)
{
	return pa ? pa->uuid : NULL;
}

const char *fdisk_partition_get_attrs(struct fdisk_partition *pa)
{
	return pa ? pa->attrs : NULL;
}

/* nested partition means logical (within extended partition) */
int fdisk_partition_set_nested(struct fdisk_partition *pa, int nested)
{
	if (!pa)
		return -EINVAL;
	pa->nested = nested ? 1 : 0;
	return 0;
}

int fdisk_partition_is_nested(struct fdisk_partition *pa)
{
	return pa && pa->nested;
}

int fdisk_partition_is_used(struct fdisk_partition *pa)
{
	return pa && pa->used;
}


/**
 * fdisk_partition_to_string:
 * @pa: partition
 * @id: column (FDISK_COL_*)
 * @data: returns string with allocated data
 *
 * Returns info about partition converted to printable string.
 *
 * For exmaple
 *
 *      struct fdisk_parition *pa = fdisk_new_partition();
 *
 *      fdisk_get_partition(cxt, 0, &pa);
 *	fdisk_partition_to_string(pa, FDISK_COL_UUID, &data);
 *	printf("first partition uuid: %s\n", data);
 *	free(data);
 *	fdisk_free_partition(pa);
 *
 * returns UUID for the first partition.
 *
 * Returns 0 on success, otherwise, a corresponding error.
 */

int fdisk_partition_to_string(struct fdisk_partition *pa,
			      int id,
			      char **data)
{
	char *p = NULL;
	int rc = 0;

	if (!pa || !pa->cxt)
		return -EINVAL;

	switch (id) {
	case FDISK_COL_DEVICE:
		if (pa->cxt->label->flags & FDISK_LABEL_FL_INCHARS_PARTNO)
			rc = asprintf(&p, "%c", (int) pa->partno + 'a');
		else
			p = fdisk_partname(pa->cxt->dev_path, pa->partno + 1);
		break;
	case FDISK_COL_START:
		rc = pa->start_post ?
				asprintf(&p, "%ju%c", pa->start, pa->start_post) :
				asprintf(&p, "%ju", pa->start);
		break;
	case FDISK_COL_END:
		rc = pa->end_post ?
				asprintf(&p, "%ju%c", pa->end, pa->end_post) :
				asprintf(&p, "%ju", pa->end);
		break;
	case FDISK_COL_SIZE:
		if (fdisk_context_display_details(pa->cxt)) {
			rc = pa->size_post ?
					asprintf(&p, "%ju%c", pa->size, pa->size_post) :
					asprintf(&p, "%ju", pa->size);
		} else {
			p = size_to_human_string(SIZE_SUFFIX_1LETTER, pa->size);
			if (!p)
				rc = -ENOMEM;
		}
		break;
	case FDISK_COL_BSIZE:
		rc = asprintf(&p, "%ju", pa->bsize);
		break;
	case FDISK_COL_FSIZE:
		rc = asprintf(&p, "%ju", pa->fsize);
		break;
	case FDISK_COL_CPG:
		rc = asprintf(&p, "%ju", pa->cpg);
		break;
	case FDISK_COL_TYPE:
		p = pa->type && pa->type->name ? strdup(pa->type->name) : NULL;
		break;
	case FDISK_COL_UUID:
		p = pa->uuid ? strdup(pa->uuid) : NULL;
		break;
	case FDISK_COL_NAME:
		p = pa->name ? strdup(pa->name) : NULL;
		break;
	case FDISK_COL_ATTR:
		p = pa->attrs ? strdup(pa->attrs) : NULL;
		break;
	default:
		return -EINVAL;
	}

	if (rc < 0)
		rc = -ENOMEM;
	else if (rc > 0)
		rc = 0;

	if (data)
		*data = p;
	return rc;
}