truncate a file

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

truncate a file

Postby davdav » Fri May 18, 2018 2:23 pm

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

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: truncate a file

Postby kolban » Fri May 18, 2018 2:27 pm

Have you included a header that defines the function truncate()? Perhaps <unistd.h> or <sys/unistd.h>?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: truncate a file

Postby davdav » Fri May 18, 2018 3:10 pm

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

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: truncate a file

Postby kolban » Fri May 18, 2018 4:49 pm

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).
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: truncate a file

Postby davdav » Mon May 21, 2018 7:52 am

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

Who is online

Users browsing this forum: No registered users and 122 guests