VirtualBox

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

Last change on this file since 2940 was 2713, checked in by bird, 11 years ago

Some unlink(), rmdir() and kmk_rm optimizations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/* $Id: ntunlink.c 2713 2013-11-21 21:11:00Z bird $ */
2/** @file
3 * MSC + NT unlink and variations.
4 */
5
6/*
7 * Copyright (c) 2005-2013 knut st. osmundsen <[email protected]>
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(pNtPath,
49 FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
50 FILE_ATTRIBUTE_NORMAL,
51 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
52 FILE_OPEN,
53 FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
54 OBJ_CASE_INSENSITIVE,
55 &hFile);
56 if (MY_NT_SUCCESS(rcNt))
57 {
58 MY_FILE_BASIC_INFORMATION BasicInfo;
59 MY_IO_STATUS_BLOCK Ios;
60 DWORD dwAttr;
61
62 Ios.Information = -1;
63 Ios.u.Status = -1;
64 rcNt = g_pfnNtQueryInformationFile(hFile, &Ios, &BasicInfo, sizeof(BasicInfo), MyFileBasicInformation);
65
66 if (MY_NT_SUCCESS(rcNt) && MY_NT_SUCCESS(Ios.u.Status))
67 dwAttr = BasicInfo.FileAttributes & ~FILE_ATTRIBUTE_READONLY;
68 else
69 dwAttr = FILE_ATTRIBUTE_NORMAL;
70 memset(&BasicInfo, 0, sizeof(BasicInfo));
71 BasicInfo.FileAttributes = dwAttr;
72
73 Ios.Information = -1;
74 Ios.u.Status = -1;
75 rcNt = g_pfnNtSetInformationFile(hFile, &Ios, &BasicInfo, sizeof(BasicInfo), MyFileBasicInformation);
76
77 birdCloseFile(hFile);
78 }
79
80 return rcNt;
81}
82
83
84static int birdUnlinkInternal(const char *pszFile, int fReadOnlyToo, int fFast)
85{
86 MY_UNICODE_STRING NtPath;
87 int rc;
88
89 rc = birdDosToNtPath(pszFile, &NtPath);
90 if (rc == 0)
91 {
92 MY_NTSTATUS rcNt;
93 if (fFast)
94 {
95 /* This uses FILE_DELETE_ON_CLOSE. Probably only suitable when in a hurry... */
96 MY_OBJECT_ATTRIBUTES ObjAttr;
97 MyInitializeObjectAttributes(&ObjAttr, &NtPath, OBJ_CASE_INSENSITIVE, NULL /*hRoot*/, NULL /*pSecAttr*/);
98 rcNt = g_pfnNtDeleteFile(&ObjAttr);
99
100 /* In case some file system does things differently than NTFS. */
101 if (rcNt == STATUS_CANNOT_DELETE)
102 {
103 birdMakeWritable(&NtPath);
104 rcNt = g_pfnNtDeleteFile(&ObjAttr);
105 }
106 }
107 else
108 {
109 /* Use the set information stuff. Probably more reliable. */
110 HANDLE hFile;
111 int fMayTryAgain = 1;
112 for (;;)
113 {
114 rcNt = birdOpenFileUniStr(&NtPath,
115 DELETE,
116 FILE_ATTRIBUTE_NORMAL,
117 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
118 FILE_OPEN,
119 FILE_OPEN_FOR_BACKUP_INTENT | FILE_OPEN_REPARSE_POINT,
120 OBJ_CASE_INSENSITIVE,
121 &hFile);
122 if (MY_NT_SUCCESS(rcNt))
123 {
124 MY_FILE_DISPOSITION_INFORMATION DispInfo;
125 MY_IO_STATUS_BLOCK Ios;
126
127 DispInfo.DeleteFile = TRUE;
128
129 Ios.Information = -1;
130 Ios.u.Status = -1;
131
132 rcNt = g_pfnNtSetInformationFile(hFile, &Ios, &DispInfo, sizeof(DispInfo), MyFileDispositionInformation);
133
134 birdCloseFile(hFile);
135 }
136 if (rcNt != STATUS_CANNOT_DELETE || !fMayTryAgain)
137 break;
138
139 fMayTryAgain = 0;
140 birdMakeWritable(&NtPath);
141 }
142 }
143
144 birdFreeNtPath(&NtPath);
145
146 if (MY_NT_SUCCESS(rcNt))
147 rc = 0;
148 else
149 rc = birdSetErrnoFromNt(rcNt);
150 }
151 return rc;
152}
153
154
155int birdUnlink(const char *pszFile)
156{
157 return birdUnlinkInternal(pszFile, 0 /*fReadOnlyToo*/, 0 /*fFast*/);
158}
159
160
161int birdUnlinkForced(const char *pszFile)
162{
163 return birdUnlinkInternal(pszFile, 1 /*fReadOnlyToo*/, 0 /*fFast*/);
164}
165
166
167int birdUnlinkForcedFast(const char *pszFile)
168{
169 return birdUnlinkInternal(pszFile, 1 /*fReadOnlyToo*/, 1 /*fFast*/);
170}
171
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