|
17 | 17 | #include <stdlib.h> |
18 | 18 | #include <string.h> |
19 | 19 | #include <errno.h> |
| 20 | +#include <sys/stat.h> |
20 | 21 |
|
21 | 22 | #ifdef _WIN32 |
22 | | -#include <windows.h> |
23 | | -#include <io.h> |
| 23 | + #include <windows.h> |
24 | 24 | #else |
25 | | -#include <sys/stat.h> |
26 | | -#include <sys/types.h> |
27 | | -#include <fcntl.h> |
28 | | -#include <unistd.h> |
| 25 | + #include <unistd.h> |
| 26 | + #include <fcntl.h> |
29 | 27 | #endif |
30 | 28 |
|
31 | | -// Define portable permission flags |
32 | | -#define FOSSIL_FILE_READ 0x01 // Read permission |
33 | | -#define FOSSIL_FILE_WRITE 0x02 // Write permission |
34 | | -#define FOSSIL_FILE_EXEC 0x04 // Execute permission |
35 | | - |
36 | 29 | typedef enum { |
37 | 30 | FOSSIL_BUFFER_SMALL = 100, |
38 | 31 | FOSSIL_BUFFER_MEDIUM = 500, |
@@ -349,3 +342,121 @@ int fossil_fstream_get_type(const char *filename) { |
349 | 342 | int32_t fossil_fstream_is_open(const fossil_fstream_t *stream) { |
350 | 343 | return stream != NULL && stream->file != NULL; |
351 | 344 | } |
| 345 | + |
| 346 | +/** |
| 347 | + * Check if a file is readable. |
| 348 | + * |
| 349 | + * This function checks if a file has read permissions. |
| 350 | + * |
| 351 | + * @param filename The name of the file to check. |
| 352 | + * @return 1 if readable, 0 otherwise. |
| 353 | + */ |
| 354 | +int32_t fossil_fstream_is_readable(const char *filename) { |
| 355 | +#ifdef _WIN32 |
| 356 | + DWORD attrs = GetFileAttributesA(filename); |
| 357 | + return (attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY)); |
| 358 | +#else |
| 359 | + return (access(filename, R_OK) == 0) ? 1 : 0; |
| 360 | +#endif |
| 361 | +} |
| 362 | + |
| 363 | +/** |
| 364 | + * Check if a file is writable. |
| 365 | + * |
| 366 | + * This function checks if a file has write permissions. |
| 367 | + * |
| 368 | + * @param filename The name of the file to check. |
| 369 | + * @return 1 if writable, 0 otherwise. |
| 370 | + */ |
| 371 | +int32_t fossil_fstream_is_writable(const char *filename) { |
| 372 | +#ifdef _WIN32 |
| 373 | + DWORD attrs = GetFileAttributesA(filename); |
| 374 | + if (attrs == INVALID_FILE_ATTRIBUTES || (attrs & FILE_ATTRIBUTE_DIRECTORY)) { |
| 375 | + return 0; |
| 376 | + } |
| 377 | + return !(attrs & FILE_ATTRIBUTE_READONLY); |
| 378 | +#else |
| 379 | + return (access(filename, W_OK) == 0) ? 1 : 0; |
| 380 | +#endif |
| 381 | +} |
| 382 | + |
| 383 | +/** |
| 384 | + * Check if a file is executable. |
| 385 | + * |
| 386 | + * This function checks if a file has execute permissions. |
| 387 | + * |
| 388 | + * @param filename The name of the file to check. |
| 389 | + * @return 1 if executable, 0 otherwise. |
| 390 | + */ |
| 391 | +int32_t fossil_fstream_is_executable(const char *filename) { |
| 392 | +#ifdef _WIN32 |
| 393 | + // On Windows, executables typically have extensions like .exe, .bat, .cmd |
| 394 | + const char *ext = strrchr(filename, '.'); |
| 395 | + return (ext && (_stricmp(ext, ".exe") == 0 || _stricmp(ext, ".bat") == 0 || _stricmp(ext, ".cmd") == 0)) ? 1 : 0; |
| 396 | +#else |
| 397 | + return (access(filename, X_OK) == 0) ? 1 : 0; |
| 398 | +#endif |
| 399 | +} |
| 400 | + |
| 401 | +/** |
| 402 | + * Set file permissions. |
| 403 | + * |
| 404 | + * This function sets the permissions for a file. |
| 405 | + * |
| 406 | + * @param filename The name of the file to set permissions for. |
| 407 | + * @param mode The permissions to set (POSIX: chmod-style). |
| 408 | + * @return 0 on success, non-zero on failure. |
| 409 | + */ |
| 410 | +int32_t fossil_fstream_set_permissions(const char *filename, int32_t mode) { |
| 411 | +#ifdef _WIN32 |
| 412 | + DWORD attrs = GetFileAttributesA(filename); |
| 413 | + if (attrs == INVALID_FILE_ATTRIBUTES) { |
| 414 | + return -1; // File not found or other error |
| 415 | + } |
| 416 | + |
| 417 | + if (mode & _S_IWRITE) { |
| 418 | + attrs &= ~FILE_ATTRIBUTE_READONLY; // Remove readonly |
| 419 | + } else { |
| 420 | + attrs |= FILE_ATTRIBUTE_READONLY; // Add readonly |
| 421 | + } |
| 422 | + |
| 423 | + return (SetFileAttributesA(filename, attrs) != 0) ? 0 : -1; |
| 424 | +#else |
| 425 | + return chmod(filename, mode); |
| 426 | +#endif |
| 427 | +} |
| 428 | + |
| 429 | +/** |
| 430 | + * Get file permissions. |
| 431 | + * |
| 432 | + * This function retrieves the permissions of a file. |
| 433 | + * |
| 434 | + * @param filename The name of the file to retrieve permissions for. |
| 435 | + * @param mode Pointer to store the retrieved permissions (POSIX style). |
| 436 | + * @return 0 on success, non-zero on failure. |
| 437 | + */ |
| 438 | +int32_t fossil_fstream_get_permissions(const char *filename, int32_t *mode) { |
| 439 | + if (!mode) { |
| 440 | + return -1; // Null pointer error |
| 441 | + } |
| 442 | + |
| 443 | +#ifdef _WIN32 |
| 444 | + DWORD attrs = GetFileAttributesA(filename); |
| 445 | + if (attrs == INVALID_FILE_ATTRIBUTES) { |
| 446 | + return -1; // File not found or other error |
| 447 | + } |
| 448 | + |
| 449 | + *mode = _S_IREAD; |
| 450 | + if (!(attrs & FILE_ATTRIBUTE_READONLY)) { |
| 451 | + *mode |= _S_IWRITE; |
| 452 | + } |
| 453 | + return 0; |
| 454 | +#else |
| 455 | + struct stat st; |
| 456 | + if (stat(filename, &st) != 0) { |
| 457 | + return -1; // File not found or error |
| 458 | + } |
| 459 | + *mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); // User, Group, Other permissions |
| 460 | + return 0; |
| 461 | +#endif |
| 462 | +} |
0 commit comments