59 lines
1.2 KiB
C
59 lines
1.2 KiB
C
#include <sys/stat.h>
|
|
#include <sys/fcntl.h>
|
|
#include <sys/mman.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int fin;
|
|
struct stat st;
|
|
|
|
if (argc != 2)
|
|
{
|
|
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
fin = open(argv[1], O_RDWR);
|
|
assert(fin >= 0);
|
|
|
|
fstat(fin, &st);
|
|
|
|
if (st.st_nlink > 1)
|
|
{
|
|
unlink(argv[1]);
|
|
close(fin);
|
|
return 0;
|
|
}
|
|
|
|
char *src = mmap(NULL, st.st_size, PROT_WRITE, MAP_SHARED, fin, 0);
|
|
assert(src != MAP_FAILED);
|
|
|
|
memset(src, 0xff, st.st_size);
|
|
|
|
munmap(src, st.st_size);
|
|
src = mmap(NULL, st.st_size, PROT_WRITE, MAP_SHARED, fin, 0);
|
|
assert(src != MAP_FAILED);
|
|
|
|
int urandom = open("/dev/urandom", O_RDONLY);
|
|
assert(urandom >= 0);
|
|
|
|
ssize_t r = read(urandom, src, st.st_size);
|
|
assert(r == st.st_size);
|
|
|
|
munmap(src, st.st_size);
|
|
|
|
char new_name[64];
|
|
r = read(urandom, new_name, sizeof(new_name));
|
|
assert(r == sizeof(new_name));
|
|
|
|
close(urandom);
|
|
close(fin);
|
|
|
|
rename(argv[1], new_name);
|
|
remove(new_name);
|
|
} |