summaryrefslogtreecommitdiffstats
path: root/private/eventlog/server/copy.c
blob: 3344bc3e45c89912cb3ba52221691168fad1365c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    copy.c

Abstract:

    This module contains the routine to copy a file.

Author:

    Dan Hinsley (DanHi) 24-Feb-1991

Revision History:

    02-Feb-1994     Danl
        Fixed memory leak where ioBuffer wasn't getting free'd when doing
        an error exit from ElfpCopyFile.

--*/

//
// INCLUDES
//

#include <eventp.h>


NTSTATUS
ElfpCopyFile (
    IN HANDLE SourceHandle,
    IN PUNICODE_STRING TargetFileName
    )

/*++

Routine Description:

    This routine copies or appends from the source file to the target file.
    If the target file already exists, the copy fails.

Arguments:

    SourceHandle - An open handle to the source file.

    TargetFileName - The name of the file to copy to.


Return Value:

    NTSTATUS - STATUS_SUCCESS or error.

--*/

{

    NTSTATUS Status;

    IO_STATUS_BLOCK IoStatusBlock;
    FILE_STANDARD_INFORMATION sourceStandardInfo;

    OBJECT_ATTRIBUTES ObjectAttributes;
    HANDLE TargetHandle;

    PCHAR ioBuffer;
    ULONG ioBufferSize;
    ULONG bytesRead;

    //
    // Get the size of the file so we can set the attributes of the target
    // file.
    //

    Status = NtQueryInformationFile(
                 SourceHandle,
                 &IoStatusBlock,
                 &sourceStandardInfo,
                 sizeof(sourceStandardInfo),
                 FileStandardInformation
                 );

    if ( !NT_SUCCESS(Status) ) {

        return(Status);
    }

    //
    // Open the target file, fail if the file already exists.
    //

    InitializeObjectAttributes(
                    &ObjectAttributes,
                    TargetFileName,
                    OBJ_CASE_INSENSITIVE,
                    NULL,
                    NULL
                    );

    Status = NtCreateFile(&TargetHandle,
                        GENERIC_WRITE | SYNCHRONIZE,
                        &ObjectAttributes,
                        &IoStatusBlock,
                        &(sourceStandardInfo.EndOfFile),
                        FILE_ATTRIBUTE_NORMAL,
                        0,                       // Share access
                        FILE_CREATE,
                        FILE_SYNCHRONOUS_IO_ALERT | FILE_SEQUENTIAL_ONLY,
                        NULL,                    // EA buffer
                        0                        // EA length
                        );

    if (!NT_SUCCESS(Status)) {
        return(Status);
    }

    //
    // Allocate a buffer to use for the data copy.
    //

    ioBufferSize = 4096;

    ioBuffer = ElfpAllocateBuffer ( ioBufferSize);

    if ( ioBuffer == NULL ) {

        return (STATUS_NO_MEMORY);
    }

    //
    // Copy data--read from source, write to target.  Do this until
    // all the data is written or an error occurs.
    //

    while ( TRUE ) {

        Status = NtReadFile(
                         SourceHandle,
                         NULL,                // Event
                         NULL,                // ApcRoutine
                         NULL,                // ApcContext
                         &IoStatusBlock,
                         ioBuffer,
                         ioBufferSize,
                         NULL,                // ByteOffset
                         NULL                 // Key
                         );

        if ( !NT_SUCCESS(Status) && Status != STATUS_END_OF_FILE ) {

            ElfDbgPrint(("[ELF] Copy failed reading source file - %X\n",
                Status));
            ElfpFreeBuffer(ioBuffer);
            return (Status);

        }

        if ( IoStatusBlock.Information == 0 ||
             Status == STATUS_END_OF_FILE ) {
            break;
        }

        bytesRead = IoStatusBlock.Information;

        Status = NtWriteFile(
                          TargetHandle,
                          NULL,               // Event
                          NULL,               // ApcRoutine
                          NULL,               // ApcContext
                          &IoStatusBlock,
                          ioBuffer,
                          bytesRead,
                          NULL,               // ByteOffset
                          NULL                // Key
                          );

        if ( !NT_SUCCESS(Status) ) {
            ElfDbgPrint(("[ELF] Copy failed writing target file - %X\n",
                Status));
            ElfpFreeBuffer(ioBuffer);
            return (Status);
        }
    }

    ElfpFreeBuffer ( ioBuffer );

    Status = NtClose(TargetHandle);

    ASSERT(NT_SUCCESS(Status));

    return STATUS_SUCCESS;

} // ElfpCopyFile