summaryrefslogtreecommitdiffstats
path: root/include/iprt/symlink.h
blob: 8b5e60f0f3ef5368c4c903e5bbf857f632ded2ad (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/** @file
 * IPRT - Symbolic Link Manipulation.
 */

/*
 * Copyright (C) 2010-2022 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */

#ifndef IPRT_INCLUDED_symlink_h
#define IPRT_INCLUDED_symlink_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif

#include <iprt/cdefs.h>
#include <iprt/types.h>



RT_C_DECLS_BEGIN

/** @defgroup grp_rt_symlink    RTSymlink - Symbolic Link Manipulation
 * @ingroup grp_rt
 *
 * For querying and changing symlink info (mode, ownership, etc) please refer
 * to the @ref grp_rt_path "RTPath" API: RTPathQueryInfoEx, RTPathSetOwnerEx,
 * RTPathSetModeEx and RTPathSetTimesEx.
 *
 * @{
 */

/**
 * Checks if the specified path exists and is a symlink.
 *
 * @returns true if it's a symlink, false if it isn't.
 * @param   pszSymlink      The path to the symlink.
 *
 * @sa      RTDirExists, RTPathExists, RTSymlinkExists.
 */
RTDECL(bool) RTSymlinkExists(const char *pszSymlink);

/**
 * Checks if this is a dangling link or not.
 *
 * If the target of @a pszSymlink is a symbolic link, this may return false if
 * that or any subsequent links are dangling.
 *
 * @returns true if it's dangling, false if it isn't.
 * @param   pszSymlink      The path to the symlink.
 */
RTDECL(bool) RTSymlinkIsDangling(const char *pszSymlink);

/**
 * RTSymlinkCreate link type argument.
 */
typedef enum RTSYMLINKTYPE
{
    /** Invalid value. */
    RTSYMLINKTYPE_INVALID = 0,
    /** The link targets a directory. */
    RTSYMLINKTYPE_DIR,
    /** The link targets a file (or whatever else). */
    RTSYMLINKTYPE_FILE,
    /** It is not known what is being targeted.
     * @remarks The RTSymlinkCreate API may probe the target to try figure
     *          out what is being targeted. */
    RTSYMLINKTYPE_UNKNOWN,
    /** The end of the valid type values. */
    RTSYMLINKTYPE_END,
    /** Blow the type up to 32-bit. */
    RTSYMLINKTYPE_32BIT_HACK = 0x7fffffff
} RTSYMLINKTYPE;

/** @name RTSymlinkCreate flags.
 * @{ */
/** Don't allow symbolic links as part of the path.
 * @remarks this flag is currently not implemented and will be ignored. */
#define RTSYMLINKCREATE_FLAGS_NO_SYMLINKS  RT_BIT(0)
/** @} */

/**
 * Creates a symbolic link (@a pszSymlink) targeting @a pszTarget.
 *
 * @returns IPRT status code.
 *
 * @param   pszSymlink      The name of the symbolic link.
 * @param   pszTarget       The path to the symbolic link target.  This is
 *                          relative to @a pszSymlink or an absolute path.
 * @param   enmType         The symbolic link type.  For Windows compatability
 *                          it is very important to set this correctly.  When
 *                          RTSYMLINKTYPE_UNKNOWN is used, the API will try
 *                          make a guess and may attempt query information
 *                          about @a pszTarget in the process.
 * @param   fCreate         Create flags, RTSYMLINKCREATE_FLAGS_*.
 */
RTDECL(int) RTSymlinkCreate(const char *pszSymlink, const char *pszTarget,
                            RTSYMLINKTYPE enmType, uint32_t fCreate);

/** @name RTSymlinkDelete flags.
 * @{ */
/** Don't allow symbolic links as part of the path.
 * @remarks this flag is currently not implemented and will be ignored. */
#define RTSYMLINKDELETE_FLAGS_NO_SYMLINKS  RT_BIT(0)
/** @} */

/**
 * Deletes the specified symbolic link.
 *
 * This will try to refuse deleting non-symlinks, however there are usually
 * races in the implementation of this check so no guarantees can be are made.
 *
 * @returns IPRT status code.
 * @retval  VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
 *
 * @param   pszSymlink      The symbolic link that should be removed.
 * @param   fDelete         Delete flags, RTSYMLINKDELETE_FLAGS_*.
 */
RTDECL(int) RTSymlinkDelete(const char *pszSymlink, uint32_t fDelete);

/** @name RTSymlinkRead  flags.
 * @{ */
/** Don't allow symbolic links as part of the path.
 * @remarks this flag is currently not implemented and will be ignored. */
#define RTSYMLINKREAD_FLAGS_NO_SYMLINKS  RT_BIT(0)
/** @} */

/**
 * Read the symlink target.
 *
 * @returns IPRT status code.
 * @retval  VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
 * @retval  VERR_BUFFER_OVERFLOW if the link is larger than @a cbTarget.  The
 *          buffer will contain what all we managed to read, fully terminated
 *          if @a cbTarget > 0.
 *
 * @param   pszSymlink      The symbolic link that should be read.
 * @param   pszTarget       The target buffer.
 * @param   cbTarget        The size of the target buffer.
 * @param   fRead           Read flags, RTSYMLINKREAD_FLAGS_*.
 */
RTDECL(int) RTSymlinkRead(const char *pszSymlink, char *pszTarget, size_t cbTarget, uint32_t fRead);

/**
 * Read the symlink target into an API allocated buffer.
 *
 * This API eliminates the race involved in determining the right buffer size.
 *
 * @returns IPRT status code.
 * @retval  VERR_NOT_SYMLINK if @a pszSymlink does not specify a symbolic link.
 *
 * @param   pszSymlink      The symbolic link that should be read.
 * @param   ppszTarget      Where to return the target string.  Free the string
 *                          by calling RTStrFree.
 */
RTDECL(int) RTSymlinkReadA(const char *pszSymlink, char **ppszTarget);

/** @} */

RT_C_DECLS_END

#endif /* !IPRT_INCLUDED_symlink_h */