[fdb] check some read error for file mode

This commit is contained in:
朱天龙 (Armink) 2023-05-28 19:02:04 +08:00
parent 55fd31eeb3
commit 8608140d88
1 changed files with 10 additions and 9 deletions

View File

@ -79,9 +79,10 @@ fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void *buf, size_t size)
fdb_err_t result = FDB_NO_ERR; fdb_err_t result = FDB_NO_ERR;
int fd = open_db_file(db, addr, false); int fd = open_db_file(db, addr, false);
if (fd > 0) { if (fd > 0) {
/* get the offset address is relative to the start of the current file */
addr = addr % db->sec_size; addr = addr % db->sec_size;
lseek(fd, addr, SEEK_SET); if ((lseek(fd, addr, SEEK_SET) != addr) || (read(fd, buf, size) != size))
read(fd, buf, size); result = FDB_READ_ERR;
} else { } else {
result = FDB_READ_ERR; result = FDB_READ_ERR;
} }
@ -93,16 +94,16 @@ fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t si
fdb_err_t result = FDB_NO_ERR; fdb_err_t result = FDB_NO_ERR;
int fd = open_db_file(db, addr, false); int fd = open_db_file(db, addr, false);
if (fd > 0) { if (fd > 0) {
/* get the offset address is relative to the start of the current file */
addr = addr % db->sec_size; addr = addr % db->sec_size;
lseek(fd, addr, SEEK_SET); if ((lseek(fd, addr, SEEK_SET) != addr) || (write(fd, buf, size) != size))
write(fd, buf, size); result = FDB_WRITE_ERR;
if(sync) { if(sync) {
fsync(fd); fsync(fd);
} }
} else { } else {
result = FDB_WRITE_ERR; result = FDB_WRITE_ERR;
} }
return result; return result;
} }
@ -166,8 +167,8 @@ fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void *buf, size_t size)
FILE *fp = open_db_file(db, addr, false); FILE *fp = open_db_file(db, addr, false);
if (fp) { if (fp) {
addr = addr % db->sec_size; addr = addr % db->sec_size;
fseek(fp, addr, SEEK_SET); if ((fseek(fp, addr, SEEK_SET) != 0) || (fread(buf, size, 1, fp) != size))
fread(buf, size, 1, fp); result = FDB_READ_ERR;
} else { } else {
result = FDB_READ_ERR; result = FDB_READ_ERR;
} }
@ -180,8 +181,8 @@ fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t si
FILE *fp = open_db_file(db, addr, false); FILE *fp = open_db_file(db, addr, false);
if (fp) { if (fp) {
addr = addr % db->sec_size; addr = addr % db->sec_size;
fseek(fp, addr, SEEK_SET); if ((fseek(fp, addr, SEEK_SET) != 0) || (fwrite(buf, size, 1, fp) != size))
fwrite(buf, size, 1, fp); result = FDB_READ_ERR;
if(sync) { if(sync) {
fflush(fp); fflush(fp);
} }