summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Golle <daniel@makrotopia.org>2022-04-12 22:51:04 +0100
committerDaniel Golle <daniel@makrotopia.org>2022-05-01 16:55:49 +0100
commitce5eacb0762bb19c61a543b64167f7b27f189bed (patch)
tree7db17a2e71c93e74fba2d16cbb0c791180c48164
parent922f1b3c091dbe12af4ba12a76ba8563418b688c (diff)
downloadfstools-ce5eacb0762bb19c61a543b64167f7b27f189bed.tar.gz
libfstools: mtd: improve error handling
Use -1 to mark invalid file descriptors as 0 can theoretically be a valid open file descriptor. Do not ignore lseek() return value and check if an error has occured. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
-rw-r--r--libfstools/mtd.c10
1 files changed, 4 insertions, 6 deletions
diff --git a/libfstools/mtd.c b/libfstools/mtd.c
index c5dce50..3696828 100644
--- a/libfstools/mtd.c
+++ b/libfstools/mtd.c
@@ -67,7 +67,7 @@ static void mtd_volume_close(struct mtd_volume *p)
return;
close(p->fd);
- p->fd = 0;
+ p->fd = -1;
}
static int mtd_volume_load(struct mtd_volume *p)
@@ -76,17 +76,14 @@ static int mtd_volume_load(struct mtd_volume *p)
struct mtd_info_user mtdInfo;
struct erase_info_user mtdLockInfo;
- if (p->fd) {
- lseek(p->fd, 0, SEEK_SET);
- return 0;
- }
+ if (p->fd >= 0)
+ return (lseek(p->fd, 0, SEEK_SET) == -1);
if (!p->chr)
return -1;
p->fd = mtd_open(p->chr, 0);
if (p->fd < 0) {
- p->fd = 0;
ULOG_ERR("Could not open mtd device: %s\n", p->chr);
return -1;
}
@@ -167,6 +164,7 @@ static struct volume *mtd_volume_find(char *name)
v->name = strdup(name);
v->drv = &mtd_driver;
p->idx = atoi(idx);
+ p->fd = -1;
snprintf(buffer, sizeof(buffer), "/dev/mtdblock%s", idx);
v->blk = strdup(buffer);