xref: /reactos/ntoskrnl/config/cmwraprs.c (revision 02e84521)
1 /*
2  * PROJECT:         ReactOS Kernel
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * FILE:            ntoskrnl/config/cmwraprs.c
5  * PURPOSE:         Configuration Manager - Wrappers for Hive Operations
6  * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include "ntoskrnl.h"
12 #define NDEBUG
13 #include "debug.h"
14 
15 /* FUNCTIONS *****************************************************************/
16 
17 NTSTATUS
18 NTAPI
19 CmpCreateEvent(IN EVENT_TYPE EventType,
20                OUT PHANDLE EventHandle,
21                OUT PKEVENT *Event)
22 {
23     NTSTATUS Status;
24     OBJECT_ATTRIBUTES ObjectAttributes;
25 
26     /* Create the event */
27     InitializeObjectAttributes(&ObjectAttributes,
28                                NULL,
29                                OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
30                                NULL,
31                                NULL);
32     Status = ZwCreateEvent(EventHandle,
33                            EVENT_ALL_ACCESS,
34                            &ObjectAttributes,
35                            EventType,
36                            FALSE);
37     if (!NT_SUCCESS(Status)) return Status;
38 
39     /* Get a pointer to the object itself */
40     Status = ObReferenceObjectByHandle(*EventHandle,
41                                        EVENT_ALL_ACCESS,
42                                        NULL,
43                                        KernelMode,
44                                        (PVOID*)Event,
45                                        NULL);
46     if (!NT_SUCCESS(Status)) ZwClose(*EventHandle);
47 
48     /* Return status */
49     return Status;
50 }
51 
52 PVOID
53 NTAPI
54 CmpAllocate(IN SIZE_T Size,
55             IN BOOLEAN Paged,
56             IN ULONG Tag)
57 {
58     return ExAllocatePoolWithTag(Paged ? PagedPool : NonPagedPool,
59                                  Size,
60                                  Tag);
61 }
62 
63 VOID
64 NTAPI
65 CmpFree(IN PVOID Ptr,
66         IN ULONG Quota)
67 {
68     ExFreePool(Ptr);
69 }
70 
71 BOOLEAN
72 NTAPI
73 CmpFileRead(IN PHHIVE RegistryHive,
74             IN ULONG FileType,
75             IN PULONG FileOffset,
76             OUT PVOID Buffer,
77             IN SIZE_T BufferLength)
78 {
79     PCMHIVE CmHive = (PCMHIVE)RegistryHive;
80     HANDLE HiveHandle = CmHive->FileHandles[FileType];
81     LARGE_INTEGER _FileOffset;
82     IO_STATUS_BLOCK IoStatusBlock;
83     NTSTATUS Status;
84 
85     /* Just return success if no file is associated with this hive */
86     if (HiveHandle == NULL)
87         return TRUE;
88 
89     _FileOffset.QuadPart = *FileOffset;
90     Status = ZwReadFile(HiveHandle, NULL, NULL, NULL, &IoStatusBlock,
91                         Buffer, (ULONG)BufferLength, &_FileOffset, NULL);
92     return NT_SUCCESS(Status) ? TRUE : FALSE;
93 }
94 
95 BOOLEAN
96 NTAPI
97 CmpFileWrite(IN PHHIVE RegistryHive,
98              IN ULONG FileType,
99              IN PULONG FileOffset,
100              IN PVOID Buffer,
101              IN SIZE_T BufferLength)
102 {
103     PCMHIVE CmHive = (PCMHIVE)RegistryHive;
104     HANDLE HiveHandle = CmHive->FileHandles[FileType];
105     LARGE_INTEGER _FileOffset;
106     IO_STATUS_BLOCK IoStatusBlock;
107     NTSTATUS Status;
108 
109     /* Just return success if no file is associated with this hive */
110     if (HiveHandle == NULL)
111         return TRUE;
112 
113     /* Don't do anything if we're not supposed to */
114     if (CmpNoWrite)
115         return TRUE;
116 
117     _FileOffset.QuadPart = *FileOffset;
118     Status = ZwWriteFile(HiveHandle, NULL, NULL, NULL, &IoStatusBlock,
119                          Buffer, (ULONG)BufferLength, &_FileOffset, NULL);
120     return NT_SUCCESS(Status) ? TRUE : FALSE;
121 }
122 
123 BOOLEAN
124 NTAPI
125 CmpFileSetSize(IN PHHIVE RegistryHive,
126                IN ULONG FileType,
127                IN ULONG FileSize,
128                IN ULONG OldFileSize)
129 {
130     PCMHIVE CmHive = (PCMHIVE)RegistryHive;
131     HANDLE HiveHandle = CmHive->FileHandles[FileType];
132     FILE_END_OF_FILE_INFORMATION EndOfFileInfo;
133     FILE_ALLOCATION_INFORMATION FileAllocationInfo;
134     IO_STATUS_BLOCK IoStatusBlock;
135     NTSTATUS Status;
136 
137     /* Just return success if no file is associated with this hive */
138     if (HiveHandle == NULL)
139         return TRUE;
140 
141     EndOfFileInfo.EndOfFile.QuadPart = FileSize;
142     Status = ZwSetInformationFile(HiveHandle,
143                                   &IoStatusBlock,
144                                   &EndOfFileInfo,
145                                   sizeof(FILE_END_OF_FILE_INFORMATION),
146                                   FileEndOfFileInformation);
147     if (!NT_SUCCESS(Status)) return FALSE;
148 
149     FileAllocationInfo.AllocationSize.QuadPart = FileSize;
150     Status = ZwSetInformationFile(HiveHandle,
151                                   &IoStatusBlock,
152                                   &FileAllocationInfo,
153                                   sizeof(FILE_ALLOCATION_INFORMATION),
154                                   FileAllocationInformation);
155     if (!NT_SUCCESS(Status)) return FALSE;
156 
157     return TRUE;
158 }
159 
160 BOOLEAN
161 NTAPI
162 CmpFileFlush(IN PHHIVE RegistryHive,
163              IN ULONG FileType,
164              IN OUT PLARGE_INTEGER FileOffset,
165              IN ULONG Length)
166 {
167     PCMHIVE CmHive = (PCMHIVE)RegistryHive;
168     HANDLE HiveHandle = CmHive->FileHandles[FileType];
169     IO_STATUS_BLOCK IoStatusBlock;
170     NTSTATUS Status;
171 
172     /* Just return success if no file is associated with this hive */
173     if (HiveHandle == NULL)
174         return TRUE;
175 
176     /* Don't do anything if we're not supposed to */
177     if (CmpNoWrite)
178         return TRUE;
179 
180     Status = ZwFlushBuffersFile(HiveHandle, &IoStatusBlock);
181     return NT_SUCCESS(Status) ? TRUE : FALSE;
182 }
183