1 | /* $Id: dir-win.cpp 62592 2016-07-27 13:24:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Directory, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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 | #define LOG_GROUP RTLOGGROUP_DIR
|
---|
32 | #include <iprt/win/windows.h>
|
---|
33 |
|
---|
34 | #include <iprt/dir.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/file.h>
|
---|
41 | #include <iprt/log.h>
|
---|
42 | #include "internal/fs.h"
|
---|
43 | #include "internal/path.h"
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | RTDECL(int) RTDirCreate(const char *pszPath, RTFMODE fMode, uint32_t fCreate)
|
---|
48 | {
|
---|
49 | /*
|
---|
50 | * Validate the file mode.
|
---|
51 | */
|
---|
52 | int rc;
|
---|
53 | fMode = rtFsModeNormalize(fMode, pszPath, 0);
|
---|
54 | if (rtFsModeIsValidPermissions(fMode))
|
---|
55 | {
|
---|
56 | /*
|
---|
57 | * Convert to UTF-16.
|
---|
58 | */
|
---|
59 | PRTUTF16 pwszString;
|
---|
60 | rc = RTStrToUtf16(pszPath, &pwszString);
|
---|
61 | AssertRC(rc);
|
---|
62 | if (RT_SUCCESS(rc))
|
---|
63 | {
|
---|
64 | /*
|
---|
65 | * Create the directory.
|
---|
66 | */
|
---|
67 | if (CreateDirectoryW((LPCWSTR)pwszString, NULL))
|
---|
68 | rc = VINF_SUCCESS;
|
---|
69 | else
|
---|
70 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Turn off indexing of directory through Windows Indexing Service
|
---|
74 | */
|
---|
75 | /** @todo This FILE_ATTRIBUTE_NOT_CONTENT_INDEXED hack (for .VDI files,
|
---|
76 | * really) may cause failures on samba shares. That really sweet and
|
---|
77 | * need to be addressed differently. We shouldn't be doing this
|
---|
78 | * unless the caller actually asks for it, must less returning failure,
|
---|
79 | * for crying out loud! This is only important a couple of places in
|
---|
80 | * main, if important is the right way to put it... */
|
---|
81 | if ( RT_SUCCESS(rc)
|
---|
82 | && !(fCreate & RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET))
|
---|
83 | {
|
---|
84 | if ( SetFileAttributesW((LPCWSTR)pwszString, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
|
---|
85 | || (fCreate & RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL) )
|
---|
86 | rc = VINF_SUCCESS;
|
---|
87 | else
|
---|
88 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
89 | }
|
---|
90 |
|
---|
91 | RTUtf16Free(pwszString);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | else
|
---|
95 | {
|
---|
96 | AssertMsgFailed(("Invalid file mode! %RTfmode\n", fMode));
|
---|
97 | rc = VERR_INVALID_FMODE;
|
---|
98 | }
|
---|
99 |
|
---|
100 | LogFlow(("RTDirCreate(%p:{%s}, %RTfmode): returns %Rrc\n", pszPath, pszPath, fMode, rc));
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | RTDECL(int) RTDirRemove(const char *pszPath)
|
---|
106 | {
|
---|
107 | /*
|
---|
108 | * Convert to UTF-16.
|
---|
109 | */
|
---|
110 | PRTUTF16 pwszString;
|
---|
111 | int rc = RTStrToUtf16(pszPath, &pwszString);
|
---|
112 | AssertRC(rc);
|
---|
113 | if (RT_SUCCESS(rc))
|
---|
114 | {
|
---|
115 | /*
|
---|
116 | * Remove the directory.
|
---|
117 | */
|
---|
118 | if (RemoveDirectoryW((LPCWSTR)pwszString))
|
---|
119 | rc = VINF_SUCCESS;
|
---|
120 | else
|
---|
121 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
122 |
|
---|
123 | RTUtf16Free(pwszString);
|
---|
124 | }
|
---|
125 |
|
---|
126 | LogFlow(("RTDirRemove(%p:{%s}): returns %Rrc\n", pszPath, pszPath, rc));
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(int) RTDirFlush(const char *pszPath)
|
---|
132 | {
|
---|
133 | RT_NOREF_PV(pszPath);
|
---|
134 | return VERR_NOT_SUPPORTED;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
139 | {
|
---|
140 | /*
|
---|
141 | * Validate input.
|
---|
142 | */
|
---|
143 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
144 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
145 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
146 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
147 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Call the worker.
|
---|
151 | */
|
---|
152 | int rc = rtPathWin32MoveRename(pszSrc, pszDst,
|
---|
153 | fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0,
|
---|
154 | RTFS_TYPE_DIRECTORY);
|
---|
155 |
|
---|
156 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n", pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
157 | return rc;
|
---|
158 | }
|
---|
159 |
|
---|