1 | /* $Id: DnDPath.cpp 85028 2020-07-01 14:37:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DnD - Path handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_GUEST_DND
|
---|
23 | #include <VBox/GuestHost/DragAndDrop.h>
|
---|
24 |
|
---|
25 | #include <iprt/dir.h>
|
---|
26 | #include <iprt/err.h>
|
---|
27 | #include <iprt/file.h>
|
---|
28 | #include <iprt/path.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Sanitizes a path so that unsupported characters will be replaced by an underscore ("_").
|
---|
34 | *
|
---|
35 | * @return IPRT status code.
|
---|
36 | * @param pszPath Path to sanitize.
|
---|
37 | * @param cbPath Size (in bytes) of path to sanitize.
|
---|
38 | */
|
---|
39 | int DnDPathSanitize(char *pszPath, size_t cbPath)
|
---|
40 | {
|
---|
41 | if (!pszPath) /* No path given? Bail out early. */
|
---|
42 | return VINF_SUCCESS;
|
---|
43 |
|
---|
44 | AssertReturn(cbPath, VERR_INVALID_PARAMETER);
|
---|
45 |
|
---|
46 | int rc = VINF_SUCCESS;
|
---|
47 | #ifdef RT_OS_WINDOWS
|
---|
48 | RT_NOREF1(cbPath);
|
---|
49 | /* Replace out characters not allowed on Windows platforms, put in by RTTimeSpecToString(). */
|
---|
50 | /** @todo Use something like RTPathSanitize() if available later some time. */
|
---|
51 | static const RTUNICP s_uszValidRangePairs[] =
|
---|
52 | {
|
---|
53 | ' ', ' ',
|
---|
54 | '(', ')',
|
---|
55 | '-', '.',
|
---|
56 | '0', '9',
|
---|
57 | 'A', 'Z',
|
---|
58 | 'a', 'z',
|
---|
59 | '_', '_',
|
---|
60 | 0xa0, 0xd7af,
|
---|
61 | '\0'
|
---|
62 | };
|
---|
63 |
|
---|
64 | ssize_t cReplaced = RTStrPurgeComplementSet(pszPath, s_uszValidRangePairs, '_' /* chReplacement */);
|
---|
65 | if (cReplaced < 0)
|
---|
66 | rc = VERR_INVALID_UTF8_ENCODING;
|
---|
67 | #else
|
---|
68 | RT_NOREF2(pszPath, cbPath);
|
---|
69 | #endif
|
---|
70 | return rc;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Validates whether a given path matches our set of rules or not.
|
---|
75 | *
|
---|
76 | * @returns VBox status code.
|
---|
77 | * @param pcszPath Path to validate.
|
---|
78 | * @param fMustExist Whether the path to validate also must exist.
|
---|
79 | * @sa shClTransferValidatePath().
|
---|
80 | */
|
---|
81 | int DnDPathValidate(const char *pcszPath, bool fMustExist)
|
---|
82 | {
|
---|
83 | int rc = VINF_SUCCESS;
|
---|
84 |
|
---|
85 | if (!strlen(pcszPath))
|
---|
86 | rc = VERR_INVALID_PARAMETER;
|
---|
87 |
|
---|
88 | if ( RT_SUCCESS(rc)
|
---|
89 | && !RTStrIsValidEncoding(pcszPath))
|
---|
90 | {
|
---|
91 | rc = VERR_INVALID_UTF8_ENCODING;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if ( RT_SUCCESS(rc)
|
---|
95 | && RTStrStr(pcszPath, ".."))
|
---|
96 | {
|
---|
97 | rc = VERR_INVALID_PARAMETER;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if ( RT_SUCCESS(rc)
|
---|
101 | && fMustExist)
|
---|
102 | {
|
---|
103 | RTFSOBJINFO objInfo;
|
---|
104 | rc = RTPathQueryInfo(pcszPath, &objInfo, RTFSOBJATTRADD_NOTHING);
|
---|
105 | if (RT_SUCCESS(rc))
|
---|
106 | {
|
---|
107 | if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
|
---|
108 | {
|
---|
109 | if (!RTDirExists(pcszPath)) /* Path must exist. */
|
---|
110 | rc = VERR_PATH_NOT_FOUND;
|
---|
111 | }
|
---|
112 | else if (RTFS_IS_FILE(objInfo.Attr.fMode))
|
---|
113 | {
|
---|
114 | if (!RTFileExists(pcszPath)) /* File must exist. */
|
---|
115 | rc = VERR_FILE_NOT_FOUND;
|
---|
116 | }
|
---|
117 | else /* Everything else (e.g. symbolic links) are not supported. */
|
---|
118 | rc = VERR_NOT_SUPPORTED;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Converts a DnD path.
|
---|
127 | *
|
---|
128 | * @returns VBox status code.
|
---|
129 | * @param pszPath Path to convert.
|
---|
130 | * @param cbPath Size (in bytes) of path to convert.
|
---|
131 | * @param fFlags Conversion flags of type DNDPATHCONVERT_FLAGS_.
|
---|
132 | */
|
---|
133 | int DnDPathConvert(char *pszPath, size_t cbPath, DNDPATHCONVERTFLAGS fFlags)
|
---|
134 | {
|
---|
135 | RT_NOREF(cbPath);
|
---|
136 | AssertReturn(!(fFlags & ~DNDPATHCONVERT_FLAGS_VALID_MASK), VERR_INVALID_FLAGS);
|
---|
137 |
|
---|
138 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
139 | if (fFlags & DNDPATHCONVERT_FLAGS_TO_NATIVE)
|
---|
140 | RTPathChangeToDosSlashes(pszPath, true);
|
---|
141 | else
|
---|
142 | #else
|
---|
143 | RT_NOREF(fFlags);
|
---|
144 | #endif
|
---|
145 | {
|
---|
146 | RTPathChangeToUnixSlashes(pszPath, true /* fForce */);
|
---|
147 | }
|
---|
148 |
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | }
|
---|
151 |
|
---|