1 /*=============================================================================
4 Copyright © 2008 Bruno Santos <nayart3@gmail.com>
5 =============================================================================*/
13 ////////////////////////////////////////////////////////////////////////////////
14 static inline int file_attr_to_st_mode(DWORD attr
)
18 if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
20 else if (attr
& FILE_ATTRIBUTE_REPARSE_POINT
)
25 if (!(attr
& FILE_ATTRIBUTE_READONLY
))
31 int chmod(const char* path
, mode_t mode
)
33 tcrt::buffer
<wchar_t> wpath
;
37 len
= strlen(path
) + 1;
38 if (!wpath
.resize(len
* sizeof(WCHAR
)))
41 tcrt_utf8_to_utf16(path
, len
, wpath
.get(), len
);
42 attr
= GetFileAttributesW(wpath
.get());
43 if (attr
== INVALID_FILE_ATTRIBUTES
) {
44 tcrt_set_errno_win32();
48 attr
&= ~FILE_ATTRIBUTE_READONLY
;
50 attr
|= FILE_ATTRIBUTE_READONLY
;
51 if (!SetFileAttributesW(wpath
.get(), attr
)) {
52 tcrt_set_errno_win32();
58 int fchmod(int fildes
, mode_t mode
)
60 FILE_BASIC_INFO binfo
;
64 handle
= tcrt::fildes_to_handle(fildes
);
65 if (!GetFileInformationByHandleEx(handle
, FileBasicInfo
, &binfo
, sizeof(binfo
))) {
66 tcrt_set_errno_win32();
70 binfo
.FileAttributes
&= ~FILE_ATTRIBUTE_READONLY
;
72 binfo
.FileAttributes
|= FILE_ATTRIBUTE_READONLY
;
73 GetSystemTimeAsFileTime(&ctime
);
74 binfo
.ChangeTime
.LowPart
= ctime
.dwLowDateTime
;
75 binfo
.ChangeTime
.HighPart
= ctime
.dwHighDateTime
;
76 if (!SetFileInformationByHandle(handle
, FileBasicInfo
, &binfo
, sizeof(binfo
))) {
77 tcrt_set_errno_win32();
83 int fstat(int fildes
, struct stat
*buf
)
85 BY_HANDLE_FILE_INFORMATION info
;
88 handle
= tcrt::fildes_to_handle(fildes
);
89 if (GetFileInformationByHandle(handle
, &info
)) {
92 size
.LowPart
= info
.nFileSizeLow
;
93 size
.HighPart
= info
.nFileSizeHigh
;
96 buf
->st_uid
= 0; //getuid();
97 buf
->st_mode
= file_attr_to_st_mode(info
.dwFileAttributes
);
98 buf
->st_size
= size
.QuadPart
;
100 buf
->st_atime
= filetime_to_time_t(&info
.ftLastAccessTime
);
101 buf
->st_mtime
= filetime_to_time_t(&info
.ftLastWriteTime
);
102 buf
->st_ctime
= filetime_to_time_t(&info
.ftCreationTime
);
103 buf
->st_blksize
= 4096;
104 buf
->st_blocks
= ((buf
->st_size
+ 4095) / 4096);
107 } else if (fildes
< 3 && handle
!= INVALID_HANDLE_VALUE
) {
108 memset(buf
, 0, sizeof(*buf
));
111 tcrt_set_errno_win32();
115 int mkdir(const char* path
, mode_t mode
)
117 tcrt::buffer
<wchar_t> wpath
;
120 len
= strlen(path
) + 1;
121 if (!wpath
.resize(len
* sizeof(wchar_t)))
123 tcrt_utf8_to_utf16(path
, len
, wpath
.get(), len
);
124 if (!CreateDirectoryW(wpath
.get(), NULL
)) {
125 tcrt_set_errno_win32();
131 int lstat(const char *path
, struct stat
*buf
)
136 return stat(path
, buf
);
139 int stat(const char *path
, struct stat
*buf
)
144 fd
= open(path
, O_RDONLY
);
147 res
= fstat(fd
, buf
);
152 mode_t
umask(mode_t mode
)
159 // EOF ////////////////////////////////////////////////////////////////////////