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