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

Add a file exists check to adaptived #24

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions adaptived/include/adaptived-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,15 @@ int adaptived_path_walk_next(struct adaptived_path_walk_handle **handle, char **
*/
void adaptived_path_walk_end(struct adaptived_path_walk_handle **handle);

/**
* Check if the file/directory exists.
* @param path to file/directory
*
* @Note if an asterisk char is in the path, it will be replaced with '\0'
* @return 0 if file exists, else return -EEXIST
*/
int adaptived_file_exists(const char * const path);


enum cg_setting_enum {
CG_SETTING = 0,
Expand Down
17 changes: 17 additions & 0 deletions adaptived/src/utils/path_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,20 @@ API void adaptived_path_walk_end(struct adaptived_path_walk_handle **handle)

*handle = NULL;
}

API int adaptived_file_exists(const char * const path)
{
char check_path[FILENAME_MAX];
char *subp;

strcpy(check_path, path);
subp = strstr(check_path, "*");
Copy link
Member

@drakenclimber drakenclimber Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of replacing * with \0. While unusual, * is valid in filenames and could occur anywhere in the file. Also, * is used by adaptived for wildcarding of paths.

I don't think we want this check.

if (subp)
*subp = '\0';

if (access(check_path, F_OK) != 0) {
adaptived_err("%s: can't find %s, errno=%d\n", __func__, check_path, errno);
return -EEXIST;
}
return 0;
}