1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

Replace _dm_snprintf with EMIT_PARAMS macro for creating target lines

This commit is contained in:
Zdenek Kabelac 2008-12-11 16:25:51 +00:00
parent 9db16312b8
commit ffa9b6a571
2 changed files with 38 additions and 101 deletions

View File

@ -1,5 +1,6 @@
Version 1.02.30 - Version 1.02.30 -
==================================== ====================================
Replace _dm_snprintf with EMIT_PARAMS macro for creating target lines
Version 1.02.29 - 10th November 2008 Version 1.02.29 - 10th November 2008
==================================== ====================================

View File

@ -135,22 +135,6 @@ struct dm_tree {
int no_flush; /* 1 sets noflush (mirrors/multipath) */ 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 *dm_tree_create(void)
{ {
struct dm_tree *dtree; struct dm_tree *dtree;
@ -1238,27 +1222,32 @@ static int _build_dev_string(char *devbuf, size_t bufsize, struct dm_tree_node *
return 1; 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)), static int _emit_areas_line(struct dm_task *dmt __attribute((unused)),
struct load_segment *seg, char *params, struct load_segment *seg, char *params,
size_t paramsize, int *pos) size_t paramsize, int *pos)
{ {
struct seg_area *area; struct seg_area *area;
char devbuf[DM_FORMAT_DEV_BUFSIZE]; char devbuf[DM_FORMAT_DEV_BUFSIZE];
int tw;
const char *prefix = "";
dm_list_iterate_items(area, &seg->areas) { dm_list_iterate_items(area, &seg->areas) {
if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node)) if (!_build_dev_string(devbuf, sizeof(devbuf), area->dev_node))
return_0; return_0;
if ((tw = _dm_snprintf(params + *pos, paramsize - *pos, "%s%s %" PRIu64, EMIT_PARAMS(*pos, " %s %" PRIu64, devbuf, area->offset);
prefix, devbuf, area->offset)) < 0) {
stack; /* Out of space */
return -1;
}
prefix = " ";
*pos += tw;
} }
return 1; return 1;
@ -1268,7 +1257,6 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
{ {
unsigned log_parm_count; unsigned log_parm_count;
int pos = 0; int pos = 0;
int tw;
int r; int r;
char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE]; char originbuf[DM_FORMAT_DEV_BUFSIZE], cowbuf[DM_FORMAT_DEV_BUFSIZE];
char logbuf[DM_FORMAT_DEV_BUFSIZE]; char logbuf[DM_FORMAT_DEV_BUFSIZE];
@ -1289,11 +1277,7 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
if (seg->clustered) { if (seg->clustered) {
if (seg->uuid) if (seg->uuid)
log_parm_count++; log_parm_count++;
if ((tw = _dm_snprintf(params + pos, paramsize - pos, "clustered-")) < 0) { EMIT_PARAMS(pos, "clustered-");
stack; /* Out of space */
return -1;
}
pos += tw;
} }
if (!seg->log) if (!seg->log)
@ -1305,61 +1289,25 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
return_0; return_0;
} }
if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s %u ", logtype, log_parm_count)) < 0) { EMIT_PARAMS(pos, "%s %u", logtype, log_parm_count);
stack; /* Out of space */
return -1;
}
pos += tw;
if (seg->log) { if (seg->log)
if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s ", logbuf)) < 0) { EMIT_PARAMS(pos, " %s", logbuf);
stack; /* Out of space */
return -1;
}
pos += tw;
}
if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%u ", seg->region_size)) < 0) { EMIT_PARAMS(pos, " %u", seg->region_size);
stack; /* Out of space */
return -1;
}
pos += tw;
if (seg->clustered && seg->uuid) { if (seg->clustered && seg->uuid)
if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%s ", seg->uuid)) < 0) { EMIT_PARAMS(pos, " %s", seg->uuid);
stack; /* Out of space */
return -1;
}
pos += tw;
}
if ((seg->flags & DM_NOSYNC)) { if ((seg->flags & DM_NOSYNC))
if ((tw = _dm_snprintf(params + pos, paramsize - pos, "nosync ")) < 0) { EMIT_PARAMS(pos, " nosync");
stack; /* Out of space */ else if ((seg->flags & DM_FORCESYNC))
return -1; EMIT_PARAMS(pos, " sync");
}
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->flags & DM_BLOCK_ON_ERROR)) { if ((seg->flags & DM_BLOCK_ON_ERROR))
if ((tw = _dm_snprintf(params + pos, paramsize - pos, "block_on_error ")) < 0) { EMIT_PARAMS(pos, " block_on_error");
stack; /* Out of space */
return -1;
}
pos += tw;
}
if ((tw = _dm_snprintf(params + pos, paramsize - pos, "%u ", seg->mirror_area_count)) < 0) { EMIT_PARAMS(pos, " %u", seg->mirror_area_count);
stack; /* Out of space */
return -1;
}
pos += tw;
break; break;
case SEG_SNAPSHOT: case SEG_SNAPSHOT:
@ -1367,30 +1315,16 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
return_0; return_0;
if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow)) if (!_build_dev_string(cowbuf, sizeof(cowbuf), seg->cow))
return_0; return_0;
if ((pos = _dm_snprintf(params, paramsize, "%s %s %c %d", EMIT_PARAMS(pos, "%s %s %c %d", originbuf, cowbuf,
originbuf, cowbuf, seg->persistent ? 'P' : 'N', seg->chunk_size);
seg->persistent ? 'P' : 'N',
seg->chunk_size)) < 0) {
stack; /* Out of space */
return -1;
}
break; break;
case SEG_SNAPSHOT_ORIGIN: case SEG_SNAPSHOT_ORIGIN:
if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin)) if (!_build_dev_string(originbuf, sizeof(originbuf), seg->origin))
return_0; return_0;
if ((pos = _dm_snprintf(params, paramsize, "%s", EMIT_PARAMS(pos, "%s", originbuf);
originbuf)) < 0) {
stack; /* Out of space */
return -1;
}
break; break;
case SEG_STRIPED: case SEG_STRIPED:
if ((pos = _dm_snprintf(params, paramsize, "%u %u ", EMIT_PARAMS(pos, "%u %u", seg->area_count, seg->stripe_size);
seg->area_count,
seg->stripe_size)) < 0) {
stack; /* Out of space */
return -1;
}
break; break;
} }
@ -1421,6 +1355,8 @@ static int _emit_segment_line(struct dm_task *dmt, struct load_segment *seg, uin
return 1; return 1;
} }
#undef EMIT_PARAMS
static int _emit_segment(struct dm_task *dmt, struct load_segment *seg, static int _emit_segment(struct dm_task *dmt, struct load_segment *seg,
uint64_t *seg_start) uint64_t *seg_start)
{ {