summaryrefslogtreecommitdiff
path: root/libdm
diff options
context:
space:
mode:
authorZdenek Kabelac <zkabelac@redhat.com>2008-12-11 16:25:51 +0000
committerZdenek Kabelac <zkabelac@redhat.com>2008-12-11 16:25:51 +0000
commitffa9b6a571b1f22c0c8f1e6cf186e50a6dc9514d (patch)
tree5699a7465f1d25388874976ba34468448f62dc36 /libdm
parent9db16312b88be6febb86747181b76fcd9b2a1927 (diff)
downloadlvm2-ffa9b6a571b1f22c0c8f1e6cf186e50a6dc9514d.tar.gz
Replace _dm_snprintf with EMIT_PARAMS macro for creating target lines
Diffstat (limited to 'libdm')
-rw-r--r--libdm/libdm-deptree.c140
1 files changed, 38 insertions, 102 deletions
diff --git a/libdm/libdm-deptree.c b/libdm/libdm-deptree.c
index a101007c5..d70e1b43a 100644
--- a/libdm/libdm-deptree.c
+++ b/libdm/libdm-deptree.c
@@ -135,22 +135,6 @@ struct dm_tree {
int no_flush; /* 1 sets noflush (mirrors/multipath) */
};
-/* FIXME Consider exporting this */
-static int _dm_snprintf(char *buf, size_t bufsize, const char *format, ...)
-{
- int n;
- va_list ap;
-
- va_start(ap, format);
- n = vsnprintf(buf, bufsize, format, ap);
- va_end(ap);
-
- if (n < 0 || (n > (int) bufsize - 1))
- return -1;
-
- return n;
-}
-
struct dm_tree *dm_tree_create(void)
{
struct dm_tree *dtree;
@@ -1238,27 +1222,32 @@ static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *
return 1;
}
+/* simplify string emiting code */
+#define EMIT_PARAMS(p, str...)\
+ do {\
+ const size_t bufsize = paramsize - (size_t)p;\
+ int w;\
+ \
+ if ((w = snprintf(params + p, bufsize, str)) < 0\
+ || ((size_t)w >= bufsize)) {\
+ stack; /* Out of space */\
+ return -1;\
+ }\
+ p += w;\
+ } while (0)
+
static int _emit_areas_line(struct dm_task *dmt __attribute((unused)),
struct load_segment *seg, char *params,
size_t paramsize, int *pos)
{
struct seg_area *area;
char devbuf[DM_FORMAT_DEV_BUFSIZE];
- int tw;
- const char *prefix = "";
dm_list_iterate_items(area, &seg->areas) {
if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
return_0;
- if ((tw = _dm_snprintf(params + *pos, paramsize - *pos, "%s%s %" PRIu64,
- prefix, devbuf, area->offset)) < 0) {
- stack; /* Out of space */
- return -1;
- }
-
- prefix = " ";
- *pos += tw;
+ EMIT_PARAMS(*pos, " %s %" PRIu64, devbuf, area->offset);
}
return 1;
@@ -1267,9 +1256,8 @@ static int _emit_areas_line(struct dm_task *dmt __attribute((unused)),
static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uint64_t *seg_start, char *params, size_t paramsize)
{
unsigned log_parm_count;
- int pos = 0;
- int tw;
- int r;
+ int pos = 0;
+ int r;
char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
char logbuf[DM_FORMAT_DEV_BUFSIZE];
const char *logtype;
@@ -1289,11 +1277,7 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
if (seg->clustered) {
if (seg->uuid)
log_parm_count++;
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "clustered-")) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
+ EMIT_PARAMS(pos, "clustered-");
}
if (!seg->log)
@@ -1305,61 +1289,25 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
return_0;
}
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s %u ", logtype, log_parm_count)) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
-
- if (seg->log) {
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s ", logbuf)) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
- }
+ EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%u ", seg->region_size)) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
+ if (seg->log)
+ EMIT_PARAMS(pos, " %s", logbuf);
- if (seg->clustered && seg->uuid) {
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s ", seg->uuid)) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
- }
+ EMIT_PARAMS(pos, " %u", seg->region_size);
- if ((seg->flags & DM_NOSYNC)) {
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "nosync ")) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
- } else if ((seg->flags & DM_FORCESYNC)) {
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "sync ")) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
- }
+ if (seg->clustered && seg->uuid)
+ EMIT_PARAMS(pos, " %s", seg->uuid);
- if ((seg->flags & DM_BLOCK_ON_ERROR)) {
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "block_on_error ")) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
- }
+ if ((seg->flags & DM_NOSYNC))
+ EMIT_PARAMS(pos, " nosync");
+ else if ((seg->flags & DM_FORCESYNC))
+ EMIT_PARAMS(pos, " sync");
- if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%u ", seg->mirror_area_count)) < 0) {
- stack; /* Out of space */
- return -1;
- }
- pos += tw;
+ if ((seg->flags & DM_BLOCK_ON_ERROR))
+ EMIT_PARAMS(pos, " block_on_error");
+
+ EMIT_PARAMS(pos, " %u", seg->mirror_area_count);
break;
case SEG_SNAPSHOT:
@@ -1367,30 +1315,16 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
return_0;
if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
return_0;
- if ((pos = _dm_snprintf(params, paramsize, "%s %s %c %d",
- originbuf, cowbuf,
- seg->persistent ? 'P' : 'N',
- seg->chunk_size)) < 0) {
- stack; /* Out of space */
- return -1;
- }
+ EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
+ seg->persistent ? 'P' : 'N', seg->chunk_size);
break;
case SEG_SNAPSHOT_ORIGIN:
if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
return_0;
- if ((pos = _dm_snprintf(params, paramsize, "%s",
- originbuf)) < 0) {
- stack; /* Out of space */
- return -1;
- }
+ EMIT_PARAMS(pos, "%s", originbuf);
break;
case SEG_STRIPED:
- if ((pos = _dm_snprintf(params, paramsize, "%u %u ",
- seg->area_count,
- seg->stripe_size)) < 0) {
- stack; /* Out of space */
- return -1;
- }
+ EMIT_PARAMS(pos, "%u %u", seg->area_count, seg->stripe_size);
break;
}
@@ -1421,6 +1355,8 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
return 1;
}
+#undef EMIT_PARAMS
+
static int _emit_segment(struct dm_task *dmt, struct load_segment *seg,
uint64_t *seg_start)
{