1 /*
2  * Copyright (c) 2007 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <lk/err.h>
13 #include <lk/debug.h>
14 #include <lk/trace.h>
15 #include "ext2_priv.h"
16 
17 #define LOCAL_TRACE 0
18 
ext2_open_file(fscookie * cookie,const char * path,filecookie ** fcookie)19 int ext2_open_file(fscookie *cookie, const char *path, filecookie **fcookie) {
20     ext2_t *ext2 = (ext2_t *)cookie;
21     int err;
22 
23     /* do a path lookup */
24     inodenum_t inum;
25     err = ext2_lookup(ext2, path, &inum);
26     if (err < 0)
27         return err;
28 
29     /* create the file object */
30     ext2_file_t *file = malloc(sizeof(ext2_file_t));
31     memset(file, 0, sizeof(ext2_file_t));
32 
33     /* read in the inode */
34     err = ext2_load_inode(ext2, inum, &file->inode);
35     if (err < 0) {
36         free(file);
37         return err;
38     }
39 
40     file->ext2 = ext2;
41     *fcookie = (filecookie *)file;
42 
43     return 0;
44 }
45 
ext2_read_file(filecookie * fcookie,void * buf,off_t offset,size_t len)46 ssize_t ext2_read_file(filecookie *fcookie, void *buf, off_t offset, size_t len) {
47     ext2_file_t *file = (ext2_file_t *)fcookie;
48     int err;
49 
50     // test that it's a file
51     if (!S_ISREG(file->inode.i_mode)) {
52         dprintf(INFO, "ext2_read_file: not a file\n");
53         return -1;
54     }
55 
56     // read from the inode
57     err = ext2_read_inode(file->ext2, &file->inode, buf, offset, len);
58 
59     return err;
60 }
61 
ext2_close_file(filecookie * fcookie)62 int ext2_close_file(filecookie *fcookie) {
63     ext2_file_t *file = (ext2_file_t *)fcookie;
64 
65     // see if we need to free any of the cache blocks
66     int i;
67     for (i=0; i < 3; i++) {
68         if (file->ind_cache[i].num != 0) {
69             free(file->ind_cache[i].ptr);
70         }
71     }
72 
73     free(file);
74 
75     return 0;
76 }
77 
ext2_file_len(ext2_t * ext2,struct ext2_inode * inode)78 off_t ext2_file_len(ext2_t *ext2, struct ext2_inode *inode) {
79     /* calculate the file size */
80     off_t len = inode->i_size;
81     if ((ext2->sb.s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_LARGE_FILE) && (S_ISREG(inode->i_mode))) {
82         /* can potentially be a large file */
83         len |= (off_t)inode->i_size_high << 32;
84     }
85 
86     return len;
87 }
88 
ext2_stat_file(filecookie * fcookie,struct file_stat * stat)89 int ext2_stat_file(filecookie *fcookie, struct file_stat *stat) {
90     ext2_file_t *file = (ext2_file_t *)fcookie;
91 
92     stat->size = ext2_file_len(file->ext2, &file->inode);
93 
94     /* is it a dir? */
95     stat->is_dir = false;
96     if (S_ISDIR(file->inode.i_mode))
97         stat->is_dir = true;
98 
99     return 0;
100 }
101 
ext2_read_link(ext2_t * ext2,struct ext2_inode * inode,char * str,size_t len)102 int ext2_read_link(ext2_t *ext2, struct ext2_inode *inode, char *str, size_t len) {
103     LTRACEF("inode %p, str %p, len %zu\n", inode, str, len);
104 
105     off_t linklen = ext2_file_len(ext2, inode);
106 
107     if ((linklen < 0) || (linklen + 1 > (off_t)len))
108         return ERR_NO_MEMORY;
109 
110     if (linklen > 60) {
111         int err = ext2_read_inode(ext2, inode, str, 0, linklen);
112         if (err < 0)
113             return err;
114         str[linklen] = 0;
115     } else {
116         memcpy(str, &inode->i_block[0], linklen);
117         str[linklen] = 0;
118     }
119 
120     LTRACEF("read link '%s'\n", str);
121 
122     return linklen;
123 }
124 
125