| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 18 мар. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/io/Dir.h> |
| 23 |
|
| 24 |
#ifdef PLATFORM_WINDOWS |
| 25 |
#include <windows.h> |
| 26 |
#include <fileapi.h> |
| 27 |
#include <winbase.h> |
| 28 |
#else |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <limits.h> |
| 34 |
#endif /* PLATFORM_WINDOWS */ |
| 35 |
|
| 36 |
namespace lsp |
| 37 |
{ |
| 38 |
namespace io |
| 39 |
{ |
| 40 |
#ifdef PLATFORM_WINDOWS |
| 41 |
typedef struct dirhandle_t |
| 42 |
{ |
| 43 |
HANDLE hHandle; // Directory handle |
| 44 |
WIN32_FIND_DATAW sData; // Last data read |
| 45 |
status_t nPending; // Pending error code |
| 46 |
} dirhandle_t; |
| 47 |
#endif /* PLATFORM_WINDOWS */ |
| 48 |
|
| 49 |
static inline bool check_closed(dirhandle_t *dir) |
| 50 |
{ |
| 51 |
return dir == NULL; |
| 52 |
} |
| 53 |
|
| 54 |
Dir::Dir() |
| 55 |
{ |
| 56 |
nErrorCode = STATUS_OK; |
| 57 |
hDir = NULL; |
| 58 |
} |
| 59 |
|
| 60 |
Dir::~Dir() |
| 61 |
{ |
| 62 |
close(); |
| 63 |
} |
| 64 |
|
| 65 |
status_t Dir::open(const char *path) |
| 66 |
{ |
| 67 |
if (!check_closed(hDir)) |
| 68 |
return set_error(STATUS_BAD_STATE); |
| 69 |
else if (path == NULL) |
| 70 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 71 |
|
| 72 |
LSPString spath; |
| 73 |
if (!spath.set_utf8(path)) |
| 74 |
return set_error(STATUS_NO_MEM); |
| 75 |
return open (&spath); |
| 76 |
} |
| 77 |
|
| 78 |
status_t Dir::open(const LSPString *path) |
| 79 |
{ |
| 80 |
if (!check_closed(hDir)) |
| 81 |
return set_error(STATUS_BAD_STATE); |
| 82 |
else if (path == NULL) |
| 83 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 84 |
|
| 85 |
if (sPath.set(path) != STATUS_OK) |
| 86 |
return set_error(STATUS_NO_MEM); |
| 87 |
|
| 88 |
#ifdef PLATFORM_WINDOWS |
| 89 |
// Create dir handle |
| 90 |
dirhandle_t *dir = reinterpret_cast<dirhandle_t *>(malloc(sizeof(dirhandle_t))); |
| 91 |
if (dir == NULL) |
| 92 |
return STATUS_NO_MEM; |
| 93 |
lsp_finally { |
| 94 |
if (dir != NULL) |
| 95 |
free(dir); |
| 96 |
}; |
| 97 |
bzero(dir, sizeof(dirhandle_t)); |
| 98 |
|
| 99 |
// Create search mask |
| 100 |
Path mask; |
| 101 |
status_t res = mask.set(path); |
| 102 |
if (res == STATUS_OK) |
| 103 |
res = mask.append_child("*"); |
| 104 |
if (res != STATUS_OK) |
| 105 |
{ |
| 106 |
sPath.clear(); |
| 107 |
return set_error(res); |
| 108 |
} |
| 109 |
|
| 110 |
// Call API for FindFirst |
| 111 |
status_t pending = STATUS_OK; |
| 112 |
HANDLE dh = ::FindFirstFileW(mask.as_string()->get_utf16(), &dir->sData); |
| 113 |
if (dh == INVALID_HANDLE_VALUE) |
| 114 |
{ |
| 115 |
DWORD err = ::GetLastError(); |
| 116 |
switch (err) |
| 117 |
{ |
| 118 |
case ERROR_NO_MORE_FILES: |
| 119 |
case ERROR_FILE_NOT_FOUND: |
| 120 |
pending = STATUS_EOF; |
| 121 |
break; |
| 122 |
case ERROR_PATH_NOT_FOUND: |
| 123 |
case ERROR_INVALID_DRIVE: |
| 124 |
return set_error(STATUS_NOT_DIRECTORY); |
| 125 |
case ERROR_NOT_READY: return set_error(STATUS_NO_DATA); |
| 126 |
case ERROR_TOO_MANY_OPEN_FILES: return set_error(STATUS_OVERFLOW); |
| 127 |
case ERROR_OUTOFMEMORY: |
| 128 |
case ERROR_NOT_ENOUGH_MEMORY: |
| 129 |
return set_error(STATUS_NO_MEM); |
| 130 |
case ERROR_INVALID_ACCESS: |
| 131 |
case ERROR_ACCESS_DENIED: |
| 132 |
return set_error(STATUS_PERMISSION_DENIED); |
| 133 |
default: |
| 134 |
return set_error(STATUS_UNKNOWN_ERR); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// Commit state |
| 139 |
dir->nPending = pending; |
| 140 |
dir->hHandle = dh; |
| 141 |
lsp::swap(hDir, dir); |
| 142 |
#else |
| 143 |
DIR *dh = ::opendir(path->get_native()); |
| 144 |
if (dh == NULL) |
| 145 |
{ |
| 146 |
sPath.clear(); |
| 147 |
int error = errno; |
| 148 |
switch (error) |
| 149 |
{ |
| 150 |
case EACCES: return set_error(STATUS_PERMISSION_DENIED); |
| 151 |
case EMFILE: |
| 152 |
case ENFILE: return set_error(STATUS_TOO_BIG); |
| 153 |
case ENOENT: return set_error(STATUS_NOT_FOUND); |
| 154 |
case ENOMEM: return set_error(STATUS_NO_MEM); |
| 155 |
case ENOTDIR: return set_error(STATUS_BAD_TYPE); |
| 156 |
default: |
| 157 |
return set_error(STATUS_UNKNOWN_ERR); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
hDir = dh; |
| 162 |
#endif /* PLATFORM_WINDOWS */ |
| 163 |
|
| 164 |
return set_error(STATUS_OK); |
| 165 |
} |
| 166 |
|
| 167 |
status_t Dir::open(const Path *path) |
| 168 |
{ |
| 169 |
if (!check_closed(hDir)) |
| 170 |
return set_error(STATUS_BAD_STATE); |
| 171 |
else if (path == NULL) |
| 172 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 173 |
return open(path->as_string()); |
| 174 |
} |
| 175 |
|
| 176 |
status_t Dir::rewind() |
| 177 |
{ |
| 178 |
if (check_closed(hDir)) |
| 179 |
return set_error(STATUS_BAD_STATE); |
| 180 |
|
| 181 |
#ifdef PLATFORM_WINDOWS |
| 182 |
// Create search mask |
| 183 |
Path mask; |
| 184 |
status_t res = mask.set(&sPath); |
| 185 |
if (res == STATUS_OK) |
| 186 |
res = mask.append_child("*"); |
| 187 |
if (res != STATUS_OK) |
| 188 |
{ |
| 189 |
sPath.clear(); |
| 190 |
return set_error(res); |
| 191 |
} |
| 192 |
|
| 193 |
// Call API for FindFirst |
| 194 |
status_t pending = STATUS_OK; |
| 195 |
HANDLE dh = ::FindFirstFileW(mask.as_string()->get_utf16(), &hDir->sData); |
| 196 |
if (dh == INVALID_HANDLE_VALUE) |
| 197 |
{ |
| 198 |
DWORD err = ::GetLastError(); |
| 199 |
switch (err) |
| 200 |
{ |
| 201 |
case ERROR_NO_MORE_FILES: |
| 202 |
case ERROR_FILE_NOT_FOUND: |
| 203 |
pending = STATUS_EOF; |
| 204 |
break; |
| 205 |
case ERROR_PATH_NOT_FOUND: |
| 206 |
case ERROR_INVALID_DRIVE: |
| 207 |
return set_error(STATUS_NOT_DIRECTORY); |
| 208 |
case ERROR_NOT_READY: return set_error(STATUS_NO_DATA); |
| 209 |
case ERROR_TOO_MANY_OPEN_FILES: return set_error(STATUS_OVERFLOW); |
| 210 |
case ERROR_OUTOFMEMORY: |
| 211 |
case ERROR_NOT_ENOUGH_MEMORY: |
| 212 |
return set_error(STATUS_NO_MEM); |
| 213 |
case ERROR_INVALID_ACCESS: |
| 214 |
case ERROR_ACCESS_DENIED: |
| 215 |
return set_error(STATUS_PERMISSION_DENIED); |
| 216 |
default: |
| 217 |
return set_error(STATUS_UNKNOWN_ERR); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// Close current handle |
| 222 |
if ((hDir->hHandle != INVALID_HANDLE_VALUE)) |
| 223 |
::FindClose(hDir->hHandle); |
| 224 |
|
| 225 |
// Replace closed handle by new handle |
| 226 |
hDir->hHandle = dh; |
| 227 |
hDir->nPending = pending; |
| 228 |
#else |
| 229 |
::rewinddir(hDir); |
| 230 |
#endif /* PLATFORM_WINDOWS */ |
| 231 |
|
| 232 |
return set_error(STATUS_OK); |
| 233 |
} |
| 234 |
|
| 235 |
status_t Dir::read(LSPString *path, bool full) |
| 236 |
{ |
| 237 |
if (check_closed(hDir)) |
| 238 |
return set_error(STATUS_BAD_STATE); |
| 239 |
else if (path == NULL) |
| 240 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 241 |
|
| 242 |
LSPString out; |
| 243 |
|
| 244 |
#ifdef PLATFORM_WINDOWS |
| 245 |
if (hDir->nPending != STATUS_OK) |
| 246 |
return set_error(hDir->nPending); |
| 247 |
else if (hDir->hHandle == INVALID_HANDLE_VALUE) |
| 248 |
return set_error(STATUS_BAD_STATE); |
| 249 |
|
| 250 |
// Set result |
| 251 |
if (!out.set_utf16(hDir->sData.cFileName)) |
| 252 |
return set_error(STATUS_NO_MEM); |
| 253 |
|
| 254 |
// Perform next iteration |
| 255 |
if (!::FindNextFileW(hDir->hHandle, &hDir->sData)) |
| 256 |
{ |
| 257 |
DWORD err = ::GetLastError(); |
| 258 |
switch (err) |
| 259 |
{ |
| 260 |
case ERROR_NO_MORE_FILES: |
| 261 |
case ERROR_FILE_NOT_FOUND: |
| 262 |
hDir->nPending = STATUS_EOF; |
| 263 |
break; |
| 264 |
case ERROR_OUTOFMEMORY: |
| 265 |
case ERROR_NOT_ENOUGH_MEMORY: |
| 266 |
return set_error(STATUS_NO_MEM); |
| 267 |
default: |
| 268 |
hDir->nPending = STATUS_UNKNOWN_ERR; |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
#else |
| 273 |
// Read directory |
| 274 |
errno = 0; |
| 275 |
struct dirent *dent = ::readdir(hDir); |
| 276 |
if (dent == NULL) |
| 277 |
{ |
| 278 |
if (errno == 0) |
| 279 |
return set_error(STATUS_EOF); |
| 280 |
return set_error(STATUS_UNKNOWN_ERR); |
| 281 |
} |
| 282 |
|
| 283 |
// Return value |
| 284 |
if (!out.set_native(dent->d_name)) |
| 285 |
return set_error(STATUS_NO_MEM); |
| 286 |
|
| 287 |
#endif /* PLATFORM_WINDOWS */ |
| 288 |
if (full) |
| 289 |
{ |
| 290 |
Path tmp; |
| 291 |
status_t res = tmp.set(&sPath); |
| 292 |
if (res == STATUS_OK) |
| 293 |
res = tmp.append_child(&out); |
| 294 |
if (res == STATUS_OK) |
| 295 |
res = (out.set(tmp.as_string())) ? STATUS_OK : STATUS_NO_MEM; |
| 296 |
if (res != STATUS_OK) |
| 297 |
set_error(res); |
| 298 |
} |
| 299 |
|
| 300 |
path->swap(&out); |
| 301 |
return set_error(STATUS_OK); |
| 302 |
} |
| 303 |
|
| 304 |
status_t Dir::read(Path *path, bool full) |
| 305 |
{ |
| 306 |
if (path == NULL) |
| 307 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 308 |
|
| 309 |
LSPString xpath; |
| 310 |
status_t res = read(&xpath, false); |
| 311 |
if (res == STATUS_OK) |
| 312 |
{ |
| 313 |
if (full) |
| 314 |
{ |
| 315 |
Path tmp; |
| 316 |
res = tmp.set(&sPath); |
| 317 |
if (res == STATUS_OK) |
| 318 |
res = tmp.append_child(&xpath); |
| 319 |
if (res == STATUS_OK) |
| 320 |
path->take(&tmp); |
| 321 |
} |
| 322 |
else |
| 323 |
res = path->set(&xpath); |
| 324 |
} |
| 325 |
return set_error(res); |
| 326 |
} |
| 327 |
|
| 328 |
status_t Dir::reads(LSPString *path, fattr_t *attr, bool full) |
| 329 |
{ |
| 330 |
if (check_closed(hDir)) |
| 331 |
return set_error(STATUS_BAD_STATE); |
| 332 |
else if (path == NULL) |
| 333 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 334 |
|
| 335 |
LSPString out; |
| 336 |
|
| 337 |
#ifdef PLATFORM_WINDOWS |
| 338 |
if (hDir->nPending != STATUS_OK) |
| 339 |
return set_error(hDir->nPending); |
| 340 |
else if (hDir->hHandle == INVALID_HANDLE_VALUE) |
| 341 |
return set_error(STATUS_BAD_STATE); |
| 342 |
|
| 343 |
// Set result |
| 344 |
if (!out.set_utf16(hDir->sData.cFileName)) |
| 345 |
return set_error(STATUS_NO_MEM); |
| 346 |
|
| 347 |
// Decode file state |
| 348 |
// Decode file type |
| 349 |
attr->type = fattr_t::FT_REGULAR; |
| 350 |
if (hDir->sData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 351 |
attr->type = fattr_t::FT_DIRECTORY; |
| 352 |
else if (hDir->sData.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) |
| 353 |
attr->type = fattr_t::FT_BLOCK; |
| 354 |
|
| 355 |
attr->blk_size = 4096; |
| 356 |
attr->size = (wsize_t(hDir->sData.nFileSizeHigh) << 32) | hDir->sData.nFileSizeLow; |
| 357 |
attr->inode = 0; |
| 358 |
attr->ctime = ((wsize_t(hDir->sData.ftCreationTime.dwHighDateTime) << 32) | hDir->sData.ftCreationTime.dwLowDateTime) / 10000; |
| 359 |
attr->mtime = ((wsize_t(hDir->sData.ftLastWriteTime.dwHighDateTime) << 32) | hDir->sData.ftLastWriteTime.dwLowDateTime) / 10000; |
| 360 |
attr->atime = ((wsize_t(hDir->sData.ftLastAccessTime.dwHighDateTime) << 32) | hDir->sData.ftLastAccessTime.dwLowDateTime) / 10000; |
| 361 |
|
| 362 |
// Perform next iteration |
| 363 |
if (!::FindNextFileW(hDir->hHandle, &hDir->sData)) |
| 364 |
{ |
| 365 |
DWORD err = ::GetLastError(); |
| 366 |
switch (err) |
| 367 |
{ |
| 368 |
case ERROR_NO_MORE_FILES: |
| 369 |
case ERROR_FILE_NOT_FOUND: |
| 370 |
hDir->nPending = STATUS_EOF; |
| 371 |
break; |
| 372 |
default: |
| 373 |
hDir->nPending = STATUS_UNKNOWN_ERR; |
| 374 |
break; |
| 375 |
} |
| 376 |
} |
| 377 |
#else |
| 378 |
// Read directory |
| 379 |
errno = 0; |
| 380 |
struct dirent *dent = ::readdir(hDir); |
| 381 |
if (dent == NULL) |
| 382 |
{ |
| 383 |
if (errno == 0) |
| 384 |
return set_error(STATUS_EOF); |
| 385 |
return set_error(STATUS_UNKNOWN_ERR); |
| 386 |
} |
| 387 |
|
| 388 |
// Stat the record |
| 389 |
struct stat sb; |
| 390 |
#if ((_POSIX_C_SOURCE >= 200809L) || defined(_ATFILE_SOURCE)) |
| 391 |
int code = ::fstatat(::dirfd(hDir), dent->d_name, &sb, AT_SYMLINK_NOFOLLOW); |
| 392 |
#else |
| 393 |
LSPString xpath, xname; |
| 394 |
if (!xname.set_native(dent->d_name)) |
| 395 |
return set_error(STATUS_NO_MEM); |
| 396 |
if (!xpath.set(sPath.as_string())) |
| 397 |
return set_error(STATUS_NO_MEM); |
| 398 |
if (!xpath.append(FILE_SEPARATOR_C)) |
| 399 |
return set_error(STATUS_NO_MEM); |
| 400 |
if (!xpath.append(&xname)) |
| 401 |
return set_error(STATUS_NO_MEM); |
| 402 |
int code = ::lstat(xpath.get_native(), &sb); |
| 403 |
#endif |
| 404 |
if (code != 0) |
| 405 |
{ |
| 406 |
code = errno; |
| 407 |
switch (code) |
| 408 |
{ |
| 409 |
case EACCES: return set_error(STATUS_PERMISSION_DENIED); |
| 410 |
case EBADF: return set_error(STATUS_INVALID_VALUE); |
| 411 |
case ENAMETOOLONG: return set_error(STATUS_OVERFLOW); |
| 412 |
case EOVERFLOW: return set_error(STATUS_OVERFLOW); |
| 413 |
case ENOENT: return set_error(STATUS_NOT_FOUND); |
| 414 |
case ENOMEM: return set_error(STATUS_NO_MEM); |
| 415 |
default: break; |
| 416 |
} |
| 417 |
return set_error(STATUS_IO_ERROR); |
| 418 |
} |
| 419 |
|
| 420 |
// Return value |
| 421 |
if (!out.set_native(dent->d_name)) |
| 422 |
return set_error(STATUS_NO_MEM); |
| 423 |
|
| 424 |
// Decode file type |
| 425 |
switch (sb.st_mode & S_IFMT) { |
| 426 |
case S_IFBLK: attr->type = fattr_t::FT_BLOCK; break; |
| 427 |
case S_IFCHR: attr->type = fattr_t::FT_CHARACTER; break; |
| 428 |
case S_IFDIR: attr->type = fattr_t::FT_DIRECTORY; break; |
| 429 |
case S_IFIFO: attr->type = fattr_t::FT_FIFO; break; |
| 430 |
case S_IFLNK: attr->type = fattr_t::FT_SYMLINK; break; |
| 431 |
case S_IFREG: attr->type = fattr_t::FT_REGULAR; break; |
| 432 |
case S_IFSOCK: attr->type = fattr_t::FT_SOCKET; break; |
| 433 |
default: attr->type = fattr_t::FT_UNKNOWN; break; |
| 434 |
} |
| 435 |
|
| 436 |
attr->blk_size = sb.st_blksize; |
| 437 |
attr->size = sb.st_size; |
| 438 |
attr->inode = sb.st_ino; |
| 439 |
|
| 440 |
// In C headers st_atime, st_mtime and st_ctime macros are defined for backward comparibility |
| 441 |
// to provide access to field tv_sec of st_atim, st_mtim and st_ctim structures. |
| 442 |
// In MacOS, these fields have another names: st_atimespec, st_ctimespec and st_mtimespec |
| 443 |
#if defined(PLATFORM_MACOSX) |
| 444 |
attr->ctime = (sb.st_ctimespec.tv_sec * 1000LL) + (sb.st_ctimespec.tv_nsec / 1000000); |
| 445 |
attr->mtime = (sb.st_mtimespec.tv_sec * 1000LL) + (sb.st_mtimespec.tv_nsec / 1000000); |
| 446 |
attr->atime = (sb.st_atimespec.tv_sec * 1000LL) + (sb.st_atimespec.tv_nsec / 1000000); |
| 447 |
#elif defined(st_ctime) || defined(st_mtime) || defined(st_atime) |
| 448 |
attr->ctime = (sb.st_ctim.tv_sec * 1000LL) + (sb.st_ctim.tv_nsec / 1000000); |
| 449 |
attr->mtime = (sb.st_mtim.tv_sec * 1000LL) + (sb.st_mtim.tv_nsec / 1000000); |
| 450 |
attr->atime = (sb.st_atim.tv_sec * 1000LL) + (sb.st_atim.tv_nsec / 1000000); |
| 451 |
#else |
| 452 |
attr->ctime = sb.st_ctime * 1000LL; |
| 453 |
attr->mtime = sb.st_mtime * 1000LL; |
| 454 |
attr->atime = sb.st_atime * 1000LL; |
| 455 |
#endif |
| 456 |
#endif /* PLATFORM_WINDOWS */ |
| 457 |
|
| 458 |
if (full) |
| 459 |
{ |
| 460 |
Path tmp; |
| 461 |
status_t res = tmp.set(&sPath); |
| 462 |
if (res == STATUS_OK) |
| 463 |
res = tmp.append_child(&out); |
| 464 |
if (res == STATUS_OK) |
| 465 |
res = (out.set(tmp.as_string())) ? STATUS_OK : STATUS_NO_MEM; |
| 466 |
if (res != STATUS_OK) |
| 467 |
set_error(res); |
| 468 |
} |
| 469 |
|
| 470 |
path->swap(&out); |
| 471 |
|
| 472 |
return set_error(STATUS_OK); |
| 473 |
} |
| 474 |
|
| 475 |
status_t Dir::reads(Path *path, fattr_t *attr, bool full) |
| 476 |
{ |
| 477 |
if (path == NULL) |
| 478 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 479 |
|
| 480 |
LSPString xpath; |
| 481 |
fattr_t xattr; |
| 482 |
|
| 483 |
status_t res = reads(&xpath, &xattr, false); |
| 484 |
if (res == STATUS_OK) |
| 485 |
{ |
| 486 |
if (full) |
| 487 |
{ |
| 488 |
Path tmp; |
| 489 |
res = tmp.set(&sPath); |
| 490 |
if (res == STATUS_OK) |
| 491 |
res = tmp.append_child(&xpath); |
| 492 |
if (res == STATUS_OK) |
| 493 |
path->take(&tmp); |
| 494 |
} |
| 495 |
else |
| 496 |
res = path->set(&xpath); |
| 497 |
|
| 498 |
if (res == STATUS_OK) |
| 499 |
*attr = xattr; |
| 500 |
} |
| 501 |
return set_error(res); |
| 502 |
} |
| 503 |
|
| 504 |
status_t Dir::close() |
| 505 |
{ |
| 506 |
if (check_closed(hDir)) |
| 507 |
return set_error(STATUS_BAD_STATE); |
| 508 |
|
| 509 |
status_t res = STATUS_OK; |
| 510 |
#ifdef PLATFORM_WINDOWS |
| 511 |
if (hDir != NULL) |
| 512 |
{ |
| 513 |
if ((hDir->hHandle != NULL) && |
| 514 |
(hDir->hHandle != INVALID_HANDLE_VALUE)) |
| 515 |
{ |
| 516 |
if (!::FindClose(hDir->hHandle)) |
| 517 |
res = STATUS_UNKNOWN_ERR; |
| 518 |
hDir->hHandle = NULL; |
| 519 |
} |
| 520 |
free(hDir); |
| 521 |
hDir = NULL; |
| 522 |
} |
| 523 |
#else |
| 524 |
if (::closedir(hDir) != 0) |
| 525 |
{ |
| 526 |
int error = errno; |
| 527 |
res = (error == EBADF) ? STATUS_BAD_STATE : STATUS_IO_ERROR; |
| 528 |
} |
| 529 |
hDir = NULL; |
| 530 |
#endif /* PLATFORM_WINDOWS */ |
| 531 |
|
| 532 |
return set_error(res); |
| 533 |
} |
| 534 |
|
| 535 |
status_t Dir::stat(const char *path, fattr_t *attr) |
| 536 |
{ |
| 537 |
if ((path == NULL) || (attr == NULL)) |
| 538 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 539 |
if (check_closed(hDir)) |
| 540 |
return set_error(STATUS_BAD_STATE); |
| 541 |
|
| 542 |
Path xpath; |
| 543 |
status_t res = xpath.set(&sPath); |
| 544 |
if (res == STATUS_OK) |
| 545 |
res = xpath.append_child(path); |
| 546 |
if (res != STATUS_OK) |
| 547 |
return set_error(res); |
| 548 |
return set_error(File::stat(xpath.as_string(), attr)); |
| 549 |
} |
| 550 |
|
| 551 |
status_t Dir::stat(const LSPString *path, fattr_t *attr) |
| 552 |
{ |
| 553 |
if ((path == NULL) || (attr == NULL)) |
| 554 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 555 |
if (check_closed(hDir)) |
| 556 |
return set_error(STATUS_BAD_STATE); |
| 557 |
|
| 558 |
Path xpath; |
| 559 |
status_t res = xpath.set(&sPath); |
| 560 |
if (res == STATUS_OK) |
| 561 |
res = xpath.append_child(path); |
| 562 |
if (res != STATUS_OK) |
| 563 |
return set_error(res); |
| 564 |
return set_error(File::stat(&xpath, attr)); |
| 565 |
} |
| 566 |
|
| 567 |
status_t Dir::stat(const Path *path, fattr_t *attr) |
| 568 |
{ |
| 569 |
if ((path == NULL) || (attr == NULL)) |
| 570 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 571 |
if (check_closed(hDir)) |
| 572 |
return set_error(STATUS_BAD_STATE); |
| 573 |
|
| 574 |
Path xpath; |
| 575 |
status_t res = xpath.set(&sPath); |
| 576 |
if (res == STATUS_OK) |
| 577 |
res = xpath.append_child(path); |
| 578 |
if (res != STATUS_OK) |
| 579 |
return set_error(res); |
| 580 |
return set_error(File::stat(xpath.as_string(), attr)); |
| 581 |
} |
| 582 |
|
| 583 |
status_t Dir::sym_stat(const char *path, fattr_t *attr) |
| 584 |
{ |
| 585 |
if ((path == NULL) || (attr == NULL)) |
| 586 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 587 |
if (check_closed(hDir)) |
| 588 |
return set_error(STATUS_BAD_STATE); |
| 589 |
|
| 590 |
Path xpath; |
| 591 |
status_t res = xpath.set(&sPath); |
| 592 |
if (res == STATUS_OK) |
| 593 |
res = xpath.append_child(path); |
| 594 |
if (res != STATUS_OK) |
| 595 |
return set_error(res); |
| 596 |
return set_error(File::sym_stat(xpath.as_string(), attr)); |
| 597 |
} |
| 598 |
|
| 599 |
status_t Dir::sym_stat(const LSPString *path, fattr_t *attr) |
| 600 |
{ |
| 601 |
if ((path == NULL) || (attr == NULL)) |
| 602 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 603 |
if (check_closed(hDir)) |
| 604 |
return set_error(STATUS_BAD_STATE); |
| 605 |
|
| 606 |
Path xpath; |
| 607 |
status_t res = xpath.set(&sPath); |
| 608 |
if (res == STATUS_OK) |
| 609 |
res = xpath.append_child(path); |
| 610 |
if (res != STATUS_OK) |
| 611 |
return set_error(res); |
| 612 |
return set_error(File::sym_stat(xpath.as_string(), attr)); |
| 613 |
} |
| 614 |
|
| 615 |
status_t Dir::sym_stat(const Path *path, fattr_t *attr) |
| 616 |
{ |
| 617 |
if ((path == NULL) || (attr == NULL)) |
| 618 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 619 |
if (check_closed(hDir)) |
| 620 |
return set_error(STATUS_BAD_STATE); |
| 621 |
|
| 622 |
Path xpath; |
| 623 |
status_t res = xpath.set(&sPath); |
| 624 |
if (res == STATUS_OK) |
| 625 |
res = xpath.append_child(path); |
| 626 |
if (res != STATUS_OK) |
| 627 |
return set_error(res); |
| 628 |
return set_error(File::sym_stat(xpath.as_string(), attr)); |
| 629 |
} |
| 630 |
|
| 631 |
status_t Dir::create(const char *path) |
| 632 |
{ |
| 633 |
if (path == NULL) |
| 634 |
return STATUS_BAD_ARGUMENTS; |
| 635 |
|
| 636 |
LSPString spath; |
| 637 |
if (!spath.set_utf8(path)) |
| 638 |
return STATUS_NO_MEM; |
| 639 |
return create(&spath); |
| 640 |
} |
| 641 |
|
| 642 |
status_t Dir::create(const Path *path) |
| 643 |
{ |
| 644 |
if (path == NULL) |
| 645 |
return STATUS_BAD_ARGUMENTS; |
| 646 |
return create(path->as_string()); |
| 647 |
} |
| 648 |
|
| 649 |
status_t Dir::create(const LSPString *path) |
| 650 |
{ |
| 651 |
if (path == NULL) |
| 652 |
return STATUS_BAD_ARGUMENTS; |
| 653 |
|
| 654 |
#ifdef PLATFORM_WINDOWS |
| 655 |
const WCHAR *xp = path->get_utf16(); |
| 656 |
if (::CreateDirectoryW(xp, NULL)) |
| 657 |
return STATUS_OK; |
| 658 |
|
| 659 |
// Analyze error code |
| 660 |
DWORD code = ::GetLastError(); |
| 661 |
switch (code) |
| 662 |
{ |
| 663 |
case ERROR_ALREADY_EXISTS: |
| 664 |
return STATUS_OK; |
| 665 |
case ERROR_PATH_NOT_FOUND: |
| 666 |
return STATUS_NOT_FOUND; |
| 667 |
default: |
| 668 |
return STATUS_IO_ERROR; |
| 669 |
} |
| 670 |
#else |
| 671 |
// Try to create directory |
| 672 |
if (::mkdir(path->get_native(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) |
| 673 |
return STATUS_OK; |
| 674 |
|
| 675 |
// Analyze error code |
| 676 |
int code = errno; |
| 677 |
switch (code) |
| 678 |
{ |
| 679 |
case EACCES: |
| 680 |
case EPERM: |
| 681 |
return STATUS_PERMISSION_DENIED; |
| 682 |
case EDQUOT: |
| 683 |
case ENOSPC: |
| 684 |
return STATUS_OVERFLOW; |
| 685 |
case ENOTDIR: |
| 686 |
return STATUS_BAD_HIERARCHY; |
| 687 |
case EFAULT: |
| 688 |
case EINVAL: |
| 689 |
case ENAMETOOLONG: |
| 690 |
return STATUS_BAD_ARGUMENTS; |
| 691 |
case EEXIST: // pathname already exists (not necessarily as a directory). This includes the case where pathname is a symbolic link, dangling or not. |
| 692 |
{ |
| 693 |
fattr_t attr; |
| 694 |
File::sym_stat(path, &attr); |
| 695 |
if (attr.type == fattr_t::FT_DIRECTORY) |
| 696 |
return STATUS_OK; |
| 697 |
return STATUS_ALREADY_EXISTS; |
| 698 |
} |
| 699 |
case ENOENT: // A directory component in pathname does not exist or is a dangling symbolic link. |
| 700 |
return STATUS_NOT_FOUND; |
| 701 |
default: |
| 702 |
return STATUS_IO_ERROR; |
| 703 |
} |
| 704 |
#endif /* PLATFORM_WINDOWS */ |
| 705 |
return STATUS_OK; |
| 706 |
} |
| 707 |
|
| 708 |
status_t Dir::remove(const char *path) |
| 709 |
{ |
| 710 |
if (path == NULL) |
| 711 |
return STATUS_BAD_ARGUMENTS; |
| 712 |
|
| 713 |
LSPString spath; |
| 714 |
if (!spath.set_utf8(path)) |
| 715 |
return STATUS_NO_MEM; |
| 716 |
return remove(&spath); |
| 717 |
} |
| 718 |
|
| 719 |
status_t Dir::remove(const Path *path) |
| 720 |
{ |
| 721 |
if (path == NULL) |
| 722 |
return STATUS_BAD_ARGUMENTS; |
| 723 |
return remove(path->as_string()); |
| 724 |
} |
| 725 |
|
| 726 |
status_t Dir::remove(const LSPString *path) |
| 727 |
{ |
| 728 |
if (path == NULL) |
| 729 |
return STATUS_BAD_ARGUMENTS; |
| 730 |
|
| 731 |
#ifdef PLATFORM_WINDOWS |
| 732 |
if (::RemoveDirectoryW(path->get_utf16())) |
| 733 |
return STATUS_OK; |
| 734 |
|
| 735 |
// Analyze error code |
| 736 |
DWORD code = ::GetLastError(); |
| 737 |
switch (code) |
| 738 |
{ |
| 739 |
case ERROR_PATH_NOT_FOUND: |
| 740 |
return STATUS_NOT_FOUND; |
| 741 |
case ERROR_DIR_NOT_EMPTY: |
| 742 |
return STATUS_NOT_EMPTY; |
| 743 |
case ERROR_SHARING_VIOLATION: |
| 744 |
return STATUS_PERMISSION_DENIED; |
| 745 |
default: |
| 746 |
return STATUS_IO_ERROR; |
| 747 |
} |
| 748 |
#else |
| 749 |
// Try to remove directory |
| 750 |
if (::rmdir(path->get_native()) == 0) |
| 751 |
return STATUS_OK; |
| 752 |
|
| 753 |
// Analyze error code |
| 754 |
int code = errno; |
| 755 |
switch (code) |
| 756 |
{ |
| 757 |
case EACCES: |
| 758 |
case EPERM: |
| 759 |
return STATUS_PERMISSION_DENIED; |
| 760 |
case EDQUOT: |
| 761 |
case ENOSPC: |
| 762 |
return STATUS_OVERFLOW; |
| 763 |
case EFAULT: |
| 764 |
case EINVAL: |
| 765 |
case ENAMETOOLONG: |
| 766 |
return STATUS_BAD_ARGUMENTS; |
| 767 |
case ENOTDIR: |
| 768 |
return STATUS_NOT_DIRECTORY; |
| 769 |
case ENOENT: |
| 770 |
return STATUS_NOT_FOUND; |
| 771 |
case ENOTEMPTY: |
| 772 |
return STATUS_NOT_EMPTY; |
| 773 |
default: |
| 774 |
return STATUS_IO_ERROR; |
| 775 |
} |
| 776 |
#endif /* PLATFORM_WINDOWS */ |
| 777 |
return STATUS_OK; |
| 778 |
} |
| 779 |
|
| 780 |
status_t Dir::get_current(LSPString *path) |
| 781 |
{ |
| 782 |
if (path == NULL) |
| 783 |
return STATUS_BAD_ARGUMENTS; |
| 784 |
|
| 785 |
#ifdef PLATFORM_WINDOWS |
| 786 |
DWORD len = ::GetCurrentDirectoryW(0, NULL); |
| 787 |
if (len == 0) |
| 788 |
return STATUS_UNKNOWN_ERR; |
| 789 |
|
| 790 |
WCHAR *buf = reinterpret_cast<WCHAR *>(::malloc(sizeof(WCHAR) * (len + 1))); |
| 791 |
if (buf == NULL) |
| 792 |
return STATUS_NO_MEM; |
| 793 |
|
| 794 |
len = ::GetCurrentDirectoryW(len, buf); |
| 795 |
if (len == 0) |
| 796 |
{ |
| 797 |
::free(buf); |
| 798 |
return STATUS_UNKNOWN_ERR; |
| 799 |
} |
| 800 |
|
| 801 |
status_t res = (path->set_utf16(buf, len)) ? STATUS_OK : STATUS_NO_MEM; |
| 802 |
::free(buf); |
| 803 |
return res; |
| 804 |
#else |
| 805 |
char *spath = reinterpret_cast<char *>(malloc(PATH_MAX * sizeof(char))); |
| 806 |
if (spath == NULL) |
| 807 |
return STATUS_NO_MEM; |
| 808 |
lsp_finally { free(spath); }; |
| 809 |
|
| 810 |
char *p = ::getcwd(spath, PATH_MAX); |
| 811 |
if (p == NULL) |
| 812 |
{ |
| 813 |
int code = errno; |
| 814 |
switch (code) |
| 815 |
{ |
| 816 |
case EACCES: |
| 817 |
case EPERM: |
| 818 |
return STATUS_PERMISSION_DENIED; |
| 819 |
case ENOENT: |
| 820 |
return STATUS_REMOVED; |
| 821 |
case ENAMETOOLONG: |
| 822 |
return STATUS_OVERFLOW; |
| 823 |
case ENOMEM: |
| 824 |
return STATUS_NO_MEM; |
| 825 |
default: |
| 826 |
return STATUS_IO_ERROR; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
return (path->set_native(p)) ? STATUS_OK : STATUS_NO_MEM; |
| 831 |
#endif /* PLATFORM_WINDOWS */ |
| 832 |
} |
| 833 |
|
| 834 |
status_t Dir::get_current(Path *path) |
| 835 |
{ |
| 836 |
if (path == NULL) |
| 837 |
return STATUS_BAD_ARGUMENTS; |
| 838 |
LSPString tmp; |
| 839 |
status_t res = get_current(&tmp); |
| 840 |
if (res == STATUS_OK) |
| 841 |
path->take(&tmp); |
| 842 |
return res; |
| 843 |
} |
| 844 |
|
| 845 |
} /* namespace io */ |
| 846 |
} /* namespace lsp */ |