Browse Source

Handle file size bigger than 2G

master
parent
commit
6c33ae83e0
  1. 14
      src/fs/fs.c
  2. 5
      src/fs/fs.h
  3. 2
      testing/test.c

14
src/fs/fs.c

@ -14,21 +14,13 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
// Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong
uint64_t file_size(char* path) {
FILE* file_stream;
uint64_t file_size;
struct stat file_stats;
file_stream = fopen(path, "rb");
if (!file_stream) {
if (stat(path , &file_stats) != 0) {
return UINT64_MAX;
}
fseek(file_stream, 0, SEEK_END);
file_size = ftell(file_stream);
fseek(file_stream, 0, SEEK_SET);
fclose(file_stream);
return file_size;
return (uint64_t) file_stats.st_size;
}
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns -1 in case of an error and 0 if the operation was sucessfull

5
src/fs/fs.h

@ -13,6 +13,11 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#if __WIN32__
#define stat _stat64
#endif
// Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong
uint64_t file_size(char* path);

2
testing/test.c

@ -121,7 +121,7 @@ int test_endian() {
int test_fs() {
if (file_size("test_img512x512.ppm") == UINT64_MAX) {
printf("[ERROR] Failed to determine file size of test ppm image");
printf("[ERROR] Failed to determine file size of test ppm image\n");
return EXIT_FAILURE;
}

Loading…
Cancel
Save