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
|
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include "winfile.h"
#include "strconv.h"
#include "winutil.h"
const char *winfile_ptty_identifier = "\\\\.\\pipe\\";
file_error win_open_ptty(const char *path, UINT32 openflags, osd_file **file, UINT64 *filesize)
{
TCHAR *t_name;
HANDLE pipe;
if((t_name = tstring_from_utf8(path)) == NULL)
return FILERR_OUT_OF_MEMORY;
pipe = CreateNamedPipe(t_name, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, 1, 32, 32, 0, NULL);
osd_free(t_name);
if(pipe == INVALID_HANDLE_VALUE)
return FILERR_ACCESS_DENIED;
(*file)->handle = pipe;
*filesize = 0;
return FILERR_NONE;
}
file_error win_read_ptty(osd_file *file, void *buffer, UINT64 offset, UINT32 count, UINT32 *actual)
{
BOOL res;
DWORD bytes_read;
res = ReadFile(file->handle, buffer, count, &bytes_read, NULL);
if(res == FALSE)
return win_error_to_file_error(GetLastError());
if(actual != NULL)
*actual = bytes_read;
return FILERR_NONE;
}
file_error win_write_ptty(osd_file *file, const void *buffer, UINT64 offset, UINT32 count, UINT32 *actual)
{
BOOL res;
DWORD bytes_wrote;
res = WriteFile(file->handle, buffer, count, &bytes_wrote, NULL);
if(res == FALSE)
return win_error_to_file_error(GetLastError());
if(actual != NULL)
*actual = bytes_wrote;
return FILERR_NONE;
}
file_error win_close_ptty(osd_file *file)
{
FlushFileBuffers(file->handle);
DisconnectNamedPipe(file->handle);
CloseHandle(file->handle);
return FILERR_NONE;
}
|