X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fsync.c;h=b848ab54b337d56f136bd9f9b7131e80298e74b6;hp=9ade84039cc07fd69d2991541d7cdc769960dbd9;hb=4d900cdac8258daa2e99c6ceb2a4985154e94150;hpb=40ca9a57829f2e82362e391d7d998bf33c8bd671 diff --git a/daemon/sync.c b/daemon/sync.c index 9ade840..b848ab5 100644 --- a/daemon/sync.c +++ b/daemon/sync.c @@ -1,5 +1,5 @@ /* libguestfs - the guestfsd daemon - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,15 +18,109 @@ #include +#ifdef HAVE_WINDOWS_H +#include +#endif + #include #include +#include "daemon.h" #include "actions.h" +#ifdef WIN32 +static int sync_win32 (void); +#endif + +int +do_sync (void) +{ + if (sync_disks () == -1) { + reply_with_perror ("sync"); + return -1; + } + + return 0; +} + +/* This is a replacement for sync(2) which is called from + * this file and from other places in the daemon. It works + * on Windows too. + */ int -do_sync () +sync_disks (void) { +#if defined(HAVE_SYNC) sync (); - fprintf (stderr, "guestfsd: disk synched\n"); + return 0; +#elif defined(WIN32) + return sync_win32 (); +#else +#error "no known sync() API" +#endif +} + +#ifdef WIN32 +static int +sync_win32 (void) +{ + DWORD n1, n2; + + n1 = GetLogicalDriveStrings (0, NULL); + if (n1 == 0) + return -1; + + TCHAR buffer[n1+2]; /* sic */ + n2 = GetLogicalDriveStrings (n1, buffer); + if (n2 == 0) + return -1; + + TCHAR *p = buffer; + + /* The MSDN example code itself assumes that there is always one + * drive in the system. However we will be better than that and not + * make the assumption ... + */ + while (*p) { + HANDLE drive; + DWORD drive_type; + + if (verbose) + fprintf (stderr, "sync_win32: examining drive %s\n", p); + + /* Ignore removable drives. */ + drive_type = GetDriveType (p); + if (drive_type == DRIVE_FIXED) { + /* To open the volume you have to specify the volume name, not + * the mount point. MSDN documents use of the constant 50 + * below. + */ + TCHAR volname[50]; + if (!GetVolumeNameForVolumeMountPoint (p, volname, 50)) + return -1; + + drive = CreateFile (volname, GENERIC_READ|GENERIC_WRITE, + FILE_SHARE_READ|FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, 0, 0); + if (drive == INVALID_HANDLE_VALUE) + return -1; + if (verbose) + fprintf (stderr, "sync_win32: flushing %s\n", volname); + + BOOL r; + /* This always fails in Wine: + * http://bugs.winehq.org/show_bug.cgi?id=14915 + */ + r = FlushFileBuffers (drive); + CloseHandle (drive); + if (!r) + return -1; + } + + /* Skip to next \0 character. */ + while (*p++); + } + return 0; } +#endif /* WIN32 */