mirror of
https://github.com/MariaDB/server.git
synced 2025-01-23 15:24:16 +01:00
61 lines
1.4 KiB
C
61 lines
1.4 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <stdint.h>
|
||
|
#include <unistd.h>
|
||
|
#include <assert.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <string.h>
|
||
|
#include <sys/stat.h>
|
||
|
|
||
|
#if 0 && defined _WIN32
|
||
|
#include <windows.h>
|
||
|
static int ftruncate(int fd, uint64_t offset) {
|
||
|
HANDLE h = (HANDLE) _get_osfhandle(fd);
|
||
|
printf("%s:%d %p\n", __FILE__, __LINE__, h); fflush(stdout);
|
||
|
if (h == INVALID_HANDLE_VALUE)
|
||
|
return -1;
|
||
|
int r = _lseeki64(fd, 0, SEEK_SET);
|
||
|
printf("%s:%d %d\n", __FILE__, __LINE__, r); fflush(stdout);
|
||
|
if (r != 0)
|
||
|
return -2;
|
||
|
BOOL b = SetEndOfFile(h);
|
||
|
printf("%s:%d %d\n", __FILE__, __LINE__, b); fflush(stdout);
|
||
|
if (!b)
|
||
|
return -3;
|
||
|
return 0;
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
int main(void) {
|
||
|
int r;
|
||
|
int fd;
|
||
|
|
||
|
fd = open("test-file-truncate", O_CREAT+O_RDWR+O_TRUNC, S_IREAD+S_IWRITE);
|
||
|
assert(fd != -1);
|
||
|
|
||
|
int i;
|
||
|
for (i=0; i<32; i++) {
|
||
|
char junk[4096];
|
||
|
memset(junk, 0, sizeof junk);
|
||
|
r = write(fd, junk, sizeof junk);
|
||
|
assert(r == sizeof junk);
|
||
|
}
|
||
|
|
||
|
struct stat filestat;
|
||
|
r = fstat(fd, &filestat);
|
||
|
assert(r == 0);
|
||
|
|
||
|
printf("orig size %lu\n", (unsigned long) filestat.st_size); fflush(stdout);
|
||
|
|
||
|
r = ftruncate(fd, 0);
|
||
|
assert(r == 0);
|
||
|
|
||
|
r = fstat(fd, &filestat);
|
||
|
assert(r == 0);
|
||
|
|
||
|
printf("truncated size %lu\n", (unsigned long) filestat.st_size); fflush(stdout);
|
||
|
assert(filestat.st_size == 0);
|
||
|
|
||
|
return 0;
|
||
|
}
|