Initial commit of visual studio 9 git build superproject.
[git-build-vc9.git] / tcrt / src / stat.cpp
blobe6661bc80c060188d75f491c4f943506a3d0a95d
1 /*=============================================================================
2 stat.h :
4 Copyright © 2008 Bruno Santos <nayart3@gmail.com>
5 =============================================================================*/
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include "utils.h"
13 ////////////////////////////////////////////////////////////////////////////////
14 static inline int file_attr_to_st_mode(DWORD attr)
16 int mode = S_IRUSR;
18 if (attr & FILE_ATTRIBUTE_DIRECTORY)
19 mode |= S_IFDIR;
20 else if (attr & FILE_ATTRIBUTE_REPARSE_POINT)
21 mode |= S_IFLNK;
22 else
23 mode |= S_IFREG;
25 if (!(attr & FILE_ATTRIBUTE_READONLY))
26 mode |= S_IWUSR;
28 return mode;
31 int chmod(const char* path, mode_t mode)
33 tcrt::buffer<wchar_t> wpath;
34 size_t len;
35 DWORD attr;
37 len = strlen(path) + 1;
38 if (!wpath.resize(len * sizeof(WCHAR)))
39 return -1;
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();
45 return -1;
47 if (mode & S_IWUSR)
48 attr &= ~FILE_ATTRIBUTE_READONLY;
49 else
50 attr |= FILE_ATTRIBUTE_READONLY;
51 if (!SetFileAttributesW(wpath.get(), attr)) {
52 tcrt_set_errno_win32();
53 return -1;
55 return 0;
58 int fchmod(int fildes, mode_t mode)
60 FILE_BASIC_INFO binfo;
61 FILETIME ctime;
62 HANDLE handle;
64 handle = tcrt::fildes_to_handle(fildes);
65 if (!GetFileInformationByHandleEx(handle, FileBasicInfo, &binfo, sizeof(binfo))) {
66 tcrt_set_errno_win32();
67 return -1;
69 if (mode & S_IWUSR)
70 binfo.FileAttributes &= ~FILE_ATTRIBUTE_READONLY;
71 else
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();
78 return -1;
80 return 0;
83 int fstat(int fildes, struct stat *buf)
85 BY_HANDLE_FILE_INFORMATION info;
86 HANDLE handle;
88 handle = tcrt::fildes_to_handle(fildes);
89 if (GetFileInformationByHandle(handle, &info)) {
90 LARGE_INTEGER size;
92 size.LowPart = info.nFileSizeLow;
93 size.HighPart = info.nFileSizeHigh;
94 buf->st_ino = 0;
95 buf->st_gid = 0;
96 buf->st_uid = 0; //getuid();
97 buf->st_mode = file_attr_to_st_mode(info.dwFileAttributes);
98 buf->st_size = size.QuadPart;
99 buf->st_dev = 0;
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);
105 return 0;
107 } else if (fildes < 3 && handle != INVALID_HANDLE_VALUE) {
108 memset(buf, 0, sizeof(*buf));
109 return 0;
111 tcrt_set_errno_win32();
112 return -1;
115 int mkdir(const char* path, mode_t mode)
117 tcrt::buffer<wchar_t> wpath;
118 size_t len;
120 len = strlen(path) + 1;
121 if (!wpath.resize(len * sizeof(wchar_t)))
122 return -1;
123 tcrt_utf8_to_utf16(path, len, wpath.get(), len);
124 if (!CreateDirectoryW(wpath.get(), NULL)) {
125 tcrt_set_errno_win32();
126 return -1;
128 return 0;
131 int lstat(const char *path, struct stat *buf)
134 // FIXME
136 return stat(path, buf);
139 int stat(const char *path, struct stat *buf)
141 int fd;
142 int res;
144 fd = open(path, O_RDONLY);
145 if (fd < 0)
146 return fd;
147 res = fstat(fd, buf);
148 close(fd);
149 return res;
152 mode_t umask(mode_t mode)
154 __debugbreak();
155 errno = ENOSYS;
156 return -1;
159 // EOF ////////////////////////////////////////////////////////////////////////