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 5, 2023
1 parent 2d6ffeb commit dab165a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/fedora.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
tag: [36, 37, rawhide]
tag: [38, 37]

container: fedora:${{ matrix.tag }}

Expand Down
28 changes: 13 additions & 15 deletions src/core/cp.c → src/copy.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#include <errno.h>
#include <stdio.h>

#include <fcntl.h>
#include <unistd.h>

#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];
Expand All @@ -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) {
Expand All @@ -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;
}
}
}
11 changes: 0 additions & 11 deletions src/core/cp.h

This file was deleted.

24 changes: 18 additions & 6 deletions src/depends.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
}
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/generate-url-transform-sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 1 addition & 2 deletions src/install.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "core/sysinfo.h"
#include "core/rm-r.h"
#include "core/tar.h"
#include "core/cp.h"

#include "uppm.h"

Expand Down Expand Up @@ -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;
Expand Down
18 changes: 13 additions & 5 deletions src/integrate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 22 additions & 3 deletions src/upgrade-self.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/uppm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dab165a

Please sign in to comment.