daemon: Print error for invalid chunk.cancel field.
[libguestfs.git] / daemon / sync.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 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
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #ifdef HAVE_WINDOWS_H
22 #include <windows.h>
23 #endif
24
25 #include <stdio.h>
26 #include <unistd.h>
27
28 #include "daemon.h"
29 #include "actions.h"
30
31 #ifdef WIN32
32 static int sync_win32 (void);
33 #endif
34
35 int
36 do_sync (void)
37 {
38   if (sync_disks () == -1) {
39     reply_with_perror ("sync");
40     return -1;
41   }
42
43   return 0;
44 }
45
46 /* Older versions of libguestfs used to issue separate 'umount_all'
47  * and 'sync' commands just before closing the handle.  Since
48  * libguestfs 1.9.7 the library issues this 'internal_autosync'
49  * internal operation instead, allowing more control in the daemon.
50  */
51 int
52 do_internal_autosync (void)
53 {
54   int r = 0;
55
56   if (autosync_umount)
57     r = do_umount_all ();
58
59   sync_disks ();
60
61   return r;
62 }
63
64 /* This is a replacement for sync(2) which is called from
65  * this file and from other places in the daemon.  It works
66  * on Windows too.
67  */
68 int
69 sync_disks (void)
70 {
71 #if defined(HAVE_SYNC)
72   sync ();
73   return 0;
74 #elif defined(WIN32)
75   return sync_win32 ();
76 #else
77 #error "no known sync() API"
78 #endif
79 }
80
81 #ifdef WIN32
82 static int
83 sync_win32 (void)
84 {
85   DWORD n1, n2;
86
87   n1 = GetLogicalDriveStrings (0, NULL);
88   if (n1 == 0)
89     return -1;
90
91   TCHAR buffer[n1+2]; /* sic */
92   n2 = GetLogicalDriveStrings (n1, buffer);
93   if (n2 == 0)
94     return -1;
95
96   TCHAR *p = buffer;
97
98   /* The MSDN example code itself assumes that there is always one
99    * drive in the system.  However we will be better than that and not
100    * make the assumption ...
101    */
102   while (*p) {
103     HANDLE drive;
104     DWORD drive_type;
105
106     if (verbose)
107       fprintf (stderr, "sync_win32: examining drive %s\n", p);
108
109     /* Ignore removable drives. */
110     drive_type = GetDriveType (p);
111     if (drive_type == DRIVE_FIXED) {
112       /* To open the volume you have to specify the volume name, not
113        * the mount point.  MSDN documents use of the constant 50
114        * below.
115        */
116       TCHAR volname[50];
117       if (!GetVolumeNameForVolumeMountPoint (p, volname, 50))
118         return -1;
119
120       drive = CreateFile (volname, GENERIC_READ|GENERIC_WRITE,
121                           FILE_SHARE_READ|FILE_SHARE_WRITE,
122                           NULL, OPEN_EXISTING, 0, 0);
123       if (drive == INVALID_HANDLE_VALUE)
124         return -1;
125       if (verbose)
126         fprintf (stderr, "sync_win32: flushing %s\n", volname);
127
128       BOOL r;
129       /* This always fails in Wine:
130        * http://bugs.winehq.org/show_bug.cgi?id=14915
131        */
132       r = FlushFileBuffers (drive);
133       CloseHandle (drive);
134       if (!r)
135         return -1;
136     }
137
138     /* Skip to next \0 character. */
139     while (*p++);
140   }
141
142   return 0;
143 }
144 #endif /* WIN32 */