update for HEAD-2003091401
[reactos.git] / lib / kernel32 / file / cnotify.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS system libraries
5  * FILE:            lib/kernel32/file/find.c
6  * PURPOSE:         Find functions
7  * PROGRAMMER:      Ariadne ( ariadne@xs4all.nl)
8  * UPDATE HISTORY:
9  *                  Created 01/11/98
10  */
11
12 #include <k32.h>
13
14 #define NDEBUG
15 #include <kernel32/kernel32.h>
16
17 /*
18  * @implemented
19  */
20 WINBOOL STDCALL
21 FindCloseChangeNotification (HANDLE hChangeHandle)
22 {
23    NtClose(hChangeHandle);
24    return TRUE;
25 }
26
27
28 /*
29  * @implemented
30  */
31 HANDLE
32 STDCALL
33 FindFirstChangeNotificationA (
34         LPCSTR  lpPathName,
35         WINBOOL bWatchSubtree,
36         DWORD   dwNotifyFilter
37         )
38 {
39         UNICODE_STRING PathNameU;
40         ANSI_STRING PathName;
41         HANDLE hNotify;
42
43         RtlInitAnsiString (&PathName,
44                            (LPSTR)lpPathName);
45
46         /* convert ansi (or oem) string to unicode */
47         if (bIsFileApiAnsi)
48    {
49                 RtlAnsiStringToUnicodeString (&PathNameU,
50                                               &PathName,
51                                               TRUE);
52    }
53         else
54    {
55                 RtlOemStringToUnicodeString (&PathNameU,
56                                              &PathName,
57                                              TRUE);
58    }
59
60    hNotify = FindFirstChangeNotificationW (PathNameU.Buffer,
61                                                 bWatchSubtree,
62                                                 dwNotifyFilter);
63
64    RtlFreeUnicodeString(&PathNameU);
65
66    return hNotify;
67 }
68
69
70 /*
71  * @implemented
72  */
73 HANDLE
74 STDCALL
75 FindFirstChangeNotificationW (
76         LPCWSTR lpPathName,
77         WINBOOL bWatchSubtree,
78         DWORD   dwNotifyFilter
79         )
80 {
81    NTSTATUS Status;
82    UNICODE_STRING NtPathU;
83    IO_STATUS_BLOCK IoStatus;
84    OBJECT_ATTRIBUTES ObjectAttributes;
85    HANDLE hDir;
86
87    /*
88    RtlDosPathNameToNtPathName takes a fully qualified file name "C:\Projects\LoadLibrary\Debug\TestDll.dll" 
89    and returns something like "\??\C:\Projects\LoadLibrary\Debug\TestDll.dll."
90    If the file name cannot be interpreted, then the routine returns STATUS_OBJECT_PATH_SYNTAX_BAD and 
91    ends execution.
92    */
93
94    if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpPathName,
95                                           &NtPathU,
96                                           NULL,
97                                           NULL))
98    {
99       SetLastErrorByStatus(STATUS_OBJECT_PATH_SYNTAX_BAD);
100       return INVALID_HANDLE_VALUE;
101    }
102
103    InitializeObjectAttributes (&ObjectAttributes,
104                                &NtPathU,
105                                0,
106                                NULL,
107                                NULL);
108
109    Status = NtOpenFile (&hDir,
110                         SYNCHRONIZE|FILE_LIST_DIRECTORY,
111                         &ObjectAttributes,
112                         &IoStatus,
113                         FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
114                         FILE_DIRECTORY_FILE);
115
116    if (!NT_SUCCESS(Status))
117    {
118       SetLastErrorByStatus(Status);
119       return INVALID_HANDLE_VALUE;
120    }
121
122    Status = NtNotifyChangeDirectoryFile(hDir,
123                                         NULL,
124                                         NULL,
125                                         NULL,
126                                         &IoStatus,
127                                         NULL,//Buffer,
128                                         0,//BufferLength,
129                                         dwNotifyFilter,
130                                         bWatchSubtree);
131    if (!NT_SUCCESS(Status))
132    {
133       SetLastErrorByStatus(Status);
134       return INVALID_HANDLE_VALUE;
135    }
136
137    return hDir;
138 }
139
140
141 /*
142  * @implemented
143  */
144 WINBOOL
145 STDCALL
146 FindNextChangeNotification (
147         HANDLE  hChangeHandle
148         )
149 {
150    IO_STATUS_BLOCK IoStatus;
151    NTSTATUS Status;
152
153    Status = NtNotifyChangeDirectoryFile(hChangeHandle,
154                                         NULL,
155                                         NULL,
156                                         NULL,
157                                         &IoStatus,
158                                         NULL,//Buffer,
159                                         0,//BufferLength,
160                                         FILE_NOTIFY_CHANGE_SECURITY,//meaningless for subsequent calls, but must contain a valid flag(s)
161                                         0//meaningless for subsequent calls 
162                                         );
163    if (!NT_SUCCESS(Status))
164    {
165       SetLastErrorByStatus(Status);
166       return FALSE;
167    }
168
169    return TRUE;
170 }
171
172 /* EOF */