Skip to content

Commit

Permalink
Merge pull request #186 from MetPX/issue140_rs
Browse files Browse the repository at this point in the history
Add realpath_post_baseDir for #140
  • Loading branch information
petersilva authored Nov 20, 2024
2 parents c7b91a2 + 8f061bc commit 7da1389
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
66 changes: 45 additions & 21 deletions sr_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,13 +873,23 @@ int sr_config_parse_option(struct sr_config_s *sr_cfg, char *option, char *arg,
if (sr_cfg->post_baseDir)
free(sr_cfg->post_baseDir);
sr_cfg->post_baseDir = argument;
if (sr_cfg->realpath_post_baseDir) {
free(sr_cfg->realpath_post_baseDir);
sr_cfg->realpath_post_baseDir = NULL;
}
sr_cfg->realpath_post_baseDir = realpath(sr_cfg->post_baseDir, sr_cfg->realpath_post_baseDir);
argument = NULL;

} else if (!strcmp(option, "post_base_dir") || !strcmp(option, "pbd")
|| !strcmp(option, "post_baseDir") || !strcmp(option, "post_basedir")) {
if (sr_cfg->post_baseDir)
free(sr_cfg->post_baseDir);
sr_cfg->post_baseDir = argument;
if (sr_cfg->realpath_post_baseDir) {
free(sr_cfg->realpath_post_baseDir);
sr_cfg->realpath_post_baseDir = NULL;
}
sr_cfg->realpath_post_baseDir = realpath(sr_cfg->post_baseDir, sr_cfg->realpath_post_baseDir);
argument = NULL;
retval = 2;

Expand Down Expand Up @@ -1292,6 +1302,9 @@ void sr_config_free(struct sr_config_s *sr_cfg)
if (sr_cfg->post_baseDir)
free(sr_cfg->post_baseDir);
sr_cfg->post_baseDir = NULL;
if (sr_cfg->realpath_post_baseDir)
free(sr_cfg->realpath_post_baseDir);
sr_cfg->realpath_post_baseDir = NULL;
if (sr_cfg->exchange)
free(sr_cfg->exchange);
sr_cfg->exchange = NULL;
Expand Down Expand Up @@ -1418,6 +1431,7 @@ void sr_config_init(struct sr_config_s *sr_cfg, const char *progname)
sr_cfg->delete = 0;
sr_cfg->directory = NULL;
sr_cfg->post_baseDir = NULL;
sr_cfg->realpath_post_baseDir = NULL;
sr_cfg->durable = 1;
sr_cfg->events =
(SR_EVENT_CREATE | SR_EVENT_MODIFY | SR_EVENT_DELETE | SR_EVENT_LINK | SR_EVENT_MKDIR |
Expand Down Expand Up @@ -1931,25 +1945,35 @@ int sr_config_finalize(struct sr_config_s *sr_cfg, const int is_consumer)
sr_cfg->to = strdup(sr_cfg->post_broker->hostname);
}
if (!(sr_cfg->post_baseDir)) {
if (sr_cfg->post_baseUrl) {
if (!strncmp(sr_cfg->post_baseUrl, "file:", 5)) {
sr_cfg->post_baseDir = strdup(sr_cfg->post_baseUrl + 5);
} else if (!strncmp(sr_cfg->post_baseUrl, "sftp:", 5)) {
char *slash;
char *at;
slash = index(sr_cfg->post_baseUrl+7,'/');
at = index(sr_cfg->post_baseUrl+7,'@');
if (slash) {
while (at > slash) { // there is a slash in a password...
slash = index(slash,'/');
}
if (slash) {
while (*(slash+1) == '/') slash++;
sr_cfg->post_baseDir = strdup(slash);
}
}
}
}
if (sr_cfg->post_baseUrl) {
if (!strncmp(sr_cfg->post_baseUrl, "file:", 5)) {
sr_cfg->post_baseDir = strdup(sr_cfg->post_baseUrl + 5);
if (sr_cfg->realpath_post_baseDir) {
free(sr_cfg->realpath_post_baseDir);
sr_cfg->realpath_post_baseDir = NULL;
}
sr_cfg->realpath_post_baseDir = realpath(sr_cfg->post_baseDir, sr_cfg->realpath_post_baseDir);
} else if (!strncmp(sr_cfg->post_baseUrl, "sftp:", 5)) {
char *slash;
char *at;
slash = index(sr_cfg->post_baseUrl+7,'/');
at = index(sr_cfg->post_baseUrl+7,'@');
if (slash) {
while (at > slash) { // there is a slash in a password...
slash = index(slash,'/');
}
if (slash) {
while (*(slash+1) == '/') slash++;
sr_cfg->post_baseDir = strdup(slash);
if (sr_cfg->realpath_post_baseDir) {
free(sr_cfg->realpath_post_baseDir);
sr_cfg->realpath_post_baseDir = NULL;
}
sr_cfg->realpath_post_baseDir = realpath(sr_cfg->post_baseDir, sr_cfg->realpath_post_baseDir);
}
}
}
}
}
}
if (strcmp(sr_cfg->action, "sanity")) {
Expand All @@ -1958,8 +1982,8 @@ int sr_config_finalize(struct sr_config_s *sr_cfg, const int is_consumer)
sr_cfg->message_ttl, sr_cfg->post_exchange, sr_cfg->post_exchangeSplit,
sr_cfg->post_exchangeSuffix);
sr_log_msg(sr_cfg->logctx, ll,
"\tsource=%s, to=%s, post_baseUrl=%s, post_baseDir=%s\n",
sr_cfg->source, sr_cfg->to, sr_cfg->post_baseUrl, sr_cfg->post_baseDir );
"\tsource=%s, to=%s, post_baseUrl=%s, post_baseDir=%s, realpath_post_baseDir=%s\n",
sr_cfg->source, sr_cfg->to, sr_cfg->post_baseUrl, sr_cfg->post_baseDir, sr_cfg->realpath_post_baseDir);
sr_log_msg(sr_cfg->logctx, ll,"\ttopicPrefix=%s, post_topicPrefix=%s, pid=%d\n", sr_cfg->topicPrefix,
sr_cfg->post_topicPrefix, sr_cfg->pid);
sr_log_msg(sr_cfg->logctx, ll, "man sr3_cpost(1) for more information\n");
Expand Down
1 change: 1 addition & 0 deletions sr_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ struct sr_config_s {
/**< the list of configurations or files given on the command line.*/
int pipe; /**< pipe mode, read file names from standard input*/
char *post_baseDir; /**< the local directory at the root of the url tree.*/
char *realpath_post_baseDir; /**< the local directory at the root of the url tree (set from realpath of post_baseDir).*/
char *post_baseUrl; /**< the url that corresponds to the base directory.*/
struct sr_broker_s *post_broker;
/**< the broker to post to.*/
Expand Down
28 changes: 20 additions & 8 deletions sr_post.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ int sr_file2message_start(struct sr_context *sr_c, const char *pathspec,
char *c, *d;
int lasti;
int linklen;
int pbdlen = 0;
char linkstr[PATH_MAXNUL];
char tmprk[PATH_MAXNUL + 100];

Expand Down Expand Up @@ -722,19 +723,30 @@ int sr_file2message_start(struct sr_context *sr_c, const char *pathspec,
}
*c = '\0';

// build relPath that gets posted by removing (realpath_)post_baseDir from fn
if (sr_c->cfg->post_baseDir && (strlen(sr_c->cfg->post_baseDir) > 1 ) ) {
// first try post_baseDir
// the +1 is to because baseDir is always absolute, and relPath is always relative
drfound = strstr(fn, (sr_c->cfg->post_baseDir)+1);

// replace only if at the beginning of the string.
// replace post_baseDir only if at the beginning of the string.
if (drfound==fn+1) {
drfound += strlen(sr_c->cfg->post_baseDir);
pbdlen = strlen(sr_c->cfg->post_baseDir);
drfound += (sr_c->cfg->post_baseDir[pbdlen-1] == '/') ? pbdlen - 1 : pbdlen; //adjust for trailing /
strcpy(m->relPath, drfound);
} else if (absolute_path) {
sr_log_msg(sr_c->cfg->logctx,LOG_ERROR, "%s posting outside of post_baseDir (%s) invalid path: %s\n",
sr_c->cfg->progname, sr_c->cfg->post_baseDir, fn );
return(0);
}

// if post_baseDir didn't match, try realpath_post_baseDir
} else {
drfound = strstr(fn, (sr_c->cfg->realpath_post_baseDir)+1);
if (drfound==fn+1) {
pbdlen = strlen(sr_c->cfg->realpath_post_baseDir);
drfound += (sr_c->cfg->realpath_post_baseDir[pbdlen-1] == '/') ? pbdlen - 1 : pbdlen; //adjust for trailing /
strcpy(m->relPath, drfound);
} else if (absolute_path) {
sr_log_msg(sr_c->cfg->logctx,LOG_ERROR, "%s invalid path: %s is outside of post_baseDir (%s)\n",
sr_c->cfg->progname, fn, sr_c->cfg->post_baseDir);
return(0);
}
}
}

// Strip option: remove prefix from path according to / #
Expand Down

0 comments on commit 7da1389

Please sign in to comment.