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
+ }
0 commit comments