Page 1 of 1

truncate a file

Posted: Fri May 18, 2018 2:23 pm
by davdav
Hi everybody,
I have a FATFS filesystem and I open/write/read/close files without problems using stdio.h functions (fopen, fread,ecc..).

I need to truncate a file to a length less than original and I would like to use

Code: Select all

truncate, (const char *, off_t __length);
reported in header file.

Code: Select all

~\esp-idf\components\newlib\include\sys/unistd.h
However, this seems not available when compiled I got

Code: Select all

error: implicit declaration of function 'truncate'
How can I truncate a file without reading all file content and writing it back with only new content?

Thanks

Re: truncate a file

Posted: Fri May 18, 2018 2:27 pm
by kolban
Have you included a header that defines the function truncate()? Perhaps <unistd.h> or <sys/unistd.h>?

Re: truncate a file

Posted: Fri May 18, 2018 3:10 pm
by davdav
Hello @Kolban,

yes I have included <unistd.h> (newlib/include/unistd.h) which inside includes <sys/unistd.h> (newlib/include/sys/unistd.h)
where "truncate" is defined.

However when opened in eclipse, the definition are surronded by some "define"

Code: Select all

#if defined(__CYGWIN__) || defined(__rtems__) || defined(__aarch64__) || defined (__arm__) || defined(__sh__) || defined(__SPU__)
#if !defined(__INSIDE_CYGWIN__)
int     _EXFUN(ftruncate, (int __fd, off_t __length));
int     _EXFUN(truncate, (const char *, off_t __length));
#endif
#endif
and they are gey (so not defined). That's the reason, but why aren't they available?

Thanks

Re: truncate a file

Posted: Fri May 18, 2018 4:49 pm
by kolban
If it were me, I'd try the following test.

In your application, near the top, add the following logic:

Code: Select all

extern int truncate(const char* path, off_t length);
This should resolve (bypass?) the compilation error relating to truncate being undefined. Next we will find if it is resolved at link time ... if we get an error that truncate() symbol couldn't be found by the linker then we have a different issue. If the linker finds it and we get a clean executable, the next step would be to test that it actually works (truncates the file).

If these tests work, then you have an intermediate resolution and we should then create a Github issue raised describing the problem and what we did to work around it ... it may be that this function hasn't been exercised before (it happens).

Re: truncate a file

Posted: Mon May 21, 2018 7:52 am
by davdav
Hello Kolban,

I followed your suggestion and tried to extern the function

Code: Select all

extern int truncate(const char *, off_t __length);
Unfortunately it seems the function is not implemented in any library because linker complains

Code: Select all

C:/msys32/home/Davide/esp/Avior_ESP32_WiFi/build/main\libmain.a(parse.o):(.literal.PARSE_EditMap+0x240): undefined reference to `truncate'
C:/msys32/home/Davide/esp/Avior_ESP32_WiFi/build/main\libmain.a(parse.o): In function `PARSE_EditMap':
C:/msys32/home/Davide/esp/Avior_ESP32_WiFi/main/parse.c:2770: undefined reference to `truncate'
collect2.exe: error: ld returned 1 exit status
make: *** [C:/msys32/home/Davide/esp/esp-idf/make/project.mk:406: /home/Davide/esp/Avior_ESP32_WiFi/build/Avior_ESP32_WiFi_v2.elf] Error 1

I will open an issue on github and hopefully espressif guys will include in "newlib" library.

Thanks