2 * Copyright (c) 2014 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup sysinst
32 /** @file File manipulation utility functions for installer
46 #define BUF_SIZE 16384
47 static char buf
[BUF_SIZE
];
51 * @param srcp Source path
52 * @param dstp Destination path
54 * @return EOK on success, EIO on I/O error
56 errno_t
futil_copy_file(const char *srcp
, const char *destp
)
61 aoff64_t posr
= 0, posw
= 0;
63 printf("Copy '%s' to '%s'.\n", srcp
, destp
);
65 rc
= vfs_lookup_open(srcp
, WALK_REGULAR
, MODE_READ
, &sf
);
69 rc
= vfs_lookup_open(destp
, WALK_REGULAR
| WALK_MAY_CREATE
, MODE_WRITE
, &df
);
74 rc
= vfs_read(sf
, &posr
, buf
, BUF_SIZE
, &nr
);
80 rc
= vfs_write(df
, &posw
, buf
, nr
, &nw
);
84 } while (nr
== BUF_SIZE
);
99 /** Copy contents of srcdir (recursively) into destdir.
101 * @param srcdir Source directory
102 * @param destdir Destination directory
104 * @return EOK on success, ENOMEM if out of memory, EIO on I/O error
106 errno_t
futil_rcopy_contents(const char *srcdir
, const char *destdir
)
114 dir
= opendir(srcdir
);
120 if (asprintf(&srcp
, "%s/%s", srcdir
, de
->d_name
) < 0)
122 if (asprintf(&destp
, "%s/%s", destdir
, de
->d_name
) < 0)
125 rc
= vfs_stat_path(srcp
, &s
);
130 rc
= futil_copy_file(srcp
, destp
);
133 } else if (s
.is_directory
) {
134 printf("Create directory '%s'\n", destp
);
135 rc
= vfs_link_path(destp
, KIND_DIRECTORY
, NULL
);
138 rc
= futil_rcopy_contents(srcp
, destp
);
151 /** Return file contents as a heap-allocated block of bytes.
153 * @param srcp File path
154 * @param rdata Place to store pointer to data
155 * @param rsize Place to store size of data
157 * @return EOK on success, ENOENT if failed to open file, EIO on other
158 * I/O error, ENOMEM if out of memory
160 errno_t
futil_get_file(const char *srcp
, void **rdata
, size_t *rsize
)
169 rc
= vfs_lookup_open(srcp
, WALK_REGULAR
, MODE_READ
, &sf
);
173 if (vfs_stat(sf
, &st
) != EOK
) {
180 data
= calloc(fsize
, 1);
186 rc
= vfs_read(sf
, (aoff64_t
[]) { 0 }, data
, fsize
, &nr
);
187 if (rc
!= EOK
|| nr
!= fsize
) {