67 lines
1.3 KiB
C
67 lines
1.3 KiB
C
#define _GNU_SOURCE /* Required for linux fallocate */
|
|
#include <fcntl.h>
|
|
#include <linux/falloc.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
int posix_falloc(void);
|
|
int file_trunc(void);
|
|
int linux_falloc(void);
|
|
|
|
#define FILENAME "allocated.bin"
|
|
|
|
int main(void) {
|
|
posix_falloc();
|
|
file_trunc();
|
|
linux_falloc();
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int posix_falloc(void) {
|
|
int fd = open(FILENAME, O_CREAT | O_WRONLY, 0666);
|
|
if (fd == -1) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
off_t length = 1024 * 1024; // 1 MB
|
|
int ret = posix_fallocate(fd, 0, length);
|
|
if (ret != 0) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
close(fd);
|
|
remove(FILENAME);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int linux_falloc(void) {
|
|
int fd = open(FILENAME, O_CREAT | O_WRONLY, 0666);
|
|
if (fd == -1) {
|
|
perror("Error");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
off_t length = 1024 * 1024; // 1 MB
|
|
if (fallocate(fd, 0, 0, length) == -1) {
|
|
perror("fallocate");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
close(fd);
|
|
remove(FILENAME);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int file_trunc(void) {
|
|
int fd = open(FILENAME, O_CREAT | O_WRONLY, 0666);
|
|
if (fd == -1) {
|
|
perror("Error");
|
|
return EXIT_FAILURE;
|
|
}
|
|
off_t length = 1024 * 1024; // 1 MB
|
|
ftruncate(fd, length);
|
|
remove(FILENAME);
|
|
return EXIT_SUCCESS;
|
|
}
|