From dab165a0aee968f88c5257e98041ee8fbcd90019 Mon Sep 17 00:00:00 2001 From: leleliu008 Date: Wed, 6 Sep 2023 04:31:41 +0800 Subject: [PATCH] optimized Signed-off-by: leleliu008 --- .github/workflows/fedora.yml | 2 +- src/{core/cp.c => copy.c} | 28 +++++++++++++--------------- src/core/cp.h | 11 ----------- src/depends.c | 24 ++++++++++++++++++------ src/generate-url-transform-sample.c | 12 ++++++++++-- src/install.c | 3 +-- src/integrate.c | 18 +++++++++++++----- src/upgrade-self.c | 25 ++++++++++++++++++++++--- src/uppm.h | 2 ++ 9 files changed, 80 insertions(+), 45 deletions(-) rename src/{core/cp.c => copy.c} (64%) delete mode 100644 src/core/cp.h diff --git a/.github/workflows/fedora.yml b/.github/workflows/fedora.yml index e5f2aa5..ac01576 100644 --- a/.github/workflows/fedora.yml +++ b/.github/workflows/fedora.yml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - tag: [36, 37, rawhide] + tag: [38, 37] container: fedora:${{ matrix.tag }} diff --git a/src/core/cp.c b/src/copy.c similarity index 64% rename from src/core/cp.c rename to src/copy.c index 52da295..5488c8e 100644 --- a/src/core/cp.c +++ b/src/copy.c @@ -1,24 +1,24 @@ -#include +#include #include #include -#include "cp.h" +#include "uppm.h" -int copy_file(const char * fromFilePath, const char * toFilePath) { +int uppm_copy_file(const char * fromFilePath, const char * toFilePath) { int fromFD = open(fromFilePath, O_RDONLY); if (fromFD == -1) { - return -1; + perror(fromFilePath); + return UPPM_ERROR; } int toFD = open(toFilePath, O_CREAT | O_TRUNC | O_WRONLY, 0666); if (toFD == -1) { - int err = errno; + perror(toFilePath); close(fromFD); - errno = err; - return -1; + return UPPM_ERROR; } unsigned char buf[1024]; @@ -27,11 +27,10 @@ int copy_file(const char * fromFilePath, const char * toFilePath) { ssize_t readSize = read(fromFD, buf, 1024); if (readSize == -1) { - int err = errno; + perror(fromFilePath); close(fromFD); close(toFD); - errno = err; - return -1; + return UPPM_ERROR; } if (readSize == 0) { @@ -43,18 +42,17 @@ int copy_file(const char * fromFilePath, const char * toFilePath) { ssize_t writeSize = write(toFD, buf, readSize); if (writeSize != -1) { - int err = errno; + perror(toFilePath); close(fromFD); close(toFD); - errno = err; - return -1; + return UPPM_ERROR; } if (writeSize != readSize) { close(fromFD); close(toFD); - errno = EIO; - return -1; + fprintf(stderr, "not fully written to %s\n", toFilePath); + return UPPM_ERROR; } } } diff --git a/src/core/cp.h b/src/core/cp.h deleted file mode 100644 index e3bd1c5..0000000 --- a/src/core/cp.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _CP_H -#define _CP_H - -/** copy a file from one place to other place. - * - * On success, 0 is returned. - * On error, -1 is returned and errno is set to indicate the error. - */ -int copy_file(const char * fromFilePath, const char * toFilePath); - -#endif diff --git a/src/depends.c b/src/depends.c index 6341b8c..38c075d 100644 --- a/src/depends.c +++ b/src/depends.c @@ -396,8 +396,12 @@ int uppm_depends(const char * packageName, UPPMDependsOutputType outputType, con if (rename(boxFilePath, outputFilePath) == 0) { return UPPM_OK; } else { - perror(outputFilePath); - return UPPM_ERROR; + if (errno == EXDEV) { + return uppm_copy_file(boxFilePath, outputFilePath); + } else { + perror(outputFilePath); + return UPPM_ERROR; + } } } } @@ -436,8 +440,12 @@ int uppm_depends(const char * packageName, UPPMDependsOutputType outputType, con if (rename(dotFilePath, outputFilePath) == 0) { return UPPM_OK; } else { - perror(outputFilePath); - return UPPM_ERROR; + if (errno == EXDEV) { + return uppm_copy_file(dotFilePath, outputFilePath); + } else { + perror(outputFilePath); + return UPPM_ERROR; + } } } @@ -477,8 +485,12 @@ int uppm_depends(const char * packageName, UPPMDependsOutputType outputType, con if (rename(tmpFilePath, outputFilePath) == 0) { return UPPM_OK; } else { - perror(outputFilePath); - return UPPM_ERROR; + if (errno == EXDEV) { + return uppm_copy_file(tmpFilePath, outputFilePath); + } else { + perror(outputFilePath); + return UPPM_ERROR; + } } } diff --git a/src/generate-url-transform-sample.c b/src/generate-url-transform-sample.c index 204928e..d351d60 100644 --- a/src/generate-url-transform-sample.c +++ b/src/generate-url-transform-sample.c @@ -123,8 +123,16 @@ int uppm_generate_url_transform_sample() { snprintf(outFilePath, outFilePathLength, "%s/url-transform.sample", uppmHomeDIR); if (rename(tmpFilePath, outFilePath) != 0) { - perror(tmpFilePath); - return UPPM_ERROR; + if (errno == EXDEV) { + ret = uppm_copy_file(tmpFilePath, outFilePath); + + if (ret != UPPM_OK) { + return ret; + } + } else { + perror(tmpFilePath); + return UPPM_ERROR; + } } //////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/install.c b/src/install.c index 0b25bb3..761fe68 100644 --- a/src/install.c +++ b/src/install.c @@ -16,7 +16,6 @@ #include "core/sysinfo.h" #include "core/rm-r.h" #include "core/tar.h" -#include "core/cp.h" #include "uppm.h" @@ -297,7 +296,7 @@ int uppm_install(const char * packageName, bool verbose, bool force) { char toFilePath[toFilePathLength]; snprintf(toFilePath, toFilePathLength, "%s/%s", packageInstalledDIR, sessionID); - ret = copy_file(binFilePath, toFilePath); + ret = uppm_copy_file(binFilePath, toFilePath); if (ret != UPPM_OK) { return ret; diff --git a/src/integrate.c b/src/integrate.c index 7a45b53..36eba00 100644 --- a/src/integrate.c +++ b/src/integrate.c @@ -166,12 +166,20 @@ int uppm_integrate_zsh_completion(const char * outputDIR, bool verbose) { snprintf(outputFilePath, outputFilePathLength, "%s/_uppm", outputDIR); if (rename(tmpFilePath, outputFilePath) != 0) { - perror(outputFilePath); - return UPPM_ERROR; - } else { - printf("zsh completion script for uppm has been written to %s\n", outputFilePath); - return UPPM_OK; + if (errno == EXDEV) { + ret = uppm_copy_file(tmpFilePath, outputFilePath); + + if (ret != UPPM_OK) { + return ret; + } + } else { + perror(outputFilePath); + return UPPM_ERROR; + } } + + printf("zsh completion script for uppm has been written to %s\n", outputFilePath); + return UPPM_OK; } int uppm_integrate_bash_completion(const char * outputDIR, bool verbose) { diff --git a/src/upgrade-self.c b/src/upgrade-self.c index 9a12597..367e84b 100644 --- a/src/upgrade-self.c +++ b/src/upgrade-self.c @@ -273,9 +273,28 @@ int uppm_upgrade_self(bool verbose) { } if (rename(upgradableExecutableFilePath, selfRealPath) != 0) { - perror(selfRealPath); - ret = UPPM_ERROR; - goto finally; + if (errno == EXDEV) { + if (unlink(selfRealPath) != 0) { + perror(selfRealPath); + ret = UPPM_ERROR; + goto finally; + } + + ret = uppm_copy_file(upgradableExecutableFilePath, selfRealPath); + + if (ret != UPPM_OK) { + goto finally; + } + + if (chmod(selfRealPath, S_IRWXU) != 0) { + perror(selfRealPath); + ret = UPPM_ERROR; + } + } else { + perror(selfRealPath); + ret = UPPM_ERROR; + goto finally; + } } finally: diff --git a/src/uppm.h b/src/uppm.h index b6eafde..b5ad202 100644 --- a/src/uppm.h +++ b/src/uppm.h @@ -252,4 +252,6 @@ int uppm_examine_file_extension_from_url(char buf[], size_t maxSize, const char int uppm_http_fetch_to_file(const char * url, const char * outputFilePath, bool verbose, bool showProgress); +int uppm_copy_file(const char * fromFilePath, const char * toFilePath); + #endif