change.c 6.5 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Win32 file change notification functions
 *
 * Copyright 1998 Ulrich Weigand
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22
#include "config.h"

23
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
24 25
#include <stdlib.h>
#include <string.h>
26

27 28
#include "ntstatus.h"
#define WIN32_NO_STATUS
29
#define NONAMELESSUNION
30
#include "windef.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
31 32
#include "winbase.h"
#include "winerror.h"
33
#include "winternl.h"
34
#include "kernel_private.h"
35
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
36

37
WINE_DEFAULT_DEBUG_CHANNEL(file);
Alexandre Julliard's avatar
Alexandre Julliard committed
38 39

/****************************************************************************
40
 *		FindFirstChangeNotificationA (KERNEL32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
41
 */
42
HANDLE WINAPI FindFirstChangeNotificationA( LPCSTR lpPathName, BOOL bWatchSubtree,
43
                                            DWORD dwNotifyFilter )
Alexandre Julliard's avatar
Alexandre Julliard committed
44
{
45
    WCHAR *pathW;
46

47 48
    if (!(pathW = FILE_name_AtoW( lpPathName, FALSE ))) return INVALID_HANDLE_VALUE;
    return FindFirstChangeNotificationW( pathW, bWatchSubtree, dwNotifyFilter );
Alexandre Julliard's avatar
Alexandre Julliard committed
49 50
}

51 52 53 54 55 56 57
/*
 * NtNotifyChangeDirectoryFile may write back to the IO_STATUS_BLOCK
 * asynchronously.  We don't care about the contents, but it can't
 * be placed on the stack since it will go out of scope when we return.
 */
static IO_STATUS_BLOCK FindFirstChange_iosb;

Alexandre Julliard's avatar
Alexandre Julliard committed
58
/****************************************************************************
59
 *		FindFirstChangeNotificationW (KERNEL32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
60
 */
61 62
HANDLE WINAPI FindFirstChangeNotificationW( LPCWSTR lpPathName, BOOL bWatchSubtree,
                                            DWORD dwNotifyFilter)
Alexandre Julliard's avatar
Alexandre Julliard committed
63
{
64 65 66
    UNICODE_STRING nt_name;
    OBJECT_ATTRIBUTES attr;
    NTSTATUS status;
67
    HANDLE handle = INVALID_HANDLE_VALUE;
68

69
    TRACE( "%s %d %x\n", debugstr_w(lpPathName), bWatchSubtree, dwNotifyFilter );
70

71 72 73
    if (!RtlDosPathNameToNtPathName_U( lpPathName, &nt_name, NULL, NULL ))
    {
        SetLastError( ERROR_PATH_NOT_FOUND );
74
        return handle;
75 76 77 78 79 80 81 82 83
    }

    attr.Length = sizeof(attr);
    attr.RootDirectory = 0;
    attr.Attributes = OBJ_CASE_INSENSITIVE;
    attr.ObjectName = &nt_name;
    attr.SecurityDescriptor = NULL;
    attr.SecurityQualityOfService = NULL;

84 85
    status = NtOpenFile( &handle, FILE_LIST_DIRECTORY | SYNCHRONIZE,
                         &attr, &FindFirstChange_iosb,
86
                         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
87 88 89 90 91 92
                         FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
    RtlFreeUnicodeString( &nt_name );

    if (status != STATUS_SUCCESS)
    {
        SetLastError( RtlNtStatusToDosError(status) );
93
        return INVALID_HANDLE_VALUE;
94
    }
95

96 97
    status = NtNotifyChangeDirectoryFile( handle, NULL, NULL, NULL,
                                          &FindFirstChange_iosb,
98 99
                                          NULL, 0, dwNotifyFilter, bWatchSubtree );
    if (status != STATUS_PENDING)
100
    {
101 102 103
        NtClose( handle );
        SetLastError( RtlNtStatusToDosError(status) );
        return INVALID_HANDLE_VALUE;
104
    }
105
    return handle;
Alexandre Julliard's avatar
Alexandre Julliard committed
106 107 108
}

/****************************************************************************
109
 *		FindNextChangeNotification (KERNEL32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
110
 */
111
BOOL WINAPI FindNextChangeNotification( HANDLE handle )
Alexandre Julliard's avatar
Alexandre Julliard committed
112
{
113
    NTSTATUS status;
114 115 116

    TRACE("%p\n",handle);

117 118
    status = NtNotifyChangeDirectoryFile( handle, NULL, NULL, NULL,
                                          &FindFirstChange_iosb,
119 120
                                          NULL, 0, FILE_NOTIFY_CHANGE_SIZE, 0 );
    if (status != STATUS_PENDING)
121
    {
122 123
        SetLastError( RtlNtStatusToDosError(status) );
        return FALSE;
124
    }
125
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
126 127 128
}

/****************************************************************************
129
 *		FindCloseChangeNotification (KERNEL32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
130
 */
131
BOOL WINAPI FindCloseChangeNotification( HANDLE handle )
Alexandre Julliard's avatar
Alexandre Julliard committed
132 133 134
{
    return CloseHandle( handle );
}
135

136 137 138 139 140 141
static void WINAPI invoke_completion(LPVOID ctx, IO_STATUS_BLOCK *ios, ULONG res)
{
    LPOVERLAPPED_COMPLETION_ROUTINE completion = ctx;
    completion(ios->u.Status, ios->Information, (LPOVERLAPPED)ios);
}

142 143 144 145 146 147 148 149 150 151 152 153 154
/****************************************************************************
 *		ReadDirectoryChangesW (KERNEL32.@)
 *
 * NOTES
 *
 *  The filter is remember from the first run and ignored on successive runs.
 *
 *  If there's no output buffer on the first run, it's ignored successive runs
 *   and STATUS_NOTIFY_ENUM_DIRECTORY is returned with an empty buffer.
 *
 *  If a NULL overlapped->hEvent is passed, the directory handle is used
 *   for signalling.
 */
155 156 157 158
BOOL WINAPI ReadDirectoryChangesW( HANDLE handle, LPVOID buffer, DWORD len, BOOL subtree,
                                   DWORD filter, LPDWORD returned, LPOVERLAPPED overlapped,
                                   LPOVERLAPPED_COMPLETION_ROUTINE completion )
{
159 160
    OVERLAPPED ov, *pov;
    IO_STATUS_BLOCK *ios;
161 162
    NTSTATUS status;
    BOOL ret = TRUE;
163
    LPVOID cvalue = NULL;
164

165
    TRACE("%p %p %08x %d %08x %p %p %p\n", handle, buffer, len, subtree, filter,
166 167
           returned, overlapped, completion );

168 169 170 171 172 173
    if (!overlapped)
    {
        memset( &ov, 0, sizeof ov );
        ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
        pov = &ov;
    }
174
    else
175
    {
176
        pov = overlapped;
177 178
        if(completion) cvalue = completion;
        else if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
179
    }
180

181
    ios = (PIO_STATUS_BLOCK) pov;
182
    ios->u.Status = STATUS_PENDING;
183

184 185 186
    status = NtNotifyChangeDirectoryFile( handle, completion && overlapped ? NULL : pov->hEvent,
            completion && overlapped ? invoke_completion : NULL,
            cvalue, ios, buffer, len, filter, subtree );
187 188 189 190 191 192 193 194
    if (status == STATUS_PENDING)
    {
        if (overlapped)
            return TRUE;

        WaitForSingleObjectEx( ov.hEvent, INFINITE, TRUE );
        if (returned)
            *returned = ios->Information;
195
        status = ios->u.Status;
196 197
    }

198 199 200
    if (!overlapped)
        CloseHandle( ov.hEvent );

201
    if (status != STATUS_SUCCESS)
202 203 204 205 206 207
    {
        SetLastError( RtlNtStatusToDosError(status) );
        ret = FALSE;
    }

    return ret;
208
}