Instantly share code, notes, and snippets.

Embed
What would you like to do?
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, const char* argv[])
{
char* alignedBuffer;
char* temp;
char* dirBuf;
char readBuffer[4096];
int wfd, dfd, rfd, rc;
size_t i;
alignedBuffer = aligned_alloc(4096, 4096);
if (alignedBuffer == NULL) {
printf("Line %d Failed to allocate aligned memory : %s\n", __LINE__, strerror(errno));
return __LINE__;
}
memset(alignedBuffer, '$', 4096);
if (argc != 2) {
printf("Line %d Usage: %s <file>\n", __LINE__, argv[0]);
return __LINE__;
}
wfd = open(argv[1], O_DIRECT | O_DSYNC | O_WRONLY | O_TRUNC | O_CREAT, S_IWUSR | S_IRUSR);
if (wfd == -1) {
printf("Line %d Failed to open file: %s\n", __LINE__, strerror(errno));
return __LINE__;
}
rc = pwrite(wfd, alignedBuffer, 4096, (256 * 1024 * 1024) - 4096 );
if (rc != 4096) {
printf("Line %d Failed to write / extend to end of file, wrote %d : %s\n", __LINE__, rc, strerror(errno));
return __LINE__;
}
rc = fsync(wfd);
if (rc != 0) {
printf("Line %d Failed to fsync file : %s\n", __LINE__, strerror(errno));
return __LINE__;
}
temp = strdup(argv[1]);
if(temp == NULL){
printf("Line %d Failed duplicate file string: %s\n", __LINE__, strerror(errno));
return __LINE__;
}
dirBuf = dirname(temp); // get directory
dfd = open(dirBuf, 0, 0);
if (dfd == -1) {
printf("Line %d Failed to open directory: %s\n", __LINE__, strerror(errno));
return __LINE__;
}
free(temp);
// rc = fsync(dfd);
if (rc != 0) {
printf("Line %d Failed to fsync directory : %s\n", __LINE__, strerror(errno));
return __LINE__;
}
rc = close(dfd);
if( rc == -1) {
printf("Line %d Failed to close directory : %s\n", __LINE__, strerror(errno));
return __LINE__;
}
for (i = 0; i < 256; i++)
{
rc = pwrite(wfd, alignedBuffer, 4096, i * 4096);
if (rc != 4096) {
printf("Line %d Failed to write full data to disk, wrote %d : %s\n", __LINE__, rc, strerror(errno));
return __LINE__;
}
}
rfd = open(argv[1], O_RDONLY, S_IRUSR);
if( rfd == -1) {
printf("Line %d Failed to open the file for reading : %d %s\n", __LINE__, rc, strerror(errno));
return __LINE__;
}
rc = pwrite(wfd, alignedBuffer, 4096, 256 * 4096);
if (rc != 4096) {
printf("Line %d Failed to write full data to disk, wrote %d : %s\n", __LINE__, rc, strerror(errno));
return __LINE__;
}
for (i = 0; i < 257; i++)
{
memset(readBuffer, 1, 4096);
rc = pread(rfd, readBuffer, 4096, i * 4096);
if (rc = 0) {
printf("Line %d Failed to read data from disk : %d %s\n", __LINE__, rc, strerror(errno));
return __LINE__;
}
if (readBuffer[0] != '$') {
printf("Line %d Read corrupted data from disk: on %zu %c\n", __LINE__, i, readBuffer[0]);
return __LINE__;
}
}
memset(readBuffer, 1, 4096);
rc = pread(rfd, readBuffer, 4096, 257 * 4096);
if (rc = 0) {
printf("Line %d Failed to read data from disk : %d %s\n", __LINE__, rc, strerror(errno));
return __LINE__;
}
if (readBuffer[0] != 0) {
printf("Line %d Read corrupted data from disk, expected zero: %c\n", __LINE__, readBuffer[0]);
return __LINE__;
}
rc = pwrite(wfd, alignedBuffer, 4096, 257 * 4096);
if (rc != 4096) {
printf("Line %d Failed to write full data to disk, wrote %d : %s\n", __LINE__, rc, strerror(errno));
return __LINE__;
}
memset(readBuffer, 1, 4096);
rc = pread(rfd, readBuffer, 4096, 257 * 4096);
if (rc = 0) {
printf("Line %d Failed to read data from disk : %d %s\n", __LINE__, rc, strerror(errno));
return __LINE__;
}
if (readBuffer[0] != '$') {
printf("Line %d Read corrupted data from disk: %d but exepcted '$' \n", __LINE__, readBuffer[0]);
return __LINE__;
}
printf("Line %d Test ended successfully\n");
free(alignedBuffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment