Just because you are seeking to the beginning of the file doesn't mean that the file is truncated. It's possible to do random writes on a file.
Writing down a block on a full file system is going to cause issues. If you want to truncate the file use truncate(2)
or ftruncate
call on the file BEFORE lseek
try:
if(errno == ENOSPC) {
ftruncate(gi4LogFd, 0);
lseek(gi4LogFd, 0, SEEK_SET);
break;
}
Okay so ext3 filesystem with journaling support does not create an issue on a full fs:
Setup:
Create an image file:
dd if=/dev/zero of=/tmp/img.dsk count=8192
Created an ext3 filesystem on a 4k image file:
mkfs.ext3 /tmp/img.dsk
sudo mount /tmp/img.dsk /mnt/internal
sudo chown masud.users /mnt/internal
touch /mnt/internal/file.bin
sudo dd if=/dev/urandom of=/mnt/internal/file.bin
here sudo is necessary for dd to make sure that the reserve for superuser is filled up.
so now :
df /mnt/internal/ shows:
/dev/loop/0 3963 3963 0 100% /mnt/internal
Using the following code:
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
char buf[8192];
int main(int argc, char *argv[])
{
int rv;
char *filename;
if ( argc < 2 ) {
fprintf(stderr, "provide the filename\n");
return -1;
}
filename = argv[1];
int rd = open("/dev/urandom", O_RDONLY);
read(rd, buf, sizeof(buf));
close(rd);
int fd = open(filename, O_SYNC|O_RDWR);
lseek(fd, -sizeof(buf), SEEK_END);
rv = write(fd, buf, sizeof(buf));
if ( rv < 0 ) {
perror(filename);
goto out;
}
lseek(fd, 0, SEEK_SET);
rv = write(fd, "foo", 3);
if ( rv < 0 ) {
perror(filename);
}
out:
close(fd);
return rv;
}
Now:
./foo /mnt/internal/file.bin
Succeeds.
So question is how is this different from your environment?