1 | /** @file
|
---|
2 | * IPRT - Symbolic Link Manipulation.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_symlink_h
|
---|
37 | #define IPRT_INCLUDED_symlink_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/cdefs.h>
|
---|
43 | #include <iprt/types.h>
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | RT_C_DECLS_BEGIN
|
---|
48 |
|
---|
49 | /** @defgroup grp_rt_symlink RTSymlink - Symbolic Link Manipulation
|
---|
50 | * @ingroup grp_rt
|
---|
51 | *
|
---|
52 | * For querying and changing symlink info (mode, ownership, etc) please refer
|
---|
53 | * to the @ref grp_rt_path "RTPath" API: RTPathQueryInfoEx, RTPathSetOwnerEx,
|
---|
54 | * RTPathSetModeEx and RTPathSetTimesEx.
|
---|
55 | *
|
---|
56 | * @{
|
---|
57 | */
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Checks if the specified path exists and is a symlink.
|
---|
61 | *
|
---|
62 | * @returns true if it's a symlink, false if it isn't.
|
---|
63 | * @param pszSymlink The path to the symlink.
|
---|
64 | *
|
---|
65 | * @sa RTDirExists, RTPathExists, RTSymlinkExists.
|
---|
66 | */
|
---|
67 | RTDECL(bool) RTSymlinkExists(const char *pszSymlink);
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Checks if this is a dangling link or not.
|
---|
71 | *
|
---|
72 | * If the target of @a pszSymlink is a symbolic link, this may return false if
|
---|
73 | * that or any subsequent links are dangling.
|
---|
74 | *
|
---|
75 | * @returns true if it's dangling, false if it isn't.
|
---|
76 | * @param pszSymlink The path to the symlink.
|
---|
77 | */
|
---|
78 | RTDECL(bool) RTSymlinkIsDangling(const char *pszSymlink);
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * RTSymlinkCreate link type argument.
|
---|
82 | */
|
---|
83 | typedef enum RTSYMLINKTYPE
|
---|
84 | {
|
---|
85 | /** Invalid value. */
|
---|
86 | RTSYMLINKTYPE_INVALID = 0,
|
---|
87 | /** The link targets a directory. */
|
---|
88 | RTSYMLINKTYPE_DIR,
|
---|
89 | /** The link targets a file (or whatever else). */
|
---|
90 | RTSYMLINKTYPE_FILE,
|
---|
91 | /** It is not known what is being targeted.
|
---|
92 | * @remarks The RTSymlinkCreate API may probe the target to try figure
|
---|
93 | * out what is being targeted. */
|
---|
94 | RTSYMLINKTYPE_UNKNOWN,
|
---|
95 | /** The end of the valid type values. */
|
---|
96 | RTSYMLINKTYPE_END,
|
---|
97 | /** Blow the type up to 32-bit. */
|
---|
98 | RTSYMLINKTYPE_32BIT_HACK = 0x7fffffff
|
---|
99 | } RTSYMLINKTYPE;
|
---|
100 |
|
---|
101 | /** @name RTSymlinkCreate flags.
|
---|
102 | * @{ */
|
---|
103 | /** Don't allow symbolic links as part of the path.
|
---|
104 | * @remarks this flag is currently not implemented and will be ignored. */
|
---|
105 | #define RTSYMLINKCREATE_FLAGS_NO_SYMLINKS RT_BIT(0)
|
---|
106 | /** @} */
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Creates a symbolic link (@a pszSymlink) targeting @a pszTarget.
|
---|
110 | *
|
---|
111 | * @returns IPRT status code.
|
---|
112 | *
|
---|
113 | * @param pszSymlink The name of the symbolic link.
|
---|
114 | * @param pszTarget The path to the symbolic link target. This is
|
---|
115 | * relative to @a pszSymlink or an absolute path.
|
---|
116 | * @param enmType The symbolic link type. For Windows compatability
|
---|
117 | * it is very important to set this correctly. When
|
---|
118 | * RTSYMLINKTYPE_UNKNOWN is used, the API will try
|
---|
119 | * make a guess and may attempt query information
|
---|
120 | * about @a pszTarget in the process.
|
---|
121 | * @param fCreate Create flags, RTSYMLINKCREATE_FLAGS_*.
|
---|
122 | */
|
---|
123 | RTDECL(int) RTSymlinkCreate(const char *pszSymlink, const char *pszTarget,
|
---|
124 | RTSYMLINKTYPE enmType, uint32_t fCreate);
|
---|
125 |
|
---|
126 | /** @name RTSymlinkDelete flags.
|
---|
127 | * @{ */
|
---|
128 | /** Don't allow symbolic links as part of the path.
|
---|
129 | * @remarks this flag is currently not implemented and will be ignored. */
|
---|
130 | #define RTSYMLINKDELETE_FLAGS_NO_SYMLINKS RT_BIT(0)
|
---|
131 | /** @} */
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Deletes the specified symbolic link.
|
---|
135 | *
|
---|
136 | * This will try to refuse deleting non-symlinks, however there are usually
|
---|
137 | * races in the implementation of this check so no guarantees can be are made.
|
---|
138 | *
|
---|
139 | * @returns IPRT status code.
|
---|
140 | * @retval VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
|
---|
141 | *
|
---|
142 | * @param pszSymlink The symbolic link that should be removed.
|
---|
143 | * @param fDelete Delete flags, RTSYMLINKDELETE_FLAGS_*.
|
---|
144 | */
|
---|
145 | RTDECL(int) RTSymlinkDelete(const char *pszSymlink, uint32_t fDelete);
|
---|
146 |
|
---|
147 | /** @name RTSymlinkRead flags.
|
---|
148 | * @{ */
|
---|
149 | /** Don't allow symbolic links as part of the path.
|
---|
150 | * @remarks this flag is currently not implemented and will be ignored. */
|
---|
151 | #define RTSYMLINKREAD_FLAGS_NO_SYMLINKS RT_BIT(0)
|
---|
152 | /** @} */
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Read the symlink target.
|
---|
156 | *
|
---|
157 | * @returns IPRT status code.
|
---|
158 | * @retval VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
|
---|
159 | * @retval VERR_BUFFER_OVERFLOW if the link is larger than @a cbTarget. The
|
---|
160 | * buffer will contain what all we managed to read, fully terminated
|
---|
161 | * if @a cbTarget > 0.
|
---|
162 | *
|
---|
163 | * @param pszSymlink The symbolic link that should be read.
|
---|
164 | * @param pszTarget The target buffer.
|
---|
165 | * @param cbTarget The size of the target buffer.
|
---|
166 | * @param fRead Read flags, RTSYMLINKREAD_FLAGS_*.
|
---|
167 | */
|
---|
168 | RTDECL(int) RTSymlinkRead(const char *pszSymlink, char *pszTarget, size_t cbTarget, uint32_t fRead);
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Read the symlink target into an API allocated buffer.
|
---|
172 | *
|
---|
173 | * This API eliminates the race involved in determining the right buffer size.
|
---|
174 | *
|
---|
175 | * @returns IPRT status code.
|
---|
176 | * @retval VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
|
---|
177 | *
|
---|
178 | * @param pszSymlink The symbolic link that should be read.
|
---|
179 | * @param ppszTarget Where to return the target string. Free the string
|
---|
180 | * by calling RTStrFree.
|
---|
181 | */
|
---|
182 | RTDECL(int) RTSymlinkReadA(const char *pszSymlink, char **ppszTarget);
|
---|
183 |
|
---|
184 | /** @} */
|
---|
185 |
|
---|
186 | RT_C_DECLS_END
|
---|
187 |
|
---|
188 | #endif /* !IPRT_INCLUDED_symlink_h */
|
---|
189 |
|
---|