1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
24 #include <sys/types.h>
26 #include <sys/statvfs.h>
30 #include "../src/guestfs_protocol.h"
35 do_stat (const char *path)
38 guestfs_int_stat *ret;
42 r = stat (path, &statbuf);
46 reply_with_perror ("stat");
50 ret = malloc (sizeof *ret);
52 reply_with_perror ("malloc");
56 ret->dev = statbuf.st_dev;
57 ret->ino = statbuf.st_ino;
58 ret->mode = statbuf.st_mode;
59 ret->nlink = statbuf.st_nlink;
60 ret->uid = statbuf.st_uid;
61 ret->gid = statbuf.st_gid;
62 ret->rdev = statbuf.st_rdev;
63 ret->size = statbuf.st_size;
64 ret->blksize = statbuf.st_blksize;
65 ret->blocks = statbuf.st_blocks;
66 ret->atime = statbuf.st_atime;
67 ret->mtime = statbuf.st_mtime;
68 ret->ctime = statbuf.st_ctime;
74 do_lstat (const char *path)
77 guestfs_int_stat *ret;
81 r = lstat (path, &statbuf);
85 reply_with_perror ("stat");
89 ret = malloc (sizeof *ret);
91 reply_with_perror ("malloc");
95 ret->dev = statbuf.st_dev;
96 ret->ino = statbuf.st_ino;
97 ret->mode = statbuf.st_mode;
98 ret->nlink = statbuf.st_nlink;
99 ret->uid = statbuf.st_uid;
100 ret->gid = statbuf.st_gid;
101 ret->rdev = statbuf.st_rdev;
102 ret->size = statbuf.st_size;
103 ret->blksize = statbuf.st_blksize;
104 ret->blocks = statbuf.st_blocks;
105 ret->atime = statbuf.st_atime;
106 ret->mtime = statbuf.st_mtime;
107 ret->ctime = statbuf.st_ctime;
112 guestfs_int_stat_list *
113 do_lstatlist (const char *path, char *const *names)
116 guestfs_int_stat_list *ret;
119 nr_names = count_strings (names);
121 ret = malloc (sizeof *ret);
123 reply_with_perror ("malloc");
126 ret->guestfs_int_stat_list_len = nr_names;
127 ret->guestfs_int_stat_list_val = calloc (nr_names, sizeof (guestfs_int_stat));
128 if (ret->guestfs_int_stat_list_val == NULL) {
129 reply_with_perror ("malloc");
135 path_fd = open (path, O_RDONLY | O_DIRECTORY);
139 reply_with_perror ("lstatlist: %s", path);
140 free (ret->guestfs_int_stat_list_val);
145 for (i = 0; names[i] != NULL; ++i) {
149 r = fstatat (path_fd, names[i], &statbuf, AT_SYMLINK_NOFOLLOW);
151 ret->guestfs_int_stat_list_val[i].ino = -1;
153 ret->guestfs_int_stat_list_val[i].dev = statbuf.st_dev;
154 ret->guestfs_int_stat_list_val[i].ino = statbuf.st_ino;
155 ret->guestfs_int_stat_list_val[i].mode = statbuf.st_mode;
156 ret->guestfs_int_stat_list_val[i].nlink = statbuf.st_nlink;
157 ret->guestfs_int_stat_list_val[i].uid = statbuf.st_uid;
158 ret->guestfs_int_stat_list_val[i].gid = statbuf.st_gid;
159 ret->guestfs_int_stat_list_val[i].rdev = statbuf.st_rdev;
160 ret->guestfs_int_stat_list_val[i].size = statbuf.st_size;
161 ret->guestfs_int_stat_list_val[i].blksize = statbuf.st_blksize;
162 ret->guestfs_int_stat_list_val[i].blocks = statbuf.st_blocks;
163 ret->guestfs_int_stat_list_val[i].atime = statbuf.st_atime;
164 ret->guestfs_int_stat_list_val[i].mtime = statbuf.st_mtime;
165 ret->guestfs_int_stat_list_val[i].ctime = statbuf.st_ctime;
169 if (close (path_fd) == -1) {
170 reply_with_perror ("close: %s", path);
171 free (ret->guestfs_int_stat_list_val);
179 guestfs_int_statvfs *
180 do_statvfs (const char *path)
183 guestfs_int_statvfs *ret;
184 struct statvfs statbuf;
187 r = statvfs (path, &statbuf);
191 reply_with_perror ("statvfs");
195 ret = malloc (sizeof *ret);
197 reply_with_perror ("malloc");
201 ret->bsize = statbuf.f_bsize;
202 ret->frsize = statbuf.f_frsize;
203 ret->blocks = statbuf.f_blocks;
204 ret->bfree = statbuf.f_bfree;
205 ret->bavail = statbuf.f_bavail;
206 ret->files = statbuf.f_files;
207 ret->ffree = statbuf.f_ffree;
208 ret->favail = statbuf.f_favail;
209 ret->fsid = statbuf.f_fsid;
210 ret->flag = statbuf.f_flag;
211 ret->namemax = statbuf.f_namemax;