1 /* 2 * Copyright (c) 2012 Dave Vasilevsky <dave@vasilevsky.ca> 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 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 #ifndef SQFS_DIR_H 26 #define SQFS_DIR_H 27 28 #include "common.h" 29 30 #include "squashfs_fs.h" 31 32 typedef struct { 33 sqfs_md_cursor cur; 34 sqfs_off_t offset, total; 35 struct squashfs_dir_header header; 36 } sqfs_dir; 37 38 typedef struct { 39 sqfs_inode_id inode; 40 sqfs_inode_num inode_number; 41 int type; 42 char *name; 43 size_t name_size; 44 sqfs_off_t offset, next_offset; 45 } sqfs_dir_entry; 46 47 typedef char sqfs_name[SQUASHFS_NAME_LEN + 1]; 48 49 /* Begin a directory traversal, initializing the dir structure. 50 If offset is non-zero, fast-forward to that offset in the directory. */ 51 sqfs_err sqfs_dir_open(sqfs *fs, sqfs_inode *inode, sqfs_dir *dir, 52 off_t offset); 53 54 /* Initialize a dir_entry structure before use. 55 'namebuf' should be a character buffer of enough size to hold any name, 56 see sqfs_name. It may also be NULL, in which case no names will be placed 57 into this dir_entry. */ 58 void sqfs_dentry_init(sqfs_dir_entry *entry, char *namebuf); 59 60 /* Get the next directory entry, filling in the dir_entry. 61 Returns false when out of entries, or on error. */ 62 bool sqfs_dir_next(sqfs *fs, sqfs_dir *dir, sqfs_dir_entry *entry, 63 sqfs_err *err); 64 65 /* Lookup an entry in a directory inode. 66 The dir_entry must have been initialized with a buffer. */ 67 sqfs_err sqfs_dir_lookup(sqfs *fs, sqfs_inode *inode, 68 const char *name, size_t namelen, sqfs_dir_entry *entry, bool *found); 69 70 /* Lookup a complete path, and replace *inode with the results. 71 Uses / (slash) as the directory separator. */ 72 sqfs_err sqfs_lookup_path(sqfs *fs, sqfs_inode *inode, const char *path, 73 bool *found); 74 75 76 /* Accessors on sqfs_dir_entry */ 77 sqfs_off_t sqfs_dentry_offset (sqfs_dir_entry *entry); 78 sqfs_off_t sqfs_dentry_next_offset (sqfs_dir_entry *entry); 79 int sqfs_dentry_type (sqfs_dir_entry *entry); 80 sqfs_mode_t sqfs_dentry_mode (sqfs_dir_entry *entry); 81 sqfs_inode_id sqfs_dentry_inode (sqfs_dir_entry *entry); 82 sqfs_inode_num sqfs_dentry_inode_num (sqfs_dir_entry *entry); 83 size_t sqfs_dentry_name_size (sqfs_dir_entry *entry); 84 bool sqfs_dentry_is_dir (sqfs_dir_entry *entry); 85 86 /* Yields the name of this directory entry, or NULL if the dir_entry structure 87 was initialized without a name buffer. Name will be nul-terminated. */ 88 const char * sqfs_dentry_name (sqfs_dir_entry *entry); 89 90 #endif 91