VirtualBox

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

Last change on this file since 78403 was 78205, checked in by vboxsync, 6 years ago

IPRT/rtFsModeFromDos: Ignore read-only DOS attribute on directories. ticketref:18345 bugref:9172

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