* Import chmod() from sbase as r_file_chmod()

- Support recursive chmod
  - Handles string-based octal/expression formats
  - Used by rabin2 -c to chmod +x the created binary
This commit is contained in:
pancake 2011-07-25 23:42:39 +02:00
parent a173eedcaf
commit d2cf5e4425
5 changed files with 158 additions and 8 deletions

View File

@ -779,6 +779,7 @@ int main(int argc, char **argv) {
}
}
if (action & ACTION_CREATE) {
// TODO: move in a function outside
RBuffer *b;
int datalen, codelen;
ut8 *data = NULL, *code = NULL;
@ -806,9 +807,10 @@ int main(int argc, char **argv) {
}
b = r_bin_create (bin, code, codelen, data, datalen);
if (b) {
if (r_file_dump (file, b->buf, b->length))
if (r_file_dump (file, b->buf, b->length)) {
eprintf ("dumped %d bytes in '%s'\n", b->length, file);
else eprintf ("error dumping into a.out\n");
r_file_chmod (file, "+x", 0);
} else eprintf ("error dumping into a.out\n");
r_buf_free (b);
} else eprintf ("Cannot create binary for this format '%s'.\n", create);
r_bin_free (bin);

View File

@ -321,6 +321,7 @@ R_API char *r_hex_bin2strdup(const ut8 *in, int len);
R_API int r_hex_to_byte(ut8 *val, ut8 c);
R_API st64 r_hex_bin_truncate (ut64 in, int n);
R_API int r_file_chmod (const char *file, const char *mod, int recursive);
R_API char *r_file_temp (const char *prefix);
R_API char *r_file_path(const char *bin);
R_API const char *r_file_basename (const char *path);

View File

@ -3,7 +3,7 @@ include ../config.mk
NAME=r_util
OBJ=mem.o pool.o num.o str.o re.o hex.o file.o alloca.o range.o log.o
OBJ+=prof.o cache.o sys.o buf.o w32-sys.o base64.o name.o
OBJ+=list.o flist.o ht.o ht64.o mixed.o btree.o
OBJ+=list.o flist.o ht.o ht64.o mixed.o btree.o chmod.o
# DO NOT BUILD r_big api (not yet used and its buggy)
ifeq (1,0)

152
libr/util/chmod.c Normal file
View File

@ -0,0 +1,152 @@
/* radare - LGPL - Copyright 2011 pancake<nopcode.org> */
#include <r_util.h>
#if __UNIX__
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
static int chmodr(const char *, int recursive);
static int parsemode(const char *);
static void recurse(const char *path, int rec, int (*fn)(const char *,int));
static char oper = '=';
static mode_t mode = 0;
R_API int r_file_chmod (const char *file, const char *mod, int recursive) {
#if __UNIX__
oper = '=';
mode = 0;
if (!parsemode (mod))
return R_FALSE;
return chmodr (file, recursive);
#else
return -1;
#endif
}
/* copied from sbase/chmod.c (suckless.org) */
int chmodr(const char *path, int rflag) {
struct stat st;
if (stat (path, &st) == -1)
return 0;
switch (oper) {
case '+':
st.st_mode |= mode;
break;
case '-':
st.st_mode &= ~mode;
break;
case '=':
st.st_mode = mode;
break;
}
if (chmod (path, st.st_mode) == -1) {
eprintf ("chmod %s:", path);
return R_FALSE;
}
if (rflag)
recurse (path, rflag, chmodr);
return R_TRUE;
}
int parsemode(const char *str) {
char *end;
const char *p;
int octal;
mode_t mask = 0;
octal = strtol(str, &end, 8);
if (*end == '\0') {
if (octal & 04000) mode |= S_ISUID;
if (octal & 02000) mode |= S_ISGID;
if (octal & 00400) mode |= S_IRUSR;
if (octal & 00200) mode |= S_IWUSR;
if (octal & 00100) mode |= S_IXUSR;
if (octal & 00040) mode |= S_IRGRP;
if (octal & 00020) mode |= S_IWGRP;
if (octal & 00010) mode |= S_IXGRP;
if (octal & 00004) mode |= S_IROTH;
if (octal & 00002) mode |= S_IWOTH;
if (octal & 00001) mode |= S_IXOTH;
return R_TRUE;
}
for(p = str; *p; p++)
switch(*p) {
/* masks */
case 'u':
mask |= S_IRWXU;
break;
case 'g':
mask |= S_IRWXG;
break;
case 'o':
mask |= S_IRWXO;
break;
case 'a':
mask |= S_IRWXU|S_IRWXG|S_IRWXO;
break;
/* opers */
case '+':
case '-':
case '=':
oper = *p;
break;
/* modes */
case 'r':
mode |= S_IRUSR|S_IRGRP|S_IROTH;
break;
case 'w':
mode |= S_IWUSR|S_IWGRP|S_IWOTH;
break;
case 'x':
mode |= S_IXUSR|S_IXGRP|S_IXOTH;
break;
case 's':
mode |= S_ISUID|S_ISGID;
break;
/* error */
default:
eprintf ("%s: invalid mode\n", str);
return R_FALSE;
}
if (mask)
mode &= mask;
return R_TRUE;
}
char * agetcwd(void) {
char *buf = malloc (4096);
if(!getcwd(buf, 4096))
eprintf("getcwd:");
return buf;
}
static void recurse(const char *path, int rec, int (*fn)(const char *,int)) {
char *cwd;
struct dirent *d;
struct stat st;
DIR *dp;
if(lstat(path, &st) == -1 || !S_ISDIR(st.st_mode))
return;
else if(!(dp = opendir(path)))
eprintf("opendir %s:", path);
cwd = agetcwd();
if(chdir(path) == -1)
eprintf("chdir %s:", path);
while((d = readdir(dp)))
if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
fn(d->d_name, 1);
closedir(dp);
if(chdir(cwd) == -1)
eprintf("chdir %s:", cwd);
free(cwd);
}
#endif

View File

@ -325,8 +325,3 @@ R_API const char *r_file_tmpdir() {
return path;
}
R_API int r_file_chmod (const char *file, int mod) {
#if __UNIX__
return chmod (file, mod);
#endif
}