gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > need (f)truncate function

#169963 - hamm - Wed Aug 19, 2009 11:50 am

I want to reduce the size of a file without copying. I can't copy the files because they could be to large and the space is very limeted.

I've found the fatfile.c in the libfat library including the function
Code:
int _FAT_ftruncate_r (struct _reent *r, int fd, off_t len)

but I don't know how to use this.

Any ideas how to use this function or another one or way to solve my problem?

#169964 - ritz - Wed Aug 19, 2009 2:20 pm

http://linux.die.net/man/2/ftruncate
Is this the info your looking for?

#169967 - hamm - Wed Aug 19, 2009 6:32 pm

Sorry this is is not the info I'm looking for because the function is not included in libfat.

Furthermore the function from your link uses only 2 arguments
Code:
int ftruncate(int fd, off_t length)

but the function from the libfat needs 3 arguments.
Code:
int _FAT_ftruncate_r (struct _reent *r, int fd, off_t len)

The first argument is different and I don't know what this argument needs.

#169968 - Mighty Max - Wed Aug 19, 2009 6:37 pm

hamm wrote:
Sorry this is is not the info I'm looking for because the function is not included in libfat..


Doesn't need to. libfat is used indirect. The fopen etc functions arn't included from libfat either but by stdio.h which through some magic uses libfat's functions.
_________________
GBAMP Multiboot

#169969 - hamm - Wed Aug 19, 2009 6:51 pm

If I include the stdio.h and try to use the functions truncate, ftruncate or _FAT_ftruncate_r I got ever the warning
Code:
implicit declaration of function

Whats wrong?

#169978 - kusma - Thu Aug 20, 2009 11:08 am

hamm wrote:
If I include the stdio.h and try to use the functions truncate, ftruncate or _FAT_ftruncate_r I got ever the warning
Code:
implicit declaration of function

Whats wrong?

Possibly because ftruncate() is defined in unistd.h, and not stdio.h?

#169988 - hamm - Thu Aug 20, 2009 6:46 pm

Thanks the (f)trunced is defined in unistd.h.

Now I can't get working the function.

I tried this.

Code:

#include <nds.h>
#include <stdio.h>
#include <unistd.h>

int main(void) {

truncate("test.dat", 123);

while(1) {
      swiWaitForVBlank();
   }

   return 0;
}

After running the nds file the test.dat has the same filesize like before.

Is there anything I've forgotton?

#169989 - DiscoStew - Thu Aug 20, 2009 8:10 pm

Code:
#include <fat.h>

fatInitDefault();


Don't forget to add -lfat in your LIBS section of the Makefile.
_________________
DS - It's all about DiscoStew

#169991 - hamm - Thu Aug 20, 2009 8:31 pm

I tried this also but it hangs on.

#170016 - hamm - Fri Aug 21, 2009 10:14 pm

fsync is the golden tip.

The following works fine.

Code:
#include <unistd.h>
#include <fcntl.h>

...

int fd = open("TEST.DAT", O_RDWR);
ftruncate (fd, 123);
fsync(fd);