Version 1.7.6.
[libguestfs.git] / capitests / test-last-errno.c
1 /* libguestfs
2  * Copyright (C) 2010 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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 /* Test that we can get correct errnos all the way back from the
20  * appliance, translated to the local operating system.
21  */
22
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31
32 #include "guestfs.h"
33
34 int
35 main (int argc, char *argv[])
36 {
37   guestfs_h *g;
38   int fd, r, err;
39   struct guestfs_stat *stat;
40   const char *filename = "test1.img";
41
42   g = guestfs_create ();
43   if (g == NULL) {
44     fprintf (stderr, "failed to create handle\n");
45     exit (EXIT_FAILURE);
46   }
47
48   fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
49   if (fd == -1) {
50     perror (filename);
51     exit (EXIT_FAILURE);
52   }
53   if (ftruncate (fd, 524288000) == -1) {
54     perror (filename);
55     close (fd);
56     unlink (filename);
57     exit (EXIT_FAILURE);
58   }
59   if (close (fd) == -1) {
60     perror (filename);
61     unlink (filename);
62     exit (EXIT_FAILURE);
63   }
64
65   if (guestfs_add_drive_opts (g, filename,
66                               GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
67                               -1) == -1)
68     exit (EXIT_FAILURE);
69
70   if (guestfs_launch (g) == -1)
71     exit (EXIT_FAILURE);
72
73   if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1)
74     exit (EXIT_FAILURE);
75
76   if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1)
77     exit (EXIT_FAILURE);
78
79   /* Mount read-only, and check that errno == EROFS is passed back when
80    * we create a file.
81    */
82   if (guestfs_mount_ro (g, "/dev/sda1", "/") == -1)
83     exit (EXIT_FAILURE);
84
85   r = guestfs_touch (g, "/test");
86   if (r != -1) {
87     fprintf (stderr,
88              "guestfs_touch: expected error for read-only filesystem\n");
89     exit (EXIT_FAILURE);
90   }
91
92   err = guestfs_last_errno (g);
93   if (err != EROFS) {
94     fprintf (stderr,
95              "guestfs_touch: expected errno == EROFS, but got %d\n", err);
96     exit (EXIT_FAILURE);
97   }
98
99   if (guestfs_umount (g, "/") == -1)
100     exit (EXIT_FAILURE);
101
102   /* Mount it writable and test some other errors. */
103   if (guestfs_mount_options (g, "", "/dev/sda1", "/") == -1)
104     exit (EXIT_FAILURE);
105
106   stat = guestfs_lstat (g, "/nosuchfile");
107   if (stat != NULL) {
108     fprintf (stderr,
109              "guestfs_lstat: expected error for missing file\n");
110     exit (EXIT_FAILURE);
111   }
112
113   err = guestfs_last_errno (g);
114   if (err != ENOENT) {
115     fprintf (stderr,
116              "guestfs_lstat: expected errno == ENOENT, but got %d\n", err);
117     exit (EXIT_FAILURE);
118   }
119
120   if (guestfs_touch (g, "/test") == -1)
121     exit (EXIT_FAILURE);
122
123   r = guestfs_mkdir (g, "/test");
124   if (r != -1) {
125     fprintf (stderr,
126              "guestfs_mkdir: expected error for file which exists\n");
127     exit (EXIT_FAILURE);
128   }
129
130   err = guestfs_last_errno (g);
131   if (err != EEXIST) {
132     fprintf (stderr,
133              "guestfs_mkdir: expected errno == EEXIST, but got %d\n", err);
134     exit (EXIT_FAILURE);
135   }
136
137   guestfs_close (g);
138
139   unlink (filename);
140
141   exit (EXIT_SUCCESS);
142 }