VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/symlink-posix.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: symlink-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Symbolic Links, POSIX.
4 */
5
6/*
7 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_SYMLINK
42
43#include <errno.h>
44#include <sys/stat.h>
45#include <unistd.h>
46
47#include <iprt/symlink.h>
48
49#include <iprt/assert.h>
50#include <iprt/err.h>
51#include <iprt/log.h>
52#include <iprt/mem.h>
53#include <iprt/string.h>
54#include "internal/path.h"
55
56
57
58RTDECL(bool) RTSymlinkExists(const char *pszSymlink)
59{
60 bool fRc = false;
61 char const *pszNativeSymlink;
62 int rc = rtPathToNative(&pszNativeSymlink, pszSymlink, NULL);
63 if (RT_SUCCESS(rc))
64 {
65 struct stat s;
66 fRc = !lstat(pszNativeSymlink, &s)
67 && S_ISLNK(s.st_mode);
68
69 rtPathFreeNative(pszNativeSymlink, pszSymlink);
70 }
71
72 LogFlow(("RTSymlinkExists(%p={%s}): returns %RTbool\n", pszSymlink, pszSymlink, fRc));
73 return fRc;
74}
75
76
77RTDECL(bool) RTSymlinkIsDangling(const char *pszSymlink)
78{
79 bool fRc = false;
80 char const *pszNativeSymlink;
81 int rc = rtPathToNative(&pszNativeSymlink, pszSymlink, NULL);
82 if (RT_SUCCESS(rc))
83 {
84 struct stat s;
85 fRc = !lstat(pszNativeSymlink, &s)
86 && S_ISLNK(s.st_mode);
87 if (fRc)
88 {
89 errno = 0;
90 fRc = stat(pszNativeSymlink, &s) != 0
91 && ( errno == ENOENT
92 || errno == ENOTDIR
93 || errno == ELOOP);
94 }
95
96 rtPathFreeNative(pszNativeSymlink, pszSymlink);
97 }
98
99 LogFlow(("RTSymlinkIsDangling(%p={%s}): returns %RTbool\n", pszSymlink, pszSymlink, fRc));
100 return fRc;
101}
102
103
104RTDECL(int) RTSymlinkCreate(const char *pszSymlink, const char *pszTarget, RTSYMLINKTYPE enmType, uint32_t fCreate)
105{
106 RT_NOREF_PV(fCreate);
107
108 /*
109 * Validate the input.
110 */
111 AssertReturn(enmType > RTSYMLINKTYPE_INVALID && enmType < RTSYMLINKTYPE_END, VERR_INVALID_PARAMETER);
112 AssertPtrReturn(pszSymlink, VERR_INVALID_POINTER);
113 AssertPtrReturn(pszTarget, VERR_INVALID_POINTER);
114
115 /*
116 * Convert the paths.
117 */
118 char const *pszNativeSymlink;
119 int rc = rtPathToNative(&pszNativeSymlink, pszSymlink, NULL);
120 if (RT_SUCCESS(rc))
121 {
122 const char *pszNativeTarget;
123 rc = rtPathToNative(&pszNativeTarget, pszTarget, NULL);
124 if (RT_SUCCESS(rc))
125 {
126 /*
127 * Create the link.
128 */
129 if (symlink(pszNativeTarget, pszNativeSymlink) == 0)
130 rc = VINF_SUCCESS;
131 else
132 rc = RTErrConvertFromErrno(errno);
133
134 rtPathFreeNative(pszNativeTarget, pszTarget);
135 }
136 rtPathFreeNative(pszNativeSymlink, pszSymlink);
137 }
138
139 LogFlow(("RTSymlinkCreate(%p={%s}, %p={%s}, %d, %#x): returns %Rrc\n", pszSymlink, pszSymlink, pszTarget, pszTarget, enmType, fCreate, rc));
140 return rc;
141}
142
143
144RTDECL(int) RTSymlinkDelete(const char *pszSymlink, uint32_t fDelete)
145{
146 RT_NOREF_PV(fDelete);
147
148 char const *pszNativeSymlink;
149 int rc = rtPathToNative(&pszNativeSymlink, pszSymlink, NULL);
150 if (RT_SUCCESS(rc))
151 {
152 struct stat s;
153 if (!lstat(pszNativeSymlink, &s))
154 {
155 if (S_ISLNK(s.st_mode))
156 {
157 if (unlink(pszNativeSymlink) == 0)
158 rc = VINF_SUCCESS;
159 else
160 rc = RTErrConvertFromErrno(errno);
161 }
162 else
163 rc = VERR_NOT_SYMLINK;
164 }
165 else
166 rc = RTErrConvertFromErrno(errno);
167 rtPathFreeNative(pszNativeSymlink, pszSymlink);
168 }
169
170 LogFlow(("RTSymlinkDelete(%p={%s}, #%x): returns %Rrc\n", pszSymlink, pszSymlink, fDelete, rc));
171 return rc;
172}
173
174
175RTDECL(int) RTSymlinkRead(const char *pszSymlink, char *pszTarget, size_t cbTarget, uint32_t fRead)
176{
177 RT_NOREF_PV(fRead);
178
179 char *pszMyTarget;
180 int rc = RTSymlinkReadA(pszSymlink, &pszMyTarget);
181 if (RT_SUCCESS(rc))
182 {
183 rc = RTStrCopy(pszTarget, cbTarget, pszMyTarget);
184 RTStrFree(pszMyTarget);
185 }
186 LogFlow(("RTSymlinkRead(%p={%s}): returns %Rrc\n", pszSymlink, pszSymlink, rc));
187 return rc;
188}
189
190
191RTDECL(int) RTSymlinkReadA(const char *pszSymlink, char **ppszTarget)
192{
193 AssertPtr(ppszTarget);
194 char const *pszNativeSymlink;
195 int rc = rtPathToNative(&pszNativeSymlink, pszSymlink, NULL);
196 if (RT_SUCCESS(rc))
197 {
198 /* Guess the initial buffer size. */
199 ssize_t cbBuf;
200 struct stat s;
201 if (!lstat(pszNativeSymlink, &s))
202 cbBuf = RT_MAX(RT_ALIGN_Z(s.st_size, 64), 64);
203 else
204 cbBuf = 1024;
205
206 /* Read loop that grows the buffer. */
207 char *pszBuf = NULL;
208 for (;;)
209 {
210 RTMemTmpFree(pszBuf);
211 pszBuf = (char *)RTMemTmpAlloc(cbBuf);
212 if (pszBuf)
213 {
214 ssize_t cbReturned = readlink(pszNativeSymlink, pszBuf, cbBuf);
215 if (cbReturned >= cbBuf)
216 {
217 /* Increase the buffer size and try again */
218 cbBuf *= 2;
219 continue;
220 }
221
222 if (cbReturned > 0)
223 {
224 pszBuf[cbReturned] = '\0';
225 rc = rtPathFromNativeDup(ppszTarget, pszBuf, pszSymlink);
226 }
227 else if (errno == EINVAL)
228 rc = VERR_NOT_SYMLINK;
229 else
230 rc = RTErrConvertFromErrno(errno);
231 }
232 else
233 rc = VERR_NO_TMP_MEMORY;
234 break;
235 } /* for loop */
236
237 RTMemTmpFree(pszBuf);
238 rtPathFreeNative(pszNativeSymlink, pszSymlink);
239 }
240
241 if (RT_SUCCESS(rc))
242 LogFlow(("RTSymlinkReadA(%p={%s},%p): returns %Rrc *ppszTarget=%p:{%s}\n", pszSymlink, pszSymlink, ppszTarget, rc, *ppszTarget, *ppszTarget));
243 else
244 LogFlow(("RTSymlinkReadA(%p={%s},%p): returns %Rrc\n", pszSymlink, pszSymlink, ppszTarget, rc));
245 return rc;
246}
247
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use