Skip to contentSkip to conversation

edit: Return error when save_file failed

PATCH Superseded Enrico Jorns 3 participants 6 messages Nov 3, 2015 – Nov 5, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 edit: Return error when save_file failed

When writing a file failed (e.g. due to a read-only file system), no error was reported by the 'edit' tool. To be valid (and to not confuse the poor user) at least '1' should be returned to indicate an error.

---
 commands/edit.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
commands/edit.c +2 -1
index b28e2b9..2d9c8b5 100644
@@ -533,7 +533,8 @@ static int do_edit(int argc, char *argv[])
 			}
 			break;
 		case 4:
-			save_file(argv[1]);
+			if (save_file(argv[1]))
+				return 1;
 			goto out;
 		case 3:
 			goto out;
Signature · 7 lines
2.6.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH v2 1/1 edit: Return error when save_file failed

When writing a file failed (e.g. due to a read-only file system), no error was reported by the 'edit' tool. To be valid (and to not confuse the poor user) at least '1' should be returned to indicate an error.

---
 commands/edit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
commands/edit.c +3 -2
index b28e2b9..3b653b7 100644
@@ -375,6 +375,7 @@ static int do_edit(int argc, char *argv[])
 	int i;
 	int linepos;
 	int c;
+	int ret = 0;
 
 	if (argc != 2)
 		return COMMAND_ERROR_USAGE;
@@ -533,7 +534,7 @@ static int do_edit(int argc, char *argv[])
 			}
 			break;
 		case 4:
-			save_file(argv[1]);
+			ret = save_file(argv[1]);
 			goto out;
 		case 3:
 			goto out;
@@ -546,7 +547,7 @@ out:
 	free_buffer();
 	printf("%c[2J%c[r", 27, 27);
 	printf("\n");
-	return 0;
+	return ret;
 }
 
 static const char *edit_aliases[] = { "sedit", NULL};
Signature · 7 lines
2.6.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw
Quoted 14 lines · On Wed, 4 Nov 2015 12:03:24 +0100 Enrico Jorns <[email protected]> wrote:

When writing a file failed (e.g. due to a read-only file system), no error was reported by the 'edit' tool. To be valid (and to not confuse the poor user) at least '1' should be returned to indicate an error.

---
 commands/edit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
commands/edit.c +0 -0
index b28e2b9..3b653b7 100644

commands/edit.c static int do_edit(int argc, char *argv[]) · line 378

Quoted 4 lines
commands/edit.c +1 -0
@@ -375,6 +375,7 @@ static int do_edit(int argc, char *argv[])
 	int i;
 	int linepos;
 	int c;
+	int ret = 0;

Can we use COMMAND_SUCCESS insted of 0?

commands/edit.c static int do_edit(int argc, char *argv[]) · line 537

Quoted 9 lines
commands/edit.c +1 -1
 	if (argc != 2)
 		return COMMAND_ERROR_USAGE;
@@ -533,7 +534,7 @@ static int do_edit(int argc, char *argv[])
 			}
 			break;
 		case 4:
-			save_file(argv[1]);
+			ret = save_file(argv[1]);

Actually save_file() returns open()'s error. We have to convert it to COMMAND_ERROR, e.g.:

if (save_file(argv[1]) != 0)

	ret = COMMAND_ERROR;

Also save_file() does not check write()'s error.

Quoted 20 lines
commands/edit.c +1 -1
 			goto out;
 		case 3:
 			goto out;
@@ -546,7 +547,7 @@ out:
 	free_buffer();
 	printf("%c[2J%c[r", 27, 27);
 	printf("\n");
-	return 0;
+	return ret;
 }
 
 static const char *edit_aliases[] = { "sedit", NULL};
Signature · 7 lines
2.6.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 8 lines
-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw
Quoted 26 lines · Antony Pavlov
Quoted 14 lines · On Wed, 4 Nov 2015 12:03:24 +0100 Enrico Jorns <[email protected]> wrote:

When writing a file failed (e.g. due to a read-only file system), no error was reported by the 'edit' tool. To be valid (and to not confuse the poor user) at least '1' should be returned to indicate an error.

---
 commands/edit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
commands/edit.c +0 -0
index b28e2b9..3b653b7 100644

commands/edit.c static int do_edit(int argc, char *argv[]) · line 378

Quoted 4 lines
commands/edit.c +1 -0
@@ -375,6 +375,7 @@ static int do_edit(int argc, char *argv[])
 	int i;
 	int linepos;
 	int c;
+	int ret = 0;

Can we use COMMAND_SUCCESS insted of 0?

Quoted 1 line
Quoted 10 lines

commands/edit.c static int do_edit(int argc, char *argv[]) · line 537

Quoted 8 lines
commands/edit.c +1 -1
 	if (argc != 2)
 		return COMMAND_ERROR_USAGE;
@@ -533,7 +534,7 @@ static int do_edit(int argc, char *argv[])
 			}
 			break;
 		case 4:
-			save_file(argv[1]);
+			ret = save_file(argv[1]);

Actually save_file() returns open()'s error. We have to convert it to COMMAND_ERROR, e.g.:

If we return an error code from a command then the caller will print the corresponding error string to the console which might be what we want here.

Quoted 5 lines

if (save_file(argv[1]) != 0)

	ret = COMMAND_ERROR;

Also save_file() does not check write()'s error.

Indeed, this could be fixed while touching this code.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw
Quoted 38 lines · On Wed, 4 Nov 2015 14:30:53 +0100 Sascha Hauer <[email protected]> wrote:
Quoted 26 lines · Antony Pavlov
Quoted 14 lines · On Wed, 4 Nov 2015 12:03:24 +0100 Enrico Jorns <[email protected]> wrote:

When writing a file failed (e.g. due to a read-only file system), no error was reported by the 'edit' tool. To be valid (and to not confuse the poor user) at least '1' should be returned to indicate an error.

---
 commands/edit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
commands/edit.c +0 -0
index b28e2b9..3b653b7 100644

commands/edit.c static int do_edit(int argc, char *argv[]) · line 378

Quoted 4 lines
commands/edit.c +1 -0
@@ -375,6 +375,7 @@ static int do_edit(int argc, char *argv[])
 	int i;
 	int linepos;
 	int c;
+	int ret = 0;

Can we use COMMAND_SUCCESS insted of 0?

Quoted 1 line
Quoted 10 lines

commands/edit.c static int do_edit(int argc, char *argv[]) · line 537

Quoted 8 lines
commands/edit.c +1 -1
 	if (argc != 2)
 		return COMMAND_ERROR_USAGE;
@@ -533,7 +534,7 @@ static int do_edit(int argc, char *argv[])
 			}
 			break;
 		case 4:
-			save_file(argv[1]);
+			ret = save_file(argv[1]);

Actually save_file() returns open()'s error. We have to convert it to COMMAND_ERROR, e.g.:

Quoted 8 lines

If we return an error code from a command then the caller will print the corresponding error string to the console which might be what we want here.

Quoted 4 lines

if (save_file(argv[1]) != 0)

	ret = COMMAND_ERROR;

At the moment the only cmd() caller is inside of execute_command():

        ret = cmdtp->cmd(argc, argv);
        if (ret == COMMAND_ERROR_USAGE) {
                barebox_cmd_usage(cmdtp);
                ret = COMMAND_ERROR;
        }
        ...
        return ret;

But we brop execute_command() result in run_command():

        if (execute_command(argc, argv) != COMMAND_SUCCESS)
	        rc = -1;

We have a chance to see error message only if we call execute_command() from run_pipe_real():

        ret = execute_binfmt(globbuf.gl_pathc, globbuf.gl_pathv);
        if (ret < 0) {
            printf("%s: %s\n", globbuf.gl_pathv[0], strerror(-ret));
            ret = 127;
        }

So, yes, you are right we have no reason to convert open()'s error into COMMAND_ERROR, but for consistency we have toadd strerror() print to run_command() code.

Quoted 11 lines
Quoted 1 line

Also save_file() does not check write()'s error.

Indeed, this could be fixed while touching this code.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Signature · 8 lines
-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw
Quoted 68 lines · Antony Pavlov
Quoted 38 lines · On Wed, 4 Nov 2015 14:30:53 +0100 Sascha Hauer <[email protected]> wrote:
Quoted 26 lines · Antony Pavlov
Quoted 14 lines · On Wed, 4 Nov 2015 12:03:24 +0100 Enrico Jorns <[email protected]> wrote:

When writing a file failed (e.g. due to a read-only file system), no error was reported by the 'edit' tool. To be valid (and to not confuse the poor user) at least '1' should be returned to indicate an error.

---
 commands/edit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
commands/edit.c +0 -0
index b28e2b9..3b653b7 100644

commands/edit.c static int do_edit(int argc, char *argv[]) · line 378

Quoted 4 lines
commands/edit.c +1 -0
@@ -375,6 +375,7 @@ static int do_edit(int argc, char *argv[])
 	int i;
 	int linepos;
 	int c;
+	int ret = 0;

Can we use COMMAND_SUCCESS insted of 0?

Quoted 1 line
Quoted 10 lines

commands/edit.c static int do_edit(int argc, char *argv[]) · line 537

Quoted 8 lines
commands/edit.c +1 -1
 	if (argc != 2)
 		return COMMAND_ERROR_USAGE;
@@ -533,7 +534,7 @@ static int do_edit(int argc, char *argv[])
 			}
 			break;
 		case 4:
-			save_file(argv[1]);
+			ret = save_file(argv[1]);

Actually save_file() returns open()'s error. We have to convert it to COMMAND_ERROR, e.g.:

Quoted 8 lines

If we return an error code from a command then the caller will print the corresponding error string to the console which might be what we want here.

Quoted 4 lines

if (save_file(argv[1]) != 0)

	ret = COMMAND_ERROR;

At the moment the only cmd() caller is inside of execute_command():

        ret = cmdtp->cmd(argc, argv);
        if (ret == COMMAND_ERROR_USAGE) {
                barebox_cmd_usage(cmdtp);
                ret = COMMAND_ERROR;
        }
        ...
        return ret;

But we brop execute_command() result in run_command():

        if (execute_command(argc, argv) != COMMAND_SUCCESS)
	        rc = -1;

We have a chance to see error message only if we call execute_command() from run_pipe_real():

Quoted 8 lines
        ret = execute_binfmt(globbuf.gl_pathc, globbuf.gl_pathv);
        if (ret < 0) {
            printf("%s: %s\n", globbuf.gl_pathv[0], strerror(-ret));
            ret = 127;
        }

So, yes, you are right we have no reason to convert open()'s error into COMMAND_ERROR, but for consistency we have toadd strerror() print to run_command() code.

Yes, right.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox