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>
29 #include "guestfs_protocol.h"
34 do_stat (const char *path)
37 guestfs_int_stat *ret;
41 r = stat (path, &statbuf);
45 reply_with_perror ("%s", path);
49 ret = malloc (sizeof *ret);
51 reply_with_perror ("malloc");
55 ret->dev = statbuf.st_dev;
56 ret->ino = statbuf.st_ino;
57 ret->mode = statbuf.st_mode;
58 ret->nlink = statbuf.st_nlink;
59 ret->uid = statbuf.st_uid;
60 ret->gid = statbuf.st_gid;
61 ret->rdev = statbuf.st_rdev;
62 ret->size = statbuf.st_size;
63 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
64 ret->blksize = statbuf.st_blksize;
68 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
69 ret->blocks = statbuf.st_blocks;
73 ret->atime = statbuf.st_atime;
74 ret->mtime = statbuf.st_mtime;
75 ret->ctime = statbuf.st_ctime;
81 do_lstat (const char *path)
84 guestfs_int_stat *ret;
88 r = lstat (path, &statbuf);
92 reply_with_perror ("%s", path);
96 ret = malloc (sizeof *ret);
98 reply_with_perror ("malloc");
102 ret->dev = statbuf.st_dev;
103 ret->ino = statbuf.st_ino;
104 ret->mode = statbuf.st_mode;
105 ret->nlink = statbuf.st_nlink;
106 ret->uid = statbuf.st_uid;
107 ret->gid = statbuf.st_gid;
108 ret->rdev = statbuf.st_rdev;
109 ret->size = statbuf.st_size;
110 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
111 ret->blksize = statbuf.st_blksize;
115 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
116 ret->blocks = statbuf.st_blocks;
120 ret->atime = statbuf.st_atime;
121 ret->mtime = statbuf.st_mtime;
122 ret->ctime = statbuf.st_ctime;
127 guestfs_int_stat_list *
128 do_lstatlist (const char *path, char *const *names)
131 guestfs_int_stat_list *ret;
134 nr_names = count_strings (names);
136 ret = malloc (sizeof *ret);
138 reply_with_perror ("malloc");
141 ret->guestfs_int_stat_list_len = nr_names;
142 ret->guestfs_int_stat_list_val = calloc (nr_names, sizeof (guestfs_int_stat));
143 if (ret->guestfs_int_stat_list_val == NULL) {
144 reply_with_perror ("malloc");
150 path_fd = open (path, O_RDONLY | O_DIRECTORY);
154 reply_with_perror ("%s", path);
155 free (ret->guestfs_int_stat_list_val);
160 for (i = 0; names[i] != NULL; ++i) {
164 r = fstatat (path_fd, names[i], &statbuf, AT_SYMLINK_NOFOLLOW);
166 ret->guestfs_int_stat_list_val[i].ino = -1;
168 ret->guestfs_int_stat_list_val[i].dev = statbuf.st_dev;
169 ret->guestfs_int_stat_list_val[i].ino = statbuf.st_ino;
170 ret->guestfs_int_stat_list_val[i].mode = statbuf.st_mode;
171 ret->guestfs_int_stat_list_val[i].nlink = statbuf.st_nlink;
172 ret->guestfs_int_stat_list_val[i].uid = statbuf.st_uid;
173 ret->guestfs_int_stat_list_val[i].gid = statbuf.st_gid;
174 ret->guestfs_int_stat_list_val[i].rdev = statbuf.st_rdev;
175 ret->guestfs_int_stat_list_val[i].size = statbuf.st_size;
176 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
177 ret->guestfs_int_stat_list_val[i].blksize = statbuf.st_blksize;
179 ret->guestfs_int_stat_list_val[i].blksize = -1;
181 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
182 ret->guestfs_int_stat_list_val[i].blocks = statbuf.st_blocks;
184 ret->guestfs_int_stat_list_val[i].blocks = -1;
186 ret->guestfs_int_stat_list_val[i].atime = statbuf.st_atime;
187 ret->guestfs_int_stat_list_val[i].mtime = statbuf.st_mtime;
188 ret->guestfs_int_stat_list_val[i].ctime = statbuf.st_ctime;
192 if (close (path_fd) == -1) {
193 reply_with_perror ("close: %s", path);
194 free (ret->guestfs_int_stat_list_val);