1 | /*
|
---|
2 | * Copyright (c) 2014 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup sysinst
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file File manipulation utility functions for installer
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <dirent.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <fcntl.h>
|
---|
38 | #include <stdbool.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <sys/stat.h>
|
---|
42 | #include <vfs/vfs.h>
|
---|
43 | #include <sys/types.h>
|
---|
44 | #include <dirent.h>
|
---|
45 |
|
---|
46 | #include "futil.h"
|
---|
47 |
|
---|
48 | #define BUF_SIZE 16384
|
---|
49 | static char buf[BUF_SIZE];
|
---|
50 |
|
---|
51 | /** Copy file.
|
---|
52 | *
|
---|
53 | * @param srcp Source path
|
---|
54 | * @param dstp Destination path
|
---|
55 | *
|
---|
56 | * @return EOK on success, EIO on I/O error
|
---|
57 | */
|
---|
58 | int futil_copy_file(const char *srcp, const char *destp)
|
---|
59 | {
|
---|
60 | int sf, df;
|
---|
61 | ssize_t nr, nw;
|
---|
62 | int rc;
|
---|
63 | aoff64_t posr = 0, posw = 0;
|
---|
64 |
|
---|
65 | printf("Copy '%s' to '%s'.\n", srcp, destp);
|
---|
66 |
|
---|
67 | sf = open(srcp, O_RDONLY);
|
---|
68 | if (sf < 0)
|
---|
69 | return EIO;
|
---|
70 |
|
---|
71 | df = open(destp, O_CREAT | O_WRONLY, 0);
|
---|
72 | if (df < 0)
|
---|
73 | return EIO;
|
---|
74 |
|
---|
75 | do {
|
---|
76 | nr = read(sf, &posr, buf, BUF_SIZE);
|
---|
77 | if (nr == 0)
|
---|
78 | break;
|
---|
79 | if (nr < 0)
|
---|
80 | return EIO;
|
---|
81 |
|
---|
82 | nw = write(df, &posw, buf, nr);
|
---|
83 | if (nw <= 0)
|
---|
84 | return EIO;
|
---|
85 | } while (true);
|
---|
86 |
|
---|
87 | (void) close(sf);
|
---|
88 |
|
---|
89 | rc = close(df);
|
---|
90 | if (rc < 0)
|
---|
91 | return EIO;
|
---|
92 |
|
---|
93 | return EOK;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Copy contents of srcdir (recursively) into destdir.
|
---|
97 | *
|
---|
98 | * @param srcdir Source directory
|
---|
99 | * @param destdir Destination directory
|
---|
100 | *
|
---|
101 | * @return EOK on success, ENOMEM if out of memory, EIO on I/O error
|
---|
102 | */
|
---|
103 | int futil_rcopy_contents(const char *srcdir, const char *destdir)
|
---|
104 | {
|
---|
105 | DIR *dir;
|
---|
106 | struct dirent *de;
|
---|
107 | struct stat s;
|
---|
108 | char *srcp, *destp;
|
---|
109 | int rc;
|
---|
110 |
|
---|
111 | dir = opendir(srcdir);
|
---|
112 | if (dir == NULL)
|
---|
113 | return EIO;
|
---|
114 |
|
---|
115 | de = readdir(dir);
|
---|
116 | while (de != NULL) {
|
---|
117 | if (asprintf(&srcp, "%s/%s", srcdir, de->d_name) < 0)
|
---|
118 | return ENOMEM;
|
---|
119 | if (asprintf(&destp, "%s/%s", destdir, de->d_name) < 0)
|
---|
120 | return ENOMEM;
|
---|
121 |
|
---|
122 | rc = vfs_stat_path(srcp, &s);
|
---|
123 | if (rc != EOK)
|
---|
124 | return EIO;
|
---|
125 |
|
---|
126 | if (s.is_file) {
|
---|
127 | rc = futil_copy_file(srcp, destp);
|
---|
128 | if (rc != EOK)
|
---|
129 | return EIO;
|
---|
130 | } else if (s.is_directory) {
|
---|
131 | printf("Create directory '%s'\n", destp);
|
---|
132 | rc = mkdir(destp, 0);
|
---|
133 | if (rc != EOK)
|
---|
134 | return EIO;
|
---|
135 | rc = futil_rcopy_contents(srcp, destp);
|
---|
136 | if (rc != EOK)
|
---|
137 | return EIO;
|
---|
138 | } else {
|
---|
139 | return EIO;
|
---|
140 | }
|
---|
141 |
|
---|
142 | de = readdir(dir);
|
---|
143 | }
|
---|
144 |
|
---|
145 | return EOK;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Return file contents as a heap-allocated block of bytes.
|
---|
149 | *
|
---|
150 | * @param srcp File path
|
---|
151 | * @param rdata Place to store pointer to data
|
---|
152 | * @param rsize Place to store size of data
|
---|
153 | *
|
---|
154 | * @return EOK on success, ENOENT if failed to open file, EIO on other
|
---|
155 | * I/O error, ENOMEM if out of memory
|
---|
156 | */
|
---|
157 | int futil_get_file(const char *srcp, void **rdata, size_t *rsize)
|
---|
158 | {
|
---|
159 | int sf;
|
---|
160 | ssize_t nr;
|
---|
161 | size_t fsize;
|
---|
162 | char *data;
|
---|
163 | struct stat st;
|
---|
164 |
|
---|
165 | sf = open(srcp, O_RDONLY);
|
---|
166 | if (sf < 0)
|
---|
167 | return ENOENT;
|
---|
168 |
|
---|
169 | if (vfs_stat(sf, &st) != EOK) {
|
---|
170 | close(sf);
|
---|
171 | return EIO;
|
---|
172 | }
|
---|
173 |
|
---|
174 | fsize = st.size;
|
---|
175 |
|
---|
176 | data = calloc(fsize, 1);
|
---|
177 | if (data == NULL) {
|
---|
178 | close(sf);
|
---|
179 | return ENOMEM;
|
---|
180 | }
|
---|
181 |
|
---|
182 | nr = read(sf, (aoff64_t []) { 0 }, data, fsize);
|
---|
183 | if (nr != (ssize_t)fsize) {
|
---|
184 | close(sf);
|
---|
185 | free(data);
|
---|
186 | return EIO;
|
---|
187 | }
|
---|
188 |
|
---|
189 | (void) close(sf);
|
---|
190 | *rdata = data;
|
---|
191 | *rsize = fsize;
|
---|
192 |
|
---|
193 | return EOK;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** @}
|
---|
197 | */
|
---|