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

libdm: refactor internal mangling functions

(un)mangle_name -> (un)mangle_string
check_multiple_mangled_name_allowed -> check_multiple_mangled_string_allowed

Just for clarity as the same functions will be reused to (un)mangle dm UUIDs.
This commit is contained in:
Peter Rajnoha 2012-10-10 16:59:11 +02:00
parent f65754e6fc
commit b0f48b9533
3 changed files with 36 additions and 34 deletions

View File

@ -1559,10 +1559,10 @@ static int _do_dm_ioctl_unmangle_name(char *name)
if (mode == DM_STRING_MANGLING_NONE) if (mode == DM_STRING_MANGLING_NONE)
return 1; return 1;
if (!check_multiple_mangled_name_allowed(mode, name)) if (!check_multiple_mangled_string_allowed(name, "name", mode))
return_0; return_0;
if ((r = unmangle_name(name, DM_NAME_LEN, buf, sizeof(buf), mode)) < 0) { if ((r = unmangle_string(name, "name", DM_NAME_LEN, buf, sizeof(buf), mode)) < 0) {
log_debug("_do_dm_ioctl_unmangle_name: failed to " log_debug("_do_dm_ioctl_unmangle_name: failed to "
"unmangle \"%s\"", name); "unmangle \"%s\"", name);
return 0; return 0;

View File

@ -326,11 +326,12 @@ static int _is_whitelisted_char(char c)
return 0; return 0;
} }
int check_multiple_mangled_name_allowed(dm_string_mangling_t mode, const char *name) int check_multiple_mangled_string_allowed(const char *str, const char *str_name,
dm_string_mangling_t mode)
{ {
if (mode == DM_STRING_MANGLING_AUTO && strstr(name, "\\x5cx")) { if (mode == DM_STRING_MANGLING_AUTO && strstr(str, "\\x5cx")) {
log_error("The name \"%s\" seems to be mangled more than once. " log_error("The %s \"%s\" seems to be mangled more than once. "
"This is not allowed in auto mode.", name); "This is not allowed in auto mode.", str_name, str);
return 0; return 0;
} }
@ -341,8 +342,8 @@ int check_multiple_mangled_name_allowed(dm_string_mangling_t mode, const char *n
* Mangle all characters in the input string which are not on a whitelist * Mangle all characters in the input string which are not on a whitelist
* with '\xNN' format where NN is the hex value of the character. * with '\xNN' format where NN is the hex value of the character.
*/ */
int mangle_name(const char *str, size_t len, char *buf, int mangle_string(const char *str, const char *str_name, size_t len,
size_t buf_len, dm_string_mangling_t mode) char *buf, size_t buf_len, dm_string_mangling_t mode)
{ {
int need_mangling = -1; /* -1 don't know yet, 0 no, 1 yes */ int need_mangling = -1; /* -1 don't know yet, 0 no, 1 yes */
size_t i, j; size_t i, j;
@ -355,7 +356,7 @@ int mangle_name(const char *str, size_t len, char *buf,
return 0; return 0;
if (buf_len < DM_NAME_LEN) { if (buf_len < DM_NAME_LEN) {
log_error(INTERNAL_ERROR "mangle_name: supplied buffer too small"); log_error(INTERNAL_ERROR "mangle_string: supplied buffer too small");
return -1; return -1;
} }
@ -417,11 +418,11 @@ int mangle_name(const char *str, size_t len, char *buf,
return need_mangling; return need_mangling;
bad1: bad1:
log_error("The name \"%s\" contains mixed mangled and unmangled " log_error("The %s \"%s\" contains mixed mangled and unmangled "
"characters or it's already mangled improperly.", str); "characters or it's already mangled improperly.", str_name, str);
return -1; return -1;
bad2: bad2:
log_error("Mangled form of the name too long for \"%s\".", str); log_error("Mangled form of the %s too long for \"%s\".", str_name, str);
return -1; return -1;
} }
@ -429,8 +430,8 @@ bad2:
* Try to unmangle supplied string. * Try to unmangle supplied string.
* Return value: -1 on error, 0 when no unmangling needed, 1 when unmangling applied * Return value: -1 on error, 0 when no unmangling needed, 1 when unmangling applied
*/ */
int unmangle_name(const char *str, size_t len, char *buf, int unmangle_string(const char *str, const char *str_name, size_t len,
size_t buf_len, dm_string_mangling_t mode) char *buf, size_t buf_len, dm_string_mangling_t mode)
{ {
int strict = mode != DM_STRING_MANGLING_NONE; int strict = mode != DM_STRING_MANGLING_NONE;
char str_rest[DM_NAME_LEN]; char str_rest[DM_NAME_LEN];
@ -446,22 +447,22 @@ int unmangle_name(const char *str, size_t len, char *buf,
return 0; return 0;
if (buf_len < DM_NAME_LEN) { if (buf_len < DM_NAME_LEN) {
log_error(INTERNAL_ERROR "unmangle_name: supplied buffer too small"); log_error(INTERNAL_ERROR "unmangle_string: supplied buffer too small");
return -1; return -1;
} }
for (i = 0, j = 0; str[i]; i++, j++) { for (i = 0, j = 0; str[i]; i++, j++) {
if (strict && !(_is_whitelisted_char(str[i]) || str[i]=='\\')) { if (strict && !(_is_whitelisted_char(str[i]) || str[i]=='\\')) {
log_error("The name \"%s\" should be mangled but " log_error("The %s \"%s\" should be mangled but "
"it contains blacklisted characters.", str); "it contains blacklisted characters.", str_name, str);
j=0; r=-1; j=0; r=-1;
goto out; goto out;
} }
if (str[i] == '\\' && str[i+1] == 'x') { if (str[i] == '\\' && str[i+1] == 'x') {
if (!sscanf(&str[i+2], "%2x%s", &code, str_rest)) { if (!sscanf(&str[i+2], "%2x%s", &code, str_rest)) {
log_debug("Hex encoding mismatch detected in \"%s\" " log_debug("Hex encoding mismatch detected in %s \"%s\" "
"while trying to unmangle it.", str); "while trying to unmangle it.", str_name, str);
goto out; goto out;
} }
buf[j] = (unsigned char) code; buf[j] = (unsigned char) code;
@ -496,11 +497,11 @@ static int _dm_task_set_name(struct dm_task *dmt, const char *name,
return 0; return 0;
} }
if (!check_multiple_mangled_name_allowed(mangling_mode, name)) if (!check_multiple_mangled_string_allowed(name, "name", mangling_mode))
return_0; return_0;
if (mangling_mode != DM_STRING_MANGLING_NONE && if (mangling_mode != DM_STRING_MANGLING_NONE &&
(r = mangle_name(name, strlen(name), mangled_name, (r = mangle_string(name, "name", strlen(name), mangled_name,
sizeof(mangled_name), mangling_mode)) < 0) { sizeof(mangled_name), mangling_mode)) < 0) {
log_error("Failed to mangle device name \"%s\".", name); log_error("Failed to mangle device name \"%s\".", name);
return 0; return 0;
@ -588,7 +589,7 @@ char *dm_task_get_name_mangled(const struct dm_task *dmt)
char *rs = NULL; char *rs = NULL;
int r; int r;
if ((r = mangle_name(s, strlen(s), buf, sizeof(buf), if ((r = mangle_string(s, "name", strlen(s), buf, sizeof(buf),
dm_get_name_mangling_mode())) < 0) dm_get_name_mangling_mode())) < 0)
log_error("Failed to mangle device name \"%s\".", s); log_error("Failed to mangle device name \"%s\".", s);
else if (!(rs = r ? dm_strdup(buf) : dm_strdup(s))) else if (!(rs = r ? dm_strdup(buf) : dm_strdup(s)))
@ -609,7 +610,7 @@ char *dm_task_get_name_unmangled(const struct dm_task *dmt)
* is *already* unmangled on ioctl return! * is *already* unmangled on ioctl return!
*/ */
if (dm_get_name_mangling_mode() == DM_STRING_MANGLING_NONE && if (dm_get_name_mangling_mode() == DM_STRING_MANGLING_NONE &&
(r = unmangle_name(s, strlen(s), buf, sizeof(buf), (r = unmangle_string(s, "name", strlen(s), buf, sizeof(buf),
dm_get_name_mangling_mode())) < 0) dm_get_name_mangling_mode())) < 0)
log_error("Failed to unmangle device name \"%s\".", s); log_error("Failed to unmangle device name \"%s\".", s);
else if (!(rs = r ? dm_strdup(buf) : dm_strdup(s))) else if (!(rs = r ? dm_strdup(buf) : dm_strdup(s)))
@ -634,11 +635,11 @@ int dm_task_set_newname(struct dm_task *dmt, const char *newname)
return 0; return 0;
} }
if (!check_multiple_mangled_name_allowed(mangling_mode, newname)) if (!check_multiple_mangled_string_allowed(newname, "new name", mangling_mode))
return_0; return_0;
if (mangling_mode != DM_STRING_MANGLING_NONE && if (mangling_mode != DM_STRING_MANGLING_NONE &&
(r = mangle_name(newname, strlen(newname), mangled_name, (r = mangle_string(newname, "new name", strlen(newname), mangled_name,
sizeof(mangled_name), mangling_mode)) < 0) { sizeof(mangled_name), mangling_mode)) < 0) {
log_error("Failed to mangle new device name \"%s\"", newname); log_error("Failed to mangle new device name \"%s\"", newname);
return 0; return 0;

View File

@ -22,13 +22,14 @@
#define DEV_NAME(dmt) (dmt->mangled_dev_name ? : dmt->dev_name) #define DEV_NAME(dmt) (dmt->mangled_dev_name ? : dmt->dev_name)
int mangle_name(const char *str, size_t len, char *buf, int mangle_string(const char *str, const char *str_name, size_t len,
size_t buf_len, dm_string_mangling_t mode); char *buf, size_t buf_len, dm_string_mangling_t mode);
int unmangle_name(const char *str, size_t len, char *buf, int unmangle_string(const char *str, const char *str_name, size_t len,
size_t buf_len, dm_string_mangling_t mode); char *buf, size_t buf_len, dm_string_mangling_t mode);
int check_multiple_mangled_name_allowed(dm_string_mangling_t mode, const char *name); int check_multiple_mangled_string_allowed(const char *str, const char *str_name,
dm_string_mangling_t mode);
struct target *create_target(uint64_t start, struct target *create_target(uint64_t start,
uint64_t len, uint64_t len,