Changes in uspace/lib/futil/src/futil.c [04e520e:5df2570] in mainline
- File:
-
- 1 edited
-
uspace/lib/futil/src/futil.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/futil/src/futil.c
r04e520e r5df2570 1 1 /* 2 * Copyright (c) 202 4Jiri Svoboda2 * Copyright (c) 2026 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 47 47 static char buf[BUF_SIZE]; 48 48 49 /** Create file utility instance. 50 * 51 * @param cb Callback functions 52 * @param arg Argument to callback functions 53 * @param rfutil Place to store pointer to new file utility instance 54 * @return EOK on succcess, ENOMEM if out of memory. 55 */ 56 errno_t futil_create(futil_cb_t *cb, void *arg, futil_t **rfutil) 57 { 58 futil_t *futil; 59 60 futil = calloc(1, sizeof(futil_t)); 61 if (futil == NULL) 62 return ENOMEM; 63 64 futil->cb = cb; 65 futil->cb_arg = arg; 66 *rfutil = futil; 67 return EOK; 68 } 69 70 /** Destroy file utility instance. 71 * 72 * @param futil File utility instance 73 */ 74 void futil_destroy(futil_t *futil) 75 { 76 free(futil); 77 } 78 49 79 /** Copy file. 50 80 * 81 * @param futil File utility instance 51 82 * @param srcp Source path 52 83 * @param dstp Destination path … … 54 85 * @return EOK on success, EIO on I/O error 55 86 */ 56 errno_t futil_copy_file( const char *srcp, const char *destp)87 errno_t futil_copy_file(futil_t *futil, const char *srcp, const char *destp) 57 88 { 58 89 int sf, df; … … 61 92 aoff64_t posr = 0, posw = 0; 62 93 63 printf("Copy '%s' to '%s'.\n", srcp, destp); 94 if (futil->cb != NULL && futil->cb->copy_file != NULL) 95 futil->cb->copy_file(futil->cb_arg, srcp, destp); 64 96 65 97 rc = vfs_lookup_open(srcp, WALK_REGULAR, MODE_READ, &sf); … … 67 99 return EIO; 68 100 69 rc = vfs_lookup_open(destp, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE, &df); 70 if (rc != EOK) 71 return EIO; 101 rc = vfs_lookup_open(destp, WALK_REGULAR | WALK_MAY_CREATE, MODE_WRITE, 102 &df); 103 if (rc != EOK) { 104 vfs_put(sf); 105 return EIO; 106 } 72 107 73 108 do { … … 99 134 /** Copy contents of srcdir (recursively) into destdir. 100 135 * 136 * @param futil File utility instance 101 137 * @param srcdir Source directory 102 138 * @param destdir Destination directory … … 104 140 * @return EOK on success, ENOMEM if out of memory, EIO on I/O error 105 141 */ 106 errno_t futil_rcopy_contents(const char *srcdir, const char *destdir) 142 errno_t futil_rcopy_contents(futil_t *futil, const char *srcdir, 143 const char *destdir) 107 144 { 108 145 DIR *dir; … … 118 155 de = readdir(dir); 119 156 while (de != NULL) { 120 if (asprintf(&srcp, "%s/%s", srcdir, de->d_name) < 0) 121 return ENOMEM; 122 if (asprintf(&destp, "%s/%s", destdir, de->d_name) < 0) 123 return ENOMEM; 157 if (asprintf(&srcp, "%s/%s", srcdir, de->d_name) < 0) { 158 rc = ENOMEM; 159 goto error; 160 } 161 162 if (asprintf(&destp, "%s/%s", destdir, de->d_name) < 0) { 163 rc = ENOMEM; 164 goto error; 165 } 124 166 125 167 rc = vfs_stat_path(srcp, &s); 126 168 if (rc != EOK) 127 return EIO;169 goto error; 128 170 129 171 if (s.is_file) { 130 rc = futil_copy_file(srcp, destp); 172 rc = futil_copy_file(futil, srcp, destp); 173 if (rc != EOK) { 174 rc = EIO; 175 goto error; 176 } 177 } else if (s.is_directory) { 178 if (futil->cb != NULL && futil->cb->create_dir != NULL) 179 futil->cb->create_dir(futil->cb_arg, destp); 180 rc = vfs_link_path(destp, KIND_DIRECTORY, NULL); 181 if (rc != EOK && rc != EEXIST) 182 goto error; 183 rc = futil_rcopy_contents(futil, srcp, destp); 131 184 if (rc != EOK) 132 return EIO; 133 } else if (s.is_directory) { 134 printf("Create directory '%s'\n", destp); 135 rc = vfs_link_path(destp, KIND_DIRECTORY, NULL); 136 if (rc != EOK) 137 return EIO; 138 rc = futil_rcopy_contents(srcp, destp); 139 if (rc != EOK) 140 return EIO; 185 goto error; 141 186 } else { 142 return EIO; 187 rc = EIO; 188 goto error; 143 189 } 144 190 … … 146 192 } 147 193 194 closedir(dir); 148 195 return EOK; 149 } 150 151 /** Return file contents as a heap-allocated block of bytes. 152 * 153 * @param srcp File path 154 * @param rdata Place to store pointer to data 155 * @param rsize Place to store size of data 156 * 157 * @return EOK on success, ENOENT if failed to open file, EIO on other 158 * I/O error, ENOMEM if out of memory 159 */ 160 errno_t futil_get_file(const char *srcp, void **rdata, size_t *rsize) 161 { 162 int sf; 163 size_t nr; 164 errno_t rc; 165 size_t fsize; 166 char *data; 167 vfs_stat_t st; 168 169 rc = vfs_lookup_open(srcp, WALK_REGULAR, MODE_READ, &sf); 170 if (rc != EOK) 171 return ENOENT; 172 173 if (vfs_stat(sf, &st) != EOK) { 174 vfs_put(sf); 175 return EIO; 176 } 177 178 fsize = st.size; 179 180 data = calloc(fsize, 1); 181 if (data == NULL) { 182 vfs_put(sf); 183 return ENOMEM; 184 } 185 186 rc = vfs_read(sf, (aoff64_t []) { 0 }, data, fsize, &nr); 187 if (rc != EOK || nr != fsize) { 188 vfs_put(sf); 189 free(data); 190 return EIO; 191 } 192 193 (void) vfs_put(sf); 194 *rdata = data; 195 *rsize = fsize; 196 197 return EOK; 196 error: 197 closedir(dir); 198 return rc; 198 199 } 199 200
Note:
See TracChangeset
for help on using the changeset viewer.
