Skip to contentSkip to conversation

mkfs.f2fs: add helper strtrim() to trim string's space

RFC PATCH In review Sheng Yong 3 participants 7 messages Nov 28, 2015 – Nov 29, 2015 Library code

lore
Share & tools
lore

Sheng Yong

author
#0 lore raw

RFC PATCH v2 1/2 mkfs.f2fs: add helper strtrim() to trim string's space

Add a helper function strtrim() to trim space both at the beginning and end of a string.

---
 include/f2fs_fs.h |  1 +
 lib/libf2fs.c     | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)
include/f2fs_fs.h +1 -0
index 359deec..e85b2d0 100644
@@ -768,6 +768,7 @@ enum {
 
 extern void ASCIIToUNICODE(u_int16_t *, u_int8_t *);
 extern int log_base_2(u_int32_t);
+extern void strtrim(char **str);
 extern unsigned int addrs_per_inode(struct f2fs_inode *);
 
 extern int get_bits_in_byte(unsigned char n);
lib/libf2fs.c +24 -0
index 83d1296..e901289 100644
@@ -48,6 +48,30 @@ int log_base_2(u_int32_t num)
 	return ret;
 }
 
+void strtrim(char **str)
+{
+	int len = strlen(*str);
+	char *head = *str;
+	char *tail = *str + len - 1;
+
+	while (head != tail) {
+		if (*head == ' ')
+			head++;
+		else {
+			*str = head;
+			break;
+		}
+	}
+
+	while (tail != head) {
+		if (*tail == ' ') {
+			*tail = '\0';
+			tail--;
+		} else
+			break;
+	}
+}
+
 /*
  * f2fs bit operations
  */
Signature · 4 lines
1.9.1


------------------------------------------------------------------------------
author
#1 lore raw

RFC PATCH v2 2/2 mkfs.f2fs: avoid dumplicate extensions

Before copying an user specified extension to extension_list, check if it is already in the list.

---
 mkfs/f2fs_format.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
mkfs/f2fs_format.c +25 -1
index 176bdea..c170985 100644
@@ -118,6 +118,24 @@ const char *media_ext_lists[] = {
 	NULL
 };
 
+static bool is_extension_exist(const char *name, const int len)
+{
+	int name_len, min_len;
+	int i, ret;
+
+	for (i = 0; i < F2FS_MAX_EXTENSION; i++) {
+		char *ext = (char *) sb.extension_list[i];
+		name_len = strlen(ext);
+		min_len = name_len < len ? name_len : len;
+		ret = strncmp(ext, name, min_len);
+
+		if (ret == 0 && name_len == len)
+			return 1;
+	}
+
+	return 0;
+}
+
 static void configure_extension_list(void)
 {
 	const char **extlist = media_ext_lists;
@@ -143,8 +161,14 @@ static void configure_extension_list(void)
 	/* add user ext list */
 	ue = strtok(ext_str, ",");
 	while (ue != NULL) {
+		strtrim(&ue);
 		name_len = strlen(ue);
-		memcpy(sb.extension_list[i++], ue, name_len);
+		if (name_len > 7) {
+			MSG(0, "\tError: Extension '%s' too long\n", ue);
+			exit(1);
+		}
+		if (!is_extension_exist(ue, name_len))
+			memcpy(sb.extension_list[i++], ue, name_len);
 		ue = strtok(NULL, ",");
 		if (i >= F2FS_MAX_EXTENSION)
 			break;
Signature · 4 lines
1.9.1


------------------------------------------------------------------------------
author
#2 lore raw

RFC PATCH v3 2/2 mkfs.f2fs: avoid dumplicate extensions

Before copying an user specified extension to extension_list, check if it is already in the list.

---
 mkfs/f2fs_format.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
mkfs/f2fs_format.c +22 -1
index 176bdea..0a621ae 100644
@@ -118,6 +118,21 @@ const char *media_ext_lists[] = {
 	NULL
 };
 
+static bool is_extension_exist(const char *name, const int len)
+{
+	int i, ret;
+
+	for (i = 0; i < F2FS_MAX_EXTENSION; i++) {
+		char *ext = (char *) sb.extension_list[i];
+		ret = strcmp(ext, name);
+
+		if (ret == 0)
+			return 1;
+	}
+
+	return 0;
+}
+
 static void configure_extension_list(void)
 {
 	const char **extlist = media_ext_lists;
@@ -143,8 +158,14 @@ static void configure_extension_list(void)
 	/* add user ext list */
 	ue = strtok(ext_str, ",");
 	while (ue != NULL) {
+		strtrim(&ue);
 		name_len = strlen(ue);
-		memcpy(sb.extension_list[i++], ue, name_len);
+		if (name_len > 7) {
+			MSG(0, "\tError: Extension '%s' too long\n", ue);
+			exit(1);
+		}
+		if (!is_extension_exist(ue, name_len))
+			memcpy(sb.extension_list[i++], ue, name_len);
 		ue = strtok(NULL, ",");
 		if (i >= F2FS_MAX_EXTENSION)
 			break;
Signature · 5 lines
1.9.1



------------------------------------------------------------------------------
#3 lore raw

Re: [RFC PATCH V3 2/2] mkfs.f2fs: avoid dumplicate extensions

Quoted 13 lines · Sheng Yong

Before copying an user specified extension to extension_list, check if it is already in the list.

---
 mkfs/f2fs_format.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
mkfs/f2fs_format.c +0 -0
index 176bdea..0a621ae 100644

mkfs/f2fs_format.c const char *media_ext_lists[] = { · line 125

Quoted 8 lines
mkfs/f2fs_format.c +5 -0
@@ -118,6 +118,21 @@ const char *media_ext_lists[] = {
 	NULL
 };
 
+static bool is_extension_exist(const char *name, const int len)
+{
+	int i, ret;
+
+	for (i = 0; i < F2FS_MAX_EXTENSION; i++) {

i < last_extension_position_in_list ?

mkfs/f2fs_format.c const char *media_ext_lists[] = { · line 127

Quoted 2 lines
mkfs/f2fs_format.c +2 -0
+		char *ext = (char *) sb.extension_list[i];
+		ret = strcmp(ext, name);

if (!strcmp(ext, name))

	return 1;
Quoted 12 lines
mkfs/f2fs_format.c +8 -0
+
+		if (ret == 0)
+			return 1;
+	}
+
+	return 0;
+}
+
 static void configure_extension_list(void)
 {
 	const char **extlist = media_ext_lists;

mkfs/f2fs_format.c static void configure_extension_list(void) · line 163

Quoted 7 lines
mkfs/f2fs_format.c +2 -1
@@ -143,8 +158,14 @@ static void configure_extension_list(void)
 	/* add user ext list */
 	ue = strtok(ext_str, ",");
 	while (ue != NULL) {
+		strtrim(&ue);
 		name_len = strlen(ue);
-		memcpy(sb.extension_list[i++], ue, name_len);
+		if (name_len > 7) {

Shouldn't this be fixed in another patch?

How about:

mkfs/f2fs_format.c +5 -0
index 176bdea..78612e4 100644
@@ -144,7 +144,12 @@ static void configure_extension_list(void)
 	ue = strtok(ext_str, ",");
 	while (ue != NULL) {
 		name_len = strlen(ue);
+		if (name_len >= 8) {
+			MSG(0, "\tWarn: Extension name (%s) is too long\n", ue);
+			goto next;
+		}
 		memcpy(sb.extension_list[i++], ue, name_len);
+next:
 		ue = strtok(NULL, ",");
 		if (i >= F2FS_MAX_EXTENSION)
 			break;

Thanks,

mkfs/f2fs_format.c static void configure_extension_list(void) · line 171

Quoted 9 lines
mkfs/f2fs_format.c +5 -0
+			MSG(0, "\tError: Extension '%s' too long\n", ue);
+			exit(1);
+		}
+		if (!is_extension_exist(ue, name_len))
+			memcpy(sb.extension_list[i++], ue, name_len);
 		ue = strtok(NULL, ",");
 		if (i >= F2FS_MAX_EXTENSION)
 			break;

------------------------------------------------------------------------------

author
#4 lore raw

Re: [RFC PATCH V3 2/2] mkfs.f2fs: avoid dumplicate extensions

Quoted 17 lines · Chao Yu
Quoted 16 lines · Sheng Yong

Before copying an user specified extension to extension_list, check if it is already in the list.

---
 mkfs/f2fs_format.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
mkfs/f2fs_format.c +0 -0
index 176bdea..0a621ae 100644
@@ -118,6 +118,21 @@ const char *media_ext_lists[] = {
 	NULL
 };
 
Quoted 7 lines

mkfs/f2fs_format.c const char *media_ext_lists[] = { · line 125

Quoted 5 lines
mkfs/f2fs_format.c +5 -0
+static bool is_extension_exist(const char *name, const int len)
+{
+	int i, ret;
+
+	for (i = 0; i < F2FS_MAX_EXTENSION; i++) {

i < last_extension_position_in_list ?

Then we have to define a new global variable or add a new parameter to pass the last position in the function.

Quoted 20 lines

mkfs/f2fs_format.c const char *media_ext_lists[] = { · line 127

Quoted 3 lines
mkfs/f2fs_format.c +2 -0
+		char *ext = (char *) sb.extension_list[i];
+		ret = strcmp(ext, name);

if (!strcmp(ext, name))

	return 1;
Quoted 13 lines
mkfs/f2fs_format.c +8 -0
+
+		if (ret == 0)
+			return 1;
+	}
+
+	return 0;
+}
+
 static void configure_extension_list(void)
 {
 	const char **extlist = media_ext_lists;
@@ -143,8 +158,14 @@ static void configure_extension_list(void)
 	/* add user ext list */
Quoted 8 lines

mkfs/f2fs_format.c static void configure_extension_list(void)

Quoted 6 lines
mkfs/f2fs_format.c +2 -1
 	ue = strtok(ext_str, ",");
 	while (ue != NULL) {
+		strtrim(&ue);
 		name_len = strlen(ue);
-		memcpy(sb.extension_list[i++], ue, name_len);
+		if (name_len > 7) {

Shouldn't this be fixed in another patch?

This is much better than what I did :)

thanks,
Sheng

Quoted 23 lines

How about:

mkfs/f2fs_format.c +5 -0
index 176bdea..78612e4 100644
@@ -144,7 +144,12 @@ static void configure_extension_list(void)
 	ue = strtok(ext_str, ",");
 	while (ue != NULL) {
 		name_len = strlen(ue);
+		if (name_len >= 8) {
+			MSG(0, "\tWarn: Extension name (%s) is too long\n", ue);
+			goto next;
+		}
 		memcpy(sb.extension_list[i++], ue, name_len);
+next:
 		ue = strtok(NULL, ",");
 		if (i >= F2FS_MAX_EXTENSION)
 			break;

Thanks,

Quoted 12 lines

mkfs/f2fs_format.c static void configure_extension_list(void) · line 155

Quoted 9 lines
mkfs/f2fs_format.c +5 -0
+			MSG(0, "\tError: Extension '%s' too long\n", ue);
+			exit(1);
+		}
+		if (!is_extension_exist(ue, name_len))
+			memcpy(sb.extension_list[i++], ue, name_len);
 		ue = strtok(NULL, ",");
 		if (i >= F2FS_MAX_EXTENSION)
 			break;

.

------------------------------------------------------------------------------

#5 lore raw

Re: [RFC PATCH V3 2/2] mkfs.f2fs: avoid dumplicate extensions

Hi Sheng,

Quoted 27 lines · Sheng Yong
Quoted 17 lines · Chao Yu
Quoted 16 lines · Sheng Yong

Before copying an user specified extension to extension_list, check if it is already in the list.

---
 mkfs/f2fs_format.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
mkfs/f2fs_format.c +0 -0
index 176bdea..0a621ae 100644
@@ -118,6 +118,21 @@ const char *media_ext_lists[] = {
 	NULL
 };
 
Quoted 7 lines

mkfs/f2fs_format.c const char *media_ext_lists[] = { · line 125

Quoted 5 lines
mkfs/f2fs_format.c +5 -0
+static bool is_extension_exist(const char *name, const int len)
+{
+	int i, ret;
+
+	for (i = 0; i < F2FS_MAX_EXTENSION; i++) {

i < last_extension_position_in_list ?

Quoted 2 lines

Then we have to define a new global variable or add a new parameter to pass the last position in the function.

I think it's not a big deal to use F2FS_MAX_EXTENSION.

Quoted 2 lines
Quoted 2 lines

mkfs/f2fs_format.c const char *media_ext_lists[] = { · line 126

Quoted 2 lines
mkfs/f2fs_format.c +1 -0
+		char *ext = (char *) sb.extension_list[i];
		char *ext = (char *)sb.extension_list[i];
		                   ^^^

Coding style.

Quoted 4 lines
Quoted 4 lines

mkfs/f2fs_format.c const char *media_ext_lists[] = { · line 127

Quoted 1 line
mkfs/f2fs_format.c +1 -0
+		ret = strcmp(ext, name);

if (!strcmp(ext, name))

	return 1;

Agreed.

Quoted 15 lines
Quoted 15 lines
Quoted 13 lines
mkfs/f2fs_format.c +8 -0
+
+		if (ret == 0)
+			return 1;
+	}
+
+	return 0;
+}
+
 static void configure_extension_list(void)
 {
 	const char **extlist = media_ext_lists;

mkfs/f2fs_format.c static void configure_extension_list(void) · line 159

Quoted 2 lines
mkfs/f2fs_format.c +0 -0
@@ -143,8 +158,14 @@ static void configure_extension_list(void)
 	/* add user ext list */
 	ue = strtok(ext_str, ",");

How about using strtok(ext_str, ", ") to avoid strtrim() below?

Quoted 8 lines
Quoted 7 lines

mkfs/f2fs_format.c static void configure_extension_list(void) · line 163

Quoted 5 lines
mkfs/f2fs_format.c +2 -1
 	while (ue != NULL) {
+		strtrim(&ue);
 		name_len = strlen(ue);
-		memcpy(sb.extension_list[i++], ue, name_len);
+		if (name_len > 7) {

Shouldn't this be fixed in another patch?

This is much better than what I did :)

+2 :)

Thanks,

Quoted 38 lines

thanks,
Sheng

Quoted 35 lines

How about:

mkfs/f2fs_format.c +5 -0
index 176bdea..78612e4 100644
@@ -144,7 +144,12 @@ static void configure_extension_list(void)
 	ue = strtok(ext_str, ",");
 	while (ue != NULL) {
 		name_len = strlen(ue);
+		if (name_len >= 8) {
+			MSG(0, "\tWarn: Extension name (%s) is too long\n", ue);
+			goto next;
+		}
 		memcpy(sb.extension_list[i++], ue, name_len);
+next:
 		ue = strtok(NULL, ",");
 		if (i >= F2FS_MAX_EXTENSION)
 			break;

Thanks,

mkfs/f2fs_format.c static void configure_extension_list(void) · line 155

Quoted 9 lines
mkfs/f2fs_format.c +5 -0
+			MSG(0, "\tError: Extension '%s' too long\n", ue);
+			exit(1);
+		}
+		if (!is_extension_exist(ue, name_len))
+			memcpy(sb.extension_list[i++], ue, name_len);
 		ue = strtok(NULL, ",");
 		if (i >= F2FS_MAX_EXTENSION)
 			break;

.

------------------------------------------------------------------------------

#6 lore raw
In reply to Sheng Yong
Quoted 2 lines · Sheng Yong

Add a helper function strtrim() to trim space both at the beginning and end of a string.

I'm not sure we should add more logic when processing user defined extension string.

How about waiting for Jaegeuk's opinion?

Thanks,

Quoted 45 lines
---
 include/f2fs_fs.h |  1 +
 lib/libf2fs.c     | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)
include/f2fs_fs.h +1 -0
index 359deec..e85b2d0 100644
@@ -768,6 +768,7 @@ enum {
 
 extern void ASCIIToUNICODE(u_int16_t *, u_int8_t *);
 extern int log_base_2(u_int32_t);
+extern void strtrim(char **str);
 extern unsigned int addrs_per_inode(struct f2fs_inode *);
 
 extern int get_bits_in_byte(unsigned char n);
lib/libf2fs.c +17 -0
index 83d1296..e901289 100644
@@ -48,6 +48,30 @@ int log_base_2(u_int32_t num)
 	return ret;
 }
 
+void strtrim(char **str)
+{
+	int len = strlen(*str);
+	char *head = *str;
+	char *tail = *str + len - 1;
+
+	while (head != tail) {
+		if (*head == ' ')
+			head++;
+		else {
+			*str = head;
+			break;
+		}
+	}
+
+	while (tail != head) {
+		if (*tail == ' ') {

lib/libf2fs.c int log_base_2(u_int32_t num) · line 77

Quoted 10 lines
lib/libf2fs.c +7 -0
+			*tail = '\0';
+			tail--;
+		} else
+			break;
+	}
+}
+
 /*
  * f2fs bit operations
  */

------------------------------------------------------------------------------