/src/barebox/fs/squashfs/symlink.c
Line | Count | Source |
1 | | /* |
2 | | * Squashfs - a compressed read only filesystem for Linux |
3 | | * |
4 | | * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
5 | | * Phillip Lougher <phillip@squashfs.org.uk> |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU General Public License |
9 | | * as published by the Free Software Foundation; either version 2, |
10 | | * or (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20 | | * |
21 | | * symlink.c |
22 | | */ |
23 | | |
24 | | /* |
25 | | * This file implements code to handle symbolic links. |
26 | | * |
27 | | * The data contents of symbolic links are stored inside the symbolic |
28 | | * link inode within the inode table. This allows the normally small symbolic |
29 | | * link to be compressed as part of the inode table, achieving much greater |
30 | | * compression than if the symbolic link was compressed individually. |
31 | | */ |
32 | | |
33 | | #include <malloc.h> |
34 | | #include <linux/fs.h> |
35 | | #include <linux/stat.h> |
36 | | #include <linux/pagemap.h> |
37 | | |
38 | | #include "squashfs_fs.h" |
39 | | #include "squashfs_fs_sb.h" |
40 | | #include "squashfs_fs_i.h" |
41 | | #include "squashfs.h" |
42 | | |
43 | | static const char *squashfs_get_link(struct dentry *dentry, struct inode *inode) |
44 | 45.5k | { |
45 | 45.5k | struct super_block *sb = inode->i_sb; |
46 | 45.5k | u64 block = squashfs_i(inode)->start; |
47 | 45.5k | int offset = squashfs_i(inode)->offset; |
48 | 45.5k | size_t length; |
49 | 45.5k | int bytes; |
50 | 45.5k | unsigned char *symlink; |
51 | | |
52 | 45.5k | TRACE("Entered squashfs_symlink_readpage, start block " |
53 | 45.5k | "%llx, offset %x\n", block, offset); |
54 | | |
55 | 45.5k | if (i_size_read(inode) < 0) |
56 | 0 | return ERR_PTR(-EINVAL); |
57 | | |
58 | 45.5k | length = min_t(loff_t, i_size_read(inode), PAGE_SIZE); |
59 | | |
60 | 45.5k | symlink = malloc(length + 1); |
61 | 45.5k | if (!symlink) |
62 | 0 | return ERR_PTR(-ENOMEM); |
63 | | |
64 | 45.5k | symlink[length] = 0; |
65 | | |
66 | 45.5k | bytes = squashfs_read_metadata(sb, symlink, &block, &offset, length); |
67 | 45.5k | if (bytes < 0) { |
68 | 44.3k | ERROR("Unable to read symlink [%llx:%x]\n", |
69 | 44.3k | squashfs_i(inode)->start, |
70 | 44.3k | squashfs_i(inode)->offset); |
71 | 44.3k | goto error_out; |
72 | 44.3k | } |
73 | | |
74 | 1.26k | inode->i_link = symlink; |
75 | | |
76 | 1.26k | return inode->i_link; |
77 | | |
78 | 44.3k | error_out: |
79 | 44.3k | free(symlink); |
80 | | |
81 | 44.3k | return ERR_PTR(bytes); |
82 | 45.5k | } |
83 | | |
84 | | const struct inode_operations squashfs_symlink_inode_ops = { |
85 | | .get_link = squashfs_get_link, |
86 | | }; |