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

Change to JackTools::GetUID() on Windows that fixes metadata #991

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

kinggeek
Copy link

@kinggeek kinggeek commented Nov 3, 2024

Change to JackTools::GetUID() on Windows that fixes metadata creating a new BDB on every API call.

JackTools::GetUID() would return the PID of the calling process. (I think this was a stub because there is no Windows equivalent). The linux version appears to return a linux UID. This patch does something similar on Windows so as to create/open the same DB on Windows and metadata now works correctly.

creating a new BDB on every API call.

JackTools::GetUID() would return the PID of the calling process.
(I think this was a stub because there is no Windows equivalent).
The linux version appears to return a linux UID. This patch does
something similar on Windows so as to create/open the same DB
on Windows.
Copy link
Contributor

@imaami imaami left a comment

Choose a reason for hiding this comment

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

A few things should be fixed. For completeness' sake here's my suggestion:

    int JackTools::GetUID()
    {
#ifdef WIN32
        int id = 0;
        HANDLE tokenHandle = nullptr;

        if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &tokenHandle)) {
            TOKEN_STATISTICS tokenStats{};
            DWORD len = sizeof tokenStats;

            if (GetTokenInformation(tokenHandle, TokenStatistics,
                                    &tokenStats, sizeof tokenStats, &len)) {
                id = static_cast<int>(tokenStats.AuthenticationId.LowPart);
            }

            CloseHandle(tokenHandle);
        }

        return id;
#else
        return geteuid();
#endif
    }

}

TOKEN_STATISTICS tokenStats;
DWORD returnLength;
Copy link
Contributor

Choose a reason for hiding this comment

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

Initialize tokenStats and returnLength:

        TOKEN_STATISTICS tokenStats{};
        DWORD returnLength = sizeof tokenStats;

TOKEN_STATISTICS tokenStats;
DWORD returnLength;
if (!GetTokenInformation(tokenHandle, TokenStatistics, &tokenStats, sizeof(tokenStats), &returnLength)) {
CloseHandle(tokenHandle);
Copy link
Contributor

Choose a reason for hiding this comment

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

You're only closing the handle if GetTokenInformation() succeeds, it should be closed every time.

return 0;
}

return(tokenStats.AuthenticationId.LowPart);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: return is not a function call, don't make it look like one.

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.

3 participants