[1] | 1 | /* $Id: fs.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - File System.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #include <iprt/fs.h>
|
---|
[30365] | 42 | #include "internal/iprt.h"
|
---|
| 43 |
|
---|
| 44 | #include <iprt/asm.h>
|
---|
[1] | 45 | #include <iprt/assert.h>
|
---|
[30365] | 46 | #include <iprt/ctype.h>
|
---|
| 47 | #include <iprt/path.h>
|
---|
| 48 | #include <iprt/string.h>
|
---|
[1] | 49 | #include <iprt/time.h>
|
---|
| 50 | #include "internal/fs.h"
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Converts dos-style attributes to Unix attributes.
|
---|
| 55 | *
|
---|
[79155] | 56 | * @returns Normalized mode mask.
|
---|
[33540] | 57 | * @param fMode The mode mask containing dos-style attributes only.
|
---|
[1] | 58 | * @param pszName The filename which this applies to (exe check).
|
---|
| 59 | * @param cbName The length of that filename. (optional, set 0)
|
---|
[64620] | 60 | * @param uReparseTag The reparse tag if RTFS_DOS_NT_REPARSE_POINT is set.
|
---|
[79155] | 61 | * @param fType RTFS_TYPE_XXX to normalize against, 0 if not known.
|
---|
[1] | 62 | */
|
---|
[79155] | 63 | RTFMODE rtFsModeFromDos(RTFMODE fMode, const char *pszName, size_t cbName, uint32_t uReparseTag, RTFMODE fType)
|
---|
[1] | 64 | {
|
---|
[79155] | 65 | Assert(!(fType & ~RTFS_TYPE_MASK));
|
---|
| 66 |
|
---|
[1] | 67 | fMode &= ~((1 << RTFS_DOS_SHIFT) - 1);
|
---|
| 68 |
|
---|
[79155] | 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. */
|
---|
[1] | 74 | fMode |= RTFS_UNIX_IRUSR | RTFS_UNIX_IRGRP | RTFS_UNIX_IROTH;
|
---|
| 75 | if (fMode & RTFS_DOS_DIRECTORY)
|
---|
[79155] | 76 | /* Directories are executable. */
|
---|
[1] | 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];
|
---|
[24006] | 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]);
|
---|
[1] | 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 | }
|
---|
[33437] | 101 |
|
---|
| 102 | /* Is it really a symbolic link? */
|
---|
[64620] | 103 | if ((fMode & RTFS_DOS_NT_REPARSE_POINT) && uReparseTag == RTFSMODE_SYMLINK_REPARSE_TAG)
|
---|
[33437] | 104 | fMode = (fMode & ~RTFS_TYPE_MASK) | RTFS_TYPE_SYMLINK;
|
---|
| 105 |
|
---|
[78205] | 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)
|
---|
[1] | 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 | *
|
---|
[7169] | 123 | * @returns File mode mask.
|
---|
[33540] | 124 | * @param fMode The mode mask containing dos-style attributes only.
|
---|
[7169] | 125 | * @param pszName The filename which this applies to (hidden check).
|
---|
| 126 | * @param cbName The length of that filename. (optional, set 0)
|
---|
[79155] | 127 | * @param fType RTFS_TYPE_XXX to normalize against, 0 if not known.
|
---|
[1] | 128 | */
|
---|
[79155] | 129 | RTFMODE rtFsModeFromUnix(RTFMODE fMode, const char *pszName, size_t cbName, RTFMODE fType)
|
---|
[1] | 130 | {
|
---|
[79155] | 131 | Assert(!(fType & ~RTFS_TYPE_MASK));
|
---|
[39083] | 132 | NOREF(cbName);
|
---|
| 133 |
|
---|
[1] | 134 | fMode &= RTFS_UNIX_MASK;
|
---|
| 135 |
|
---|
[79155] | 136 | if (!(fType & RTFS_TYPE_MASK) && fType)
|
---|
| 137 | fMode |= fType;
|
---|
| 138 |
|
---|
[1] | 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;
|
---|
[2933] | 145 | if (!(fMode & RTFS_DOS_HIDDEN) && pszName)
|
---|
| 146 | {
|
---|
| 147 | pszName = RTPathFilename(pszName);
|
---|
[79293] | 148 | if ( pszName
|
---|
| 149 | && pszName[0] == '.'
|
---|
| 150 | && pszName[1] != '\0' /* exclude "." */
|
---|
| 151 | && (pszName[1] != '.' || pszName[2] != '\0')) /* exclude ".." */
|
---|
[2933] | 152 | fMode |= RTFS_DOS_HIDDEN;
|
---|
| 153 | }
|
---|
[1] | 154 | return fMode;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
[9786] | 159 | * Normalizes the give mode mask.
|
---|
[1] | 160 | *
|
---|
[9786] | 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 | *
|
---|
[1] | 165 | * @returns Normalized file mode.
|
---|
[9786] | 166 | * @param fMode The mode mask that may contain a partial/incomplete mask.
|
---|
[1] | 167 | * @param pszName The filename which this applies to (exe check).
|
---|
| 168 | * @param cbName The length of that filename. (optional, set 0)
|
---|
[79155] | 169 | * @param fType RTFS_TYPE_XXX to normalize against, 0 if not known.
|
---|
[1] | 170 | */
|
---|
[79155] | 171 | RTFMODE rtFsModeNormalize(RTFMODE fMode, const char *pszName, size_t cbName, RTFMODE fType)
|
---|
[1] | 172 | {
|
---|
[79155] | 173 | Assert(!(fType & ~RTFS_TYPE_MASK));
|
---|
| 174 |
|
---|
[1] | 175 | if (!(fMode & RTFS_UNIX_MASK))
|
---|
[79155] | 176 | fMode = rtFsModeFromDos(fMode, pszName, cbName, RTFSMODE_SYMLINK_REPARSE_TAG, fType);
|
---|
[1] | 177 | else if (!(fMode & RTFS_DOS_MASK))
|
---|
[79155] | 178 | fMode = rtFsModeFromUnix(fMode, pszName, cbName, fType);
|
---|
[1] | 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 | */
|
---|
| 194 | bool 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 | */
|
---|
| 213 | bool 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 |
|
---|
[30365] | 223 | RTDECL(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";
|
---|
[43046] | 243 | case RTFSTYPE_OCFS2: return "ocfs2";
|
---|
[47071] | 244 | case RTFSTYPE_BTRFS: return "btrfs";
|
---|
[30365] | 245 |
|
---|
| 246 | case RTFSTYPE_NTFS: return "ntfs";
|
---|
| 247 | case RTFSTYPE_FAT: return "fat";
|
---|
[67732] | 248 | case RTFSTYPE_EXFAT: return "exfat";
|
---|
[77233] | 249 | case RTFSTYPE_REFS: return "refs";
|
---|
[30365] | 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";
|
---|
[77233] | 256 | case RTFSTYPE_APFS: return "apfs";
|
---|
[30365] | 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 |
|
---|