Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions tools/please_pex/preamble/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
#include "util.h"

/*
* get_pex_dir stores the canonical absolute path to the .pex file in pex_dir. It returns NULL on
* get_pex_path stores the canonical absolute path to the .pex file in pex_dir. It returns NULL on
* success and an error on failure.
*/
err_t *get_pex_dir(char **pex_dir) {
err_t *get_pex_path(char **pex_path) {
char *exe_path = NULL;
char *exe_realpath = NULL;
char *exe_dir = NULL;
err_t *err = NULL;

#if defined(__linux__)
Expand Down Expand Up @@ -54,22 +52,20 @@ err_t *get_pex_dir(char **pex_dir) {
#error "Unsupported operating system"
#endif

if ((exe_realpath = realpath(exe_path, NULL)) == NULL) {
if (((*pex_path) = realpath(exe_path, NULL)) == NULL) {
err = err_from_errno("realpath");
goto end;
}

exe_dir = dirname(exe_realpath);
if (((*pex_dir) = strdup(exe_dir)) == NULL) {
err = err_from_errno("strdup");
goto end;
}
log_debug("Canonical path of .pex file is %s", (*pex_path));

end:
#ifndef __linux__
FREE(exe_path);
#endif
FREE(exe_realpath);

if (err != NULL) {
FREE(pex_path);
}

return err;
}
Expand All @@ -86,17 +82,19 @@ err_t *get_pex_dir(char **pex_dir) {
* tree, get_plz_bin_path fails.
*/
err_t *get_plz_bin_path(char **path) {
char *pex_path = NULL;
char *pex_dir = NULL;
char *tmp_dir = NULL;
char *tmp_dir_realpath = NULL;
size_t pex_dir_len = 0;
size_t tmp_dir_len = 0;
err_t *err = NULL;

if ((err = get_pex_dir(&pex_dir)) != NULL) {
err = err_wrap("get_pex_dir", err);
if ((err = get_pex_path(&pex_path)) != NULL) {
err = err_wrap("get_pex_path", err);
goto end;
}
pex_dir = dirname(pex_path);

if (getenv("PLZ_ENV") != NULL) {
if ((tmp_dir = getenv("TMP_DIR")) == NULL) {
Expand Down Expand Up @@ -153,7 +151,7 @@ err_t *get_plz_bin_path(char **path) {
}

end:
FREE(pex_dir);
FREE(pex_path);
FREE(tmp_dir_realpath);

return err;
Expand Down
1 change: 1 addition & 0 deletions tools/please_pex/preamble/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "pexerror.h"

err_t *get_pex_path(char **pex_path);
err_t *get_plz_bin_path(char **path);

#endif /* !__PATH_H__ */
8 changes: 7 additions & 1 deletion tools/please_pex/preamble/preamble.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ static err_t *get_interpreters(const cJSON *config, strlist_t **interps) {

int main(int argc, char **argv) {
err_t *err = NULL;
char *pex_path = NULL;
const cJSON *config = NULL;
int config_args_len = 0;
strlist_t *config_args = NULL;
Expand All @@ -301,7 +302,12 @@ int main(int argc, char **argv) {
return 1;
}

if ((err = get_config(argv[0], &config)) != NULL) {
if ((err = get_pex_path(&pex_path)) != NULL) {
log_fatal("Failed to resolve path to .pex file: %s", err_str(err));
return 1;
}

if ((err = get_config(pex_path, &config)) != NULL) {
log_fatal("Failed to get .pex preamble configuration: %s", err_str(err));
return 1;
}
Expand Down