VirtualBox

source: kBuild/trunk/src/lib/nt/ntunlink.c@ 2998

Last change on this file since 2998 was 2997, checked in by bird, 8 years ago

rm.c: Use fts_dirfd on windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: ntunlink.c 2997 2016-11-01 23:28:02Z bird $ */
2/** @file
3 * MSC + NT unlink and variations.
4 */
5
6/*
7 * Copyright (c) 2005-2013 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 *
27 * Alternatively, the content of this file may be used under the terms of the
28 * GPL version 2 or later, or LGPL version 2.1 or later.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <stdio.h>
36#include <errno.h>
37#include <malloc.h>
38
39#include "ntstuff.h"
40#include "nthlp.h"
41
42
43static MY_NTSTATUS birdMakeWritable(MY_UNICODE_STRING *pNtPath)
44{
45 MY_NTSTATUS rcNt;
46 HANDLE hFile;
47
48 rcNt = birdOpenFileUniStr(NULL /*hRoot*/,
49 pNtPath,
50 FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
51 FILE_ATTRIBUTE_NORMAL,
52 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
53 FILE_OPEN,
54 FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
55 OBJ_CASE_INSENSITIVE,
56 &hFile);
57 if (MY_NT_SUCCESS(rcNt))
58 {
59 MY_FILE_BASIC_INFORMATION BasicInfo;
60 MY_IO_STATUS_BLOCK Ios;
61 DWORD dwAttr;
62
63 Ios.Information = -1;
64 Ios.u.Status = -1;
65 rcNt = g_pfnNtQueryInformationFile(hFile, &Ios, &BasicInfo, sizeof(BasicInfo), MyFileBasicInformation);
66
67 if (MY_NT_SUCCESS(rcNt) && MY_NT_SUCCESS(Ios.u.Status))
68 dwAttr = BasicInfo.FileAttributes & ~FILE_ATTRIBUTE_READONLY;
69 else
70 dwAttr = FILE_ATTRIBUTE_NORMAL;
71 memset(&BasicInfo, 0, sizeof(BasicInfo));
72 BasicInfo.FileAttributes = dwAttr;
73
74 Ios.Information = -1;
75 Ios.u.Status = -1;
76 rcNt = g_pfnNtSetInformationFile(hFile, &Ios, &BasicInfo, sizeof(BasicInfo), MyFileBasicInformation);
77
78 birdCloseFile(hFile);
79 }
80
81 return rcNt;
82}
83
84
85static int birdUnlinkInternal(HANDLE hRoot, const char *pszFile, int fReadOnlyToo, int fFast)
86{
87 MY_UNICODE_STRING NtPath;
88 int rc;
89
90 if (hRoot == INVALID_HANDLE_VALUE)
91 hRoot = NULL;
92 if (hRoot == NULL)
93 rc = birdDosToNtPath(pszFile, &NtPath);
94 else
95 rc = birdDosToRelativeNtPath(pszFile, &NtPath);
96 if (rc == 0)
97 {
98 MY_NTSTATUS rcNt;
99 if (fFast)
100 {
101 /* This uses FILE_DELETE_ON_CLOSE. Probably only suitable when in a hurry... */
102 MY_OBJECT_ATTRIBUTES ObjAttr;
103 MyInitializeObjectAttributes(&ObjAttr, &NtPath, OBJ_CASE_INSENSITIVE, hRoot, NULL /*pSecAttr*/);
104 rcNt = g_pfnNtDeleteFile(&ObjAttr);
105
106 /* In case some file system does things differently than NTFS. */
107 if (rcNt == STATUS_CANNOT_DELETE)
108 {
109 birdMakeWritable(&NtPath);
110 rcNt = g_pfnNtDeleteFile(&ObjAttr);
111 }
112 }
113 else
114 {
115 /* Use the set information stuff. Probably more reliable. */
116 HANDLE hFile;
117 int fMayTryAgain = 1;
118 for (;;)
119 {
120 rcNt = birdOpenFileUniStr(hRoot,
121 &NtPath,
122 DELETE,
123 FILE_ATTRIBUTE_NORMAL,
124 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
125 FILE_OPEN,
126 FILE_OPEN_FOR_BACKUP_INTENT | FILE_OPEN_REPARSE_POINT,
127 OBJ_CASE_INSENSITIVE,
128 &hFile);
129 if (MY_NT_SUCCESS(rcNt))
130 {
131 MY_FILE_DISPOSITION_INFORMATION DispInfo;
132 MY_IO_STATUS_BLOCK Ios;
133
134 DispInfo.DeleteFile = TRUE;
135
136 Ios.Information = -1;
137 Ios.u.Status = -1;
138
139 rcNt = g_pfnNtSetInformationFile(hFile, &Ios, &DispInfo, sizeof(DispInfo), MyFileDispositionInformation);
140
141 birdCloseFile(hFile);
142 }
143 if (rcNt != STATUS_CANNOT_DELETE || !fMayTryAgain)
144 break;
145
146 fMayTryAgain = 0;
147 birdMakeWritable(&NtPath);
148 }
149 }
150
151 birdFreeNtPath(&NtPath);
152
153 if (MY_NT_SUCCESS(rcNt))
154 rc = 0;
155 else
156 rc = birdSetErrnoFromNt(rcNt);
157 }
158 return rc;
159}
160
161
162int birdUnlink(const char *pszFile)
163{
164 return birdUnlinkInternal(NULL /*hRoot*/, pszFile, 0 /*fReadOnlyToo*/, 0 /*fFast*/);
165}
166
167
168int birdUnlinkEx(void *hRoot, const char *pszFile)
169{
170 return birdUnlinkInternal((HANDLE)hRoot, pszFile, 0 /*fReadOnlyToo*/, 0 /*fFast*/);
171}
172
173
174int birdUnlinkForced(const char *pszFile)
175{
176 return birdUnlinkInternal(NULL /*hRoot*/, pszFile, 1 /*fReadOnlyToo*/, 0 /*fFast*/);
177}
178
179
180int birdUnlinkForcedEx(void *hRoot, const char *pszFile)
181{
182 return birdUnlinkInternal((HANDLE)hRoot, pszFile, 1 /*fReadOnlyToo*/, 0 /*fFast*/);
183}
184
185
186int birdUnlinkForcedFast(const char *pszFile)
187{
188 return birdUnlinkInternal(NULL /*hRoot*/, pszFile, 1 /*fReadOnlyToo*/, 1 /*fFast*/);
189}
190
191
192int birdUnlinkForcedFastEx(void *hRoot, const char *pszFile)
193{
194 return birdUnlinkInternal((HANDLE)hRoot, pszFile, 1 /*fReadOnlyToo*/, 1 /*fFast*/);
195}
196
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette