source: mainline/uspace/lib/fmgt/test/copy.c

Last change on this file was 2309891, checked in by Jiri Svoboda <jiri@…>, 3 days ago

Copy files (Navigator and command line).

TODO Overwrite query, new I/O error types.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * Copyright (c) 2025 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#include <errno.h>
30#include <fmgt.h>
31#include <pcut/pcut.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <str.h>
35#include <vfs/vfs.h>
36
37PCUT_INIT;
38
39PCUT_TEST_SUITE(copy);
40
41/** Copy file to file. */
42PCUT_TEST(copy_file_file)
43{
44 fmgt_t *fmgt = NULL;
45 char buf[L_tmpnam];
46 char *sname;
47 char *dname;
48 FILE *f;
49 char *p;
50 int rv;
51 fmgt_flist_t *flist;
52 errno_t rc;
53
54 /* Create name for temporary directory */
55 p = tmpnam(buf);
56 PCUT_ASSERT_NOT_NULL(p);
57
58 /* Create temporary directory */
59 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
60 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
61
62 rv = asprintf(&sname, "%s/%s", p, "a");
63 PCUT_ASSERT_TRUE(rv >= 0);
64
65 rv = asprintf(&dname, "%s/%s", p, "b");
66 PCUT_ASSERT_TRUE(rv >= 0);
67
68 f = fopen(sname, "wb");
69 PCUT_ASSERT_NOT_NULL(f);
70
71 rv = fprintf(f, "X");
72 PCUT_ASSERT_TRUE(rv >= 0);
73
74 rv = fclose(f);
75 PCUT_ASSERT_INT_EQUALS(0, rv);
76
77 rc = fmgt_create(&fmgt);
78 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
79
80 rc = fmgt_flist_create(&flist);
81 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
82
83 rc = fmgt_flist_append(flist, sname);
84 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
85
86 rc = fmgt_copy(fmgt, flist, dname);
87 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
88
89 fmgt_flist_destroy(flist);
90
91 rv = remove(sname);
92 PCUT_ASSERT_INT_EQUALS(0, rv);
93
94 rv = remove(dname);
95 PCUT_ASSERT_INT_EQUALS(0, rv);
96
97 rv = remove(p);
98 PCUT_ASSERT_INT_EQUALS(0, rv);
99
100 free(sname);
101 free(dname);
102 fmgt_destroy(fmgt);
103}
104
105/** Copy directory to directory. */
106PCUT_TEST(copy_dir_dir)
107{
108 fmgt_t *fmgt = NULL;
109 char buf[L_tmpnam];
110 char *sname;
111 char *dname;
112 char *p;
113 int rv;
114 fmgt_flist_t *flist;
115 errno_t rc;
116
117 /* Create name for temporary directory */
118 p = tmpnam(buf);
119 PCUT_ASSERT_NOT_NULL(p);
120
121 /* Create temporary directory */
122 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
123 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
124
125 rv = asprintf(&sname, "%s/%s", p, "a");
126 PCUT_ASSERT_TRUE(rv >= 0);
127
128 rv = asprintf(&dname, "%s/%s", p, "b");
129 PCUT_ASSERT_TRUE(rv >= 0);
130
131 rc = vfs_link_path(sname, KIND_DIRECTORY, NULL);
132 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
133
134 rc = fmgt_create(&fmgt);
135 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
136
137 rc = fmgt_flist_create(&flist);
138 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
139
140 rc = fmgt_flist_append(flist, sname);
141 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
142
143 rc = fmgt_copy(fmgt, flist, dname);
144 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
145
146 fmgt_flist_destroy(flist);
147
148 rv = remove(sname);
149 PCUT_ASSERT_INT_EQUALS(0, rv);
150
151 rv = remove(dname);
152 PCUT_ASSERT_INT_EQUALS(0, rv);
153
154 rv = remove(p);
155 PCUT_ASSERT_INT_EQUALS(0, rv);
156
157 free(sname);
158 free(dname);
159 fmgt_destroy(fmgt);
160}
161
162/** Copy files and directories into directory. */
163PCUT_TEST(copy_into_dir)
164{
165 fmgt_t *fmgt = NULL;
166 char buf[L_tmpnam];
167 char *fname;
168 char *dname;
169 FILE *f;
170 char *p;
171 int rv;
172 fmgt_flist_t *flist;
173 errno_t rc;
174
175 /* Create name for temporary directory */
176 p = tmpnam(buf);
177 PCUT_ASSERT_NOT_NULL(p);
178
179 /* Create temporary directory */
180 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
181 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
182
183 rv = asprintf(&fname, "%s/%s", p, "a");
184 PCUT_ASSERT_TRUE(rv >= 0);
185
186 rv = asprintf(&dname, "%s/%s", p, "b");
187 PCUT_ASSERT_TRUE(rv >= 0);
188
189 f = fopen(fname, "wb");
190 PCUT_ASSERT_NOT_NULL(f);
191
192 rv = fprintf(f, "X");
193 PCUT_ASSERT_TRUE(rv >= 0);
194
195 rv = fclose(f);
196 PCUT_ASSERT_INT_EQUALS(0, rv);
197
198 rc = vfs_link_path(dname, KIND_DIRECTORY, NULL);
199 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
200
201 rc = fmgt_create(&fmgt);
202 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
203
204 rc = fmgt_flist_create(&flist);
205 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
206
207 rc = fmgt_flist_append(flist, fname);
208 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
209
210 rc = fmgt_flist_append(flist, dname);
211 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
212
213 rc = fmgt_copy(fmgt, flist, dname);
214 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
215
216 fmgt_flist_destroy(flist);
217
218 rv = remove(fname);
219 PCUT_ASSERT_INT_EQUALS(0, rv);
220
221 rv = remove(dname);
222 PCUT_ASSERT_INT_EQUALS(0, rv);
223
224 rv = remove(p);
225 PCUT_ASSERT_INT_EQUALS(0, rv);
226
227 free(fname);
228 free(dname);
229 fmgt_destroy(fmgt);
230}
231
232PCUT_EXPORT(copy);
Note: See TracBrowser for help on using the repository browser.