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

Resolve full path of pcap which starts with '~' #607

Merged
merged 18 commits into from
Jul 30, 2024

Conversation

rajeshsingh381
Copy link
Contributor

@rajeshsingh381 rajeshsingh381 commented Feb 20, 2023

expand paths like '~/path/to/file.pcap' to '/home/myuser/path/to/file.pcap'

@rajeshsingh381 rajeshsingh381 changed the title Add support for pcap paths with '~' Resolve pcap paths with starting with '~' Feb 20, 2023
@rajeshsingh381 rajeshsingh381 changed the title Resolve pcap paths with starting with '~' Resolve full path of pcap which starts with '~' Feb 20, 2023
@rajeshsingh381 rajeshsingh381 force-pushed the pcap_tilde_path_support branch from 4bda201 to 96ae0c4 Compare February 20, 2023 13:55
@rajeshsingh381
Copy link
Contributor Author

Really wish this feature gets added in next release. To that end, any suggestions/comments to code implementation are welcome, I will try my best to cater them,

@rajeshsingh381
Copy link
Contributor Author

Hi @wdoekes , could you please take a look at this PR when you find some time and offer your suggestions, if any? I am new to github community, so please also do let me know if a direct ping is inappropriate, I will refrain from in future.

@wdoekes
Copy link
Member

wdoekes commented Mar 13, 2023

Maybe add expanduser(). That way ~someuser/blah would also work, which will be expected.

Chatgpt was helpful enough to give me this ("can you show me a C implementation of python os.path.expanduser?"):

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

char* expanduser(char* path) {
    if (path == NULL) {
        return NULL;
    }
    
    if (path[0] == '~') {
        char* homedir = NULL;
        
        if (path[1] == '\0' || path[1] == '/') {
            homedir = getenv("HOME");
        } else {
            struct passwd* pw = getpwnam(path+1);
            
            if (pw != NULL) {
                homedir = pw->pw_dir;
            }
        }
        
        if (homedir != NULL) {
            char* expanded = malloc(strlen(homedir) + strlen(path));
            sprintf(expanded, "%s%s", homedir, path+1);
            return expanded;
        }
    }
    
    return strdup(path);
}

with these caveats:

Note that this implementation uses malloc() and free() to allocate and deallocate memory for the expanded path, so you'll need to remember to free the returned string when you're done with it.

(I'd probably prefer a version with a provided buffer.)

(Also, it uses a non-reentrant getpwnam. Look into getpwnam_r instead..)

@wdoekes
Copy link
Member

wdoekes commented Mar 13, 2023

            sprintf(expanded, "%s%s", homedir, path+1);

Hehe. This can't possibly work for ~someuser unless the username length is added too; otherwise we get /home/someusersomeuser/bla

(Also.. no malloc() NULL check. But I don't want any malloc here anyway.)

@rajeshsingh381
Copy link
Contributor Author

Thankyou @wdoekes for your suggestions, I have added support for ~/someuser/blah, ~someuser/blah and ~someuser cases and tested them. Please let me know if you have any suggestions/inputs.

Copy link
Member

@wdoekes wdoekes left a comment

Choose a reason for hiding this comment

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

Please check my suggestion for the expand_user_path invocation. It should not be optional based on ~, but always be called directly.

src/actions.cpp Outdated Show resolved Hide resolved
@wdoekes
Copy link
Member

wdoekes commented Apr 8, 2023

FYI: I moved find_file() to fileutil.c where is used by both pcap and rtpstream file lookups.

@rajeshsingh381
Copy link
Contributor Author

Sorry for inactivity, I have applied the changes you suggested, and will verify the them over the different cases you mentioned tomorrow

@rajeshsingh381
Copy link
Contributor Author

Hi, have verified the new changes, for the mentioned 3 cases. I tried to reproduce and debug the regression test failures of github-#259 and github-#196, but weirdly they don't happen when I run the SIPp commands manually, call is successful with 0 return code.

rajeshsingh381 and others added 4 commits June 16, 2024 00:54
Don't hardcode retry counter max value.
Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4.3.3 to 4.3.4.
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](actions/upload-artifact@v4.3.3...v4.3.4)

---
updated-dependencies:
- dependency-name: actions/upload-artifact
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
@rajeshsingh381 rajeshsingh381 requested a review from wdoekes July 16, 2024 10:10
@rajeshsingh381
Copy link
Contributor Author

Hi, apologies for inactivity. I have fixed the regression test failures.

@lemenkov
Copy link
Member

LGTM and it passes the tests.

@rajeshsingh381 rajeshsingh381 requested a review from wdoekes July 17, 2024 13:11
@wdoekes
Copy link
Member

wdoekes commented Jul 18, 2024

Thanks for the style fixes 👍

I'm unfortunately unable to review this timely. Unassigning. Leaving the review+merge to someone else. E.g. @lemenkov

@wdoekes wdoekes removed their request for review July 18, 2024 09:58
src/fileutil.c Outdated Show resolved Hide resolved
src/fileutil.c Show resolved Hide resolved
src/fileutil.c Outdated Show resolved Hide resolved
src/fileutil.c Outdated
struct passwd pwd;
struct passwd* result;
size_t bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
char buffer[bufsize + 1];
Copy link
Contributor

Choose a reason for hiding this comment

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

Dynamic array size is a GNU extension. Do we have this elsewhere in the codebase?

Copy link
Contributor Author

@rajeshsingh381 rajeshsingh381 Jul 18, 2024

Choose a reason for hiding this comment

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

Thanks, I was not aware of this and couldn't find such examples in the codebase either. Now using malloc() reluctantly.

src/fileutil.c Outdated Show resolved Hide resolved
src/rtpstream.cpp Outdated Show resolved Hide resolved
@rajeshsingh381 rajeshsingh381 requested a review from orgads July 18, 2024 17:24
@rajeshsingh381
Copy link
Contributor Author

Hi @orgads could you please review the updated diff when you get some time. Thankyou.

Copy link
Contributor

@orgads orgads left a comment

Choose a reason for hiding this comment

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

Thanks for the reminder.

I approve, just reply on the NULL comment and then I'll merge.

src/fileutil.c Outdated Show resolved Hide resolved
@orgads
Copy link
Contributor

orgads commented Jul 30, 2024

You have conflicts. Please rebase.

@orgads orgads merged commit 682cc09 into SIPp:master Jul 30, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants