-
Notifications
You must be signed in to change notification settings - Fork 110
/
op_readlink.c
57 lines (47 loc) · 1.44 KB
/
op_readlink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Copyright (c) 2010, Gerard Lledó Vives, [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. See README and COPYING for
* more details.
*/
#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include <errno.h>
#include "common.h"
#include "disk.h"
#include "inode.h"
#include "logging.h"
#include "ops.h"
#include "super.h"
static int get_link_dest(struct ext4_inode *inode, char *buf, size_t bufsize)
{
uint64_t inode_size = inode_get_size(inode);
if (inode_size <= 60) {
/* Link destination fits in inode */
memcpy(buf, inode->i_block, inode_size);
} else {
uint64_t pblock = inode_get_data_pblock(inode, 0, NULL);
char *block_data = malloc(super_block_size());
disk_read_block(pblock, (uint8_t *)block_data);
strncpy(buf, block_data, bufsize - 1);
free(block_data);
}
buf[inode_size] = 0;
return inode_size + 1;
}
/* Check return values, bufer sizes and so on; strings are nasty... */
int op_readlink(const char *path, char *buf, size_t bufsize)
{
struct ext4_inode inode;
DEBUG("readlink");
inode_get_by_path(path, &inode);
if (!S_ISLNK(inode.i_mode)) {
return -EINVAL;
}
get_link_dest(&inode, buf, bufsize);
DEBUG("Link resolved: %s => %s", path, buf);
return 0;
}