Skip to content

Commit a0f4f35

Browse files
add permissions methods and functions
1 parent e403a76 commit a0f4f35

File tree

2 files changed

+236
-11
lines changed

2 files changed

+236
-11
lines changed

code/logic/fossil/io/stream.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,58 @@ int32_t fossil_fstream_rename(const char *old_filename, const char *new_filename
205205
*/
206206
int fossil_fstream_get_type(const char *filename);
207207

208+
/**
209+
* Check if a file is readable.
210+
*
211+
* This function checks if a file has read permissions.
212+
*
213+
* @param filename The name of the file to check.
214+
* @return 1 if readable, 0 otherwise.
215+
*/
216+
int32_t fossil_fstream_is_readable(const char *filename);
217+
218+
/**
219+
* Check if a file is writable.
220+
*
221+
* This function checks if a file has write permissions.
222+
*
223+
* @param filename The name of the file to check.
224+
* @return 1 if writable, 0 otherwise.
225+
*/
226+
int32_t fossil_fstream_is_writable(const char *filename);
227+
228+
/**
229+
* Check if a file is executable.
230+
*
231+
* This function checks if a file has execute permissions.
232+
*
233+
* @param filename The name of the file to check.
234+
* @return 1 if executable, 0 otherwise.
235+
*/
236+
int32_t fossil_fstream_is_executable(const char *filename);
237+
238+
/**
239+
* Set file permissions.
240+
*
241+
* This function sets the permissions for a file.
242+
*
243+
* @param filename The name of the file to set permissions for.
244+
* @param mode The permissions to set (POSIX: chmod-style).
245+
* @return 0 on success, non-zero on failure.
246+
*/
247+
int32_t fossil_fstream_set_permissions(const char *filename, int32_t mode);
248+
249+
/**
250+
* Get file permissions.
251+
*
252+
* This function retrieves the permissions of a file.
253+
*
254+
* @param filename The name of the file to retrieve permissions for.
255+
* @param mode Pointer to store the retrieved permissions (POSIX style).
256+
* @return 0 on success, non-zero on failure.
257+
*/
258+
int32_t fossil_fstream_get_permissions(const char *filename, int32_t *mode);
259+
208260
#ifdef __cplusplus
209261
}
210262

@@ -430,6 +482,68 @@ namespace fossil {
430482
return fossil_fstream_get_type(filename);
431483
}
432484

485+
/**
486+
* Check if a file is readable.
487+
*
488+
* This function checks if a file has read permissions.
489+
*
490+
* @param filename The name of the file to check.
491+
* @return 1 if readable, 0 otherwise.
492+
*/
493+
static int32_t is_readable(const char *filename) {
494+
return fossil_fstream_is_readable(filename);
495+
}
496+
497+
/**
498+
* Check if a file is writable.
499+
*
500+
* This function checks if a file has write permissions.
501+
*
502+
* @param filename The name of the file to check.
503+
* @return 1 if writable, 0 otherwise.
504+
*/
505+
static int32_t is_writable(const char *filename) {
506+
return fossil_fstream_is_writable(filename);
507+
}
508+
509+
/**
510+
* Check if a file is executable.
511+
*
512+
* This function checks if a file has execute permissions.
513+
*
514+
* @param filename The name of the file to check.
515+
* @return 1 if executable, 0 otherwise.
516+
*/
517+
static int32_t is_executable(const char *filename) {
518+
return fossil_fstream_is_executable(filename);
519+
}
520+
521+
/**
522+
* Set file permissions.
523+
*
524+
* This function sets the permissions for a file.
525+
*
526+
* @param filename The name of the file to set permissions for.
527+
* @param mode The permissions to set (POSIX: chmod-style).
528+
* @return 0 on success, non-zero on failure.
529+
*/
530+
static int32_t set_permissions(const char *filename, int32_t mode) {
531+
return fossil_fstream_set_permissions(filename, mode);
532+
}
533+
534+
/**
535+
* Get file permissions.
536+
*
537+
* This function retrieves the permissions of a file.
538+
*
539+
* @param filename The name of the file to retrieve permissions for.
540+
* @param mode Pointer to store the retrieved permissions (POSIX style).
541+
* @return 0 on success, non-zero on failure.
542+
*/
543+
static int32_t get_permissions(const char *filename, int32_t *mode) {
544+
return fossil_fstream_get_permissions(filename, mode);
545+
}
546+
433547
};
434548

435549
} // namespace io

code/logic/stream.c

Lines changed: 122 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,15 @@
1717
#include <stdlib.h>
1818
#include <string.h>
1919
#include <errno.h>
20+
#include <sys/stat.h>
2021

2122
#ifdef _WIN32
22-
#include <windows.h>
23-
#include <io.h>
23+
#include <windows.h>
2424
#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>
2927
#endif
3028

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-
3629
typedef enum {
3730
FOSSIL_BUFFER_SMALL = 100,
3831
FOSSIL_BUFFER_MEDIUM = 500,
@@ -349,3 +342,121 @@ int fossil_fstream_get_type(const char *filename) {
349342
int32_t fossil_fstream_is_open(const fossil_fstream_t *stream) {
350343
return stream != NULL && stream->file != NULL;
351344
}
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

Comments
 (0)