Skip to content

Commit

Permalink
fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksArt000 committed Dec 3, 2023
1 parent e3a8f6d commit aaa9ba5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/libspm.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ int count_installed();
int search(char* in);
int update();
int upgrade();

int better_mvsp(char* old_path,char* new_path);
int mkdir_parent(const char *path, mode_t mode);
//end test

// package info
Expand Down
57 changes: 51 additions & 6 deletions src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ void move_binaries(char** locations, long loc_size) {
}

// Move the files from the build directory to the destination location
if(mvsp(build_loc, dest_loc)!= 0)
switch (better_mvsp(build_loc, dest_loc))
{
msg(FATAL, "Moving %s/%s to %s, failed", getenv("SOVIET_BUILD_DIR"), locations[i], dest_loc);
}
else
{
msg(WARNING, "Moved %s/%s to %s", getenv("SOVIET_BUILD_DIR"), locations[i], dest_loc);
case -1:
msg(FATAL, "Moving %s/%s to %s failed, could not create dir", getenv("SOVIET_BUILD_DIR"), locations[i], dest_loc);
break;
case -2:
msg(FATAL, "Moving %s/%s to %s failed, destination not a dir", getenv("SOVIET_BUILD_DIR"), locations[i], dest_loc);
break;
case 0:
msg(WARNING, "Moved %s/%s to %s", getenv("SOVIET_BUILD_DIR"), locations[i], dest_loc);
break;
}


} else {
msg(WARNING, "%s is already here, use --overwrite?", locations[i]);

Expand All @@ -60,3 +65,43 @@ void move_binaries(char** locations, long loc_size) {
}
return;
}

int better_mvsp(char* old_path,char* new_path)
{
char* parent_path = calloc(strlen(new_path)+1,sizeof(char));
strncpy(parent_path,new_path,strrchr(new_path, '/')-new_path);

switch (isdir(parent_path))
{
case 1:
if (mkdir_parent(parent_path, 0777) != 0) return -1;
break;
case 2:
return -2;
case 0:
break;
}
free(parent_path);
// move file
return rename(old_path,new_path);
}

int mkdir_parent(const char *path, mode_t mode) {
char tmp[256];
char *p = NULL;
size_t len;

snprintf(tmp, sizeof(tmp),"%s", path);
len = strlen(tmp);
if(tmp[len - 1] == '/')
tmp[len - 1] = 0;
for(p = tmp + 1; *p; p++)
if(*p == '/') {
*p = 0;
mkdir(tmp, mode);
*p = '/';
}
mkdir(tmp, mode);

return 0;
}

0 comments on commit aaa9ba5

Please sign in to comment.