rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2007 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <err.h> |
| 28 | #include <debug.h> |
| 29 | #include <trace.h> |
| 30 | #include "ext2_priv.h" |
| 31 | |
| 32 | #define LOCAL_TRACE 0 |
| 33 | |
| 34 | int ext2_open_file(fscookie *cookie, const char *path, filecookie **fcookie) |
| 35 | { |
| 36 | ext2_t *ext2 = (ext2_t *)cookie; |
| 37 | int err; |
| 38 | |
| 39 | /* do a path lookup */ |
| 40 | inodenum_t inum; |
| 41 | err = ext2_lookup(ext2, path, &inum); |
| 42 | if (err < 0) |
| 43 | return err; |
| 44 | |
| 45 | /* create the file object */ |
| 46 | ext2_file_t *file = malloc(sizeof(ext2_file_t)); |
| 47 | memset(file, 0, sizeof(ext2_file_t)); |
| 48 | |
| 49 | /* read in the inode */ |
| 50 | err = ext2_load_inode(ext2, inum, &file->inode); |
| 51 | if (err < 0) { |
| 52 | free(file); |
| 53 | return err; |
| 54 | } |
| 55 | |
| 56 | file->ext2 = ext2; |
| 57 | *fcookie = (filecookie *)file; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | ssize_t ext2_read_file(filecookie *fcookie, void *buf, off_t offset, size_t len) |
| 63 | { |
| 64 | ext2_file_t *file = (ext2_file_t *)fcookie; |
| 65 | int err; |
| 66 | |
| 67 | // test that it's a file |
| 68 | if (!S_ISREG(file->inode.i_mode)) { |
| 69 | dprintf(INFO, "ext2_read_file: not a file\n"); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | // read from the inode |
| 74 | err = ext2_read_inode(file->ext2, &file->inode, buf, offset, len); |
| 75 | |
| 76 | return err; |
| 77 | } |
| 78 | |
| 79 | int ext2_close_file(filecookie *fcookie) |
| 80 | { |
| 81 | ext2_file_t *file = (ext2_file_t *)fcookie; |
| 82 | |
| 83 | // see if we need to free any of the cache blocks |
| 84 | int i; |
| 85 | for (i=0; i < 3; i++) { |
| 86 | if (file->ind_cache[i].num != 0) { |
| 87 | free(file->ind_cache[i].ptr); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | free(file); |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | off_t ext2_file_len(ext2_t *ext2, struct ext2_inode *inode) |
| 97 | { |
| 98 | /* calculate the file size */ |
| 99 | off_t len = inode->i_size; |
| 100 | if ((ext2->sb.s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_LARGE_FILE) && (S_ISREG(inode->i_mode))) { |
| 101 | /* can potentially be a large file */ |
| 102 | len |= (off_t)inode->i_size_high << 32; |
| 103 | } |
| 104 | |
| 105 | return len; |
| 106 | } |
| 107 | |
| 108 | int ext2_stat_file(filecookie *fcookie, struct file_stat *stat) |
| 109 | { |
| 110 | ext2_file_t *file = (ext2_file_t *)fcookie; |
| 111 | |
| 112 | stat->size = ext2_file_len(file->ext2, &file->inode); |
| 113 | |
| 114 | /* is it a dir? */ |
| 115 | stat->is_dir = false; |
| 116 | if (S_ISDIR(file->inode.i_mode)) |
| 117 | stat->is_dir = true; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | int ext2_read_link(ext2_t *ext2, struct ext2_inode *inode, char *str, size_t len) |
| 123 | { |
| 124 | LTRACEF("inode %p, str %p, len %zu\n", inode, str, len); |
| 125 | |
| 126 | off_t linklen = ext2_file_len(ext2, inode); |
| 127 | |
| 128 | if ((linklen < 0) || (linklen + 1 > len)) |
| 129 | return ERR_NO_MEMORY; |
| 130 | |
| 131 | if (linklen > 60) { |
| 132 | int err = ext2_read_inode(ext2, inode, str, 0, linklen); |
| 133 | if (err < 0) |
| 134 | return err; |
| 135 | str[linklen] = 0; |
| 136 | } else { |
| 137 | memcpy(str, &inode->i_block[0], linklen); |
| 138 | str[linklen] = 0; |
| 139 | } |
| 140 | |
| 141 | LTRACEF("read link '%s'\n", str); |
| 142 | |
| 143 | return linklen; |
| 144 | } |
| 145 | |