Added max id for unix_fs_driver + better macros

This commit is contained in:
thematdev 2023-07-02 12:55:42 +03:00
parent d5f0e478e1
commit 3d798c88bf
Signed by: thematdev
GPG Key ID: D12878639B090D90
3 changed files with 62 additions and 10 deletions

View File

@ -53,12 +53,20 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
UnixFsDriverData driver_data = { path };
int max_id = unix_fs_driver_get_max_id(&driver_data);
if (max_id <= 0) {
fprintf(stderr, "Warning: failed to get id of last comment\n");
} else if (id >= max_id) {
fprintf(stderr, "Last comment has id %d, which is less than %d\n", max_id - 1, id);
exit(EXIT_FAILURE);
}
CommentHeader *header = malloc(sizeof(CommentHeader)); CommentHeader *header = malloc(sizeof(CommentHeader));
header->user_sid = malloc(BUF_SZ); header->user_sid = malloc(BUF_SZ);
header->user_displayname = malloc(BUF_SZ); header->user_displayname = malloc(BUF_SZ);
UnixFsDriverData driver_data = { path };
if (driver->get_header(&driver_data, header, id) < 0) { if (driver->get_header(&driver_data, header, id) < 0) {
fprintf(stderr, "failed to get header of comment, are you sure it exists?\n"); fprintf(stderr, "failed to get header of comment, are you sure it exists?\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -12,11 +12,12 @@ typedef struct {
} UnixFsDriverData; } UnixFsDriverData;
int unix_fs_driver_leave_comment(void *driver_data_ptr, const CommentHeader *header, const char *text); int unix_fs_driver_leave_comment(void *driver_data_ptr, const CommentHeader *header, const char *text);
int unix_fs_driver_get_header(void *driver_data_ptr, CommentHeader *header, int id); int unix_fs_driver_get_header(void *driver_data_ptr, CommentHeader *header, int id);
int unix_fs_driver_get_text(void *driver_data_ptr, char *text, int id); int unix_fs_driver_get_text(void *driver_data_ptr, char *text, int id);
static const Driver unix_fs_driver = { unix_fs_driver_leave_comment, unix_fs_driver_get_header, unix_fs_driver_get_text }; static const Driver unix_fs_driver = { unix_fs_driver_leave_comment, unix_fs_driver_get_header, unix_fs_driver_get_text };
int unix_fs_driver_get_max_id(void *driver_data_ptr);
#endif /* UNIX_FS_DRIVER_H */ #endif /* UNIX_FS_DRIVER_H */

View File

@ -16,15 +16,22 @@
#define READ_BUF_SZ 256 #define READ_BUF_SZ 256
#define SWRITE(fd, buf, count) \ #define SWRITE(fd, buf, count) do { \
if (write(fd, buf, count) < count) { \ if (write(fd, buf, count) < count) { \
return -EINVAL; \ return -EINVAL; \
} } \
} while(0);
#define SREAD(fd, buf, count) \ #define SREAD(fd, buf, count) do { \
if (read(fd, buf, count) < count) { \ if (read(fd, buf, count) < count) { \
return -EINVAL; \ return -EINVAL; \
} } \
} while (0);
#define NOLCK_ABORT() do { \
fprintf(stderr, "Failed to release id control file, aborting...\n"); \
abort(); \
} while (0);
int int
is_valid_directory(const char *path) is_valid_directory(const char *path)
@ -121,6 +128,43 @@ release_id_ctrl_file(FILE *id_ctrl_file)
return fclose(id_ctrl_file); return fclose(id_ctrl_file);
} }
int
unix_fs_driver_get_max_id(void *driver_data_ptr)
{
char *path, *id_ctrl_file_path;
FILE *id_ctrl_file;
int retval;
path = ((UnixFsDriverData*) driver_data_ptr)->path;
id_ctrl_file_path = NULL;
id_ctrl_file = NULL;
if (asprintf(&id_ctrl_file_path, "%s/%s", path, ID_CTRL_FILE) < 0) {
id_ctrl_file_path = NULL;
retval = -ENOMEM;
goto defer;
}
if (!(id_ctrl_file = acquire_id_ctrl_file(id_ctrl_file_path))) {
retval = -EIO;
goto defer;
}
if (fscanf(id_ctrl_file, "%d", &retval) < 1) {
retval = -EIO;
goto defer;
}
defer:
if (id_ctrl_file) {
if (release_id_ctrl_file(id_ctrl_file) < 0) {
NOLCK_ABORT();
}
}
free(id_ctrl_file_path);
return retval;
}
int int
unix_fs_driver_leave_comment(void *driver_data_ptr, const CommentHeader *header, const char *text) unix_fs_driver_leave_comment(void *driver_data_ptr, const CommentHeader *header, const char *text)
{ {
@ -196,8 +240,7 @@ unix_fs_driver_leave_comment(void *driver_data_ptr, const CommentHeader *header,
defer: defer:
if (id_ctrl_file) { if (id_ctrl_file) {
if (release_id_ctrl_file(id_ctrl_file) < 0) { if (release_id_ctrl_file(id_ctrl_file) < 0) {
fprintf(stderr, "Failed to release id control file, aborting...\n"); NOLCK_ABORT();
abort();
} }
} }
free(id_ctrl_file_path); free(id_ctrl_file_path);