summaryrefslogtreecommitdiffstats
path: root/lib/libfile.c
Commit message (Collapse)AuthorAgeFilesLines
* copy_file: fix progress bar for files larger than 2GAhmad Fatoum2021-02-191-1/+1
| | | | | | | | | | | | | Files larger than 2G cause the benign bug, that the progress bar is no longer accurate. Use loff_t to fix this. Note that printed % HASHES_PER_LINE in show_progress implies a 64-bit division. A previous commit changed the constant divisor to a power of two, so a division shouldn't be generated. The original multiplication and modulo operations are left as is for clarity. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: copy_file: Fix ftruncate called on device filesSascha Hauer2020-08-181-5/+5
| | | | | | | | | | | | | ftruncate may only be called on regular files. This became broken in a5f73a6dcd. Revert to the previous behaviour and pass O_TRUNC to the initial open() call instead of truncating it with ftruncate(). Also we can call ftruncate() to the final size also when the file previously didn't exist. Fixes: a5f73a6dcd ("libfile: copy_file: explicitly truncate to final size") Reported-by: Bastian Krause <bst@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: copy_file: explicitly truncate to final sizeSascha Hauer2020-07-051-5/+11
| | | | | | | | When possible truncate the destination file to the final size explicitly. This allows for example ramfs to put the resulting file contiguously into memory. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: copy_file: Fix calling discard_rangeSascha Hauer2020-07-051-5/+6
| | | | | | | | | discard range is called with unitialized size argument. Call it after stat() on the source filedescriptor. Also there's no point in continuing when stat() on the source filedescriptor fails, so bail out in this case. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* treewide: remove references to CREDITSUwe Kleine-König2020-04-271-3/+0
| | | | | | | | The CREDITS file was removed from barebox in 2015 by commit 6570288f2d97 ("Remove the CREDITS file"). Remove references to it from several files. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* copy_file: call discard_range on destination fileSascha Hauer2020-02-141-0/+2
| | | | | | discard the range in the output file we are going to overwrite anyway. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: copy_file: fix error handlingRobert Karszniewicz2019-10-231-5/+12
| | | | | | | | Before this, ret was falsely polluted, which caused a misleading error message if the function bailed out at a later point. Signed-off-by: Robert Karszniewicz <r.karszniewicz@phytec.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: Document that read_file() returns a terminated bufferSascha Hauer2019-10-141-1/+3
| | | | | | | | read_file() and read_file_2() return a '\0' terminated buffer to make sure the buffer is usable as a string. Other code like read_file_line() depends on this behaviour, so document it explicitly. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: fix typosMichael Tretter2019-09-061-2/+2
| | | | | Signed-off-by: Michael Tretter <m.tretter@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: have write_full return a descriptive error codeAhmad Fatoum2019-08-301-2/+3
| | | | | | | | | | | | | | So far (p|)write_full has been returning -1 on error. Some callers of write_full like imx_bbu_write_device print out the function's return value on error, effectively rendering every printed error message to be due to EPERM. On the other hand, some callers like do_memcpy, use it correctly and use errno. Sidestep the issue by having the function return -errno on errors as well. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: Do not return 0 from write_full()Andrey Smirnov2019-05-281-2/+10
| | | | | | | | | | | | | None of the callers of write_full() expect a zero return value. Given how the documentation explicitly states that either all of the buffer is going to be written out or an error generated, treat 0 retrun from write() as a error, set errno to ENOSPC and return -1. Same logic applies to pwrite_full() as well, so make the change there while at it. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: Simplify read_full()Andrey Smirnov2019-05-281-4/+2
| | | | | | | | We can figure out the amount of written data by substracting 'insize' from 'size' so there is no need to keep a separate counter for that. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* Merge branch 'for-next/misc'Sascha Hauer2019-04-091-12/+6
|\
| * libfile: Make use of write_full() in copy_file()Andrey Smirnov2019-03-131-12/+6
| | | | | | | | | | | | | | | | Change copy_file() to rely on write_full() instead of re-implementing it locally. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* | libfile: Make failure path of open_and_lseek() consistentAndrey Smirnov2019-03-111-14/+12
|/ | | | | | | | | | Change the code of open_and_lseek() to make sure that opened file is closed in every failure path as well. While at it make sure we don't mix returning function return code and returning errno by opting to always return the former. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: Fix incorrect lseek check in open_and_lseek()Andrey Smirnov2019-02-061-2/+1
| | | | | | | | | | | We can't use "int" to capture output of lseek which is "loff_t", since former does not cover all of the range of the values of the latter and any pos >= 0xffff_ffff will result in a false positive "failure". To avoid that replace the check to check that new position matches what was requested. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: Error out if out of memory in read_file_2()Andrey Smirnov2018-10-161-1/+5
| | | | | | | | | | | | | | | All other error cases in read_file_2() are handled gracefully, so there shouldn't be any reason not do so for the case of trying to allocate too much memory. This error path can be easily triggered with: barebox_update file-bigger-than-availible-ram.img Currently this would result in a crash which is not really desirable from user experience. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: Introduce pwrite_full()Andrey Smirnov2018-08-311-0/+24
| | | | | | | | | | Analogous to what we have with write()/write_full(), introduce a lightweight wrapper around pwrite() that guarantees the either all data is going to be written or a negative error code would be returned. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: open_and_lseek: enlarge small files enough to make lseek possibleUwe Kleine-König2018-08-101-0/+18
| | | | | | | | | | | | | | | | | This makes the following do the expected thing: barebox@barebox sandbox:/ ls -l lala -rwxrwxrwx 4 lala barebox@barebox sandbox:/ mw -d lala 72 0 Without this patch mw dies with lseek: Invalid argument memset, memcpy and probably others benefit in the same way. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: open_and_lseek: don't imply O_RDONLYUwe Kleine-König2018-08-101-1/+1
| | | | | | | | | | There are several users that pass O_RDWR or O_WRONLY in mode to open_and_lseek() and use the resulting file descriptor for writing. This is no real issue becauce O_RDONLY is 0 and so can be dropped without any side effects. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: implement new helper write_file_flash()Uwe Kleine-König2018-06-081-0/+33
| | | | | | | | Compared to write_file() this new function also calls erase() to be suitable for flash devices. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* Merge branch 'for-next/tftp-workaround'Sascha Hauer2018-02-081-0/+55
|\
| * libfile: implement a function to cache a fileSascha Hauer2018-01-251-1/+29
| | | | | | | | | | | | | | | | | | | | Due to the nature of TFTP which can't lseek and due to the silliness of our filesystem implementation which can't cache accesses we have to manually cache files on TFTP filesystems sometimes. Make it easier for them by providing a cache_file() function which copies the file from TFTP to RAM. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
| * libfile: implement make_tempSascha Hauer2018-01-251-0/+27
| | | | | | | | | | | | | | | | | | | | Create a make_temp() function which creates a name for a temporary file. Since we do not have any concurrency in barebox we do not need to create the file right away and can leave that to the caller. Unlike unix mktemp the resulting filename is dynamically allocated and must be freed by the caller. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* | libfile: Make write_file buffer constSascha Hauer2018-01-311-1/+1
|/ | | | Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* lib: libfile: Fix copying files to a non existing destinationTeresa Remmet2017-01-171-4/+5
| | | | | | | | | | | | If the destination file does not exist the stat call to check the file type fails. Extend the check of the stat return value. To allow to copy files to a new destination. Fixes commit 0ec6bd3e1be8 ("libfile: copy_file: Only open regular files with O_TRUNC") Signed-off-by: Teresa Remmet <t.remmet@phytec.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: copy_file: Only open regular files with O_TRUNCTeresa Remmet2017-01-111-6/+16
| | | | | | | | Device files can not truncate in the most cases. Check if the destination is a regular file and open only those with O_TRUNC. Signed-off-by: Teresa Remmet <t.remmet@phytec.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: Change write_full to be have const bufMarkus Pargmann2016-07-081-1/+1
| | | | | | | write() uses a const pointer so write_full should do the same. Signed-off-by: Markus Pargmann <mpa@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* string: Fix (v)asprintf prototypesSascha Hauer2016-04-151-3/+3
| | | | | | | | | | Our asprintf and vasprintf have different prototypes than the glibc functions. This causes trouble when we want to share barebox code with userspace code. Change the prototypes for (v)asprintf to match the glibc prototypes. Since the current (v)asprintf are convenient to use change the existing functions to b(v)asprintf. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: move open_and_lseek() to libfileSascha Hauer2016-04-151-0/+32
| | | | | | | libfile is a collection of helpers for handling files. open_and_lseek() is a perfect match for this, so move it there. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* lib: libfile: Do not ignore errors in close()Andrey Smirnov2015-08-191-3/+3
| | | | | | | | | | | Some character devices may perform meaningful operations in their implementation of close() -- a good example would be socfpga.c which checks if the FPGA was programmed succesfully in it's close() method -- so ignoring return value of this call may cause false positives in checking exit status for success. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* read_file_2(): drop unnecessary 'else'Alexander Smirnov2015-07-021-2/+2
| | | | | Signed-off-by: Alexander Smirnov <alllecs@yandex.ru> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: Add copy_recursiveSascha Hauer2014-11-061-0/+40
| | | | | | To recursively copy a directory tree. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* libfile: add compare_file functionSascha Hauer2014-10-091-0/+71
| | | | Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* read_file_2: Fix return valueTeresa Gámez2014-09-251-1/+3
| | | | | | | | Set return value correct in error case. This prevents barebox from crashing in some conditions. Signed-off-by: Teresa Gámez <t.gamez@phytec.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* read_file: introduce read_file_2Sascha Hauer2014-08-071-14/+57
| | | | | | | | | | | | | read_file has some limitations: - It is not possible to check the error code since read_file returns NULL for failure - It is not possible to limit the buffer size to sensible limits. This patch introduces read_file_2 which doesn't have these limitations. read_file becomes a wrapper around read_file_2. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
* move file helper functions to separate fileSascha Hauer2014-08-071-0/+288
We have our file helper functions in several places. Move them all to lib/libfile.c. With this we no longer have file helpers in fs/fs.c which contains the core fs functions and no functions in lib/libbb.c which are not from busybox. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>