Add progress messages to download command.
[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 /* This is a replacement for sync(2) which is called from
47  * this file and from other places in the daemon.  It works
48  * on Windows too.
49  */
50 int
51 sync_disks (void)
52 {
53 #if defined(HAVE_SYNC)
54   sync ();
55   return 0;
56 #elif defined(WIN32)
57   return sync_win32 ();
58 #else
59 #error "no known sync() API"
60 #endif
61 }
62
63 #ifdef WIN32
64 static int
65 sync_win32 (void)
66 {
67   DWORD n1, n2;
68
69   n1 = GetLogicalDriveStrings (0, NULL);
70   if (n1 == 0)
71     return -1;
72
73   TCHAR buffer[n1+2]; /* sic */
74   n2 = GetLogicalDriveStrings (n1, buffer);
75   if (n2 == 0)
76     return -1;
77
78   TCHAR *p = buffer;
79
80   /* The MSDN example code itself assumes that there is always one
81    * drive in the system.  However we will be better than that and not
82    * make the assumption ...
83    */
84   while (*p) {
85     HANDLE drive;
86     DWORD drive_type;
87
88     if (verbose)
89       fprintf (stderr, "sync_win32: examining drive %s\n", p);
90
91     /* Ignore removable drives. */
92     drive_type = GetDriveType (p);
93     if (drive_type == DRIVE_FIXED) {
94       /* To open the volume you have to specify the volume name, not
95        * the mount point.  MSDN documents use of the constant 50
96        * below.
97        */
98       TCHAR volname[50];
99       if (!GetVolumeNameForVolumeMountPoint (p, volname, 50))
100         return -1;
101
102       drive = CreateFile (volname, GENERIC_READ|GENERIC_WRITE,
103                           FILE_SHARE_READ|FILE_SHARE_WRITE,
104                           NULL, OPEN_EXISTING, 0, 0);
105       if (drive == INVALID_HANDLE_VALUE)
106         return -1;
107       if (verbose)
108         fprintf (stderr, "sync_win32: flushing %s\n", volname);
109
110       BOOL r;
111       /* This always fails in Wine:
112        * http://bugs.winehq.org/show_bug.cgi?id=14915
113        */
114       r = FlushFileBuffers (drive);
115       CloseHandle (drive);
116       if (!r)
117         return -1;
118     }
119
120     /* Skip to next \0 character. */
121     while (*p++);
122   }
123
124   return 0;
125 }
126 #endif /* WIN32 */