26 #include "dbus-protocol.h" 
   27 #include "dbus-string.h" 
   28 #include "dbus-internals.h" 
   29 #include "dbus-sysdeps-win.h" 
   30 #include "dbus-pipe.h" 
   47 _dbus_file_read (HANDLE            hnd,
 
   57   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   61   start = _dbus_string_get_length (buffer);
 
   71   result = ReadFile (hnd, data, count, &bytes_read, 
NULL);
 
   74       char *emsg = _dbus_win_error_string (GetLastError ());
 
   76                       "Failed to read from %p: %s", hnd, emsg);
 
   77       _dbus_win_free_error_string (emsg);
 
  116   const char *filename_c;
 
  118   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
  120   filename_c = _dbus_string_get_const_data (filename);
 
  122   hnd = CreateFileA (filename_c, GENERIC_READ,
 
  123                     FILE_SHARE_READ | FILE_SHARE_WRITE,
 
  124                     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 
NULL);
 
  125   if (hnd == INVALID_HANDLE_VALUE)
 
  127       char *emsg = _dbus_win_error_string (GetLastError ());
 
  129                        "Failed to open \"%s\": %s", filename_c, emsg);
 
  130       _dbus_win_free_error_string (emsg);
 
  134   _dbus_verbose (
"file %s hnd %p opened\n", filename_c, hnd);
 
  136   fsize = GetFileSize (hnd, &fsize_hi);
 
  137   if (fsize == 0xFFFFFFFF && GetLastError() != NO_ERROR)
 
  139       char *emsg = _dbus_win_error_string (GetLastError ());
 
  141                       "Failed to get file size for \"%s\": %s",
 
  143       _dbus_win_free_error_string (emsg);
 
  145       _dbus_verbose (
"GetFileSize() failed: %s", emsg);
 
  152   if (fsize_hi != 0 || fsize > _DBUS_ONE_MEGABYTE)
 
  155                       "File size %lu/%lu of \"%s\" is too large.",
 
  156                       (
unsigned long) fsize_hi,
 
  157                       (
unsigned long) fsize, filename_c);
 
  163   orig_len = _dbus_string_get_length (str);
 
  168       while (total < fsize)
 
  170           bytes_read = _dbus_file_read (hnd, str, fsize - total, error);
 
  176                                   "Premature EOF reading \"%s\"",
 
  180                 _DBUS_ASSERT_ERROR_IS_SET (error);
 
  219   const char *filename_c;
 
  221   const char *tmp_filename_c;
 
  227   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
  229   hnd = INVALID_HANDLE_VALUE;
 
  253 #define N_TMP_FILENAME_RANDOM_BYTES 8 
  261   filename_c = _dbus_string_get_const_data (filename);
 
  262   tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
 
  265   hnd = CreateFileA (tmp_filename_c, GENERIC_WRITE,
 
  266                      FILE_SHARE_READ | FILE_SHARE_WRITE,
 
  267                      NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
 
  268                      INVALID_HANDLE_VALUE);
 
  269   if (hnd == INVALID_HANDLE_VALUE)
 
  271       char *emsg = _dbus_win_error_string (GetLastError ());
 
  273                        "Could not create \"%s\": %s", filename_c, emsg);
 
  274       _dbus_win_free_error_string (emsg);
 
  283   _dbus_verbose (
"tmp file %s hnd %p opened\n", tmp_filename_c, hnd);
 
  288   bytes_to_write = _dbus_string_get_length (str);
 
  289   str_c = _dbus_string_get_const_data (str);
 
  291   while (total < bytes_to_write)
 
  296       res = WriteFile (hnd, str_c + total, bytes_to_write - total,
 
  297                        &bytes_written, 
NULL);
 
  299       if (res == 0 || bytes_written <= 0)
 
  301           char *emsg = _dbus_win_error_string (GetLastError ());
 
  303                            "Could not write to %s: %s", tmp_filename_c, emsg);
 
  304           _dbus_win_free_error_string (emsg);
 
  308       total += bytes_written;
 
  311   if (CloseHandle (hnd) == 0)
 
  313       char *emsg = _dbus_win_error_string (GetLastError ());
 
  315                        "Could not close file %s: %s", tmp_filename_c, emsg);
 
  316       _dbus_win_free_error_string (emsg);
 
  320   hnd = INVALID_HANDLE_VALUE;
 
  323   if (!MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING))
 
  325       char *emsg = _dbus_win_error_string (GetLastError ());
 
  327                        "Could not rename %s to %s: %s",
 
  328                        tmp_filename_c, filename_c, emsg);
 
  329       _dbus_win_free_error_string (emsg);
 
  341   if (hnd != INVALID_HANDLE_VALUE)
 
  344   if (need_unlink && DeleteFileA (tmp_filename_c) == 0)
 
  346       char *emsg = _dbus_win_error_string (GetLastError ());
 
  347       _dbus_verbose (
"Failed to unlink temp file %s: %s", tmp_filename_c,
 
  349       _dbus_win_free_error_string (emsg);
 
  355     _DBUS_ASSERT_ERROR_IS_SET (error);
 
  372   const char *filename_c;
 
  374   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
  376   filename_c = _dbus_string_get_const_data (filename);
 
  378   hnd = CreateFileA (filename_c, GENERIC_WRITE,
 
  379                      FILE_SHARE_READ | FILE_SHARE_WRITE,
 
  380                      NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
 
  381                      INVALID_HANDLE_VALUE);
 
  382   if (hnd == INVALID_HANDLE_VALUE)
 
  384       char *emsg = _dbus_win_error_string (GetLastError ());
 
  386                        "Could not create file %s: %s",
 
  388       _dbus_win_free_error_string (emsg);
 
  392   _dbus_verbose (
"exclusive file %s hnd %p opened\n", filename_c, hnd);
 
  394   if (CloseHandle (hnd) == 0)
 
  396       char *emsg = _dbus_win_error_string (GetLastError ());
 
  398                        "Could not close file %s: %s",
 
  400       _dbus_win_free_error_string (emsg);