paul@181 | 1 | /* |
paul@181 | 2 | * Filesystem access functions. |
paul@181 | 3 | * |
paul@226 | 4 | * Copyright (C) 2019, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@181 | 5 | * |
paul@181 | 6 | * This program is free software; you can redistribute it and/or |
paul@181 | 7 | * modify it under the terms of the GNU General Public License as |
paul@181 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@181 | 9 | * the License, or (at your option) any later version. |
paul@181 | 10 | * |
paul@181 | 11 | * This program is distributed in the hope that it will be useful, |
paul@181 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@181 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@181 | 14 | * GNU General Public License for more details. |
paul@181 | 15 | * |
paul@181 | 16 | * You should have received a copy of the GNU General Public License |
paul@181 | 17 | * along with this program; if not, write to the Free Software |
paul@181 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@181 | 19 | * Boston, MA 02110-1301, USA |
paul@181 | 20 | */ |
paul@181 | 21 | |
paul@181 | 22 | #include <string.h> |
paul@181 | 23 | |
paul@181 | 24 | #include <ext2fs/ext2fs.h> |
paul@181 | 25 | |
paul@181 | 26 | #include "image.h" |
paul@181 | 27 | #include "path.h" |
paul@181 | 28 | |
paul@181 | 29 | |
paul@181 | 30 | |
paul@181 | 31 | /* Create an inode for a file. */ |
paul@181 | 32 | |
paul@181 | 33 | errcode_t image_create_file(ext2_filsys fs, ext2_ino_t ino_target, |
paul@181 | 34 | const char *basename, __u16 mode, |
paul@181 | 35 | __u16 uid, __u16 gid, ext2_ino_t *ino_file) |
paul@181 | 36 | { |
paul@181 | 37 | struct ext2_inode inode_file; |
paul@181 | 38 | errcode_t retval; |
paul@227 | 39 | int retry; |
paul@181 | 40 | |
paul@181 | 41 | /* Without an inode, create a new one. */ |
paul@181 | 42 | |
paul@181 | 43 | retval = ext2fs_new_inode(fs, ino_target, LINUX_S_IFREG | mode, 0, ino_file); |
paul@181 | 44 | if (retval) |
paul@181 | 45 | return retval; |
paul@181 | 46 | |
paul@181 | 47 | /* Connect the inode to its parent. */ |
paul@181 | 48 | |
paul@227 | 49 | for (retry = 0; retry <= 1; retry++) |
paul@181 | 50 | { |
paul@181 | 51 | retval = ext2fs_link(fs, ino_target, basename, *ino_file, |
paul@181 | 52 | EXT2_FT_REG_FILE); |
paul@181 | 53 | |
paul@181 | 54 | if (!retval) |
paul@181 | 55 | break; |
paul@227 | 56 | else if (retry) |
paul@227 | 57 | return retval; |
paul@181 | 58 | |
paul@181 | 59 | /* Expand the directory if necessary. */ |
paul@181 | 60 | |
paul@181 | 61 | if (retval == EXT2_ET_DIR_NO_SPACE) |
paul@181 | 62 | retval = ext2fs_expand_dir(fs, ino_target); |
paul@181 | 63 | |
paul@181 | 64 | if (retval) |
paul@181 | 65 | return retval; |
paul@181 | 66 | } |
paul@181 | 67 | |
paul@181 | 68 | /* Make sure that subsequent files employ different inodes. */ |
paul@181 | 69 | |
paul@181 | 70 | ext2fs_inode_alloc_stats2(fs, *ino_file, 1, 0); |
paul@181 | 71 | |
paul@181 | 72 | /* Populate the inode details. */ |
paul@181 | 73 | |
paul@181 | 74 | image_set_metadata(&inode_file, 1, LINUX_S_IFREG | mode, uid, gid); |
paul@225 | 75 | inode_file.i_links_count++; |
paul@181 | 76 | |
paul@181 | 77 | return ext2fs_write_new_inode(fs, *ino_file, &inode_file); |
paul@181 | 78 | } |
paul@181 | 79 | |
paul@181 | 80 | /* Set the mode, user and group metadata for a file. */ |
paul@181 | 81 | |
paul@181 | 82 | void image_set_metadata(struct ext2_inode *inode, int clean, __u16 mode, |
paul@181 | 83 | __u16 uid, __u16 gid) |
paul@181 | 84 | { |
paul@181 | 85 | if (clean) |
paul@181 | 86 | memset(inode, 0, sizeof(*inode)); |
paul@181 | 87 | |
paul@181 | 88 | inode->i_mode = mode; |
paul@181 | 89 | inode->i_uid = uid; |
paul@181 | 90 | inode->i_gid = gid; |
paul@181 | 91 | } |
paul@181 | 92 | |
paul@181 | 93 | /* Find an object in the given directory with the given name in the filesystem |
paul@181 | 94 | image, updating the name reference to refer to the next component. */ |
paul@181 | 95 | |
paul@181 | 96 | errcode_t image_find_next(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@181 | 97 | const char **basename, char *buf, ext2_ino_t *ino) |
paul@181 | 98 | { |
paul@181 | 99 | const char *end = path_component_end(*basename); |
paul@181 | 100 | errcode_t retval; |
paul@181 | 101 | |
paul@181 | 102 | /* Find the basename in the directory. */ |
paul@181 | 103 | |
paul@181 | 104 | retval = ext2fs_lookup(fs, ino_dir, *basename, end - *basename, buf, ino); |
paul@181 | 105 | |
paul@181 | 106 | /* Update the current component. */ |
paul@181 | 107 | |
paul@181 | 108 | if (!retval) |
paul@181 | 109 | *basename = path_component_next(end); |
paul@181 | 110 | |
paul@181 | 111 | return retval; |
paul@181 | 112 | } |
paul@181 | 113 | |
paul@181 | 114 | /* Find an object with the given pathname in the filesystem image. */ |
paul@181 | 115 | |
paul@181 | 116 | errcode_t image_find_path(ext2_filsys fs, const char **pathname, ext2_ino_t *ino) |
paul@181 | 117 | { |
paul@181 | 118 | char *buf; |
paul@181 | 119 | ext2_ino_t ino_dir; |
paul@181 | 120 | errcode_t retval; |
paul@181 | 121 | |
paul@181 | 122 | retval = ext2fs_get_mem(fs->blocksize, &buf); |
paul@181 | 123 | if (retval) |
paul@181 | 124 | return retval; |
paul@181 | 125 | |
paul@181 | 126 | /* Skip any leading root marker. */ |
paul@181 | 127 | |
paul@181 | 128 | if (**pathname == '/') |
paul@181 | 129 | (*pathname)++; |
paul@181 | 130 | |
paul@181 | 131 | if (!**pathname) |
paul@181 | 132 | *ino = EXT2_ROOT_INO; |
paul@181 | 133 | |
paul@181 | 134 | /* Start at the root. */ |
paul@181 | 135 | |
paul@181 | 136 | ino_dir = EXT2_ROOT_INO; |
paul@181 | 137 | |
paul@181 | 138 | /* With any remaining path, find the next component. */ |
paul@181 | 139 | |
paul@181 | 140 | while (**pathname) |
paul@181 | 141 | { |
paul@181 | 142 | retval = image_find_next(fs, ino_dir, pathname, buf, ino); |
paul@181 | 143 | if (retval) |
paul@181 | 144 | { |
paul@181 | 145 | *ino = ino_dir; |
paul@181 | 146 | break; |
paul@181 | 147 | } |
paul@181 | 148 | |
paul@181 | 149 | /* Move into the found object for searching the next component. */ |
paul@181 | 150 | |
paul@181 | 151 | ino_dir = *ino; |
paul@181 | 152 | } |
paul@181 | 153 | |
paul@181 | 154 | ext2fs_free_mem(&buf); |
paul@181 | 155 | |
paul@181 | 156 | return retval; |
paul@181 | 157 | } |
paul@181 | 158 | |
paul@181 | 159 | /* Find an object in the given directory with the given name in the filesystem |
paul@181 | 160 | image. */ |
paul@181 | 161 | |
paul@181 | 162 | errcode_t image_find_file(ext2_filsys fs, const char *dirname, |
paul@181 | 163 | const char *basename, ext2_ino_t *ino) |
paul@181 | 164 | { |
paul@181 | 165 | char pathname[strlen(dirname) + strlen(basename) + 2]; |
paul@181 | 166 | const char *s = pathname; |
paul@181 | 167 | |
paul@181 | 168 | strcpy(pathname, dirname); |
paul@181 | 169 | strcat(pathname, "/"); |
paul@181 | 170 | strcat(pathname, basename); |
paul@181 | 171 | |
paul@181 | 172 | return image_find_path(fs, &s, ino); |
paul@181 | 173 | } |
paul@181 | 174 | |
paul@181 | 175 | /* Obtain the inode for the object with the given pathname in the filesystem |
paul@181 | 176 | image. */ |
paul@181 | 177 | |
paul@181 | 178 | errcode_t image_inode(ext2_filsys fs, const char *pathname, |
paul@181 | 179 | struct ext2_inode *inode) |
paul@181 | 180 | { |
paul@181 | 181 | ext2_ino_t ino; |
paul@181 | 182 | errcode_t retval; |
paul@181 | 183 | |
paul@181 | 184 | retval = image_find_path(fs, &pathname, &ino); |
paul@181 | 185 | if (retval) |
paul@181 | 186 | return retval; |
paul@181 | 187 | |
paul@181 | 188 | return ext2fs_read_inode(fs, ino, inode); |
paul@181 | 189 | } |
paul@181 | 190 | |
paul@226 | 191 | /* List a directory in the filesystem image. */ |
paul@226 | 192 | |
paul@226 | 193 | errcode_t image_list_dir(ext2_filsys fs, const char *path, |
paul@226 | 194 | int (*proc)(struct ext2_dir_entry *, int, int, char *, |
paul@226 | 195 | void *), |
paul@226 | 196 | void *data) |
paul@226 | 197 | { |
paul@226 | 198 | char *buf; |
paul@226 | 199 | ext2_ino_t ino; |
paul@226 | 200 | errcode_t retval; |
paul@226 | 201 | |
paul@226 | 202 | retval = ext2fs_get_mem(fs->blocksize, &buf); |
paul@226 | 203 | if (retval) |
paul@226 | 204 | return retval; |
paul@226 | 205 | |
paul@226 | 206 | /* Locate the object and test whether it is a directory. */ |
paul@226 | 207 | |
paul@226 | 208 | retval = image_find_path(fs, &path, &ino); |
paul@226 | 209 | if (retval) |
paul@226 | 210 | { |
paul@226 | 211 | ext2fs_free_mem(&buf); |
paul@226 | 212 | return retval; |
paul@226 | 213 | } |
paul@226 | 214 | |
paul@226 | 215 | if (!_image_isdir(fs, ino)) |
paul@226 | 216 | return 1; |
paul@226 | 217 | |
paul@226 | 218 | /* List the directory contents. */ |
paul@226 | 219 | |
paul@226 | 220 | retval = ext2fs_dir_iterate(fs, ino, 0, buf, proc, data); |
paul@226 | 221 | if (retval) |
paul@226 | 222 | { |
paul@226 | 223 | ext2fs_free_mem(&buf); |
paul@226 | 224 | return retval; |
paul@226 | 225 | } |
paul@226 | 226 | |
paul@226 | 227 | ext2fs_free_mem(&buf); |
paul@226 | 228 | return 0; |
paul@226 | 229 | } |
paul@226 | 230 | |
paul@181 | 231 | /* Make a directory in the given directory in the filesystem image having the |
paul@181 | 232 | given name and metadata. */ |
paul@181 | 233 | |
paul@181 | 234 | errcode_t image_make_dir(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@181 | 235 | const char *basename, __u16 mode, |
paul@181 | 236 | __u16 uid, __u16 gid, ext2_ino_t *ino) |
paul@181 | 237 | { |
paul@181 | 238 | struct ext2_inode inode_dir; |
paul@181 | 239 | errcode_t retval = 0; |
paul@181 | 240 | |
paul@181 | 241 | /* Create an inode in the directory. */ |
paul@181 | 242 | |
paul@181 | 243 | retval = ext2fs_new_inode(fs, ino_dir, LINUX_S_IFDIR | mode, 0, ino); |
paul@181 | 244 | if (retval) |
paul@181 | 245 | return retval; |
paul@181 | 246 | |
paul@181 | 247 | /* Make the directory and update the metadata (due to ext2fs_mkdir |
paul@181 | 248 | limitation). */ |
paul@181 | 249 | |
paul@181 | 250 | retval = ext2fs_mkdir(fs, ino_dir, *ino, basename); |
paul@181 | 251 | if (retval) |
paul@181 | 252 | return retval; |
paul@181 | 253 | |
paul@181 | 254 | retval = ext2fs_read_inode(fs, *ino, &inode_dir); |
paul@181 | 255 | if (retval) |
paul@181 | 256 | return retval; |
paul@181 | 257 | |
paul@181 | 258 | image_set_metadata(&inode_dir, 0, LINUX_S_IFDIR | mode, uid, gid); |
paul@181 | 259 | return ext2fs_write_inode(fs, *ino, &inode_dir); |
paul@181 | 260 | } |
paul@181 | 261 | |
paul@181 | 262 | /* Make a directory in the given directory in the filesystem image, updating |
paul@181 | 263 | the name reference to refer to the next component. */ |
paul@181 | 264 | |
paul@181 | 265 | errcode_t image_make_next_dir(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@181 | 266 | const char **basename, __u16 mode, __u16 uid, |
paul@181 | 267 | __u16 gid, ext2_ino_t *ino) |
paul@181 | 268 | { |
paul@181 | 269 | char *end = (char *) path_component_end(*basename); |
paul@181 | 270 | char endchar = *end; |
paul@181 | 271 | errcode_t retval = 0; |
paul@181 | 272 | |
paul@181 | 273 | /* Delimit the basename and make a directory using the inode. */ |
paul@181 | 274 | |
paul@181 | 275 | if (endchar) |
paul@181 | 276 | *end = '\0'; |
paul@181 | 277 | |
paul@181 | 278 | /* Do not create directories for empty components. */ |
paul@181 | 279 | |
paul@181 | 280 | if (**basename) |
paul@181 | 281 | retval = image_make_dir(fs, ino_dir, *basename, mode, uid, gid, ino); |
paul@181 | 282 | |
paul@181 | 283 | /* Restore the path separator and update the current component. */ |
paul@181 | 284 | |
paul@181 | 285 | if (endchar) |
paul@181 | 286 | *end = '/'; |
paul@181 | 287 | |
paul@181 | 288 | if (!retval) |
paul@181 | 289 | *basename = path_component_next(end); |
paul@181 | 290 | |
paul@181 | 291 | return retval; |
paul@181 | 292 | } |
paul@181 | 293 | |
paul@181 | 294 | /* Make directories descending to the given path in the filesystem image. */ |
paul@181 | 295 | |
paul@181 | 296 | errcode_t image_make_dirs(ext2_filsys fs, const char **pathname, |
paul@181 | 297 | ext2_ino_t ino_dir, __u16 mode, __u16 uid, __u16 gid) |
paul@181 | 298 | { |
paul@181 | 299 | ext2_ino_t ino; |
paul@181 | 300 | errcode_t retval; |
paul@181 | 301 | |
paul@181 | 302 | while (**pathname) |
paul@181 | 303 | { |
paul@181 | 304 | retval = image_make_next_dir(fs, ino_dir, pathname, mode, uid, gid, &ino); |
paul@181 | 305 | if (retval) |
paul@181 | 306 | return retval; |
paul@181 | 307 | |
paul@181 | 308 | /* Move into the created object for handling the next component. */ |
paul@181 | 309 | |
paul@181 | 310 | ino_dir = ino; |
paul@181 | 311 | } |
paul@181 | 312 | |
paul@181 | 313 | return 0; |
paul@181 | 314 | } |
paul@181 | 315 | |
paul@181 | 316 | /* Copy file metadata into a stat structure. */ |
paul@181 | 317 | |
paul@181 | 318 | errcode_t image_stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *st) |
paul@181 | 319 | { |
paul@181 | 320 | struct ext2_inode inode; |
paul@181 | 321 | errcode_t retval = ext2fs_read_inode(fs, ino, &inode); |
paul@181 | 322 | |
paul@181 | 323 | if (retval) |
paul@181 | 324 | return retval; |
paul@181 | 325 | |
paul@181 | 326 | st->st_dev = 0; /* device identifier */ |
paul@181 | 327 | st->st_ino = ino; |
paul@181 | 328 | st->st_mode = inode.i_mode; |
paul@181 | 329 | st->st_nlink = inode.i_links_count; |
paul@181 | 330 | st->st_uid = inode_uid(inode); |
paul@181 | 331 | st->st_gid = inode_gid(inode); |
paul@181 | 332 | st->st_rdev = 0; /* special file device identifier */ |
paul@181 | 333 | st->st_size = EXT2_I_SIZE(&inode); |
paul@181 | 334 | st->st_blksize = fs->blocksize; |
paul@181 | 335 | st->st_blocks = 0; /* number of 512 byte blocks allocated */ |
paul@181 | 336 | st->st_atim.tv_sec = inode.i_atime; |
paul@181 | 337 | st->st_atim.tv_nsec = 0; /* nanosecond resolution */ |
paul@181 | 338 | st->st_mtim.tv_sec = inode.i_mtime; |
paul@181 | 339 | st->st_mtim.tv_nsec = 0; |
paul@181 | 340 | st->st_ctim.tv_sec = inode.i_ctime; |
paul@181 | 341 | st->st_ctim.tv_nsec = 0; |
paul@181 | 342 | |
paul@181 | 343 | return 0; |
paul@181 | 344 | } |
paul@181 | 345 | |
paul@231 | 346 | /* Remove an inode. */ |
paul@231 | 347 | |
paul@231 | 348 | errcode_t image_remove_by_inode(ext2_filsys fs, ext2_ino_t ino) |
paul@231 | 349 | { |
paul@231 | 350 | struct ext2_inode_large inode; |
paul@231 | 351 | errcode_t err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *) &inode, |
paul@231 | 352 | sizeof(inode)); |
paul@231 | 353 | |
paul@231 | 354 | /* Handle invalid inodes, ignore unreferenced inodes. */ |
paul@231 | 355 | |
paul@231 | 356 | if (err) |
paul@231 | 357 | return err; |
paul@231 | 358 | |
paul@231 | 359 | if (!inode.i_links_count) |
paul@231 | 360 | return 0; |
paul@231 | 361 | |
paul@231 | 362 | /* Decrement the reference count. With no more references to the inode, |
paul@231 | 363 | remove its resources. */ |
paul@231 | 364 | |
paul@231 | 365 | inode.i_links_count--; |
paul@231 | 366 | |
paul@231 | 367 | if (!inode.i_links_count) |
paul@231 | 368 | { |
paul@231 | 369 | err = ext2fs_free_ext_attr(fs, ino, &inode); |
paul@231 | 370 | |
paul@231 | 371 | if (!err) |
paul@231 | 372 | { |
paul@231 | 373 | /* Deallocate blocks, if appropriate. ~0ULL as the end represents |
paul@231 | 374 | truncation. */ |
paul@231 | 375 | |
paul@231 | 376 | if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *) &inode)) |
paul@231 | 377 | { |
paul@231 | 378 | err = ext2fs_punch(fs, ino, (struct ext2_inode *) &inode, NULL, |
paul@231 | 379 | 0, ~0ULL); |
paul@231 | 380 | |
paul@231 | 381 | /* Update allocation statistics. */ |
paul@231 | 382 | |
paul@231 | 383 | if (!err) |
paul@231 | 384 | ext2fs_inode_alloc_stats2(fs, ino, -1, |
paul@231 | 385 | LINUX_S_ISDIR(inode.i_mode)); |
paul@231 | 386 | } |
paul@231 | 387 | } |
paul@231 | 388 | } |
paul@231 | 389 | |
paul@231 | 390 | return ext2fs_write_inode_full(fs, ino, (struct ext2_inode *) &inode, |
paul@231 | 391 | sizeof(inode)); |
paul@231 | 392 | } |
paul@231 | 393 | |
paul@231 | 394 | /* Unlink a directory entry by name. */ |
paul@231 | 395 | |
paul@231 | 396 | errcode_t image_unlink_by_name(ext2_filsys fs, ext2_ino_t ino_parent, |
paul@231 | 397 | const char *basename) |
paul@231 | 398 | { |
paul@231 | 399 | return ext2fs_unlink(fs, ino_parent, basename, 0, 0); |
paul@231 | 400 | } |
paul@231 | 401 | |
paul@231 | 402 | /* Unlink a directory entry by inode number. */ |
paul@231 | 403 | |
paul@231 | 404 | errcode_t image_unlink_by_inode(ext2_filsys fs, ext2_ino_t ino_parent, |
paul@231 | 405 | ext2_ino_t ino) |
paul@231 | 406 | { |
paul@231 | 407 | return ext2fs_unlink(fs, ino_parent, 0, ino, 0); |
paul@231 | 408 | } |
paul@231 | 409 | |
paul@181 | 410 | /* Test object types in the filesystem image. */ |
paul@181 | 411 | |
paul@181 | 412 | int _image_isdir(ext2_filsys fs, ext2_ino_t ino) |
paul@181 | 413 | { |
paul@181 | 414 | struct ext2_inode inode; |
paul@181 | 415 | |
paul@181 | 416 | if (ext2fs_read_inode(fs, ino, &inode)) |
paul@181 | 417 | return 0; |
paul@181 | 418 | |
paul@181 | 419 | return LINUX_S_ISDIR(inode.i_mode); |
paul@181 | 420 | } |
paul@181 | 421 | |
paul@181 | 422 | int image_isdir(ext2_filsys fs, const char *name) |
paul@181 | 423 | { |
paul@181 | 424 | ext2_ino_t ino; |
paul@181 | 425 | |
paul@181 | 426 | if (image_find_path(fs, &name, &ino)) |
paul@181 | 427 | return 0; |
paul@181 | 428 | |
paul@181 | 429 | return _image_isdir(fs, ino); |
paul@181 | 430 | } |
paul@181 | 431 | |
paul@181 | 432 | int _image_isfile(ext2_filsys fs, ext2_ino_t ino) |
paul@181 | 433 | { |
paul@181 | 434 | struct ext2_inode inode; |
paul@181 | 435 | |
paul@181 | 436 | if (ext2fs_read_inode(fs, ino, &inode)) |
paul@181 | 437 | return 0; |
paul@181 | 438 | |
paul@181 | 439 | return LINUX_S_ISREG(inode.i_mode); |
paul@181 | 440 | } |
paul@181 | 441 | |
paul@181 | 442 | int image_isfile(ext2_filsys fs, const char *name) |
paul@181 | 443 | { |
paul@181 | 444 | ext2_ino_t ino; |
paul@181 | 445 | |
paul@181 | 446 | if (image_find_path(fs, &name, &ino)) |
paul@181 | 447 | return 0; |
paul@181 | 448 | |
paul@181 | 449 | return _image_isfile(fs, ino); |
paul@181 | 450 | } |