VirtualBox

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

Last change on this file since 95685 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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