Last active
August 29, 2015 14:19
-
-
Save hoterran/5ae365a6c0ffe38293a2 to your computer and use it in GitHub Desktop.
ftruncate a big file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <sys/stat.h> | |
#include <time.h> | |
// Purpose: ftruncate big file | |
// | |
// argv[1] file | |
// argv[2] shrinked size each loop, default is 100m | |
long current_time() { | |
struct timeval t; | |
gettimeofday(&t, NULL); | |
return t.tv_sec * 1000l + t.tv_usec/ 1000l; | |
} | |
int main(int argc, char* argv[]) { | |
struct stat st; | |
stat(argv[1], &st); | |
printf("file size is %ld\n", st.st_size); | |
off_t l = st.st_size; | |
off_t shrink_size = 100*1024*1024; | |
if (argc == 3) | |
shrink_size = atoi(argv[2]); | |
off_t shrinked = 0; | |
while (l > 0) { | |
if (l > shrink_size) { | |
l = l - shrink_size; | |
shrinked = shrink_size; | |
} else { | |
shrinked = l; | |
l = 0; | |
} | |
long s1 = current_time(); | |
truncate(argv[1], l); | |
long s2 = current_time(); | |
printf("--shrinked %ld, now is %ld , spend is %dms --\n", shrinked, l, s2-s1); | |
sleep(1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment