VirtualBox

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

Last change on this file since 49498 was 48935, checked in by vboxsync, 11 years ago

Runtime: Whitespace and svn:keyword cleanups by scm.

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