| 1 |
#include "getmode.h" |
| 2 |
#include "utils.h" |
| 3 |
|
| 4 |
#if (defined(__APPLE__) && defined(__MACH__)) || defined(__OpenBSD__) || \ |
| 5 |
defined(__FreeBSD__) || defined(__NetBSD__) |
| 6 |
#include <string.h> /* for strmode */ |
| 7 |
#include <unistd.h> /* for getmode / setmode */ |
| 8 |
#else |
| 9 |
#include "bsd/string.h" /* for strmode */ |
| 10 |
#include "bsd/unistd.h" /* for getmode / setmode */ |
| 11 |
#endif |
| 12 |
|
| 13 |
#include <R.h> |
| 14 |
#include <Rinternals.h> /* for Rf_error */ |
| 15 |
|
| 16 |
#include <sys/stat.h> |
| 17 |
|
| 18 |
#include <cstdlib> |
| 19 |
|
| 20 |
unsigned short getmode__(const char* mode_str, unsigned short mode) { |
| 21 |
void* out = setmode(mode_str); |
| 22 |
if (out == NULL) { |
| 23 |
// TODO: use stop_for_error here |
| 24 |
Rf_error("Invalid mode '%s'", mode_str); |
| 25 |
} |
| 26 |
mode_t res = getmode(out, mode); |
| 27 |
free(out); |
| 28 |
return res; |
| 29 |
} |
| 30 |
|
| 31 |
std::string strmode__(unsigned short mode) { |
| 32 |
char out[12]; |
| 33 |
strmode(mode, out); |
| 34 |
|
| 35 |
// The last character is always a space, we will set it to NUL |
| 36 |
out[10] = '\0'; |
| 37 |
|
| 38 |
// The first character is the file type, so we skip it. |
| 39 |
return out + 1; |
| 40 |
} |
| 41 |
|
| 42 |
std::string file_code__(const std::string& path, unsigned short mode) { |
| 43 |
switch (mode & S_IFMT) { |
| 44 |
case S_IFDIR: |
| 45 |
if (mode & S_IWOTH) |
| 46 |
if (mode & S_ISTXT) |
| 47 |
return "tw"; |
| 48 |
else |
| 49 |
return "ow"; |
| 50 |
else |
| 51 |
return "di"; |
| 52 |
case S_IFLNK: |
| 53 |
return "ln"; |
| 54 |
case S_IFSOCK: |
| 55 |
return "so"; |
| 56 |
case S_IFIFO: |
| 57 |
return "pi"; |
| 58 |
case S_IFBLK: |
| 59 |
return "db"; |
| 60 |
case S_IFCHR: |
| 61 |
return "cd"; |
| 62 |
default:; |
| 63 |
} |
| 64 |
if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { |
| 65 |
if (mode & S_ISUID) |
| 66 |
return "su"; |
| 67 |
else if (mode & S_ISGID) |
| 68 |
return "sg"; |
| 69 |
else |
| 70 |
return "ex"; |
| 71 |
} |
| 72 |
return ""; |
| 73 |
} |