From 7b500845ce471b72a4cbaec304f6f389365be4fc Mon Sep 17 00:00:00 2001 From: leleliu008 Date: Mon, 11 Sep 2023 06:07:36 +0800 Subject: [PATCH] optimized Signed-off-by: leleliu008 --- src/depends.c | 33 ++++++++++++++++---- src/download.c | 14 +++++++-- src/fetch.c | 18 ++++++++--- src/formula-repo-add.c | 48 +++++++++++++++++++++++------ src/formula-repo-create.c | 48 +++++++++++++++++++++++------ src/generate-url-transform-sample.c | 33 ++++++++++++++++---- src/install.c | 46 ++++++++++++++++++++++++--- src/integrate.c | 33 ++++++++++++++++---- src/session.c | 33 ++++++++++++++++---- src/upgrade-self.c | 31 ++++++++++++++++--- 10 files changed, 278 insertions(+), 59 deletions(-) diff --git a/src/depends.c b/src/depends.c index b69608e..0981d27 100644 --- a/src/depends.c +++ b/src/depends.c @@ -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) { @@ -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) { diff --git a/src/download.c b/src/download.c index 18c3c90..0536c76 100644 --- a/src/download.c +++ b/src/download.c @@ -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) { diff --git a/src/fetch.c b/src/fetch.c index 7cf3da4..7257545 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -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) { diff --git a/src/formula-repo-add.c b/src/formula-repo-add.c index e145ae1..1d542f5 100644 --- a/src/formula-repo-add.c +++ b/src/formula-repo-add.c @@ -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) { @@ -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) { @@ -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) { diff --git a/src/formula-repo-create.c b/src/formula-repo-create.c index fbd26cd..f411e88 100644 --- a/src/formula-repo-create.c +++ b/src/formula-repo-create.c @@ -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) { @@ -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) { @@ -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) { diff --git a/src/generate-url-transform-sample.c b/src/generate-url-transform-sample.c index 45bb309..a9c6530 100644 --- a/src/generate-url-transform-sample.c +++ b/src/generate-url-transform-sample.c @@ -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) { @@ -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) { diff --git a/src/install.c b/src/install.c index 46d2ffe..4f07d51 100644 --- a/src/install.c +++ b/src/install.c @@ -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; } @@ -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; } @@ -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; + } } } diff --git a/src/integrate.c b/src/integrate.c index b21acaa..080dab8 100644 --- a/src/integrate.c +++ b/src/integrate.c @@ -25,10 +25,19 @@ int uppm_integrate_zsh_completion(const char * outputDIR, bool verbose) { 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) { @@ -45,16 +54,28 @@ int uppm_integrate_zsh_completion(const char * outputDIR, bool verbose) { 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) { diff --git a/src/session.c b/src/session.c index 2b1c36b..87cc24d 100644 --- a/src/session.c +++ b/src/session.c @@ -25,10 +25,19 @@ int uppm_session_dir(char buf[], size_t bufSize, size_t * outSize) { 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) { @@ -45,16 +54,28 @@ int uppm_session_dir(char buf[], size_t bufSize, size_t * outSize) { char sessionDIR[sessionDIRCapcity]; snprintf(sessionDIR, sessionDIRCapcity, "%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) { diff --git a/src/upgrade-self.c b/src/upgrade-self.c index 82099f9..8d0f887 100644 --- a/src/upgrade-self.c +++ b/src/upgrade-self.c @@ -30,8 +30,17 @@ int uppm_upgrade_self(bool verbose) { if (stat(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) { @@ -48,16 +57,28 @@ int uppm_upgrade_self(bool verbose) { 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, verbose); 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) {