summaryrefslogtreecommitdiffstats
path: root/private/nw/test/fileio.c
blob: ed535206bcd0d8f44963850fcab997927b45b1d3 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*++

Copyright (c) 1993  Microsoft Corporation

Module Name:

    fileio.c

Abstract:

    User mode test program for the Microsoft Netware redir file system.

    This test program can be built from the command line using the
    command 'nmake UMTEST=fileio'.

Author:

    Manny Weiser (mannyw)   17-May-1993

Revision History:

--*/

#include <stdio.h>
#include <string.h>
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>

//
// Local definitions
//

VOID
DisplayUsage(
    PSZ ProgramName
    );


BOOLEAN
OpenFile(
    PSZ Name,
    PHANDLE Handle
    );

QueryFileInfo(
    IN HANDLE Handle
    );


cdecl
main(
    int argc,
    char *argv[],
    )
{
    HANDLE handle;
    BOOLEAN success;
    char *fileName;

    if ( argc > 1) {
        fileName = argv[1];
    } else {
        fileName = "testfile.txt";
    }

    success = OpenFile( fileName, &handle );
    if ( !success) {
        return 1;
    }

    success = QueryFileInfo( handle );
    if ( !success) {
        return 1;
    }

    printf( "%s exiting\n", argv[0]);
    return 0;
}


VOID
DisplayUsage(
    PSZ ProgramName
    )
{
    printf( "Usage: %s [filename]", ProgramName);
}


BOOLEAN
OpenFile(
    PSZ Name,
    PHANDLE Handle
    )
{
    UNICODE_STRING nameString;
    NTSTATUS status;
    OBJECT_ATTRIBUTES objectAttributes;
    IO_STATUS_BLOCK ioStatusBlock;
    RTL_RELATIVE_NAME RelativeName;
    STRING AnsiFileName;
    UNICODE_STRING FileName;
    BOOLEAN TranslationStatus;

    RtlInitString( &AnsiFileName, Name );
    RtlOemStringToUnicodeString( &FileName, &AnsiFileName, TRUE );

    TranslationStatus = RtlDosPathNameToNtPathName_U(
                            FileName.Buffer,
                            &FileName,
                            NULL,
                            &RelativeName
                            );

    if ( !TranslationStatus ) {
        return FALSE;
        }

    if ( RelativeName.RelativeName.Length ) {
        FileName = *(PUNICODE_STRING)&RelativeName.RelativeName;
    } else {
        RelativeName.ContainingDirectory = NULL;
    }

    //
    //  Open the file
    //

    InitializeObjectAttributes(
        &objectAttributes,
        &FileName,
        OBJ_CASE_INSENSITIVE,
        RelativeName.ContainingDirectory,
        NULL
        );

    status = NtOpenFile (
                Handle,
                FILE_GENERIC_READ,
                &objectAttributes,
                &ioStatusBlock,
                FILE_SHARE_WRITE | FILE_SHARE_READ,
                0L
                );

    if (!NT_SUCCESS(status) ) {
        printf( "Open status = %x for file %Z\n", status, &nameString );
    }

    RtlFreeHeap(RtlProcessHeap(), 0, FileName.Buffer );
    return ( (BOOLEAN) NT_SUCCESS( status ) );
}


DoSleep(
    IN ULONG time
    )
{
    ULONG ms;
    LARGE_INTEGER delayTime;

    ms = time * 100;
    delayTime = RtlEnlargedIntegerMultiply( ms, -10000 );
    NtDelayExecution( TRUE, (PLARGE_INTEGER)&delayTime );

    return( 0 );
}

QueryFileInfo(
    IN HANDLE Handle
    )
{
    NTSTATUS status;
    IO_STATUS_BLOCK IoStatusBlock;
    char buffer[200];
    PFILE_ALL_INFORMATION allInfo = (PFILE_ALL_INFORMATION)buffer;

    status = NtQueryInformationFile(
                Handle,
                &IoStatusBlock,
                allInfo,
                200,
                FileAllInformation );

    if ( NT_SUCCESS( status ) ) {
        status = IoStatusBlock.Status;
    }

    if ( !NT_SUCCESS( status ) ) {
        printf("NtQueryInformation file returns %08lx\n", status );
        return( status );
    }

    printf( "File attributes = %x\n", allInfo->BasicInformation.FileAttributes );
    printf( "File size = %d\n", allInfo->StandardInformation.AllocationSize.LowPart );
    printf( "Is a dir = %d\n", allInfo->StandardInformation.Directory );
    printf( "Index = %x\n", allInfo->InternalInformation.IndexNumber );
    printf( "Easiz = %d\n", allInfo->EaInformation.EaSize );
    printf( "Access flags = %x\n", allInfo->AccessInformation.AccessFlags );
    printf( "Current offset = %d\n", allInfo->PositionInformation.CurrentByteOffset.LowPart );
    printf( "Mode = %x\n", allInfo->ModeInformation.Mode );
    printf( "Alignment info = %d\n", allInfo->AlignmentInformation.AlignmentRequirement );
    allInfo->NameInformation.FileName[ allInfo->NameInformation.FileNameLength / 2 ] = '\0';
    printf( "Name = %ws\n", allInfo->NameInformation.FileName );

    return( status );
}