1 /*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "unistd.h"
32 #include "errno.h"
33 #include "vnode.h"
34
do_readlink(int dirfd,const char * path,char * buf,size_t bufsize)35 ssize_t do_readlink(int dirfd, const char *path, char *buf, size_t bufsize)
36 {
37 struct Vnode *vnode = NULL;
38 char *fullpath = NULL;
39 ssize_t ret;
40
41 if (!path)
42 {
43 ret = -EFAULT;
44 goto errout;
45 }
46
47 if (*path == '\0' || buf == NULL || bufsize < 0)
48 {
49 ret = -EINVAL;
50 goto errout;
51 }
52
53 ret = vfs_normalize_pathat(dirfd, path, &fullpath);
54 if (ret < 0)
55 {
56 goto errout;
57 }
58
59 VnodeHold();
60 ret = VnodeLookup(fullpath, &vnode, 0);
61 if (ret < 0)
62 {
63 goto errout_with_vnode;
64 }
65
66 if (vnode->type != VNODE_TYPE_LNK)
67 {
68 ret = -EINVAL;
69 goto errout_with_vnode;
70 }
71
72 if (!vnode->vop || !vnode->vop->Readlink)
73 {
74 ret = -ENOSYS;
75 goto errout_with_vnode;
76 }
77
78 ret = vnode->vop->Readlink(vnode, buf, bufsize);
79 if (ret < 0)
80 {
81 goto errout_with_vnode;
82 }
83
84 VnodeDrop();
85
86 free(fullpath);
87
88 return ret;
89
90 errout_with_vnode:
91 VnodeDrop();
92 free(fullpath);
93 errout:
94 set_errno(-ret);
95 return VFS_ERROR;
96 }
97
readlink(const char * pathname,char * buf,size_t bufsize)98 ssize_t readlink(const char *pathname, char *buf, size_t bufsize)
99 {
100 return do_readlink(AT_FDCWD, pathname, buf, bufsize);
101 }
102
readlinkat(int dirfd,const char * pathname,char * buf,size_t bufsize)103 ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsize)
104 {
105 return do_readlink(dirfd, pathname, buf, bufsize);
106 }
107