Implement list-devices and list-partitions.
[libguestfs.git] / fish / cmds.c
1 /* libguestfs generated file
2  * WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.
3  * ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.
4  *
5  * Copyright (C) 2009 Red Hat Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "fish.h"
27
28 void list_commands (void)
29 {
30   printf ("    %-16s     %s\n", "Command", "Description");
31   list_builtin_commands ();
32   printf ("%-20s %s\n", "cat", "list the contents of a file");
33   printf ("%-20s %s\n", "list-devices", "list the block devices");
34   printf ("%-20s %s\n", "list-partitions", "list the partitions");
35   printf ("%-20s %s\n", "ll", "list the files in a directory (long format)");
36   printf ("%-20s %s\n", "ls", "list the files in a directory");
37   printf ("%-20s %s\n", "mount", "mount a guest disk at a position in the filesystem");
38   printf ("%-20s %s\n", "sync", "sync disks, writes are flushed through to the disk image");
39   printf ("%-20s %s\n", "touch", "update file timestamps or create a new file");
40   printf ("    Use -h <cmd> / help <cmd> to show detailed help for a command.\n");
41 }
42
43 void display_command (const char *cmd)
44 {
45   if (strcasecmp (cmd, "mount") == 0)
46     pod2text ("mount - mount a guest disk at a position in the filesystem", " mount <device> <mountpoint>\n\nMount a guest disk at a position in the filesystem.  Block devices\nare named C</dev/sda>, C</dev/sdb> and so on, as they were added to\nthe guest.  If those block devices contain partitions, they will have\nthe usual names (eg. C</dev/sda1>).  Also LVM C</dev/VG/LV>-style\nnames can be used.\n\nThe rules are the same as for L<mount(2)>:  A filesystem must\nfirst be mounted on C</> before others can be mounted.  Other\nfilesystems can only be mounted on directories which already\nexist.\n\nThe mounted filesystem is writable, if we have sufficient permissions\non the underlying device.\n\nThe filesystem options C<sync> and C<noatime> are set with this\ncall, in order to improve reliability.");
47   else
48   if (strcasecmp (cmd, "sync") == 0)
49     pod2text ("sync - sync disks, writes are flushed through to the disk image", " sync\n\nThis syncs the disk, so that any writes are flushed through to the\nunderlying disk image.\n\nYou should always call this if you have modified a disk image, before\ncalling C<guestfs_close>.");
50   else
51   if (strcasecmp (cmd, "touch") == 0)
52     pod2text ("touch - update file timestamps or create a new file", " touch <path>\n\nTouch acts like the L<touch(1)> command.  It can be used to\nupdate the timestamps on a file, or, if the file does not exist,\nto create a new zero-length file.");
53   else
54   if (strcasecmp (cmd, "cat") == 0)
55     pod2text ("cat - list the contents of a file", " cat <path>\n\nReturn the contents of the file named C<path>.\n\nNote that this function cannot correctly handle binary files\n(specifically, files containing C<\\0> character which is treated\nas end of string).  For those you need to use the C<guestfs_read_file>\nfunction which has a more complex interface.\n\nBecause of the message protocol, there is a transfer limit \nof somewhere between 2MB and 4MB.  To transfer large files you should use\nFTP.");
56   else
57   if (strcasecmp (cmd, "ll") == 0)
58     pod2text ("ll - list the files in a directory (long format)", " ll <directory>\n\nList the files in C<directory> (relative to the root directory,\nthere is no cwd) in the format of 'ls -la'.\n\nThis command is mostly useful for interactive sessions.  It\nis I<not> intended that you try to parse the output string.");
59   else
60   if (strcasecmp (cmd, "ls") == 0)
61     pod2text ("ls - list the files in a directory", " ls <directory>\n\nList the files in C<directory> (relative to the root directory,\nthere is no cwd).  The '.' and '..' entries are not returned, but\nhidden files are shown.\n\nThis command is mostly useful for interactive sessions.  Programs\nshould probably use C<guestfs_readdir> instead.");
62   else
63   if (strcasecmp (cmd, "list_devices") == 0 || strcasecmp (cmd, "list-devices") == 0)
64     pod2text ("list-devices - list the block devices", " list-devices\n\nList all the block devices.\n\nThe full block device names are returned, eg. C</dev/sda>\n");
65   else
66   if (strcasecmp (cmd, "list_partitions") == 0 || strcasecmp (cmd, "list-partitions") == 0)
67     pod2text ("list-partitions - list the partitions", " list-partitions\n\nList all the partitions detected on all block devices.\n\nThe full partition device names are returned, eg. C</dev/sda1>\n\nThis does not return logical volumes.  For that you will need to\ncall C<guestfs_lvs>.");
68   else
69     display_builtin_command (cmd);
70 }
71
72 static int run_mount (const char *cmd, int argc, char *argv[])
73 {
74   int r;
75   const char *device;
76   const char *mountpoint;
77   if (argc != 2) {
78     fprintf (stderr, "%s should have 2 parameter(s)\n", cmd);
79     fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
80     return -1;
81   }
82   device = argv[0];
83   mountpoint = argv[1];
84   r = guestfs_mount (g, device, mountpoint);
85   return r;
86 }
87
88 static int run_sync (const char *cmd, int argc, char *argv[])
89 {
90   int r;
91   if (argc != 0) {
92     fprintf (stderr, "%s should have 0 parameter(s)\n", cmd);
93     fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
94     return -1;
95   }
96   r = guestfs_sync (g);
97   return r;
98 }
99
100 static int run_touch (const char *cmd, int argc, char *argv[])
101 {
102   int r;
103   const char *path;
104   if (argc != 1) {
105     fprintf (stderr, "%s should have 1 parameter(s)\n", cmd);
106     fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
107     return -1;
108   }
109   path = argv[0];
110   r = guestfs_touch (g, path);
111   return r;
112 }
113
114 static int run_cat (const char *cmd, int argc, char *argv[])
115 {
116   char *r;
117   const char *path;
118   if (argc != 1) {
119     fprintf (stderr, "%s should have 1 parameter(s)\n", cmd);
120     fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
121     return -1;
122   }
123   path = argv[0];
124   r = guestfs_cat (g, path);
125   if (r == NULL) return -1;
126   printf ("%s", r);
127   free (r);
128   return 0;
129 }
130
131 static int run_ll (const char *cmd, int argc, char *argv[])
132 {
133   char *r;
134   const char *directory;
135   if (argc != 1) {
136     fprintf (stderr, "%s should have 1 parameter(s)\n", cmd);
137     fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
138     return -1;
139   }
140   directory = argv[0];
141   r = guestfs_ll (g, directory);
142   if (r == NULL) return -1;
143   printf ("%s", r);
144   free (r);
145   return 0;
146 }
147
148 static int run_ls (const char *cmd, int argc, char *argv[])
149 {
150   char **r;
151   const char *directory;
152   if (argc != 1) {
153     fprintf (stderr, "%s should have 1 parameter(s)\n", cmd);
154     fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
155     return -1;
156   }
157   directory = argv[0];
158   r = guestfs_ls (g, directory);
159   if (r == NULL) return -1;
160   print_strings (r);
161   free_strings (r);
162   return 0;
163 }
164
165 static int run_list_devices (const char *cmd, int argc, char *argv[])
166 {
167   char **r;
168   if (argc != 0) {
169     fprintf (stderr, "%s should have 0 parameter(s)\n", cmd);
170     fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
171     return -1;
172   }
173   r = guestfs_list_devices (g);
174   if (r == NULL) return -1;
175   print_strings (r);
176   free_strings (r);
177   return 0;
178 }
179
180 static int run_list_partitions (const char *cmd, int argc, char *argv[])
181 {
182   char **r;
183   if (argc != 0) {
184     fprintf (stderr, "%s should have 0 parameter(s)\n", cmd);
185     fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
186     return -1;
187   }
188   r = guestfs_list_partitions (g);
189   if (r == NULL) return -1;
190   print_strings (r);
191   free_strings (r);
192   return 0;
193 }
194
195 int run_action (const char *cmd, int argc, char *argv[])
196 {
197   if (strcasecmp (cmd, "mount") == 0)
198     return run_mount (cmd, argc, argv);
199   else
200   if (strcasecmp (cmd, "sync") == 0)
201     return run_sync (cmd, argc, argv);
202   else
203   if (strcasecmp (cmd, "touch") == 0)
204     return run_touch (cmd, argc, argv);
205   else
206   if (strcasecmp (cmd, "cat") == 0)
207     return run_cat (cmd, argc, argv);
208   else
209   if (strcasecmp (cmd, "ll") == 0)
210     return run_ll (cmd, argc, argv);
211   else
212   if (strcasecmp (cmd, "ls") == 0)
213     return run_ls (cmd, argc, argv);
214   else
215   if (strcasecmp (cmd, "list_devices") == 0 || strcasecmp (cmd, "list-devices") == 0)
216     return run_list_devices (cmd, argc, argv);
217   else
218   if (strcasecmp (cmd, "list_partitions") == 0 || strcasecmp (cmd, "list-partitions") == 0)
219     return run_list_partitions (cmd, argc, argv);
220   else
221     {
222       fprintf (stderr, "%s: unknown command\n", cmd);
223       return -1;
224     }
225   return 0;
226 }
227