VirtualBox

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

Last change on this file since 73720 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.3 KB
Line 
1/* $Id: fs.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT - File System.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 /* writable? */
90 if (!(fMode & RTFS_DOS_READONLY))
91 fMode |= RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH;
92 return fMode;
93}
94
95
96/**
97 * Converts Unix attributes to Dos-style attributes.
98 *
99 * @returns File mode mask.
100 * @param fMode The mode mask containing dos-style attributes only.
101 * @param pszName The filename which this applies to (hidden check).
102 * @param cbName The length of that filename. (optional, set 0)
103 */
104RTFMODE rtFsModeFromUnix(RTFMODE fMode, const char *pszName, size_t cbName)
105{
106 NOREF(cbName);
107
108 fMode &= RTFS_UNIX_MASK;
109
110 if (!(fMode & (RTFS_UNIX_IWUSR | RTFS_UNIX_IWGRP | RTFS_UNIX_IWOTH)))
111 fMode |= RTFS_DOS_READONLY;
112 if (RTFS_IS_DIRECTORY(fMode))
113 fMode |= RTFS_DOS_DIRECTORY;
114 if (!(fMode & RTFS_DOS_MASK))
115 fMode |= RTFS_DOS_NT_NORMAL;
116 if (!(fMode & RTFS_DOS_HIDDEN) && pszName)
117 {
118 pszName = RTPathFilename(pszName);
119 if (pszName && *pszName == '.')
120 fMode |= RTFS_DOS_HIDDEN;
121 }
122 return fMode;
123}
124
125
126/**
127 * Normalizes the give mode mask.
128 *
129 * It will create the missing unix or dos mask from the other (one
130 * of them is required by all APIs), and guess the file type if that's
131 * missing.
132 *
133 * @returns Normalized file mode.
134 * @param fMode The mode mask that may contain a partial/incomplete mask.
135 * @param pszName The filename which this applies to (exe check).
136 * @param cbName The length of that filename. (optional, set 0)
137 */
138RTFMODE rtFsModeNormalize(RTFMODE fMode, const char *pszName, size_t cbName)
139{
140 if (!(fMode & RTFS_UNIX_MASK))
141 fMode = rtFsModeFromDos(fMode, pszName, cbName, RTFSMODE_SYMLINK_REPARSE_TAG);
142 else if (!(fMode & RTFS_DOS_MASK))
143 fMode = rtFsModeFromUnix(fMode, pszName, cbName);
144 else if (!(fMode & RTFS_TYPE_MASK))
145 fMode |= fMode & RTFS_DOS_DIRECTORY ? RTFS_TYPE_DIRECTORY : RTFS_TYPE_FILE;
146 else if (RTFS_IS_DIRECTORY(fMode))
147 fMode |= RTFS_DOS_DIRECTORY;
148 return fMode;
149}
150
151
152/**
153 * Checks if the file mode is valid or not.
154 *
155 * @return true if valid.
156 * @return false if invalid, done bitching.
157 * @param fMode The file mode.
158 */
159bool rtFsModeIsValid(RTFMODE fMode)
160{
161 AssertMsgReturn( (!RTFS_IS_DIRECTORY(fMode) && !(fMode & RTFS_DOS_DIRECTORY))
162 || (RTFS_IS_DIRECTORY(fMode) && (fMode & RTFS_DOS_DIRECTORY)),
163 ("%RTfmode\n", fMode), false);
164 AssertMsgReturn(RTFS_TYPE_MASK & fMode,
165 ("%RTfmode\n", fMode), false);
166 /** @todo more checks! */
167 return true;
168}
169
170
171/**
172 * Checks if the file mode is valid as a permission mask or not.
173 *
174 * @return true if valid.
175 * @return false if invalid, done bitching.
176 * @param fMode The file mode.
177 */
178bool rtFsModeIsValidPermissions(RTFMODE fMode)
179{
180 AssertMsgReturn( (!RTFS_IS_DIRECTORY(fMode) && !(fMode & RTFS_DOS_DIRECTORY))
181 || (RTFS_IS_DIRECTORY(fMode) && (fMode & RTFS_DOS_DIRECTORY)),
182 ("%RTfmode\n", fMode), false);
183 /** @todo more checks! */
184 return true;
185}
186
187
188RTDECL(const char *) RTFsTypeName(RTFSTYPE enmType)
189{
190 switch (enmType)
191 {
192 case RTFSTYPE_UNKNOWN: return "unknown";
193 case RTFSTYPE_UDF: return "udf";
194 case RTFSTYPE_ISO9660: return "iso9660";
195 case RTFSTYPE_FUSE: return "fuse";
196 case RTFSTYPE_VBOXSHF: return "vboxshf";
197
198 case RTFSTYPE_EXT: return "ext";
199 case RTFSTYPE_EXT2: return "ext2";
200 case RTFSTYPE_EXT3: return "ext3";
201 case RTFSTYPE_EXT4: return "ext4";
202 case RTFSTYPE_XFS: return "xfs";
203 case RTFSTYPE_CIFS: return "cifs";
204 case RTFSTYPE_SMBFS: return "smbfs";
205 case RTFSTYPE_TMPFS: return "tmpfs";
206 case RTFSTYPE_SYSFS: return "sysfs";
207 case RTFSTYPE_PROC: return "proc";
208 case RTFSTYPE_OCFS2: return "ocfs2";
209 case RTFSTYPE_BTRFS: return "btrfs";
210
211 case RTFSTYPE_NTFS: return "ntfs";
212 case RTFSTYPE_FAT: return "fat";
213 case RTFSTYPE_EXFAT: return "exfat";
214
215 case RTFSTYPE_ZFS: return "zfs";
216 case RTFSTYPE_UFS: return "ufs";
217 case RTFSTYPE_NFS: return "nfs";
218
219 case RTFSTYPE_HFS: return "hfs";
220 case RTFSTYPE_AUTOFS: return "autofs";
221 case RTFSTYPE_DEVFS: return "devfs";
222
223 case RTFSTYPE_HPFS: return "hpfs";
224 case RTFSTYPE_JFS: return "jfs";
225
226 case RTFSTYPE_END: return "end";
227 case RTFSTYPE_32BIT_HACK: break;
228 }
229
230 /* Don't put this in as 'default:', we wish GCC to warn about missing cases. */
231 static char s_asz[4][64];
232 static uint32_t volatile s_i = 0;
233 uint32_t i = ASMAtomicIncU32(&s_i) % RT_ELEMENTS(s_asz);
234 RTStrPrintf(s_asz[i], sizeof(s_asz[i]), "type=%d", enmType);
235 return s_asz[i];
236}
237
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