paul@181 | 1 | /* |
paul@181 | 2 | * Filesystem access functions. |
paul@181 | 3 | * |
paul@235 | 4 | * Copyright (C) 2019, 2021, 2022 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@236 | 31 | /* Link an inode within the given target directory having the given basename. */ |
paul@181 | 32 | |
paul@236 | 33 | static errcode_t _image_link(ext2_filsys fs, ext2_ino_t ino_target, |
paul@236 | 34 | const char *basename, ext2_ino_t ino_file, |
paul@236 | 35 | int flags) |
paul@181 | 36 | { |
paul@181 | 37 | errcode_t retval; |
paul@227 | 38 | int retry; |
paul@181 | 39 | |
paul@181 | 40 | /* Connect the inode to its parent. */ |
paul@181 | 41 | |
paul@227 | 42 | for (retry = 0; retry <= 1; retry++) |
paul@181 | 43 | { |
paul@236 | 44 | retval = ext2fs_link(fs, ino_target, basename, ino_file, flags); |
paul@181 | 45 | |
paul@181 | 46 | if (!retval) |
paul@181 | 47 | break; |
paul@227 | 48 | else if (retry) |
paul@227 | 49 | return retval; |
paul@181 | 50 | |
paul@181 | 51 | /* Expand the directory if necessary. */ |
paul@181 | 52 | |
paul@181 | 53 | if (retval == EXT2_ET_DIR_NO_SPACE) |
paul@181 | 54 | retval = ext2fs_expand_dir(fs, ino_target); |
paul@181 | 55 | |
paul@181 | 56 | if (retval) |
paul@181 | 57 | return retval; |
paul@181 | 58 | } |
paul@181 | 59 | |
paul@236 | 60 | return 0; |
paul@236 | 61 | } |
paul@236 | 62 | |
paul@248 | 63 | /* Get entry details for a directory to be removed. */ |
paul@248 | 64 | |
paul@248 | 65 | static int _image_get_parent_entry(struct ext2_dir_entry *dir_entry, |
paul@248 | 66 | int offset, int blocksize, char *buf, |
paul@248 | 67 | void *priv_data) |
paul@248 | 68 | { |
paul@251 | 69 | ext2_ino_t *ino = (ext2_ino_t *) priv_data; |
paul@248 | 70 | |
paul@248 | 71 | (void) offset; (void) blocksize; (void) buf; |
paul@248 | 72 | |
paul@248 | 73 | if (!strcmp(dir_entry->name, "..")) |
paul@251 | 74 | { |
paul@251 | 75 | *ino = dir_entry->inode; |
paul@251 | 76 | return DIRENT_ABORT; |
paul@251 | 77 | } |
paul@248 | 78 | |
paul@248 | 79 | return 0; |
paul@248 | 80 | } |
paul@248 | 81 | |
paul@248 | 82 | /* Test for objects in a directory. */ |
paul@248 | 83 | |
paul@248 | 84 | static int _image_test_directory(struct ext2_dir_entry *dir_entry, |
paul@248 | 85 | int offset, int blocksize, char *buf, |
paul@248 | 86 | void *priv_data) |
paul@248 | 87 | { |
paul@251 | 88 | int *nonempty = (int *) priv_data; |
paul@248 | 89 | |
paul@248 | 90 | (void) offset; (void) blocksize; (void) buf; |
paul@248 | 91 | |
paul@248 | 92 | if (strcmp(dir_entry->name, ".") && strcmp(dir_entry->name, "..")) |
paul@248 | 93 | { |
paul@251 | 94 | *nonempty = 1; |
paul@248 | 95 | return DIRENT_ABORT; |
paul@248 | 96 | } |
paul@248 | 97 | |
paul@248 | 98 | return 0; |
paul@248 | 99 | } |
paul@248 | 100 | |
paul@248 | 101 | /* Update the parent entry of a renamed directory. */ |
paul@248 | 102 | |
paul@236 | 103 | static int _image_update_parent_entry(struct ext2_dir_entry *dir_entry, |
paul@236 | 104 | int offset, int blocksize, char *buf, |
paul@236 | 105 | void *priv_data) |
paul@236 | 106 | { |
paul@251 | 107 | ext2_ino_t *ino = (ext2_ino_t *) priv_data; |
paul@236 | 108 | |
paul@236 | 109 | (void) offset; (void) blocksize; (void) buf; |
paul@236 | 110 | |
paul@248 | 111 | if (!strcmp(dir_entry->name, "..")) |
paul@236 | 112 | { |
paul@251 | 113 | dir_entry->inode = *ino; |
paul@236 | 114 | return DIRENT_CHANGED | DIRENT_ABORT; |
paul@236 | 115 | } |
paul@236 | 116 | else |
paul@236 | 117 | return 0; |
paul@236 | 118 | } |
paul@236 | 119 | |
paul@243 | 120 | |
paul@243 | 121 | |
paul@249 | 122 | /* Adapter function. */ |
paul@249 | 123 | |
paul@249 | 124 | errcode_t image_access_by_path(ext2_filsys fs, const char *path, |
paul@249 | 125 | errcode_t (*op)(ext2_filsys, ext2_ino_t), |
paul@249 | 126 | ext2_ino_t *ino) |
paul@249 | 127 | { |
paul@257 | 128 | errcode_t retval = image_find_by_path(fs, path, ino); |
paul@249 | 129 | |
paul@249 | 130 | if (retval) |
paul@249 | 131 | return retval; |
paul@249 | 132 | |
paul@249 | 133 | return op(fs, *ino); |
paul@249 | 134 | } |
paul@249 | 135 | |
paul@249 | 136 | |
paul@249 | 137 | |
paul@236 | 138 | /* Create an inode for a file. */ |
paul@236 | 139 | |
paul@236 | 140 | errcode_t image_create_file(ext2_filsys fs, ext2_ino_t ino_target, |
paul@236 | 141 | const char *basename, __u16 mode, |
paul@236 | 142 | __u16 uid, __u16 gid, ext2_ino_t *ino_file) |
paul@236 | 143 | { |
paul@236 | 144 | struct ext2_inode inode_file; |
paul@236 | 145 | errcode_t retval; |
paul@236 | 146 | |
paul@236 | 147 | /* Without an inode, create a new one. */ |
paul@236 | 148 | |
paul@236 | 149 | retval = ext2fs_new_inode(fs, ino_target, LINUX_S_IFREG | mode, 0, ino_file); |
paul@236 | 150 | if (retval) |
paul@236 | 151 | return retval; |
paul@236 | 152 | |
paul@236 | 153 | _image_link(fs, ino_target, basename, *ino_file, EXT2_FT_REG_FILE); |
paul@236 | 154 | |
paul@181 | 155 | /* Make sure that subsequent files employ different inodes. */ |
paul@181 | 156 | |
paul@181 | 157 | ext2fs_inode_alloc_stats2(fs, *ino_file, 1, 0); |
paul@181 | 158 | |
paul@181 | 159 | /* Populate the inode details. */ |
paul@181 | 160 | |
paul@181 | 161 | image_set_metadata(&inode_file, 1, LINUX_S_IFREG | mode, uid, gid); |
paul@225 | 162 | inode_file.i_links_count++; |
paul@181 | 163 | |
paul@181 | 164 | return ext2fs_write_new_inode(fs, *ino_file, &inode_file); |
paul@181 | 165 | } |
paul@181 | 166 | |
paul@249 | 167 | /* Test for an empty directory. */ |
paul@249 | 168 | |
paul@249 | 169 | errcode_t image_dir_empty_by_inode(ext2_filsys fs, ext2_ino_t ino) |
paul@249 | 170 | { |
paul@249 | 171 | /* Initialise the directory listing processing state. */ |
paul@249 | 172 | |
paul@251 | 173 | int nonempty = 0; |
paul@249 | 174 | errcode_t retval = ext2fs_dir_iterate(fs, ino, 0, NULL, |
paul@251 | 175 | _image_test_directory, &nonempty); |
paul@249 | 176 | |
paul@249 | 177 | if (retval) |
paul@249 | 178 | return retval; |
paul@249 | 179 | |
paul@249 | 180 | /* NOTE: Need a proper error here. */ |
paul@249 | 181 | |
paul@251 | 182 | return nonempty; |
paul@249 | 183 | } |
paul@249 | 184 | |
paul@249 | 185 | /* Test for an empty directory using its path. */ |
paul@249 | 186 | |
paul@249 | 187 | errcode_t image_dir_empty_by_path(ext2_filsys fs, const char *path, |
paul@249 | 188 | ext2_ino_t *ino) |
paul@249 | 189 | { |
paul@249 | 190 | return image_access_by_path(fs, path, image_dir_empty_by_inode, ino); |
paul@249 | 191 | } |
paul@249 | 192 | |
paul@249 | 193 | /* Find any parent reference. */ |
paul@249 | 194 | |
paul@249 | 195 | errcode_t image_dir_get_parent(ext2_filsys fs, ext2_ino_t ino, ext2_ino_t *ino_parent) |
paul@249 | 196 | { |
paul@249 | 197 | /* Initialise the directory listing processing state. */ |
paul@249 | 198 | |
paul@251 | 199 | *ino_parent = 0; |
paul@249 | 200 | |
paul@251 | 201 | /* A directory needs to be inspected for the .. entry in order to obtain the |
paul@251 | 202 | parent directory. */ |
paul@249 | 203 | |
paul@251 | 204 | return ext2fs_dir_iterate(fs, ino, 0, NULL, _image_get_parent_entry, |
paul@251 | 205 | ino_parent); |
paul@249 | 206 | } |
paul@249 | 207 | |
paul@236 | 208 | /* Return the appropriate ext2 file type value for the given mode value. */ |
paul@236 | 209 | |
paul@236 | 210 | int image_file_type(int mode) |
paul@236 | 211 | { |
paul@236 | 212 | switch (mode & LINUX_S_IFMT) |
paul@236 | 213 | { |
paul@236 | 214 | case LINUX_S_IFSOCK: return EXT2_FT_SOCK; |
paul@236 | 215 | case LINUX_S_IFLNK: return EXT2_FT_SYMLINK; |
paul@236 | 216 | case LINUX_S_IFREG: return EXT2_FT_REG_FILE; |
paul@236 | 217 | case LINUX_S_IFBLK: return EXT2_FT_BLKDEV; |
paul@236 | 218 | case LINUX_S_IFDIR: return EXT2_FT_DIR; |
paul@236 | 219 | case LINUX_S_IFCHR: return EXT2_FT_CHRDEV; |
paul@236 | 220 | case LINUX_S_IFIFO: return EXT2_FT_FIFO; |
paul@236 | 221 | |
paul@236 | 222 | /* NOTE: Perhaps signal an error. */ |
paul@236 | 223 | |
paul@236 | 224 | default: return EXT2_FT_REG_FILE; |
paul@236 | 225 | } |
paul@236 | 226 | } |
paul@236 | 227 | |
paul@272 | 228 | /* Find an object with the given path. */ |
paul@181 | 229 | |
paul@257 | 230 | errcode_t image_find_by_path(ext2_filsys fs, const char *path, ext2_ino_t *ino) |
paul@257 | 231 | { |
paul@257 | 232 | return image_resolve_by_path(fs, &path, ino); |
paul@181 | 233 | } |
paul@181 | 234 | |
paul@272 | 235 | /* Find an object with the given path. */ |
paul@181 | 236 | |
paul@257 | 237 | errcode_t image_resolve_by_path(ext2_filsys fs, const char **path, |
paul@257 | 238 | ext2_ino_t *ino) |
paul@181 | 239 | { |
paul@181 | 240 | ext2_ino_t ino_dir; |
paul@445 | 241 | errcode_t retval = 0; |
paul@181 | 242 | |
paul@272 | 243 | /* Initialise the inode in case of early failure. */ |
paul@272 | 244 | |
paul@272 | 245 | *ino = 0; |
paul@272 | 246 | |
paul@181 | 247 | /* Skip any leading root marker. */ |
paul@181 | 248 | |
paul@257 | 249 | if (**path == '/') |
paul@257 | 250 | (*path)++; |
paul@181 | 251 | |
paul@257 | 252 | if (!**path) |
paul@181 | 253 | *ino = EXT2_ROOT_INO; |
paul@181 | 254 | |
paul@181 | 255 | /* Start at the root. */ |
paul@181 | 256 | |
paul@181 | 257 | ino_dir = EXT2_ROOT_INO; |
paul@181 | 258 | |
paul@181 | 259 | /* With any remaining path, find the next component. */ |
paul@181 | 260 | |
paul@257 | 261 | while (**path) |
paul@181 | 262 | { |
paul@444 | 263 | retval = image_resolve_next(fs, ino_dir, path, 0, ino); |
paul@181 | 264 | if (retval) |
paul@181 | 265 | { |
paul@181 | 266 | *ino = ino_dir; |
paul@181 | 267 | break; |
paul@181 | 268 | } |
paul@181 | 269 | |
paul@181 | 270 | /* Move into the found object for searching the next component. */ |
paul@181 | 271 | |
paul@181 | 272 | ino_dir = *ino; |
paul@181 | 273 | } |
paul@181 | 274 | |
paul@181 | 275 | return retval; |
paul@181 | 276 | } |
paul@181 | 277 | |
paul@272 | 278 | /* Find an object in the given directory with the given name, updating the name |
paul@272 | 279 | reference to refer to the next component. */ |
paul@257 | 280 | |
paul@257 | 281 | errcode_t image_resolve_next(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@257 | 282 | const char **basename, char *buf, ext2_ino_t *ino) |
paul@257 | 283 | { |
paul@257 | 284 | const char *end = path_component_end(*basename); |
paul@257 | 285 | errcode_t retval; |
paul@257 | 286 | |
paul@257 | 287 | /* Find the basename in the directory. */ |
paul@257 | 288 | |
paul@257 | 289 | retval = ext2fs_lookup(fs, ino_dir, *basename, end - *basename, buf, ino); |
paul@257 | 290 | |
paul@257 | 291 | /* Update the current component. */ |
paul@257 | 292 | |
paul@257 | 293 | if (!retval) |
paul@257 | 294 | *basename = path_component_next(end); |
paul@257 | 295 | |
paul@257 | 296 | return retval; |
paul@257 | 297 | } |
paul@257 | 298 | |
paul@272 | 299 | /* Find an object in the given directory with the given name. */ |
paul@181 | 300 | |
paul@181 | 301 | errcode_t image_find_file(ext2_filsys fs, const char *dirname, |
paul@181 | 302 | const char *basename, ext2_ino_t *ino) |
paul@181 | 303 | { |
paul@257 | 304 | char path[strlen(dirname) + strlen(basename) + 2]; |
paul@181 | 305 | |
paul@257 | 306 | strcpy(path, dirname); |
paul@257 | 307 | strcat(path, "/"); |
paul@257 | 308 | strcat(path, basename); |
paul@181 | 309 | |
paul@257 | 310 | return image_find_by_path(fs, path, ino); |
paul@181 | 311 | } |
paul@181 | 312 | |
paul@274 | 313 | /* Find an object in the given directory with the given inode. */ |
paul@274 | 314 | |
paul@274 | 315 | errcode_t image_find_file_by_inode(ext2_filsys fs, ext2_ino_t ino_parent, |
paul@274 | 316 | const char *basename, ext2_ino_t *ino) |
paul@274 | 317 | { |
paul@444 | 318 | return ext2fs_lookup(fs, ino_parent, basename, strlen(basename), 0, ino); |
paul@274 | 319 | } |
paul@274 | 320 | |
paul@272 | 321 | /* Obtain the inode for the object with the given path. */ |
paul@181 | 322 | |
paul@257 | 323 | errcode_t image_inode(ext2_filsys fs, const char *path, |
paul@181 | 324 | struct ext2_inode *inode) |
paul@181 | 325 | { |
paul@181 | 326 | ext2_ino_t ino; |
paul@181 | 327 | errcode_t retval; |
paul@181 | 328 | |
paul@257 | 329 | retval = image_find_by_path(fs, path, &ino); |
paul@257 | 330 | |
paul@181 | 331 | if (retval) |
paul@181 | 332 | return retval; |
paul@181 | 333 | |
paul@181 | 334 | return ext2fs_read_inode(fs, ino, inode); |
paul@181 | 335 | } |
paul@181 | 336 | |
paul@253 | 337 | /* Update the reference count for the given inode. */ |
paul@249 | 338 | |
paul@253 | 339 | errcode_t image_inode_refcount_update(ext2_filsys fs, ext2_ino_t ino, int change) |
paul@249 | 340 | { |
paul@253 | 341 | struct ext2_inode inode; |
paul@253 | 342 | errcode_t retval = ext2fs_read_inode(fs, ino, &inode); |
paul@249 | 343 | |
paul@249 | 344 | if (retval) |
paul@249 | 345 | return retval; |
paul@249 | 346 | |
paul@249 | 347 | /* NOTE: FUSE implementation tests for > 1. */ |
paul@249 | 348 | |
paul@253 | 349 | if ((change < 0) && (inode.i_links_count + change < 0)) |
paul@253 | 350 | inode.i_links_count = 0; |
paul@253 | 351 | else |
paul@253 | 352 | inode.i_links_count += change; |
paul@249 | 353 | |
paul@249 | 354 | /* NOTE: Update modification time. */ |
paul@253 | 355 | |
paul@253 | 356 | return ext2fs_write_inode(fs, ino, &inode); |
paul@249 | 357 | } |
paul@249 | 358 | |
paul@272 | 359 | /* List a directory. */ |
paul@226 | 360 | |
paul@226 | 361 | errcode_t image_list_dir(ext2_filsys fs, const char *path, |
paul@226 | 362 | int (*proc)(struct ext2_dir_entry *, int, int, char *, |
paul@226 | 363 | void *), |
paul@226 | 364 | void *data) |
paul@226 | 365 | { |
paul@226 | 366 | ext2_ino_t ino; |
paul@226 | 367 | errcode_t retval; |
paul@226 | 368 | |
paul@226 | 369 | /* Locate the object and test whether it is a directory. */ |
paul@226 | 370 | |
paul@257 | 371 | retval = image_find_by_path(fs, path, &ino); |
paul@257 | 372 | |
paul@226 | 373 | if (retval) |
paul@226 | 374 | return retval; |
paul@226 | 375 | |
paul@258 | 376 | if (!image_isdir_by_inode(fs, ino)) |
paul@226 | 377 | return 1; |
paul@226 | 378 | |
paul@226 | 379 | /* List the directory contents. */ |
paul@226 | 380 | |
paul@444 | 381 | retval = ext2fs_dir_iterate(fs, ino, 0, 0, proc, data); |
paul@257 | 382 | |
paul@226 | 383 | if (retval) |
paul@226 | 384 | return retval; |
paul@226 | 385 | |
paul@226 | 386 | return 0; |
paul@226 | 387 | } |
paul@226 | 388 | |
paul@272 | 389 | /* Make a directory in the given directory having the given name and |
paul@272 | 390 | metadata. */ |
paul@181 | 391 | |
paul@181 | 392 | errcode_t image_make_dir(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@181 | 393 | const char *basename, __u16 mode, |
paul@181 | 394 | __u16 uid, __u16 gid, ext2_ino_t *ino) |
paul@181 | 395 | { |
paul@181 | 396 | struct ext2_inode inode_dir; |
paul@181 | 397 | errcode_t retval = 0; |
paul@181 | 398 | |
paul@181 | 399 | /* Create an inode in the directory. */ |
paul@181 | 400 | |
paul@181 | 401 | retval = ext2fs_new_inode(fs, ino_dir, LINUX_S_IFDIR | mode, 0, ino); |
paul@181 | 402 | if (retval) |
paul@181 | 403 | return retval; |
paul@181 | 404 | |
paul@181 | 405 | /* Make the directory and update the metadata (due to ext2fs_mkdir |
paul@181 | 406 | limitation). */ |
paul@181 | 407 | |
paul@181 | 408 | retval = ext2fs_mkdir(fs, ino_dir, *ino, basename); |
paul@181 | 409 | if (retval) |
paul@181 | 410 | return retval; |
paul@181 | 411 | |
paul@181 | 412 | retval = ext2fs_read_inode(fs, *ino, &inode_dir); |
paul@181 | 413 | if (retval) |
paul@181 | 414 | return retval; |
paul@181 | 415 | |
paul@181 | 416 | image_set_metadata(&inode_dir, 0, LINUX_S_IFDIR | mode, uid, gid); |
paul@181 | 417 | return ext2fs_write_inode(fs, *ino, &inode_dir); |
paul@181 | 418 | } |
paul@181 | 419 | |
paul@272 | 420 | /* Make a directory in the given directory, updating the name reference to refer |
paul@272 | 421 | to the next component. */ |
paul@181 | 422 | |
paul@181 | 423 | errcode_t image_make_next_dir(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@181 | 424 | const char **basename, __u16 mode, __u16 uid, |
paul@181 | 425 | __u16 gid, ext2_ino_t *ino) |
paul@181 | 426 | { |
paul@181 | 427 | char *end = (char *) path_component_end(*basename); |
paul@181 | 428 | char endchar = *end; |
paul@181 | 429 | errcode_t retval = 0; |
paul@181 | 430 | |
paul@181 | 431 | /* Delimit the basename and make a directory using the inode. */ |
paul@181 | 432 | |
paul@181 | 433 | if (endchar) |
paul@181 | 434 | *end = '\0'; |
paul@181 | 435 | |
paul@181 | 436 | /* Do not create directories for empty components. */ |
paul@181 | 437 | |
paul@181 | 438 | if (**basename) |
paul@181 | 439 | retval = image_make_dir(fs, ino_dir, *basename, mode, uid, gid, ino); |
paul@181 | 440 | |
paul@181 | 441 | /* Restore the path separator and update the current component. */ |
paul@181 | 442 | |
paul@181 | 443 | if (endchar) |
paul@181 | 444 | *end = '/'; |
paul@181 | 445 | |
paul@181 | 446 | if (!retval) |
paul@181 | 447 | *basename = path_component_next(end); |
paul@181 | 448 | |
paul@181 | 449 | return retval; |
paul@181 | 450 | } |
paul@181 | 451 | |
paul@272 | 452 | /* Make directories descending to the given path. */ |
paul@181 | 453 | |
paul@257 | 454 | errcode_t image_make_dirs(ext2_filsys fs, const char **path, |
paul@181 | 455 | ext2_ino_t ino_dir, __u16 mode, __u16 uid, __u16 gid) |
paul@181 | 456 | { |
paul@181 | 457 | ext2_ino_t ino; |
paul@181 | 458 | errcode_t retval; |
paul@181 | 459 | |
paul@257 | 460 | while (**path) |
paul@181 | 461 | { |
paul@257 | 462 | retval = image_make_next_dir(fs, ino_dir, path, mode, uid, gid, &ino); |
paul@181 | 463 | if (retval) |
paul@181 | 464 | return retval; |
paul@181 | 465 | |
paul@181 | 466 | /* Move into the created object for handling the next component. */ |
paul@181 | 467 | |
paul@181 | 468 | ino_dir = ino; |
paul@181 | 469 | } |
paul@181 | 470 | |
paul@181 | 471 | return 0; |
paul@181 | 472 | } |
paul@181 | 473 | |
paul@231 | 474 | /* Remove an inode. */ |
paul@231 | 475 | |
paul@231 | 476 | errcode_t image_remove_by_inode(ext2_filsys fs, ext2_ino_t ino) |
paul@231 | 477 | { |
paul@231 | 478 | struct ext2_inode_large inode; |
paul@249 | 479 | ext2_ino_t ino_parent = 0; |
paul@248 | 480 | errcode_t retval; |
paul@258 | 481 | int isdir = image_isdir_by_inode(fs, ino); |
paul@248 | 482 | |
paul@252 | 483 | if (isdir) |
paul@248 | 484 | { |
paul@249 | 485 | retval = image_dir_get_parent(fs, ino, &ino_parent); |
paul@248 | 486 | if (retval) |
paul@248 | 487 | return retval; |
paul@248 | 488 | } |
paul@248 | 489 | |
paul@248 | 490 | retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *) &inode, |
paul@248 | 491 | sizeof(inode)); |
paul@231 | 492 | |
paul@231 | 493 | /* Handle invalid inodes, ignore unreferenced inodes. */ |
paul@231 | 494 | |
paul@248 | 495 | if (retval) |
paul@248 | 496 | return retval; |
paul@231 | 497 | |
paul@231 | 498 | if (!inode.i_links_count) |
paul@231 | 499 | return 0; |
paul@231 | 500 | |
paul@231 | 501 | /* Decrement the reference count. With no more references to the inode, |
paul@252 | 502 | remove its resources. Directories appear to need a double decrement. */ |
paul@231 | 503 | |
paul@231 | 504 | inode.i_links_count--; |
paul@231 | 505 | |
paul@252 | 506 | if (isdir && inode.i_links_count) |
paul@252 | 507 | inode.i_links_count--; |
paul@252 | 508 | |
paul@231 | 509 | if (!inode.i_links_count) |
paul@231 | 510 | { |
paul@248 | 511 | /* NOTE: Update deletion time. */ |
paul@248 | 512 | |
paul@248 | 513 | retval = ext2fs_free_ext_attr(fs, ino, &inode); |
paul@248 | 514 | |
paul@248 | 515 | if (!retval) |
paul@231 | 516 | { |
paul@231 | 517 | /* Deallocate blocks, if appropriate. ~0ULL as the end represents |
paul@231 | 518 | truncation. */ |
paul@231 | 519 | |
paul@231 | 520 | if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *) &inode)) |
paul@231 | 521 | { |
paul@249 | 522 | retval = ext2fs_punch(fs, ino, (struct ext2_inode *) &inode, |
paul@249 | 523 | NULL, 0, ~0ULL); |
paul@231 | 524 | |
paul@231 | 525 | /* Update allocation statistics. */ |
paul@231 | 526 | |
paul@248 | 527 | if (!retval) |
paul@231 | 528 | ext2fs_inode_alloc_stats2(fs, ino, -1, |
paul@231 | 529 | LINUX_S_ISDIR(inode.i_mode)); |
paul@231 | 530 | } |
paul@231 | 531 | } |
paul@231 | 532 | } |
paul@231 | 533 | |
paul@249 | 534 | retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *) &inode, |
paul@249 | 535 | sizeof(inode)); |
paul@249 | 536 | |
paul@249 | 537 | /* Decrement the parent reference count for directories. */ |
paul@249 | 538 | |
paul@249 | 539 | if (!retval && !inode.i_links_count && ino_parent) |
paul@253 | 540 | retval = image_inode_refcount_update(fs, ino_parent, -1); |
paul@249 | 541 | |
paul@249 | 542 | return retval; |
paul@231 | 543 | } |
paul@231 | 544 | |
paul@243 | 545 | /* Remove a directory entry using its full path. */ |
paul@243 | 546 | |
paul@249 | 547 | errcode_t image_remove_by_path(ext2_filsys fs, const char *path, |
paul@249 | 548 | ext2_ino_t *ino) |
paul@248 | 549 | { |
paul@249 | 550 | return image_access_by_path(fs, path, image_remove_by_inode, ino); |
paul@248 | 551 | } |
paul@248 | 552 | |
paul@236 | 553 | /* Rename a file. */ |
paul@236 | 554 | |
paul@236 | 555 | errcode_t image_rename(ext2_filsys fs, ext2_ino_t source, |
paul@236 | 556 | ext2_ino_t source_parent, const char *source_basename, |
paul@236 | 557 | ext2_ino_t target_parent, const char *target_basename) |
paul@236 | 558 | { |
paul@236 | 559 | errcode_t retval; |
paul@253 | 560 | struct ext2_inode source_inode; |
paul@248 | 561 | |
paul@236 | 562 | /* NOTE: Should check for space. */ |
paul@236 | 563 | |
paul@236 | 564 | /* Obtain the source object. */ |
paul@236 | 565 | |
paul@236 | 566 | retval = ext2fs_read_inode(fs, source, &source_inode); |
paul@236 | 567 | |
paul@236 | 568 | if (retval) |
paul@236 | 569 | return retval; |
paul@236 | 570 | |
paul@236 | 571 | /* Link from the target parent. */ |
paul@236 | 572 | |
paul@236 | 573 | retval = _image_link(fs, target_parent, target_basename, source, |
paul@236 | 574 | image_file_type(source_inode.i_mode)); |
paul@236 | 575 | |
paul@236 | 576 | if (retval) |
paul@236 | 577 | return retval; |
paul@236 | 578 | |
paul@443 | 579 | if (LINUX_S_ISDIR(source_inode.i_mode)) |
paul@236 | 580 | { |
paul@236 | 581 | /* Update the link count for the target. */ |
paul@236 | 582 | |
paul@253 | 583 | retval = image_inode_refcount_update(fs, target_parent, 1); |
paul@236 | 584 | |
paul@236 | 585 | if (retval) |
paul@236 | 586 | return retval; |
paul@236 | 587 | |
paul@236 | 588 | /* A directory needs its .. entry updating to refer to its new |
paul@236 | 589 | parent. */ |
paul@236 | 590 | |
paul@236 | 591 | retval = ext2fs_dir_iterate(fs, source, 0, NULL, |
paul@251 | 592 | _image_update_parent_entry, &target_parent); |
paul@236 | 593 | |
paul@236 | 594 | /* Update the link count for the source. */ |
paul@236 | 595 | |
paul@253 | 596 | retval = image_inode_refcount_update(fs, source_parent, -1); |
paul@236 | 597 | |
paul@236 | 598 | if (retval) |
paul@236 | 599 | return retval; |
paul@236 | 600 | } |
paul@236 | 601 | |
paul@236 | 602 | /* Unlink from the source parent, doing so by name because the file is now |
paul@236 | 603 | already linked from the target parent, and when the parents are the same, |
paul@236 | 604 | unlinking by inode could just cause the file to disappear from the |
paul@236 | 605 | catalogue. */ |
paul@236 | 606 | |
paul@452 | 607 | retval = image_unlink_by_name(fs, source_parent, source_basename); |
paul@452 | 608 | |
paul@452 | 609 | if (retval) |
paul@452 | 610 | return retval; |
paul@452 | 611 | |
paul@452 | 612 | return ext2fs_flush2(fs, 0); |
paul@236 | 613 | } |
paul@236 | 614 | |
paul@235 | 615 | /* Set the mode, user and group metadata for a file. */ |
paul@235 | 616 | |
paul@235 | 617 | void image_set_metadata(struct ext2_inode *inode, int clean, __u16 mode, |
paul@235 | 618 | __u16 uid, __u16 gid) |
paul@235 | 619 | { |
paul@235 | 620 | if (clean) |
paul@235 | 621 | memset(inode, 0, sizeof(*inode)); |
paul@235 | 622 | |
paul@235 | 623 | inode->i_mode = mode; |
paul@235 | 624 | inode->i_uid = uid; |
paul@235 | 625 | inode->i_gid = gid; |
paul@235 | 626 | } |
paul@235 | 627 | |
paul@235 | 628 | /* Copy file metadata into a stat structure. */ |
paul@235 | 629 | |
paul@235 | 630 | errcode_t image_stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *st) |
paul@235 | 631 | { |
paul@235 | 632 | struct ext2_inode inode; |
paul@235 | 633 | errcode_t retval = ext2fs_read_inode(fs, ino, &inode); |
paul@235 | 634 | |
paul@235 | 635 | if (retval) |
paul@235 | 636 | return retval; |
paul@235 | 637 | |
paul@265 | 638 | /* NOTE: Could access large inode members and other information. */ |
paul@265 | 639 | |
paul@235 | 640 | st->st_dev = 0; /* device identifier */ |
paul@235 | 641 | st->st_ino = ino; |
paul@235 | 642 | st->st_mode = inode.i_mode; |
paul@235 | 643 | st->st_nlink = inode.i_links_count; |
paul@235 | 644 | st->st_uid = inode_uid(inode); |
paul@235 | 645 | st->st_gid = inode_gid(inode); |
paul@235 | 646 | st->st_rdev = 0; /* special file device identifier */ |
paul@235 | 647 | st->st_size = EXT2_I_SIZE(&inode); |
paul@235 | 648 | st->st_blksize = fs->blocksize; |
paul@265 | 649 | st->st_blocks = inode.i_blocks; /* number of 512 byte blocks allocated */ |
paul@235 | 650 | st->st_atim.tv_sec = inode.i_atime; |
paul@235 | 651 | st->st_atim.tv_nsec = 0; /* nanosecond resolution */ |
paul@235 | 652 | st->st_mtim.tv_sec = inode.i_mtime; |
paul@235 | 653 | st->st_mtim.tv_nsec = 0; |
paul@235 | 654 | st->st_ctim.tv_sec = inode.i_ctime; |
paul@235 | 655 | st->st_ctim.tv_nsec = 0; |
paul@235 | 656 | |
paul@235 | 657 | return 0; |
paul@235 | 658 | } |
paul@235 | 659 | |
paul@231 | 660 | /* Unlink a directory entry by name. */ |
paul@231 | 661 | |
paul@231 | 662 | errcode_t image_unlink_by_name(ext2_filsys fs, ext2_ino_t ino_parent, |
paul@231 | 663 | const char *basename) |
paul@231 | 664 | { |
paul@243 | 665 | return ext2fs_unlink(fs, ino_parent, basename, 0, 0); |
paul@243 | 666 | } |
paul@243 | 667 | |
paul@243 | 668 | /* Unlink a directory entry by full path. */ |
paul@243 | 669 | |
paul@243 | 670 | errcode_t image_unlink_by_path(ext2_filsys fs, const char *path) |
paul@243 | 671 | { |
paul@243 | 672 | char _path[strlen(path) + 1]; |
paul@243 | 673 | char *basename; |
paul@243 | 674 | ext2_ino_t ino_parent; |
paul@243 | 675 | errcode_t retval; |
paul@243 | 676 | |
paul@245 | 677 | /* Copy the path and split it. */ |
paul@245 | 678 | |
paul@243 | 679 | strcpy(_path, path); |
paul@243 | 680 | basename = path_split(_path); |
paul@243 | 681 | |
paul@245 | 682 | /* Determine the parent directory. */ |
paul@245 | 683 | |
paul@245 | 684 | if (basename != _path) |
paul@245 | 685 | { |
paul@257 | 686 | retval = image_find_by_path(fs, _path, &ino_parent); |
paul@243 | 687 | |
paul@245 | 688 | if (retval) |
paul@245 | 689 | return retval; |
paul@245 | 690 | } |
paul@245 | 691 | else |
paul@245 | 692 | ino_parent = EXT2_ROOT_INO; |
paul@236 | 693 | |
paul@231 | 694 | return ext2fs_unlink(fs, ino_parent, basename, 0, 0); |
paul@231 | 695 | } |
paul@231 | 696 | |
paul@231 | 697 | /* Unlink a directory entry by inode number. */ |
paul@231 | 698 | |
paul@231 | 699 | errcode_t image_unlink_by_inode(ext2_filsys fs, ext2_ino_t ino_parent, |
paul@231 | 700 | ext2_ino_t ino) |
paul@231 | 701 | { |
paul@231 | 702 | return ext2fs_unlink(fs, ino_parent, 0, ino, 0); |
paul@231 | 703 | } |
paul@231 | 704 | |
paul@243 | 705 | |
paul@243 | 706 | |
paul@272 | 707 | /* Test object presence and types. */ |
paul@243 | 708 | |
paul@258 | 709 | int image_exists(ext2_filsys fs, const char *path) |
paul@243 | 710 | { |
paul@243 | 711 | ext2_ino_t ino; |
paul@243 | 712 | |
paul@258 | 713 | return !image_find_by_path(fs, path, &ino); |
paul@243 | 714 | } |
paul@181 | 715 | |
paul@258 | 716 | int image_isdir_by_inode(ext2_filsys fs, ext2_ino_t ino) |
paul@181 | 717 | { |
paul@181 | 718 | struct ext2_inode inode; |
paul@181 | 719 | |
paul@181 | 720 | if (ext2fs_read_inode(fs, ino, &inode)) |
paul@181 | 721 | return 0; |
paul@181 | 722 | |
paul@181 | 723 | return LINUX_S_ISDIR(inode.i_mode); |
paul@181 | 724 | } |
paul@181 | 725 | |
paul@258 | 726 | int image_isdir_by_path(ext2_filsys fs, const char *path) |
paul@181 | 727 | { |
paul@181 | 728 | ext2_ino_t ino; |
paul@181 | 729 | |
paul@258 | 730 | if (image_find_by_path(fs, path, &ino)) |
paul@258 | 731 | return 0; |
paul@258 | 732 | |
paul@258 | 733 | return image_isdir_by_inode(fs, ino); |
paul@255 | 734 | } |
paul@255 | 735 | |
paul@258 | 736 | int image_isfile_by_inode(ext2_filsys fs, ext2_ino_t ino) |
paul@181 | 737 | { |
paul@181 | 738 | struct ext2_inode inode; |
paul@181 | 739 | |
paul@181 | 740 | if (ext2fs_read_inode(fs, ino, &inode)) |
paul@181 | 741 | return 0; |
paul@181 | 742 | |
paul@181 | 743 | return LINUX_S_ISREG(inode.i_mode); |
paul@181 | 744 | } |
paul@181 | 745 | |
paul@258 | 746 | int image_isfile_by_path(ext2_filsys fs, const char *path) |
paul@181 | 747 | { |
paul@181 | 748 | ext2_ino_t ino; |
paul@181 | 749 | |
paul@258 | 750 | if (image_find_by_path(fs, path, &ino)) |
paul@181 | 751 | return 0; |
paul@181 | 752 | |
paul@258 | 753 | return image_isfile_by_inode(fs, ino); |
paul@181 | 754 | } |
paul@236 | 755 | |
paul@236 | 756 | /* vim: tabstop=4 expandtab shiftwidth=4 |
paul@236 | 757 | */ |