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 10, 2023
1 parent 54bbc12 commit 7b50084
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 59 deletions.
33 changes: 27 additions & 6 deletions src/depends.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,19 @@ static int uppm_depends2(const char * packageName, UPPMDependsOutputType outputT
char uppmRunDIR[uppmRunDIRLength];
snprintf(uppmRunDIR, uppmRunDIRLength, "%s/run", uppmHomeDIR);

if (stat(uppmRunDIR, &st) == 0) {
if (lstat(uppmRunDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", uppmRunDIR);
return UPPM_ERROR;
if (unlink(uppmRunDIR) != 0) {
perror(uppmRunDIR);
return UPPM_ERROR;
}

if (mkdir(uppmRunDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(uppmRunDIR);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(uppmRunDIR, S_IRWXU) != 0) {
Expand All @@ -353,16 +362,28 @@ static int uppm_depends2(const char * packageName, UPPMDependsOutputType outputT
char sessionDIR[sessionDIRLength];
snprintf(sessionDIR, sessionDIRLength, "%s/%d", uppmRunDIR, getpid());

if (stat(sessionDIR, &st) == 0) {
if (lstat(sessionDIR, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
ret = uppm_rm_r(sessionDIR, false);

if (ret != UPPM_OK) {
return ret;
}

if (mkdir(sessionDIR, S_IRWXU) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}
} else {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", sessionDIR);
return UPPM_ERROR;
if (unlink(sessionDIR) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}

if (mkdir(sessionDIR, S_IRWXU) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}
}
} else {
if (mkdir(sessionDIR, S_IRWXU) != 0) {
Expand Down
14 changes: 11 additions & 3 deletions src/download.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ int uppm_download(const char * url, const char * sha256sum, const char * downloa

struct stat st;

if (stat(downloadDIR, &st) == 0) {
if (lstat(downloadDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", downloadDIR);
return UPPM_ERROR;
if (unlink(downloadDIR) != 0) {
perror(downloadDIR);
return UPPM_ERROR;
}

if (mkdir(downloadDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(downloadDIR, S_IRWXU) != 0) {
Expand Down
18 changes: 14 additions & 4 deletions src/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,21 @@ int uppm_fetch(const char * packageName, bool verbose) {
char downloadDIR[downloadDIRLength];
snprintf(downloadDIR, downloadDIRLength, "%s/downloads", uppmHomeDIR);

if (stat(downloadDIR, &st) == 0) {
if (lstat(downloadDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", downloadDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
if (unlink(downloadDIR) != 0) {
perror(downloadDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}

if (mkdir(downloadDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(downloadDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(downloadDIR, S_IRWXU) != 0) {
Expand Down
48 changes: 39 additions & 9 deletions src/formula-repo-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,19 @@ int uppm_formula_repo_add(const char * formulaRepoName, const char * formulaRepo
char uppmRunDIR[uppmRunDIRLength];
snprintf(uppmRunDIR, uppmRunDIRLength, "%s/run", uppmHomeDIR);

if (stat(uppmRunDIR, &st) == 0) {
if (lstat(uppmRunDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", uppmRunDIR);
return UPPM_ERROR;
if (unlink(uppmRunDIR) != 0) {
perror(uppmRunDIR);
return UPPM_ERROR;
}

if (mkdir(uppmRunDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(uppmRunDIR);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(uppmRunDIR, S_IRWXU) != 0) {
Expand All @@ -94,16 +103,28 @@ int uppm_formula_repo_add(const char * formulaRepoName, const char * formulaRepo
char sessionDIR[sessionDIRLength];
snprintf(sessionDIR, sessionDIRLength, "%s/%d", uppmRunDIR, getpid());

if (stat(sessionDIR, &st) == 0) {
if (lstat(sessionDIR, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
ret = uppm_rm_r(sessionDIR, false);

if (ret != UPPM_OK) {
return ret;
}

if (mkdir(sessionDIR, S_IRWXU) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}
} else {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", sessionDIR);
return UPPM_ERROR;
if (unlink(sessionDIR) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}

if (mkdir(sessionDIR, S_IRWXU) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}
}
} else {
if (mkdir(sessionDIR, S_IRWXU) != 0) {
Expand Down Expand Up @@ -147,10 +168,19 @@ int uppm_formula_repo_add(const char * formulaRepoName, const char * formulaRepo
char formulaRepoRootDIR[formulaRepoRootDIRLength];
snprintf(formulaRepoRootDIR, formulaRepoRootDIRLength, "%s/repos.d", uppmHomeDIR);

if (stat(formulaRepoRootDIR, &st) == 0) {
if (lstat(formulaRepoRootDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", formulaRepoRootDIR);
return UPPM_ERROR;
if (unlink(formulaRepoRootDIR) != 0) {
perror(formulaRepoRootDIR);
return UPPM_ERROR;
}

if (mkdir(formulaRepoRootDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(formulaRepoRootDIR);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(formulaRepoRootDIR, S_IRWXU) != 0) {
Expand Down
48 changes: 39 additions & 9 deletions src/formula-repo-create.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,19 @@ int uppm_formula_repo_create(const char * formulaRepoName, const char * formulaR
char uppmRunDIR[uppmRunDIRLength];
snprintf(uppmRunDIR, uppmRunDIRLength, "%s/run", uppmHomeDIR);

if (stat(uppmRunDIR, &st) == 0) {
if (lstat(uppmRunDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", uppmRunDIR);
return UPPM_ERROR;
if (unlink(uppmRunDIR) != 0) {
perror(uppmRunDIR);
return UPPM_ERROR;
}

if (mkdir(uppmRunDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(uppmRunDIR);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(uppmRunDIR, S_IRWXU) != 0) {
Expand All @@ -91,16 +100,28 @@ int uppm_formula_repo_create(const char * formulaRepoName, const char * formulaR
char sessionDIR[sessionDIRLength];
snprintf(sessionDIR, sessionDIRLength, "%s/%d", uppmRunDIR, getpid());

if (stat(sessionDIR, &st) == 0) {
if (lstat(sessionDIR, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
ret = uppm_rm_r(sessionDIR, false);

if (ret != UPPM_OK) {
return ret;
}

if (mkdir(sessionDIR, S_IRWXU) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}
} else {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", sessionDIR);
return UPPM_ERROR;
if (unlink(sessionDIR) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}

if (mkdir(sessionDIR, S_IRWXU) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}
}
} else {
if (mkdir(sessionDIR, S_IRWXU) != 0) {
Expand Down Expand Up @@ -173,10 +194,19 @@ int uppm_formula_repo_create(const char * formulaRepoName, const char * formulaR
char formulaRepoRootDIR[formulaRepoRootDIRLength];
snprintf(formulaRepoRootDIR, formulaRepoRootDIRLength, "%s/repos.d", uppmHomeDIR);

if (stat(formulaRepoRootDIR, &st) == 0) {
if (lstat(formulaRepoRootDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", formulaRepoRootDIR);
return UPPM_ERROR;
if (unlink(formulaRepoRootDIR) != 0) {
perror(formulaRepoRootDIR);
return UPPM_ERROR;
}

if (mkdir(formulaRepoRootDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(formulaRepoRootDIR);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(formulaRepoRootDIR, S_IRWXU) != 0) {
Expand Down
33 changes: 27 additions & 6 deletions src/generate-url-transform-sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ int uppm_generate_url_transform_sample() {
char uppmRunDIR[uppmRunDIRLength];
snprintf(uppmRunDIR, uppmRunDIRLength, "%s/run", uppmHomeDIR);

if (stat(uppmRunDIR, &st) == 0) {
if (lstat(uppmRunDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", uppmRunDIR);
return UPPM_ERROR;
if (unlink(uppmRunDIR) != 0) {
perror(uppmRunDIR);
return UPPM_ERROR;
}

if (mkdir(uppmRunDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(uppmRunDIR);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(uppmRunDIR, S_IRWXU) != 0) {
Expand All @@ -48,16 +57,28 @@ int uppm_generate_url_transform_sample() {
char sessionDIR[sessionDIRLength];
snprintf(sessionDIR, sessionDIRLength, "%s/%d", uppmRunDIR, getpid());

if (stat(sessionDIR, &st) == 0) {
if (lstat(sessionDIR, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
ret = uppm_rm_r(sessionDIR, false);

if (ret != UPPM_OK) {
return ret;
}

if (mkdir(sessionDIR, S_IRWXU) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}
} else {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", sessionDIR);
return UPPM_ERROR;
if (unlink(sessionDIR) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}

if (mkdir(sessionDIR, S_IRWXU) != 0) {
perror(sessionDIR);
return UPPM_ERROR;
}
}
} else {
if (mkdir(sessionDIR, S_IRWXU) != 0) {
Expand Down
46 changes: 41 additions & 5 deletions src/install.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,24 @@ int uppm_install(const char * packageName, bool verbose, bool force) {

if (lstat(downloadDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", downloadDIR);
return UPPM_ERROR;
if (unlink(downloadDIR) != 0) {
perror(downloadDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}

if (mkdir(downloadDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(downloadDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(downloadDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(downloadDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}
Expand Down Expand Up @@ -221,14 +233,26 @@ int uppm_install(const char * packageName, bool verbose, bool force) {
char packageInstalledRootDIR[packageInstalledRootDIRLength];
snprintf(packageInstalledRootDIR, packageInstalledRootDIRLength, "%s/installed", uppmHomeDIR);

if (stat(packageInstalledRootDIR, &st) == 0) {
if (lstat(packageInstalledRootDIR, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "%s was expected to be a directory, but it was not.\n", packageInstalledRootDIR);
return UPPM_ERROR;
if (unlink(packageInstalledRootDIR) != 0) {
perror(packageInstalledRootDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}

if (mkdir(packageInstalledRootDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(packageInstalledRootDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}
}
}
} else {
if (mkdir(packageInstalledRootDIR, S_IRWXU) != 0) {
if (errno != EEXIST) {
perror(packageInstalledRootDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}
Expand All @@ -249,12 +273,24 @@ int uppm_install(const char * packageName, bool verbose, bool force) {
uppm_formula_free(formula);
return ret;
}

if (mkdir(packageInstalledRealDIR, S_IRWXU) != 0) {
perror(packageInstalledRealDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}
} else {
if (unlink(packageInstalledRealDIR) != 0) {
perror(packageInstalledRealDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}

if (mkdir(packageInstalledRealDIR, S_IRWXU) != 0) {
perror(packageInstalledRealDIR);
uppm_formula_free(formula);
return UPPM_ERROR;
}
}
}

Expand Down
Loading

0 comments on commit 7b50084

Please sign in to comment.