1 /**
2  * Copyright (c) 2008-2015 Alper Akcan <alper.akcan@gmail.com>
3  * Copyright (c) 2009 Renzo Davoli <renzo@cs.unibo.it>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program (in the main directory of the fuse-ext2
17  * distribution in the file COPYING); if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "fuse-ext2.h"
22 
23 static inline dev_t old_decode_dev (__u16 val)
24 {
25 	return makedev((val >> 8) & 255, val & 255);
26 }
27 
28 static inline dev_t new_decode_dev (__u32 dev)
29 {
30 	unsigned major = (dev & 0xfff00) >> 8;
31 	unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
32 	return makedev(major, minor);
33 }
34 
35 void do_fillstatbuf (ext2_filsys e2fs, ext2_ino_t ino, struct ext2_inode *inode, struct stat *st)
36 {
37 	debugf("enter");
38 	memset(st, 0, sizeof(*st));
39 	/* XXX workaround
40 	 * should be unique and != existing devices */
41 	st->st_dev = (dev_t) ((long) e2fs);
42 	st->st_ino = ino;
43 	st->st_mode = inode->i_mode;
44 	st->st_nlink = inode->i_links_count;
45 	st->st_uid = ext2_read_uid(inode);
46 	st->st_gid = ext2_read_gid(inode);
47 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
48 		if (inode->i_block[0]) {
49 			st->st_rdev = old_decode_dev(ext2fs_le32_to_cpu(inode->i_block[0]));
50 		} else {
51 			st->st_rdev = new_decode_dev(ext2fs_le32_to_cpu(inode->i_block[1]));
52 		}
53 	} else {
54 		st->st_rdev = 0;
55 	}
56 	st->st_size = EXT2_I_SIZE(inode);
57 	st->st_blksize = EXT2_BLOCK_SIZE(e2fs->super);
58 	st->st_blocks = inode->i_blocks;
59 	st->st_atime = inode->i_atime;
60 	st->st_mtime = inode->i_mtime;
61 	st->st_ctime = inode->i_ctime;
62 #if __FreeBSD__ == 10
63 	st->st_gen = inode->i_generation;
64 #endif
65 	debugf("leave");
66 }
67