Skip to content

Commit

Permalink
remove resolved parameter from flexible_realpath
Browse files Browse the repository at this point in the history
  • Loading branch information
droundy committed Mar 19, 2017
1 parent 587b92b commit b2e026f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 53 deletions.
6 changes: 3 additions & 3 deletions bigbro-linux.c
Expand Up @@ -323,7 +323,7 @@ static int save_syscall_access(pid_t child, rw_status *h) {
}
char *rawpath = interpret_path_at(child, dirfd, arg);
free(arg);
char *abspath = flexible_realpath(rawpath, NULL, h, look_for_symlink, false);
char *abspath = flexible_realpath(rawpath, h, look_for_symlink, false);
delete_from_hashset(&h->read, abspath);
delete_from_hashset(&h->readdir, abspath);
delete_from_hashset(&h->written, abspath);
Expand Down Expand Up @@ -417,7 +417,7 @@ static int save_syscall_access(pid_t child, rw_status *h) {
if (arg) {
debugprintf("%d: %s('%s') -> %d\n", child, name, arg, retval);
char *rawpath = interpret_path_at(child, dirfd, arg);
char *abspath = flexible_realpath(rawpath, NULL, h, look_for_file_or_directory, false);
char *abspath = flexible_realpath(rawpath, h, look_for_file_or_directory, false);
insert_hashset(&h->mkdir, abspath);
free(rawpath);
free(abspath);
Expand Down Expand Up @@ -474,7 +474,7 @@ static int save_syscall_access(pid_t child, rw_status *h) {
}
if (from) {
char *rawpath = interpret_path_at(child, dirfd, from);
char *abspath = flexible_realpath(rawpath, NULL, h, look_for_symlink, false);
char *abspath = flexible_realpath(rawpath, h, look_for_symlink, false);
free(rawpath);
free(from);
from = abspath;
Expand Down
6 changes: 3 additions & 3 deletions fileaccesses.c
Expand Up @@ -37,13 +37,13 @@ int main(int argc, char **argv) {
// the following are to test the corner and error cases of
// flexible_realpath.
#ifndef _WIN32
assert(! flexible_realpath(NULL, NULL, NULL,
assert(! flexible_realpath(NULL, NULL,
look_for_file_or_directory, true));
assert(! flexible_realpath("", NULL, NULL,
assert(! flexible_realpath("", NULL,
look_for_file_or_directory, true));
// the following should give null because flexible_realpath wants an
// absolute path.
assert(! flexible_realpath("tmp", NULL, NULL,
assert(! flexible_realpath("tmp", NULL,
look_for_file_or_directory, true));
#endif

Expand Down
6 changes: 3 additions & 3 deletions linux-proc.h
Expand Up @@ -42,7 +42,7 @@ static inline char *interpret_path_at(pid_t pid, int fd, const char *path) {

static inline void read_dir_fd(pid_t pid, int dirfd, rw_status *h) {
char *rawpath = interpret_path_at(pid, dirfd, ".");
char *abspath = flexible_realpath(rawpath, NULL, h, look_for_file_or_directory, false);
char *abspath = flexible_realpath(rawpath, h, look_for_file_or_directory, false);
if (!lookup_in_hash(&h->mkdir, abspath)) {
insert_hashset(&h->readdir, abspath);
}
Expand All @@ -54,7 +54,7 @@ static inline void read_something_at(pid_t pid, int dirfd, const char *path,
rw_status *h, enum last_symlink_handling lh,
bool failure_is_okay) {
char *rawpath = interpret_path_at(pid, dirfd, path);
char *abspath = flexible_realpath(rawpath, NULL, h, lh, failure_is_okay);
char *abspath = flexible_realpath(rawpath, h, lh, failure_is_okay);
struct stat st;
if (!lookup_in_hash(&h->written, abspath) && !stat(abspath, &st) && S_ISREG(st.st_mode)) {
insert_hashset(&h->read, abspath);
Expand All @@ -67,7 +67,7 @@ static inline void write_something_at(pid_t pid, int dirfd, const char *path,
rw_status *h, enum last_symlink_handling lh,
bool failure_is_okay) {
char *rawpath = interpret_path_at(pid, dirfd, path);
char *abspath = flexible_realpath(rawpath, NULL, h, lh, failure_is_okay);
char *abspath = flexible_realpath(rawpath, h, lh, failure_is_okay);
insert_hashset(&h->written, abspath);
delete_from_hashset(&h->read, abspath);
free(rawpath);
Expand Down
37 changes: 7 additions & 30 deletions realpath.h
Expand Up @@ -55,20 +55,14 @@ typedef struct {

/* Return the canonical absolute name of file NAME. A canonical name
does not contain any `.', `..' components nor any repeated path
separators ('/') or symlinks. All path components must exist. If
RESOLVED is null, the result is malloc'd; otherwise, if the
canonical name is PATH_MAX chars or more, returns null with `errno'
set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
returns the name in RESOLVED. If the name cannot be resolved and
RESOLVED is non-NULL, it contains the path of the first component
that cannot be resolved. If the path can be resolved, RESOLVED
holds the same value as the value returned. */
separators ('/') or symlinks. All path components must exist. The
result is malloc'd. */

enum last_symlink_handling {
look_for_file_or_directory, look_for_symlink
};

static inline char *flexible_realpath(const char *name, char *resolved,
static inline char *flexible_realpath(const char *name,
rw_status *h,
enum last_symlink_handling lasth,
bool failure_is_okay) {
Expand All @@ -82,10 +76,6 @@ static inline char *flexible_realpath(const char *name, char *resolved,
int num_links = 0;

if (name == NULL) {
/* As per Single Unix Specification V2 we must return an error if
either parameter is a null pointer. We extend this to allow
the RESOLVED parameter to be NULL in case the we are expected to
allocate the room for the return value. */
errno = (EINVAL);
return NULL;
}
Expand All @@ -99,14 +89,9 @@ static inline char *flexible_realpath(const char *name, char *resolved,

path_max = PATH_MAX;

if (resolved == NULL) {
rpath = malloc(path_max);
if (rpath == NULL) {
return NULL;
}
} else {
rpath = resolved;
}
rpath = malloc(path_max);
if (rpath == NULL) return NULL;

rpath_limit = rpath + path_max;

if (name[0] != '/') {
Expand Down Expand Up @@ -153,12 +138,6 @@ static inline char *flexible_realpath(const char *name, char *resolved,
ptrdiff_t dest_offset = dest - rpath;
char *new_rpath;

if (resolved) {
errno = (ENAMETOOLONG);
if (dest > rpath + 1) dest--;
*dest = '\0';
goto error;
}
new_size = rpath_limit - rpath;
if (end - start + 1 > path_max) {
new_size += end - start + 1;
Expand Down Expand Up @@ -229,15 +208,13 @@ static inline char *flexible_realpath(const char *name, char *resolved,
if (dest > rpath + 1 && dest[-1] == '/') --dest;
*dest = '\0';

assert (resolved == NULL || resolved == rpath);
if (buf) free(buf);
if (extra_buf) free(extra_buf);
return rpath;

error:
assert (resolved == NULL || resolved == rpath);
if (buf) free(buf);
if (extra_buf) free(extra_buf);
if (resolved == NULL) free(rpath);
free(rpath);
return NULL;
}
16 changes: 2 additions & 14 deletions tests/unit/realpath.c
Expand Up @@ -70,7 +70,7 @@ int main() {
else printf("parent '%s' is NOT a directory\n", parent);

char *actual_rp = realpath(buf, 0);
char *rp = flexible_realpath(buf, 0, &st, look_for_file_or_directory, true);
char *rp = flexible_realpath(buf, &st, look_for_file_or_directory, true);
printf("input %s\n", buf);
printf("actual_rp %s\n", actual_rp);
printf("flexible_realpath look_for_file_or_directory true returns %s\n", rp);
Expand All @@ -89,7 +89,7 @@ int main() {
assert(actual_rp);
assert(!strcmp(rp, actual_rp));
}
char *symrp = flexible_realpath(buf, 0, &st, look_for_symlink, true);
char *symrp = flexible_realpath(buf, &st, look_for_symlink, true);
if (*buf != '/') {
printf("relative paths count as failure...\n");
assert(!rp);
Expand All @@ -102,18 +102,6 @@ int main() {
assert(!strcmp(rp, symrp));
}
}
/* rp = flexible_realpath(buf, resolved, &st, look_for_file_or_directory, false); */
/* if (!exists) { */
/* assert(!rp); */
/* } else { */
/* assert(!strcmp(rp, actual_rp)); */
/* } */
/* symrp = flexible_realpath(buf, resolved, &st, look_for_symlink, false); */
/* if (is_symlink) { */
/* assert(rp); */
/* } else { */
/* assert(!strcmp(rp, symrp)); */
/* } */
}
free_hashset(&st.read);

Expand Down

0 comments on commit b2e026f

Please sign in to comment.