Skip to content

Commit 29d2849

Browse files
authored
Merge pull request #39 from AleksArt000/main
...
2 parents 79d9abd + 32144aa commit 29d2849

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

include/libspm.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ int count_installed();
5454
int search(char* in);
5555
int update();
5656
int upgrade();
57+
void create_links(char build_loc[4096], char dest_loc[4096]);
5758
//end test
5859

5960
// package info

src/link.c

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
#include <linux/limits.h>
6+
7+
// Include necessary headers
8+
#include "globals.h"
9+
#include "libspm.h"
10+
#include "cutils.h"
11+
12+
13+
void create_links(char build_loc[PATH_MAX], char dest_loc[PATH_MAX])
14+
{
15+
// Construct a shell command to list links in the specified directory
16+
char links_location_cmd[PATH_MAX + 64];
17+
sprintf(links_location_cmd, "( cd %s && find . -type l | cut -c2- ) ", build_loc);
18+
19+
// Log the constructed command for debugging
20+
dbg(2, "Getting links locations with %s ", links_location_cmd);
21+
22+
// Execute the constructed shell command and store the output in 'res'
23+
char* res = exec(links_location_cmd);
24+
25+
// Log the retrieved file locations for debugging
26+
dbg(3, "Got locations: '%s'", res);
27+
28+
// Allocate space for links
29+
char** links;
30+
31+
// Split the 'res' string into an array of file locations using '\n' as a delimiter
32+
unsigned int count = splita(res, '\n', &links);
33+
34+
// Log the count of retrieved locations for debugging
35+
dbg(2, "Got %d locations", count);
36+
37+
char* link_cmd = calloc(count * (PATH_MAX + 64) + 1, sizeof(char));
38+
for(int i = 0; i < count; i++)
39+
{
40+
char* read_link_cmd = calloc(PATH_MAX + 64, sizeof(char));
41+
char* find_cmd = calloc(PATH_MAX + 64, sizeof(char));
42+
sprintf(read_link_cmd, "readlink %s/%s", build_loc, links[i]);
43+
44+
sprintf(find_cmd, "( cd %s && find . -name %s ", build_loc, exec(read_link_cmd));
45+
find_cmd[strcspn(find_cmd, "\n")] = 0;
46+
47+
char* tmp_1 = calloc(PATH_MAX + 64, sizeof(char));
48+
sprintf(tmp_1, " | cut -c2- ) ");
49+
strcat(find_cmd, tmp_1);
50+
51+
char* target = calloc(PATH_MAX + 64, sizeof(char));
52+
sprintf(target, exec(find_cmd));
53+
target[strcspn(target, "\n")] = 0;
54+
55+
char* tmp = calloc(PATH_MAX + 64, sizeof(char));
56+
sprintf(link_cmd, "ln -sfv %s %s", target, links[i]);
57+
58+
char* start = calloc(PATH_MAX + 64, sizeof(char));
59+
char* end = calloc(PATH_MAX + 64, sizeof(char));
60+
sprintf(start, "%s/%s", build_loc, target);
61+
sprintf(end, "%s/%s", dest_loc, target);
62+
63+
char* copy_cmd = calloc(PATH_MAX + 64, sizeof(char));
64+
sprintf(copy_cmd, "cp %s %s", start, end);
65+
66+
if (!(access(end, F_OK) == 0))
67+
{
68+
if (end == NULL) {
69+
msg(FATAL, "Location is NULL");
70+
}
71+
system(copy_cmd);
72+
}
73+
else
74+
{
75+
msg(WARNING, "%s is already here, use --overwrite?", end);
76+
if (OVERWRITE) {
77+
// Rename the file in the build directory to the destination location
78+
rename(start, end);
79+
} else {
80+
msg(FATAL, "Terminating the program");
81+
}
82+
}
83+
84+
dbg(2, "executing %s now", link_cmd);
85+
86+
system(link_cmd);
87+
88+
free(read_link_cmd);
89+
free(find_cmd);
90+
free(tmp_1);
91+
free(target);
92+
free(start);
93+
free(end);
94+
free(copy_cmd);
95+
}
96+
free(links);
97+
}

src/move.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ This function iterates through the given file locations and moves the binaries t
2424
Returns: None
2525
*/
2626
void move_binaries(char** locations, long loc_size) {
27+
28+
create_links(getenv("SOVIET_BUILD_DIR"), getenv("ROOT"));
29+
2730
// Iterate through locations and move the binaries to their correct locations
2831
for (int i = 0; i < loc_size; i++) {
2932
char dest_loc[PATH_MAX];
@@ -50,11 +53,9 @@ void move_binaries(char** locations, long loc_size) {
5053
msg(WARNING, "Moved %s/%s to %s", getenv("SOVIET_BUILD_DIR"), locations[i], dest_loc);
5154
break;
5255
}
53-
5456

5557
} else {
5658
msg(WARNING, "%s is already here, use --overwrite?", locations[i]);
57-
5859
if (OVERWRITE) {
5960
// Rename the file in the build directory to the destination location
6061
rename(build_loc, dest_loc);

0 commit comments

Comments
 (0)