paul@181 | 1 | /* |
paul@181 | 2 | * Filesystem access functions. |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 2019 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 | #include <sys/stat.h> |
paul@181 | 24 | |
paul@181 | 25 | #include <ext2fs/ext2fs.h> |
paul@181 | 26 | |
paul@181 | 27 | #include "image.h" |
paul@181 | 28 | #include "path.h" |
paul@181 | 29 | |
paul@181 | 30 | |
paul@181 | 31 | |
paul@181 | 32 | /* Create an inode for a file. */ |
paul@181 | 33 | |
paul@181 | 34 | errcode_t image_create_file(ext2_filsys fs, ext2_ino_t ino_target, |
paul@181 | 35 | const char *basename, __u16 mode, |
paul@181 | 36 | __u16 uid, __u16 gid, ext2_ino_t *ino_file) |
paul@181 | 37 | { |
paul@181 | 38 | struct ext2_inode inode_file; |
paul@181 | 39 | errcode_t retval; |
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@181 | 49 | while (1) |
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@181 | 56 | |
paul@181 | 57 | /* Expand the directory if necessary. */ |
paul@181 | 58 | |
paul@181 | 59 | if (retval == EXT2_ET_DIR_NO_SPACE) |
paul@181 | 60 | retval = ext2fs_expand_dir(fs, ino_target); |
paul@181 | 61 | |
paul@181 | 62 | if (retval) |
paul@181 | 63 | return retval; |
paul@181 | 64 | } |
paul@181 | 65 | |
paul@181 | 66 | /* Make sure that subsequent files employ different inodes. */ |
paul@181 | 67 | |
paul@181 | 68 | ext2fs_inode_alloc_stats2(fs, *ino_file, 1, 0); |
paul@181 | 69 | |
paul@181 | 70 | /* Populate the inode details. */ |
paul@181 | 71 | |
paul@181 | 72 | image_set_metadata(&inode_file, 1, LINUX_S_IFREG | mode, uid, gid); |
paul@181 | 73 | |
paul@181 | 74 | return ext2fs_write_new_inode(fs, *ino_file, &inode_file); |
paul@181 | 75 | } |
paul@181 | 76 | |
paul@181 | 77 | /* Set the mode, user and group metadata for a file. */ |
paul@181 | 78 | |
paul@181 | 79 | void image_set_metadata(struct ext2_inode *inode, int clean, __u16 mode, |
paul@181 | 80 | __u16 uid, __u16 gid) |
paul@181 | 81 | { |
paul@181 | 82 | if (clean) |
paul@181 | 83 | memset(inode, 0, sizeof(*inode)); |
paul@181 | 84 | |
paul@181 | 85 | inode->i_mode = mode; |
paul@181 | 86 | inode->i_uid = uid; |
paul@181 | 87 | inode->i_gid = gid; |
paul@181 | 88 | } |
paul@181 | 89 | |
paul@181 | 90 | /* Find an object in the given directory with the given name in the filesystem |
paul@181 | 91 | image, updating the name reference to refer to the next component. */ |
paul@181 | 92 | |
paul@181 | 93 | errcode_t image_find_next(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@181 | 94 | const char **basename, char *buf, ext2_ino_t *ino) |
paul@181 | 95 | { |
paul@181 | 96 | const char *end = path_component_end(*basename); |
paul@181 | 97 | errcode_t retval; |
paul@181 | 98 | |
paul@181 | 99 | /* Find the basename in the directory. */ |
paul@181 | 100 | |
paul@181 | 101 | retval = ext2fs_lookup(fs, ino_dir, *basename, end - *basename, buf, ino); |
paul@181 | 102 | |
paul@181 | 103 | /* Update the current component. */ |
paul@181 | 104 | |
paul@181 | 105 | if (!retval) |
paul@181 | 106 | *basename = path_component_next(end); |
paul@181 | 107 | |
paul@181 | 108 | return retval; |
paul@181 | 109 | } |
paul@181 | 110 | |
paul@181 | 111 | /* Find an object with the given pathname in the filesystem image. */ |
paul@181 | 112 | |
paul@181 | 113 | errcode_t image_find_path(ext2_filsys fs, const char **pathname, ext2_ino_t *ino) |
paul@181 | 114 | { |
paul@181 | 115 | char *buf; |
paul@181 | 116 | ext2_ino_t ino_dir; |
paul@181 | 117 | errcode_t retval; |
paul@181 | 118 | |
paul@181 | 119 | retval = ext2fs_get_mem(fs->blocksize, &buf); |
paul@181 | 120 | if (retval) |
paul@181 | 121 | return retval; |
paul@181 | 122 | |
paul@181 | 123 | /* Skip any leading root marker. */ |
paul@181 | 124 | |
paul@181 | 125 | if (**pathname == '/') |
paul@181 | 126 | (*pathname)++; |
paul@181 | 127 | |
paul@181 | 128 | if (!**pathname) |
paul@181 | 129 | *ino = EXT2_ROOT_INO; |
paul@181 | 130 | |
paul@181 | 131 | /* Start at the root. */ |
paul@181 | 132 | |
paul@181 | 133 | ino_dir = EXT2_ROOT_INO; |
paul@181 | 134 | |
paul@181 | 135 | /* With any remaining path, find the next component. */ |
paul@181 | 136 | |
paul@181 | 137 | while (**pathname) |
paul@181 | 138 | { |
paul@181 | 139 | retval = image_find_next(fs, ino_dir, pathname, buf, ino); |
paul@181 | 140 | if (retval) |
paul@181 | 141 | { |
paul@181 | 142 | *ino = ino_dir; |
paul@181 | 143 | break; |
paul@181 | 144 | } |
paul@181 | 145 | |
paul@181 | 146 | /* Move into the found object for searching the next component. */ |
paul@181 | 147 | |
paul@181 | 148 | ino_dir = *ino; |
paul@181 | 149 | } |
paul@181 | 150 | |
paul@181 | 151 | ext2fs_free_mem(&buf); |
paul@181 | 152 | |
paul@181 | 153 | return retval; |
paul@181 | 154 | } |
paul@181 | 155 | |
paul@181 | 156 | /* Find an object in the given directory with the given name in the filesystem |
paul@181 | 157 | image. */ |
paul@181 | 158 | |
paul@181 | 159 | errcode_t image_find_file(ext2_filsys fs, const char *dirname, |
paul@181 | 160 | const char *basename, ext2_ino_t *ino) |
paul@181 | 161 | { |
paul@181 | 162 | char pathname[strlen(dirname) + strlen(basename) + 2]; |
paul@181 | 163 | const char *s = pathname; |
paul@181 | 164 | |
paul@181 | 165 | strcpy(pathname, dirname); |
paul@181 | 166 | strcat(pathname, "/"); |
paul@181 | 167 | strcat(pathname, basename); |
paul@181 | 168 | |
paul@181 | 169 | return image_find_path(fs, &s, ino); |
paul@181 | 170 | } |
paul@181 | 171 | |
paul@181 | 172 | /* Obtain the inode for the object with the given pathname in the filesystem |
paul@181 | 173 | image. */ |
paul@181 | 174 | |
paul@181 | 175 | errcode_t image_inode(ext2_filsys fs, const char *pathname, |
paul@181 | 176 | struct ext2_inode *inode) |
paul@181 | 177 | { |
paul@181 | 178 | ext2_ino_t ino; |
paul@181 | 179 | errcode_t retval; |
paul@181 | 180 | |
paul@181 | 181 | retval = image_find_path(fs, &pathname, &ino); |
paul@181 | 182 | if (retval) |
paul@181 | 183 | return retval; |
paul@181 | 184 | |
paul@181 | 185 | return ext2fs_read_inode(fs, ino, inode); |
paul@181 | 186 | } |
paul@181 | 187 | |
paul@181 | 188 | /* Make a directory in the given directory in the filesystem image having the |
paul@181 | 189 | given name and metadata. */ |
paul@181 | 190 | |
paul@181 | 191 | errcode_t image_make_dir(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@181 | 192 | const char *basename, __u16 mode, |
paul@181 | 193 | __u16 uid, __u16 gid, ext2_ino_t *ino) |
paul@181 | 194 | { |
paul@181 | 195 | struct ext2_inode inode_dir; |
paul@181 | 196 | errcode_t retval = 0; |
paul@181 | 197 | |
paul@181 | 198 | /* Create an inode in the directory. */ |
paul@181 | 199 | |
paul@181 | 200 | retval = ext2fs_new_inode(fs, ino_dir, LINUX_S_IFDIR | mode, 0, ino); |
paul@181 | 201 | if (retval) |
paul@181 | 202 | return retval; |
paul@181 | 203 | |
paul@181 | 204 | /* Make the directory and update the metadata (due to ext2fs_mkdir |
paul@181 | 205 | limitation). */ |
paul@181 | 206 | |
paul@181 | 207 | retval = ext2fs_mkdir(fs, ino_dir, *ino, basename); |
paul@181 | 208 | if (retval) |
paul@181 | 209 | return retval; |
paul@181 | 210 | |
paul@181 | 211 | retval = ext2fs_read_inode(fs, *ino, &inode_dir); |
paul@181 | 212 | if (retval) |
paul@181 | 213 | return retval; |
paul@181 | 214 | |
paul@181 | 215 | image_set_metadata(&inode_dir, 0, LINUX_S_IFDIR | mode, uid, gid); |
paul@181 | 216 | return ext2fs_write_inode(fs, *ino, &inode_dir); |
paul@181 | 217 | } |
paul@181 | 218 | |
paul@181 | 219 | /* Make a directory in the given directory in the filesystem image, updating |
paul@181 | 220 | the name reference to refer to the next component. */ |
paul@181 | 221 | |
paul@181 | 222 | errcode_t image_make_next_dir(ext2_filsys fs, ext2_ino_t ino_dir, |
paul@181 | 223 | const char **basename, __u16 mode, __u16 uid, |
paul@181 | 224 | __u16 gid, ext2_ino_t *ino) |
paul@181 | 225 | { |
paul@181 | 226 | char *end = (char *) path_component_end(*basename); |
paul@181 | 227 | char endchar = *end; |
paul@181 | 228 | errcode_t retval = 0; |
paul@181 | 229 | |
paul@181 | 230 | /* Delimit the basename and make a directory using the inode. */ |
paul@181 | 231 | |
paul@181 | 232 | if (endchar) |
paul@181 | 233 | *end = '\0'; |
paul@181 | 234 | |
paul@181 | 235 | /* Do not create directories for empty components. */ |
paul@181 | 236 | |
paul@181 | 237 | if (**basename) |
paul@181 | 238 | retval = image_make_dir(fs, ino_dir, *basename, mode, uid, gid, ino); |
paul@181 | 239 | |
paul@181 | 240 | /* Restore the path separator and update the current component. */ |
paul@181 | 241 | |
paul@181 | 242 | if (endchar) |
paul@181 | 243 | *end = '/'; |
paul@181 | 244 | |
paul@181 | 245 | if (!retval) |
paul@181 | 246 | *basename = path_component_next(end); |
paul@181 | 247 | |
paul@181 | 248 | return retval; |
paul@181 | 249 | } |
paul@181 | 250 | |
paul@181 | 251 | /* Make directories descending to the given path in the filesystem image. */ |
paul@181 | 252 | |
paul@181 | 253 | errcode_t image_make_dirs(ext2_filsys fs, const char **pathname, |
paul@181 | 254 | ext2_ino_t ino_dir, __u16 mode, __u16 uid, __u16 gid) |
paul@181 | 255 | { |
paul@181 | 256 | ext2_ino_t ino; |
paul@181 | 257 | errcode_t retval; |
paul@181 | 258 | |
paul@181 | 259 | while (**pathname) |
paul@181 | 260 | { |
paul@181 | 261 | retval = image_make_next_dir(fs, ino_dir, pathname, mode, uid, gid, &ino); |
paul@181 | 262 | if (retval) |
paul@181 | 263 | return retval; |
paul@181 | 264 | |
paul@181 | 265 | /* Move into the created object for handling the next component. */ |
paul@181 | 266 | |
paul@181 | 267 | ino_dir = ino; |
paul@181 | 268 | } |
paul@181 | 269 | |
paul@181 | 270 | return 0; |
paul@181 | 271 | } |
paul@181 | 272 | |
paul@181 | 273 | /* Copy file metadata into a stat structure. */ |
paul@181 | 274 | |
paul@181 | 275 | errcode_t image_stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *st) |
paul@181 | 276 | { |
paul@181 | 277 | struct ext2_inode inode; |
paul@181 | 278 | errcode_t retval = ext2fs_read_inode(fs, ino, &inode); |
paul@181 | 279 | |
paul@181 | 280 | if (retval) |
paul@181 | 281 | return retval; |
paul@181 | 282 | |
paul@181 | 283 | st->st_dev = 0; /* device identifier */ |
paul@181 | 284 | st->st_ino = ino; |
paul@181 | 285 | st->st_mode = inode.i_mode; |
paul@181 | 286 | st->st_nlink = inode.i_links_count; |
paul@181 | 287 | st->st_uid = inode_uid(inode); |
paul@181 | 288 | st->st_gid = inode_gid(inode); |
paul@181 | 289 | st->st_rdev = 0; /* special file device identifier */ |
paul@181 | 290 | st->st_size = EXT2_I_SIZE(&inode); |
paul@181 | 291 | st->st_blksize = fs->blocksize; |
paul@181 | 292 | st->st_blocks = 0; /* number of 512 byte blocks allocated */ |
paul@181 | 293 | st->st_atim.tv_sec = inode.i_atime; |
paul@181 | 294 | st->st_atim.tv_nsec = 0; /* nanosecond resolution */ |
paul@181 | 295 | st->st_mtim.tv_sec = inode.i_mtime; |
paul@181 | 296 | st->st_mtim.tv_nsec = 0; |
paul@181 | 297 | st->st_ctim.tv_sec = inode.i_ctime; |
paul@181 | 298 | st->st_ctim.tv_nsec = 0; |
paul@181 | 299 | |
paul@181 | 300 | return 0; |
paul@181 | 301 | } |
paul@181 | 302 | |
paul@181 | 303 | /* Test object types in the filesystem image. */ |
paul@181 | 304 | |
paul@181 | 305 | int _image_isdir(ext2_filsys fs, ext2_ino_t ino) |
paul@181 | 306 | { |
paul@181 | 307 | struct ext2_inode inode; |
paul@181 | 308 | |
paul@181 | 309 | if (ext2fs_read_inode(fs, ino, &inode)) |
paul@181 | 310 | return 0; |
paul@181 | 311 | |
paul@181 | 312 | return LINUX_S_ISDIR(inode.i_mode); |
paul@181 | 313 | } |
paul@181 | 314 | |
paul@181 | 315 | int image_isdir(ext2_filsys fs, const char *name) |
paul@181 | 316 | { |
paul@181 | 317 | ext2_ino_t ino; |
paul@181 | 318 | |
paul@181 | 319 | if (image_find_path(fs, &name, &ino)) |
paul@181 | 320 | return 0; |
paul@181 | 321 | |
paul@181 | 322 | return _image_isdir(fs, ino); |
paul@181 | 323 | } |
paul@181 | 324 | |
paul@181 | 325 | int _image_isfile(ext2_filsys fs, ext2_ino_t ino) |
paul@181 | 326 | { |
paul@181 | 327 | struct ext2_inode inode; |
paul@181 | 328 | |
paul@181 | 329 | if (ext2fs_read_inode(fs, ino, &inode)) |
paul@181 | 330 | return 0; |
paul@181 | 331 | |
paul@181 | 332 | return LINUX_S_ISREG(inode.i_mode); |
paul@181 | 333 | } |
paul@181 | 334 | |
paul@181 | 335 | int image_isfile(ext2_filsys fs, const char *name) |
paul@181 | 336 | { |
paul@181 | 337 | ext2_ino_t ino; |
paul@181 | 338 | |
paul@181 | 339 | if (image_find_path(fs, &name, &ino)) |
paul@181 | 340 | return 0; |
paul@181 | 341 | |
paul@181 | 342 | return _image_isfile(fs, ino); |
paul@181 | 343 | } |