VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/fs.cpp@ 98103

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

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.8 KB
Line 
1/* $Id: fs.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - File System.
4 */
5
6/*
7 * Copyright (C) 2006-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#include <iprt/fs.h>
42#include "internal/iprt.h"
43
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/ctype.h>
47#include <iprt/path.h>
48#include <iprt/string.h>
49#include <iprt/time.h>
50#include "internal/fs.h"
51
52
53/**
54 * Converts dos-style attributes to Unix attributes.
55 *
56 * @returns Normalized mode mask.
57 * @param fMode The mode mask containing dos-style attributes only.
58 * @param pszName The filename which this applies to (exe check).
59 * @param cbName The length of that filename. (optional, set 0)
60 * @param uReparseTag The reparse tag if RTFS_DOS_NT_REPARSE_POINT is set.
61 * @param fType RTFS_TYPE_XXX to normalize against, 0 if not known.
62 */
63RTFMODE rtFsModeFromDos(RTFMODE fMode, const char *pszName, size_t cbName, uint32_t uReparseTag, RTFMODE fType)
64{
65 Assert(!(fType & ~RTFS_TYPE_MASK));
66
67 fMode &= ~((1 << RTFS_DOS_SHIFT) - 1);
68
69 /* Forcibly set the directory attribute if caller desires it. */
70 if (fType == RTFS_TYPE_DIRECTORY)
71 fMode |= RTFS_DOS_DIRECTORY;
72
73 /* Everything is readable. */
74 fMode |= RTFS_UNIX_IRUSR | RTFS_UNIX_IRGRP | RTFS_UNIX_IROTH;
75 if (fMode & RTFS_DOS_DIRECTORY)
76 /* Directories are executable. */
77 fMode |= RTFS_TYPE_DIRECTORY | RTFS_UNIX_IXUSR | RTFS_UNIX_IXGRP | RTFS_UNIX_IXOTH;
78 else
79 {
80 fMode |= RTFS_TYPE_FILE;
81 if (!cbName && pszName)
82 cbName = strlen(pszName);
83 if (cbName >= 4 && pszName[cbName - 4] == '.')
84 {
85 /* check for executable extension. */
86 const char *pszExt = &pszName[cbName - 3];
87 char szExt[4];
88 szExt[0] = RT_C_TO_LOWER(pszExt[0]);
89 szExt[1] = RT_C_TO_LOWER(pszExt[1]);
90 szExt[2] = RT_C_TO_LOWER(pszExt[2]);
91 szExt[3] = '\0';
92 if ( !memcmp(szExt, "exe", 4)
93 || !memcmp(szExt, "bat", 4)
94 || !memcmp(szExt, "com", 4)
95 || !memcmp(szExt, "cmd", 4)
96 || !memcmp(szExt, "btm", 4)
97 )
98 fMode |= RTFS_UNIX_IXUSR | RTFS_UNIX_IXGRP | RTFS_UNIX_IXOTH;
99 }
100 }
101
102 /* Is it really a symbolic link? */
103 if ((fMode & RTFS_DOS_NT_REPARSE_POINT) && uReparseTag == RTFSMODE_SYMLINK_REPARSE_TAG)
104 fMode = (fMode & ~RTFS_TYPE_MASK) | RTFS_TYPE_SYMLINK;
105
106 /*
107 * Writable?
108 *
109 * Note! We ignore the read-only flag on directories as windows seems to
110 * use it for purposes other than writability (@ticketref{18345}):
111 * https://support.microsoft.com/en-gb/help/326549/you-cannot-view-or-change-the-read-only-or-the-system-attributes-of-fo
112 *
113 */
114 if ((fMode & (RTFS_DOS_DIRECTORY | RTFS_DOS_READONLY)) != RTFS_DOS_READONLY)
115 fMode |= RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH;
116 return fMode;
117}
118
119
120/**
121 * Converts Unix attributes to Dos-style attributes.
122 *
123 * @returns File mode mask.
124 * @param fMode The mode mask containing dos-style attributes only.
125 * @param pszName The filename which this applies to (hidden check).
126 * @param cbName The length of that filename. (optional, set 0)
127 * @param fType RTFS_TYPE_XXX to normalize against, 0 if not known.
128 */
129RTFMODE rtFsModeFromUnix(RTFMODE fMode, const char *pszName, size_t cbName, RTFMODE fType)
130{
131 Assert(!(fType & ~RTFS_TYPE_MASK));
132 NOREF(cbName);
133
134 fMode &= RTFS_UNIX_MASK;
135
136 if (!(fType & RTFS_TYPE_MASK) && fType)
137 fMode |= fType;
138
139 if (!(fMode & (RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH)))
140 fMode |= RTFS_DOS_READONLY;
141 if (RTFS_IS_DIRECTORY(fMode))
142 fMode |= RTFS_DOS_DIRECTORY;
143 if (!(fMode & RTFS_DOS_MASK))
144 fMode |= RTFS_DOS_NT_NORMAL;
145 if (!(fMode & RTFS_DOS_HIDDEN) && pszName)
146 {
147 pszName = RTPathFilename(pszName);
148 if ( pszName
149 && pszName[0] == '.'
150 && pszName[1] != '\0' /* exclude "." */
151 && (pszName[1] != '.' || pszName[2] != '\0')) /* exclude ".." */
152 fMode |= RTFS_DOS_HIDDEN;
153 }
154 return fMode;
155}
156
157
158/**
159 * Normalizes the give mode mask.
160 *
161 * It will create the missing unix or dos mask from the other (one
162 * of them is required by all APIs), and guess the file type if that's
163 * missing.
164 *
165 * @returns Normalized file mode.
166 * @param fMode The mode mask that may contain a partial/incomplete mask.
167 * @param pszName The filename which this applies to (exe check).
168 * @param cbName The length of that filename. (optional, set 0)
169 * @param fType RTFS_TYPE_XXX to normalize against, 0 if not known.
170 */
171RTFMODE rtFsModeNormalize(RTFMODE fMode, const char *pszName, size_t cbName, RTFMODE fType)
172{
173 Assert(!(fType & ~RTFS_TYPE_MASK));
174
175 if (!(fMode & RTFS_UNIX_MASK))
176 fMode = rtFsModeFromDos(fMode, pszName, cbName, RTFSMODE_SYMLINK_REPARSE_TAG, fType);
177 else if (!(fMode & RTFS_DOS_MASK))
178 fMode = rtFsModeFromUnix(fMode, pszName, cbName, fType);
179 else if (!(fMode & RTFS_TYPE_MASK))
180 fMode |= fMode & RTFS_DOS_DIRECTORY ? RTFS_TYPE_DIRECTORY : RTFS_TYPE_FILE;
181 else if (RTFS_IS_DIRECTORY(fMode))
182 fMode |= RTFS_DOS_DIRECTORY;
183 return fMode;
184}
185
186
187/**
188 * Checks if the file mode is valid or not.
189 *
190 * @return true if valid.
191 * @return false if invalid, done bitching.
192 * @param fMode The file mode.
193 */
194bool rtFsModeIsValid(RTFMODE fMode)
195{
196 AssertMsgReturn( (!RTFS_IS_DIRECTORY(fMode) && !(fMode & RTFS_DOS_DIRECTORY))
197 || (RTFS_IS_DIRECTORY(fMode) && (fMode & RTFS_DOS_DIRECTORY)),
198 ("%RTfmode\n", fMode), false);
199 AssertMsgReturn(RTFS_TYPE_MASK & fMode,
200 ("%RTfmode\n", fMode), false);
201 /** @todo more checks! */
202 return true;
203}
204
205
206/**
207 * Checks if the file mode is valid as a permission mask or not.
208 *
209 * @return true if valid.
210 * @return false if invalid, done bitching.
211 * @param fMode The file mode.
212 */
213bool rtFsModeIsValidPermissions(RTFMODE fMode)
214{
215 AssertMsgReturn( (!RTFS_IS_DIRECTORY(fMode) && !(fMode & RTFS_DOS_DIRECTORY))
216 || (RTFS_IS_DIRECTORY(fMode) && (fMode & RTFS_DOS_DIRECTORY)),
217 ("%RTfmode\n", fMode), false);
218 /** @todo more checks! */
219 return true;
220}
221
222
223RTDECL(const char *) RTFsTypeName(RTFSTYPE enmType)
224{
225 switch (enmType)
226 {
227 case RTFSTYPE_UNKNOWN: return "unknown";
228 case RTFSTYPE_UDF: return "udf";
229 case RTFSTYPE_ISO9660: return "iso9660";
230 case RTFSTYPE_FUSE: return "fuse";
231 case RTFSTYPE_VBOXSHF: return "vboxshf";
232
233 case RTFSTYPE_EXT: return "ext";
234 case RTFSTYPE_EXT2: return "ext2";
235 case RTFSTYPE_EXT3: return "ext3";
236 case RTFSTYPE_EXT4: return "ext4";
237 case RTFSTYPE_XFS: return "xfs";
238 case RTFSTYPE_CIFS: return "cifs";
239 case RTFSTYPE_SMBFS: return "smbfs";
240 case RTFSTYPE_TMPFS: return "tmpfs";
241 case RTFSTYPE_SYSFS: return "sysfs";
242 case RTFSTYPE_PROC: return "proc";
243 case RTFSTYPE_OCFS2: return "ocfs2";
244 case RTFSTYPE_BTRFS: return "btrfs";
245
246 case RTFSTYPE_NTFS: return "ntfs";
247 case RTFSTYPE_FAT: return "fat";
248 case RTFSTYPE_EXFAT: return "exfat";
249 case RTFSTYPE_REFS: return "refs";
250
251 case RTFSTYPE_ZFS: return "zfs";
252 case RTFSTYPE_UFS: return "ufs";
253 case RTFSTYPE_NFS: return "nfs";
254
255 case RTFSTYPE_HFS: return "hfs";
256 case RTFSTYPE_APFS: return "apfs";
257 case RTFSTYPE_AUTOFS: return "autofs";
258 case RTFSTYPE_DEVFS: return "devfs";
259
260 case RTFSTYPE_HPFS: return "hpfs";
261 case RTFSTYPE_JFS: return "jfs";
262
263 case RTFSTYPE_END: return "end";
264 case RTFSTYPE_32BIT_HACK: break;
265 }
266
267 /* Don't put this in as 'default:', we wish GCC to warn about missing cases. */
268 static char s_asz[4][64];
269 static uint32_t volatile s_i = 0;
270 uint32_t i = ASMAtomicIncU32(&s_i) % RT_ELEMENTS(s_asz);
271 RTStrPrintf(s_asz[i], sizeof(s_asz[i]), "type=%d", enmType);
272 return s_asz[i];
273}
274
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette