posixfilesystem.h (6470B)
1 // 2 // posixfilesystem.h 3 // Smalltalk-80 4 // 5 // Created by Dan Banay on 5/3/20. 6 // Copyright © 2020 Dan Banay. All rights reserved. 7 // 8 // MIT License 9 // Permission is hereby granted, free of charge, to any person obtaining a copy 10 // of this software and associated documentation files (the "Software"), to deal 11 // in the Software without restriction, including without limitation the rights 12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 // copies of the Software, and to permit persons to whom the Software is 14 // furnished to do so, subject to the following conditions: 15 // 16 // The above copyright notice and this permission notice shall be included in all 17 // copies or substantial portions of the Software. 18 // 19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 // SOFTWARE. 26 // 27 28 #include "filesystem.h" 29 #include <string> 30 #include <stdlib.h> 31 #include <sys/stat.h> 32 #include <fcntl.h> 33 #include <cassert> 34 35 #ifndef _WIN32 36 #include <unistd.h> 37 #include <dirent.h> 38 #include <errno.h> 39 #else 40 #include <windows.h> 41 #include <io.h> 42 #endif 43 44 class PosixST80FileSystem: public IFileSystem 45 { 46 public: 47 48 PosixST80FileSystem(const std::string& root) : root_directory(root) 49 { 50 } 51 52 std::string path_for_file(const char *name) 53 { 54 return root_directory + "/" + name; 55 } 56 57 // File oriented operations 58 int open_file(const char *name) 59 { 60 int fd; 61 std::string path = path_for_file(name); 62 #ifdef _WIN32 63 64 errno_t code = _sopen_s(&fd, path.c_str(), _O_RDWR | _O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE); 65 66 #else 67 fd = open(path.c_str(), O_RDWR ); 68 69 #endif 70 return fd; 71 } 72 73 int create_file(const char *name) 74 { 75 int fd; 76 std::string path = path_for_file(name); 77 #ifdef _WIN32 78 errno_t code = _sopen_s(&fd, path.c_str(), _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _SH_DENYNO, _S_IREAD | _S_IWRITE); 79 #else 80 fd = open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644); 81 #endif 82 return fd; 83 } 84 85 86 int close_file(int file_handle) 87 { 88 #ifdef _WIN32 89 if (file_handle == -1) return -1; 90 return _close(file_handle); 91 #else 92 return close(file_handle); 93 #endif 94 } 95 96 int read(int file_handle, char *buffer, int bytes) 97 { 98 #ifdef _WIN32 99 return _read(file_handle, buffer, bytes); 100 #else 101 return (int) ::read(file_handle, buffer, bytes); 102 #endif 103 } 104 105 int write(int file_handle, const char *buffer, int bytes) 106 { 107 #ifdef _WIN32 108 return _write(file_handle, buffer, bytes); 109 #else 110 return (int) ::write(file_handle, buffer, bytes); 111 #endif 112 } 113 114 115 bool truncate_to(int file_handle, int length) 116 { 117 #ifdef _WIN32 118 return _chsize(file_handle, length) != -1; 119 #else 120 return ftruncate(file_handle, length) != -1; 121 #endif 122 } 123 124 int file_size(int file_handle) 125 { 126 #ifdef _WIN32 127 if (file_handle == -1) return -1; 128 129 struct _stat s; 130 if (_fstat(file_handle, &s) != -1) 131 { 132 return (int) s.st_size; 133 } 134 135 return -1; 136 #else 137 struct stat s; 138 if (fstat(file_handle, &s) != -1) 139 { 140 return (int) s.st_size; 141 } 142 143 return -1; 144 #endif 145 } 146 147 bool file_flush(int file_handle) 148 { 149 #ifndef _WIN32 150 return fsync(file_handle) != -1; 151 #else 152 return true; 153 #endif 154 } 155 156 #ifndef _WIN32 157 bool is_diretory(const char *name) 158 { 159 std::string path = path_for_file(name); 160 struct stat statbuf; 161 if (stat(path.c_str(), &statbuf) != 0) 162 return 0; 163 return S_ISDIR(statbuf.st_mode); 164 } 165 #endif 166 167 // Directory orientated operations 168 void enumerate_files(const std::function <void (const char *) >& each) 169 { 170 #ifndef _WIN32 171 DIR *dir = opendir(root_directory.c_str()); 172 struct dirent *entry; 173 174 while ((entry = readdir(dir))) 175 { 176 // skip hidden files and ., and .. 177 if (entry->d_name[0] != '.' && !is_diretory(entry->d_name)) 178 { 179 each(entry->d_name); 180 } 181 } 182 183 closedir(dir); 184 #else 185 HANDLE hFind = INVALID_HANDLE_VALUE; 186 _WIN32_FIND_DATAA findFileData; 187 188 std::string pattern = root_directory + "\\*"; 189 190 hFind = FindFirstFileA(pattern.c_str(), &findFileData); 191 if (hFind != INVALID_HANDLE_VALUE) 192 { 193 do 194 { 195 if (findFileData.cFileName[0] != '.') 196 { 197 if ((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) 198 { 199 each(findFileData.cFileName); 200 } 201 } 202 } 203 while (FindNextFileA(hFind, &findFileData) != 0); 204 FindClose(hFind); 205 206 } 207 #endif 208 } 209 210 bool rename_file(const char *old_name, const char *new_name) 211 { 212 std::string old_path = path_for_file(old_name); 213 std::string new_path = path_for_file(new_name); 214 215 return rename(old_path.c_str(), new_path.c_str()) != -1; 216 } 217 218 bool delete_file(const char* file_name) 219 { 220 std::string path = path_for_file(file_name); 221 222 #ifdef _WIN32 223 return _unlink(path.c_str()) != -1; 224 225 #else 226 return unlink(path.c_str()) != -1; 227 #endif 228 } 229 230 int seek_to(int file_handle, int position) 231 { 232 #ifdef _WIN32 233 return (int) _lseek(file_handle, position, SEEK_SET); 234 #else 235 return (int) lseek(file_handle, position, SEEK_SET); 236 #endif 237 238 } 239 240 int tell(int file_handle) 241 { 242 #ifdef _WIN32 243 return (int) _lseek(file_handle, 0, SEEK_CUR); 244 #else 245 return (int) lseek(file_handle, 0, SEEK_CUR); 246 #endif 247 } 248 249 // Error handling 250 const int last_error() 251 { 252 return errno; 253 } 254 255 const char *error_text(int code) 256 { 257 #ifdef _WIN32 258 static char errmsg[512]; 259 strerror_s(errmsg, sizeof(errmsg), code); 260 return errmsg; 261 #else 262 return strerror(code); 263 #endif 264 } 265 266 private: 267 std::string root_directory; 268 269 };