Uninitialized files

I was just doing some research for a project and came across SetFileValidData() on Windows, which can be used to nearly instantly allocate a huge file containing whatever happened to be on the file system's free space. Just out of curiosity, I was wondering if there's a function like that on Linux/Unix.
I think you may be interested in fallocate(2) http://man7.org/linux/man-pages/man2/fallocate.2.html
Just out of curiosity, I was wondering if there's a function like that on Linux/Unix.

I hope not since it seems like a huge security risk.

You can allocate space to a file simply by seeking to whereever you want writing from there, but I think you'll find that the space is set to zeros or random data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdio>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

using namespace std;
int main()
{
    int fd = open("foo", O_WRONLY| O_CREAT);
    printf("open returned %d: %s\n", fd, strerror(errno));
    int ret = lseek(fd, 1000000, SEEK_SET);
    printf("lseek returned %d: %s\n", ret, strerror(errno));
    ret = write(fd, "hello\n", 6);
    printf("write returned %d: %s\n", ret, strerror(errno));
    close(fd);
	
    return 0;
}
Topic archived. No new replies allowed.