Skip to content

Commit

Permalink
optimized
Browse files Browse the repository at this point in the history
Signed-off-by: leleliu008 <[email protected]>
  • Loading branch information
leleliu008 committed Sep 12, 2023
1 parent 046a4b9 commit 9355f95
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ jobs:
- run: ./output/bin/uppm formula-repo-del my_repo

- run: ./output/bin/uppm cleanup
- run: ./output/bin/uppm upgrade-self

vcpkg:

Expand Down Expand Up @@ -229,6 +230,7 @@ jobs:
- run: ./output/bin/uppm formula-repo-del my_repo

- run: ./output/bin/uppm cleanup
- run: ./output/bin/uppm upgrade-self

vcpkg-sanitizer:

Expand Down Expand Up @@ -345,6 +347,7 @@ jobs:
- run: ./output/bin/uppm formula-repo-del my_repo

- run: ./output/bin/uppm cleanup
- run: ./output/bin/uppm upgrade-self

brew-sanitizer:
strategy:
Expand Down Expand Up @@ -458,3 +461,4 @@ jobs:
- run: ./output/bin/uppm formula-repo-del my_repo

- run: ./output/bin/uppm cleanup
- run: ./output/bin/uppm upgrade-self
1 change: 1 addition & 0 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ jobs:
- run: ./output/bin/uppm formula-repo-del my_repo

- run: ./output/bin/uppm cleanup
- run: ./output/bin/uppm upgrade-self
1 change: 1 addition & 0 deletions .github/workflows/valgrind.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ jobs:
- run: $VALGRIND ./output/bin/uppm formula-repo-del my_repo

- run: $VALGRIND ./output/bin/uppm cleanup
- run: $VALGRIND ./output/bin/uppm upgrade-self
98 changes: 68 additions & 30 deletions src/upgrade-self.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ int uppm_upgrade_self(bool verbose) {
char * latestReleaseTagName = NULL;
size_t latestReleaseTagNameLength = 0U;

char latestVersion[11] = {0};
size_t latestVersionLength = 0U;
char latestReleaseVersion[11] = {0};
size_t latestReleaseVersionLength = 0U;

char * p = NULL;

Expand Down Expand Up @@ -174,8 +174,8 @@ int uppm_upgrade_self(bool verbose) {
n++;

if (p[n] == '+') {
latestVersionLength = n > 10 ? 10 : n;
strncpy(latestVersion, p, latestVersionLength);
latestReleaseVersionLength = n > 10 ? 10 : n;
strncpy(latestReleaseVersion, p, latestReleaseVersionLength);
}
}
}
Expand All @@ -192,48 +192,48 @@ int uppm_upgrade_self(bool verbose) {
return UPPM_ERROR;
}

if (latestVersion[0] == '\0') {
if (latestReleaseVersion[0] == '\0') {
fprintf(stderr, "%s return invalid json.\n", githubApiUrl);
return UPPM_ERROR;
}

char latestVersionCopy[latestVersionLength + 1U];
strncpy(latestVersionCopy, latestVersion, latestVersionLength + 1U);
char latestReleaseVersionCopy[latestReleaseVersionLength + 1U];
strncpy(latestReleaseVersionCopy, latestReleaseVersion, latestReleaseVersionLength + 1U);

char * latestVersionMajorStr = strtok(latestVersionCopy, ".");
char * latestVersionMinorStr = strtok(NULL, ".");
char * latestVersionPatchStr = strtok(NULL, ".");
char * latestReleaseVersionMajorStr = strtok(latestReleaseVersionCopy, ".");
char * latestReleaseVersionMinorStr = strtok(NULL, ".");
char * latestReleaseVersionPatchStr = strtok(NULL, ".");

int latestVersionMajor = 0;
int latestVersionMinor = 0;
int latestVersionPatch = 0;
int latestReleaseVersionMajor = 0;
int latestReleaseVersionMinor = 0;
int latestReleaseVersionPatch = 0;

if (latestVersionMajorStr != NULL) {
latestVersionMajor = atoi(latestVersionMajorStr);
if (latestReleaseVersionMajorStr != NULL) {
latestReleaseVersionMajor = atoi(latestReleaseVersionMajorStr);
}

if (latestVersionMinorStr != NULL) {
latestVersionMinor = atoi(latestVersionMinorStr);
if (latestReleaseVersionMinorStr != NULL) {
latestReleaseVersionMinor = atoi(latestReleaseVersionMinorStr);
}

if (latestVersionPatchStr != NULL) {
latestVersionPatch = atoi(latestVersionPatchStr);
if (latestReleaseVersionPatchStr != NULL) {
latestReleaseVersionPatch = atoi(latestReleaseVersionPatchStr);
}

if (latestVersionMajor == 0 && latestVersionMinor == 0 && latestVersionPatch == 0) {
fprintf(stderr, "invalid version format: %s\n", latestVersion);
if (latestReleaseVersionMajor == 0 && latestReleaseVersionMinor == 0 && latestReleaseVersionPatch == 0) {
fprintf(stderr, "invalid version format: %s\n", latestReleaseVersion);
return UPPM_ERROR;
}

if (latestVersionMajor < UPPM_VERSION_MAJOR) {
if (latestReleaseVersionMajor < UPPM_VERSION_MAJOR) {
LOG_SUCCESS1("this software is already the latest version.");
return UPPM_OK;
} else if (latestVersionMajor == UPPM_VERSION_MAJOR) {
if (latestVersionMinor < UPPM_VERSION_MINOR) {
} else if (latestReleaseVersionMajor == UPPM_VERSION_MAJOR) {
if (latestReleaseVersionMinor < UPPM_VERSION_MINOR) {
LOG_SUCCESS1("this software is already the latest version.");
return UPPM_OK;
} else if (latestVersionMinor == UPPM_VERSION_MINOR) {
if (latestVersionPatch <= UPPM_VERSION_PATCH) {
} else if (latestReleaseVersionMinor == UPPM_VERSION_MINOR) {
if (latestReleaseVersionPatch <= UPPM_VERSION_PATCH) {
LOG_SUCCESS1("this software is already the latest version.");
return UPPM_OK;
}
Expand All @@ -256,9 +256,47 @@ int uppm_upgrade_self(bool verbose) {
return UPPM_ERROR;
}

size_t tarballFileNameLength = latestVersionLength + strlen(osType) + strlen(osArch) + 26U;
char tarballFileName[tarballFileNameLength];
snprintf(tarballFileName, tarballFileNameLength, "uppm-%s-%s-%s.tar.xz", latestVersion, osType, osArch);
size_t tarballFileNameLength = strlen(latestReleaseVersion) + strlen(osType) + strlen(osArch) + 15U + 5U;
char tarballFileName[tarballFileNameLength];

if (strcmp(osType, "macos") == 0) {
char osVersion[31] = {0};

if (sysinfo_vers(osVersion, 30) != 0) {
return UPPM_ERROR;
}

int i = 0;

for (;;) {
char c = osVersion[i];

if (c == '.') {
osVersion[i] = '\0';
break;
}

if (c == '\0') {
break;
}
}

const char * x;

if (strcmp(osVersion, "10") == 0) {
x = "10.15";
} else if (strcmp(osVersion, "11") == 0) {
x = "11.0";
} else if (strcmp(osVersion, "12") == 0) {
x = "12.0";
} else {
x = "13.0";
}

snprintf(tarballFileName, tarballFileNameLength, "uppm-%s-%s%s-%s.tar.xz", latestReleaseVersion, osType, x, osArch);
} else {
snprintf(tarballFileName, tarballFileNameLength, "uppm-%s-%s-%s.tar.xz", latestReleaseVersion, osType, osArch);
}

size_t tarballUrlLength = tarballFileNameLength + strlen(latestReleaseTagName) + 66U;
char tarballUrl[tarballUrlLength];
Expand Down Expand Up @@ -323,7 +361,7 @@ int uppm_upgrade_self(bool verbose) {
free(selfRealPath);

if (ret == UPPM_OK) {
fprintf(stderr, "uppm is up to date with version %s\n", latestVersion);
fprintf(stderr, "uppm is up to date with version %s\n", latestReleaseVersion);
} else {
fprintf(stderr, "Can't upgrade self. the latest version of executable was downloaded to %s, you can manually replace the current running program with it.\n", upgradableExecutableFilePath);
}
Expand Down

0 comments on commit 9355f95

Please sign in to comment.