1 /* Copyright (c) 2008, XenSource Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of XenSource Inc. nor the names of its contributors
12  *       may be used to endorse or promote products derived from this software
13  *       without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Altering operations:
28  *
29  * 1. Change the parent pointer to another file.
30  * 2. Change the size of the file containing the VHD image. This does NOT
31  * affect the VHD disk capacity, only the physical size of the file containing
32  * the VHD. Naturally, it is not possible to set the file size to be less than
33  * the what VHD utilizes.
34  * The operation doesn't actually change the file size, but it writes the
35  * footer in the right location such that resizing the file (manually, as a
36  * separate step) will produce the correct results. If the new file size is
37  * greater than the current file size, the file must first be expanded and then
38  * altered with this operation. If the new size is smaller than the current
39  * size, the VHD must first be altered with this operation and then the file
40  * must be shrunk. Failing to resize the file will result in a corrupted VHD.
41 */
42 
43 #include <errno.h>
44 //#include <fcntl.h>
45 #include <stdio.h>
46 //#include <stdlib.h>
47 #include <unistd.h>
48 
49 #include "libvhd.h"
50 #include "libvhd-journal.h"
51 
52 int
vhd_util_revert(int argc,char ** argv)53 vhd_util_revert(int argc, char **argv)
54 {
55 	char *name, *jname;
56 	vhd_journal_t journal;
57 	int c, err;
58 
59 	name  = NULL;
60 	jname = NULL;
61 
62 	optind = 0;
63 	while ((c = getopt(argc, argv, "n:j:h")) != -1) {
64 		switch (c) {
65 		case 'n':
66 			name = optarg;
67 			break;
68 		case 'j':
69 			jname = optarg;
70 			break;
71 		case 'h':
72 		default:
73 			goto usage;
74 		}
75 	}
76 
77 	if (!name || !jname || argc != optind)
78 		goto usage;
79 
80 	libvhd_set_log_level(1);
81 	err = vhd_journal_open(&journal, name, jname);
82 	if (err) {
83 		printf("opening journal failed: %d\n", err);
84 		return err;
85 	}
86 
87 	err = vhd_journal_revert(&journal);
88 	if (err) {
89 		printf("reverting journal failed: %d\n", err);
90 		vhd_journal_close(&journal);
91 		return err;
92 	}
93 
94 	err = vhd_journal_remove(&journal);
95 	if (err) {
96 		printf("removing journal failed: %d\n", err);
97 		vhd_journal_close(&journal);
98 		return err;
99 	}
100 
101 	return 0;
102 
103 usage:
104 	printf("options: <-n name> <-j journal> [-h help]\n");
105 	return -EINVAL;
106 }
107