Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor joinpaths() #394

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
15 changes: 8 additions & 7 deletions src/config_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ parse_line_waste(st_waste * waste_curr, st_canfigger_node * node,
trim_char('/', node->value);

sn_check(snprintf(tmp_waste_parent_folder, sizeof tmp_waste_parent_folder,
"%s", node->value), sizeof tmp_waste_parent_folder);
"%s", node->value), sizeof tmp_waste_parent_folder);

if (realize_str(tmp_waste_parent_folder, homedir, uid) != 0)
{
Expand Down Expand Up @@ -223,13 +223,13 @@ parse_line_waste(st_waste * waste_curr, st_canfigger_node * node,
waste_curr->removable = removable ? true : false;

/* make the parent... */
waste_curr->parent = malloc(strlen(tmp_waste_parent_folder) + 1);
if (!waste_curr->parent)
fatal_malloc();
strcpy(waste_curr->parent, tmp_waste_parent_folder);
sn_check(snprintf
(waste_curr->parent, sizeof_memb(st_waste, parent), "%s",
tmp_waste_parent_folder), sizeof_memb(st_waste, parent));

/* and the files... */
waste_curr->files = join_paths(waste_curr->parent, lit_files);
join_paths2(waste_curr->files, sizeof_memb(st_waste, files),
waste_curr->parent, lit_files);
waste_curr->len_files = strlen(waste_curr->files);

int p_state = check_pathname_state(waste_curr->files);
Expand All @@ -246,7 +246,8 @@ parse_line_waste(st_waste * waste_curr, st_canfigger_node * node,
else if (p_state == P_STATE_ERR)
exit(p_state);

waste_curr->info = join_paths(waste_curr->parent, lit_info);
join_paths2(waste_curr->info, sizeof_memb(st_waste, info),
waste_curr->parent, lit_info);
waste_curr->len_info = strlen(waste_curr->info);

if ((p_state = check_pathname_state(waste_curr->info)) == P_STATE_ENOENT)
Expand Down
4 changes: 4 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#define LEN_MAX_ESCAPED_PATH (((PATH_MAX - 1) * 3) + 1)

// Get the size of a struct member without creating an instance
// of the struct
#define sizeof_memb(a,m) sizeof(((a*)0)->m)

#define EBUF 11

extern int verbose;
6 changes: 3 additions & 3 deletions src/trashinfo_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ typedef struct st_waste st_waste;
struct st_waste
{
/** The parent directory, e.g. $HOME/.local/share/Trash */
char *parent;
char parent[LEN_MAX_PATH];

/*! The info directory (where .trashinfo files are written) will be appended to the parent directory */
char *info;
char info[LEN_MAX_PATH];
int len_info;

/** Appended to the parent directory, where files are moved to when they are rmw'ed
*/
char *files;
char files[LEN_MAX_PATH];
int len_files;

/** The device number of the filesystem on which the file resides. rmw does
Expand Down
50 changes: 47 additions & 3 deletions src/utils_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ dispose_waste(st_waste * node)
if (node != NULL)
{
dispose_waste(node->next_node);
free(node->parent);
free(node->files);
free(node->info);
if (node->media_root != NULL)
free(node->media_root);
free(node);
Expand Down Expand Up @@ -433,6 +430,35 @@ trim_char(const int c, char *str)
}


void
real_join_paths2(char *path, size_t size, const char *argv, ...)
{
*path = '\0';
va_list ap;
char *buf = (char *) argv;
va_start(ap, argv);

while (buf != NULL)
{
size_t len = 0;
char *dup_buf = strdup(buf);
if (!dup_buf)
fatal_malloc();
trim_char('/', dup_buf);
len = strlen(path);
int max_len = size - len;
int r = snprintf(path + len, max_len, "%s/", dup_buf);
free(dup_buf);
sn_check(r, max_len);
buf = va_arg(ap, char *);
}

va_end(ap);
trim_char('/', path);
return;
}


char *
real_join_paths(const char *argv, ...)
{
Expand Down Expand Up @@ -627,6 +653,23 @@ test_join_paths(void)
return;
}


static void
test_join_paths2(void)
{
char path[LEN_MAX_PATH];
*path = '\0';
join_paths2(path, LEN_MAX_PATH, "home", "foo//", "bar");
assert(strcmp(path, "home/foo/bar") == 0);

*path = '\0';
join_paths2(path, LEN_MAX_PATH, "/home/foo", "bar", "world/");
assert(strcmp(path, "/home/foo/bar/world") == 0);

return;
}


void
test_trim_char(void)
{
Expand Down Expand Up @@ -737,6 +780,7 @@ main()
test_rmw_dirname();
test_make_size_human_readable();
test_join_paths();
test_join_paths2();
test_trim_char();
test_check_pathname_state(HOMEDIR);
test_is_dir_f(HOMEDIR);
Expand Down
3 changes: 3 additions & 0 deletions src/utils_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ enum

#define join_paths(...) real_join_paths(__VA_ARGS__, NULL)

#define join_paths2(...) real_join_paths2(__VA_ARGS__, NULL)

#define LEN_MAX_HUMAN_READABLE_SIZE (sizeof "xxxx.y GiB")
#define LEN_MAX_FILE_DETAILS (LEN_MAX_HUMAN_READABLE_SIZE + sizeof "[] (D)" - 1)

Expand Down Expand Up @@ -63,6 +65,7 @@ char *resolve_path(const char *file, const char *b);
void trim_char(const int c, char *str);

char *real_join_paths(const char *argv, ...);
void real_join_paths2(char *path, size_t size, const char *argv, ...);

bool is_dir_f(const char *pathname);

Expand Down