New API calls: truncate, truncate_size, mkdir_mode, utimens, lchown.
[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 (const char *path)
32 {
33   ssize_t r;
34   char *ret;
35   char link[PATH_MAX];
36
37   CHROOT_IN;
38   r = readlink (path, link, sizeof link);
39   CHROOT_OUT;
40   if (r == -1) {
41     reply_with_perror ("readlink");
42     return NULL;
43   }
44
45   ret = strndup (link, r);
46   if (ret == NULL) {
47     reply_with_perror ("strndup");
48     return NULL;
49   }
50
51   return ret;                   /* caller frees */
52 }
53
54 static int
55 _link (const char *flag, int symbolic, const char *target, const char *linkname)
56 {
57   int r;
58   char *err;
59   char *buf_linkname;
60   char *buf_target;
61
62   /* Prefix linkname with sysroot. */
63   buf_linkname = sysroot_path (linkname);
64   if (!buf_linkname) {
65     reply_with_perror ("malloc");
66     return -1;
67   }
68
69   /* Only prefix target if it's _not_ a symbolic link, and if
70    * the target is absolute.  Note that the resulting link will
71    * always be "broken" from the p.o.v. of the appliance, ie:
72    * /a -> /b but the path as seen here is /sysroot/b
73    */
74   buf_target = NULL;
75   if (!symbolic && target[0] == '/') {
76     buf_target = sysroot_path (target);
77     if (!buf_target) {
78       reply_with_perror ("malloc");
79       free (buf_linkname);
80       return -1;
81     }
82   }
83
84   if (flag)
85     r = command (NULL, &err,
86                  "ln", flag, "--", /* target could begin with '-' */
87                  buf_target ? : target, buf_linkname, NULL);
88   else
89     r = command (NULL, &err,
90                  "ln", "--",
91                  buf_target ? : target, buf_linkname, NULL);
92   free (buf_linkname);
93   free (buf_target);
94   if (r == -1) {
95     reply_with_error ("ln%s%s: %s: %s: %s",
96                       flag ? " " : "",
97                       flag ? : "",
98                       target, linkname, err);
99     free (err);
100     return -1;
101   }
102
103   free (err);
104
105   return 0;
106 }
107
108 int
109 do_ln (const char *target, const char *linkname)
110 {
111   return _link (NULL, 0, target, linkname);
112 }
113
114 int
115 do_ln_f (const char *target, const char *linkname)
116 {
117   return _link ("-f", 0, target, linkname);
118 }
119
120 int
121 do_ln_s (const char *target, const char *linkname)
122 {
123   return _link ("-s", 1, target, linkname);
124 }
125
126 int
127 do_ln_sf (const char *target, const char *linkname)
128 {
129   return _link ("-sf", 1, target, linkname);
130 }