-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eadd7bd
commit 8441dbc
Showing
8 changed files
with
221 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,112 @@ | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
#include <sys/file.h> | ||
#include <errno.h> | ||
#include <sys/types.h> // for lseek() | ||
#include <unistd.h> // for lseek() | ||
|
||
|
||
#include "reader.h" | ||
#include "shared_stuff.h" | ||
#include "reader_constants.h" | ||
|
||
static const int reader_f_flags = O_RDONLY; | ||
|
||
int reader(int file_num) { | ||
return reader_ranged(file_num, 0, LINES_PER_FILE - 1); | ||
} | ||
|
||
int reader(int file_num){ | ||
char file_name[64]; | ||
int fd, file_value; | ||
sprintf(file_name, FILENAME, file_num); // place file_num in FILENAME | ||
|
||
fd = open(file_name, reader_f_flags); | ||
if (fd == -1) { // error opening the file (does not exist?) | ||
printf("open(%s) failed. errno=%d\n", file_name, errno); | ||
return FILE_IS_INVALID; | ||
} | ||
|
||
int reader_ranged(int file_num, int first_line, int last_line) { | ||
char file_name[64]; | ||
int fd, file_value; | ||
sprintf(file_name, FILENAME, file_num); // place file_num in FILENAME | ||
fd = open(file_name, reader_f_flags); | ||
if (fd == -1) { // error opening the file (does not exist?) | ||
printf("open(%s) failed. errno=%d\n", file_name, errno); | ||
return FILE_IS_INVALID; | ||
} | ||
flock(fd, LOCK_SH); | ||
|
||
if ( file_contents_are_valid(fd, WRITER_STRING_LEN, LINES_PER_FILE) == TRUE ){ | ||
file_value = FILE_IS_VALID; | ||
} else{ | ||
file_value = FILE_IS_INVALID; | ||
} | ||
|
||
|
||
if ( file_contents_are_valid(fd, WRITER_STRING_LEN, | ||
first_line, last_line) == TRUE ) { | ||
file_value = FILE_IS_VALID; | ||
} else { | ||
file_value = FILE_IS_INVALID; | ||
} | ||
|
||
flock(fd, LOCK_UN); | ||
|
||
close(fd); | ||
return file_value; | ||
close(fd); | ||
return file_value; | ||
} | ||
|
||
|
||
int file_contents_are_valid(int fd, int line_length, int line_count) | ||
{ | ||
// number of bytes to read at a time from the file | ||
int line_size = line_length * sizeof(char); | ||
|
||
char *first_line = (char*) malloc(line_size); | ||
|
||
// try to read and validate the first line of the file | ||
if ( read(fd, first_line, line_size) != line_size ) { | ||
// the first line doesn't even have the correct number of characters | ||
free(first_line); | ||
return FALSE; // file is invalid | ||
} | ||
if ( !known_writer_string(first_line, line_length) ){ | ||
free(first_line); | ||
return FALSE; // file is invalid | ||
} | ||
|
||
// the first line is valid, so we can compare it with all other lines | ||
char *line_buffer = (char*) malloc(line_size); | ||
|
||
// compare all lines with the first line | ||
int i; | ||
for ( i = 1 ; read(fd, line_buffer, line_size) == line_size; i++ ) | ||
{ | ||
if ( strncmp(line_buffer, first_line, line_length) != 0 || i >= line_count ){ | ||
free(first_line); | ||
free(line_buffer); | ||
return FALSE; // file is invalid | ||
} | ||
} | ||
|
||
free(first_line); | ||
free(line_buffer); | ||
|
||
if (i != line_count) | ||
return FALSE; | ||
|
||
return TRUE; // file is valid | ||
int file_contents_are_valid(int fd, int line_length, int first_line, int last_line){ | ||
int line_count = last_line - first_line + 1; | ||
|
||
// number of bytes to read at a time from the file | ||
int line_size = line_length * sizeof(char); | ||
DBG_PRINTF("fd=%d, line_size = %d\n", fd, line_size); | ||
|
||
char *first_line_buf = (char*) malloc(line_size); | ||
|
||
// try to read and validate the first line of the file | ||
if ( read(fd, first_line_buf, line_size) != line_size ) { | ||
// the first line doesn't even have the correct number of characters | ||
free(first_line_buf); | ||
DBG_PRINT("invalid file detected here\n"); | ||
return FALSE; // file is invalid | ||
} | ||
|
||
DBG_PRINTF("fd=%d, first_line_buf='%s'\n",fd,first_line_buf); | ||
if ( !known_writer_string(first_line_buf, line_length) ){ | ||
free(first_line_buf); | ||
DBG_PRINT("invalid file detected here\n"); | ||
return FALSE; // file is invalid | ||
} | ||
|
||
// the first line is valid, so we can compare it with all other lines | ||
char *line_buffer = (char*) malloc(line_size); | ||
|
||
lseek( fd, first_line * line_size, SEEK_SET ); | ||
|
||
// compare all lines with the first line | ||
int i; | ||
for ( i = 0 ; read(fd, line_buffer, line_size) == line_size; i++ ) | ||
{ | ||
if ( strncmp(line_buffer, first_line_buf, line_length) != 0 || i >= line_count ){ | ||
free(first_line_buf); | ||
free(line_buffer); | ||
DBG_PRINTF("fd=%d, invalid file detected here; first_line=%d, i=%d\nline_buff='%s'; first_line_buf='%s'\n", | ||
fd,first_line, i, line_buffer, first_line_buf); | ||
return FALSE; // file is invalid | ||
} | ||
} | ||
|
||
free(first_line_buf); | ||
free(line_buffer); | ||
|
||
if (i != line_count) { | ||
DBG_PRINT("invalid file detected here\n"); | ||
return FALSE; | ||
} | ||
|
||
return TRUE; // file is valid | ||
} | ||
|
||
int known_writer_string(char *str, int str_len){ | ||
int i; | ||
|
||
for ( i = 0; i < WRITER_STRING_COUNT; i++ ){ | ||
if ( strncmp(str, writer_strings[i], str_len) == 0 ){ | ||
return TRUE; | ||
} | ||
} | ||
|
||
return FALSE; | ||
int i; | ||
|
||
for ( i = 0; i < WRITER_STRING_COUNT; i++ ){ | ||
if ( strncmp(str, writer_strings[i], str_len) == 0 ){ | ||
return TRUE; | ||
} | ||
} | ||
DBG_PRINT("invalid file detected here\n"); | ||
|
||
return FALSE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.