summaryrefslogtreecommitdiffstats
path: root/lib/libfile.c
Commit message (Collapse)AuthorAgeFilesLines
* 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>