Skip to content

Commit

Permalink
Added standard file handles
Browse files Browse the repository at this point in the history
  • Loading branch information
evrhel committed Apr 18, 2024
1 parent 93897af commit a581bce
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
6 changes: 5 additions & 1 deletion include/lysys/ls_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
#define LS_ASYNC_IO_RESERVED_SIZE 32 // 32 bytes for OVERLAPPED structure
#else
#define LS_ASYNC_IO_RESERVED_SIZE 0
#endif
#endif // LS_WINDOWS

#define LS_STDIN ((ls_handle)1)
#define LS_STDOUT ((ls_handle)2)
#define LS_STDERR ((ls_handle)3)

// File access modes

Expand Down
29 changes: 16 additions & 13 deletions src/ls_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,16 @@ int64_t ls_seek(ls_handle file, int64_t offset, int origin)
BOOL bRet;
LARGE_INTEGER liDist = { .QuadPart = offset };
LARGE_INTEGER liNewPointer;
HANDLE hFile = *(PHANDLE)file;

HANDLE hFile;

hFile = ls_resolve_file(file);
bRet = SetFilePointerEx(hFile, liDist, &liNewPointer, origin);
if (!bRet)
return -1;

return liNewPointer.QuadPart;
#else
return lseek(*(int *)file, offset, origin);
return lseek(ls_resolve_file(file), offset, origin);
#endif // LS_WINDOWS
}

Expand All @@ -111,8 +112,9 @@ size_t ls_read(ls_handle file, void *buffer, size_t size,
DWORD dwRead, dwToRead;
size_t remaining;
LPOVERLAPPED lpOverlapped;
HANDLE hFile = *(PHANDLE)file;

HANDLE hFile;

hFile = ls_resolve_file(file);
remaining = size;
dwToRead = (DWORD)(remaining & 0xffffffff);

Expand Down Expand Up @@ -156,7 +158,7 @@ size_t ls_read(ls_handle file, void *buffer, size_t size,
size_t bytes_read;
size_t remaining;

fd = *(int *)file;
fd = ls_resolve_file(file);

if (async)
return -1; // TODO: Implement async I/O
Expand Down Expand Up @@ -191,8 +193,9 @@ size_t ls_write(ls_handle file, const void *buffer, size_t size,
DWORD dwWritten, dwToWrite;
size_t remaining;
LPOVERLAPPED lpOl;
HANDLE hFile = *(PHANDLE)file;

HANDLE hFile;

hFile = ls_resolve_file(file);
remaining = size;
dwToWrite = (DWORD)(remaining & 0xffffffff);

Expand Down Expand Up @@ -233,7 +236,7 @@ size_t ls_write(ls_handle file, const void *buffer, size_t size,
size_t bytes_written;
size_t remaining;

fd = *(int *)file;
fd = ls_resolve_file(file);

if (async)
return -1; // TODO: Implement async I/O
Expand Down Expand Up @@ -263,7 +266,7 @@ size_t ls_write(ls_handle file, const void *buffer, size_t size,
int ls_flush(ls_handle file)
{
#if LS_WINDOWS
return FlushFileBuffers(*(PHANDLE)file) ? 0 : -1;
return FlushFileBuffers(ls_resolve_handle(file)) ? 0 : -1;
#else
return fsync(*(int *)file);
#endif // LS_WINDOWS
Expand All @@ -277,8 +280,9 @@ int ls_get_async_io_result(ls_handle file, struct ls_async_io *async,
BOOL bRet;
LPOVERLAPPED lpOl = (LPOVERLAPPED)async->reserved;
DWORD dwStatus;
HANDLE hFile = *(PHANDLE)file;
HANDLE hFile;

hFile = ls_resolve_file(file);
bRet = GetOverlappedResultEx(hFile, lpOl, &dwRet, ms, FALSE);
if (!bRet)
{
Expand All @@ -305,9 +309,8 @@ int ls_get_async_io_result(ls_handle file, struct ls_async_io *async,
int ls_cancel_async_io(ls_handle file, struct ls_async_io *async)
{
#if LS_WINDOWS
HANDLE hFile = *(PHANDLE)file;
LPOVERLAPPED lpOl = async ? (LPOVERLAPPED)async->reserved : NULL;
return CancelIoEx(hFile, lpOl) ? 0 : -1;
return CancelIoEx(ls_resolve_file(file), lpOl) ? 0 : -1;
#else
// TODO: Implement async I/O
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/ls_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void *ls_mmap(ls_handle file, size_t size, size_t offset, int protect, ls_handle
else
liSize.QuadPart = size + offset;

hMap = CreateFileMappingW(file, NULL, ls_protect_to_flags(protect), liSize.HighPart, liSize.LowPart, NULL);
hMap = CreateFileMappingW(ls_resolve_file(file), NULL, ls_protect_to_flags(protect), liSize.HighPart, liSize.LowPart, NULL);
if (!hMap) return NULL;

lpView = MapViewOfFile(hMap, dwAccess, liOffset.HighPart, liOffset.LowPart, 0);
Expand Down Expand Up @@ -88,7 +88,7 @@ void *ls_mmap(ls_handle file, size_t size, size_t offset, int protect, ls_handle
int fd;
size_t *map_res;

fd = *(int *)file;
fd = ls_resolve_file(file);

if (!map)
return NULL;
Expand Down
21 changes: 21 additions & 0 deletions src/ls_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,24 @@ int ls_flags_to_protect(native_flags_t prot)
return protect;
#endif // LS_WINDOWS
}

native_file_t ls_resolve_file(ls_handle fh)
{
#if LS_WINDOWS
switch ((intptr_t)fh)
{
case LS_STDIN: return GetStdHandle(STD_INPUT_HANDLE);
case LS_STDOUT: return GetStdHandle(STD_OUTPUT_HANDLE);
case LS_STDERR: return GetStdHandle(STD_ERROR_HANDLE);
default: return *(PHANDLE)fh;
}
#else
switch ((intptr_t)fh)
{
case LS_STDIN: return 0;
case LS_STDOUT: return 1;
case LS_STDERR: return 2;
default: return *(int *)fh;
}
#endif // LS_WINDOWS
}
7 changes: 6 additions & 1 deletion src/ls_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
#include <strsafe.h>
#include <Psapi.h>

typedef HANDLE native_file_t;
typedef DWORD native_flags_t;

// at least Windows 2000
#if WINVER < _WIN32_WINNT_WIN2K
#error "Windows version too old"
#endif
#endif // WINVER

// utf8 string to wchar_t string
// returns number of characters written to buf, including null terminator
Expand Down Expand Up @@ -48,6 +49,7 @@ LPWSTR ls_build_environment(const char *envp[]);
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/stat.h>
Expand All @@ -62,6 +64,7 @@ LPWSTR ls_build_environment(const char *envp[]);
#include <sys/inotify.h>
#endif // LS_DARWIN

typedef int native_file_t;
typedef int native_flags_t;

#endif // LS_POSIX
Expand All @@ -73,3 +76,5 @@ int ls_create_to_oflags(int create);

native_flags_t ls_protect_to_flags(int protect);
int ls_flags_to_protect(native_flags_t prot);

native_file_t ls_resolve_file(ls_handle fh);

0 comments on commit a581bce

Please sign in to comment.