update all uses of ABS_PATH
[libguestfs.git] / daemon / link.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <limits.h>
26
27 #include "daemon.h"
28 #include "actions.h"
29
30 char *
31 do_readlink (char *path)
32 {
33   ssize_t r;
34   char *ret;
35   char link[PATH_MAX];
36
37   NEED_ROOT (NULL);
38   ABS_PATH (path, return NULL);
39
40   CHROOT_IN;
41   r = readlink (path, link, sizeof link);
42   CHROOT_OUT;
43   if (r == -1) {
44     reply_with_perror ("readlink");
45     return NULL;
46   }
47
48   ret = strndup (link, r);
49   if (ret == NULL) {
50     reply_with_perror ("strndup");
51     return NULL;
52   }
53
54   return ret;                   /* caller frees */
55 }
56
57 static int
58 _link (const char *flag, int symbolic, const char *target, const char *linkname)
59 {
60   int r;
61   char *err;
62   char *buf_linkname;
63   char *buf_target;
64
65   NEED_ROOT (-1);
66   ABS_PATH (linkname, return -1);
67   /* but target does not need to be absolute */
68
69   /* Prefix linkname with sysroot. */
70   buf_linkname = sysroot_path (linkname);
71   if (!buf_linkname) {
72     reply_with_perror ("malloc");
73     return -1;
74   }
75
76   /* Only prefix target if it's _not_ a symbolic link, and if
77    * the target is absolute.  Note that the resulting link will
78    * always be "broken" from the p.o.v. of the appliance, ie:
79    * /a -> /b but the path as seen here is /sysroot/b
80    */
81   buf_target = NULL;
82   if (!symbolic && target[0] == '/') {
83     buf_target = sysroot_path (target);
84     if (!buf_target) {
85       reply_with_perror ("malloc");
86       free (buf_linkname);
87       return -1;
88     }
89   }
90
91   if (flag)
92     r = command (NULL, &err,
93                  "ln", flag, "--", /* target could begin with '-' */
94                  buf_target ? : target, buf_linkname, NULL);
95   else
96     r = command (NULL, &err,
97                  "ln", "--",
98                  buf_target ? : target, buf_linkname, NULL);
99   free (buf_linkname);
100   free (buf_target);
101   if (r == -1) {
102     reply_with_error ("ln%s%s: %s: %s: %s",
103                       flag ? " " : "",
104                       flag ? : "",
105                       target, linkname, err);
106     free (err);
107     return -1;
108   }
109
110   free (err);
111
112   return 0;
113 }
114
115 int
116 do_ln (char *target, char *linkname)
117 {
118   return _link (NULL, 0, target, linkname);
119 }
120
121 int
122 do_ln_f (char *target, char *linkname)
123 {
124   return _link ("-f", 0, target, linkname);
125 }
126
127 int
128 do_ln_s (char *target, char *linkname)
129 {
130   return _link ("-s", 1, target, linkname);
131 }
132
133 int
134 do_ln_sf (char *target, char *linkname)
135 {
136   return _link ("-sf", 1, target, linkname);
137 }