Win32: Adjust error printing
authorEric Biggers <ebiggers3@gmail.com>
Sun, 18 Aug 2013 16:16:07 +0000 (11:16 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sun, 18 Aug 2013 16:16:07 +0000 (11:16 -0500)
include/wimlib/win32_common.h
src/reparse.c
src/update_image.c
src/util.c
src/win32_apply.c
src/win32_capture.c
src/win32_common.c
src/win32_replacements.c

index ce23f19..0d594f6 100644 (file)
@@ -9,27 +9,21 @@
 #include "wimlib/types.h"
 #include "wimlib/win32.h"
 
+extern void
+set_errno_from_GetLastError(void);
 
-#ifdef ENABLE_ERROR_MESSAGES
 extern void
-win32_error(DWORD err_code);
-#else
-static inline void
-win32_error(DWORD err_code)
-{
-}
-#endif
+set_errno_from_win32_error(DWORD err);
 
+#ifdef WITH_NTDLL
 extern void
-set_errno_from_GetLastError(void);
+set_errno_from_nt_status(DWORD status);
+#endif
 
 extern bool
 win32_path_is_root_of_drive(const wchar_t *path);
 
 extern int
-win32_error_to_errno(DWORD err_code);
-
-extern int
 win32_get_vol_flags(const wchar_t *path, unsigned *vol_flags_ret,
                    bool *supports_SetFileShortName_ret);
 
index 9003766..1cbca56 100644 (file)
@@ -86,15 +86,15 @@ static const utf16lechar volume_junction_prefix[11] = {
  * Return value is:
  *
  * Non-negative integer:
- *     The name is an absolute symbolic link in one of several formats,
- *     and the return value is the number of UTF-16LE characters that need to
- *     be advanced to reach a simple "absolute" path starting with a backslash
- *     (i.e. skip over \??\ and/or drive letter)
+ *     The name is an absolute symbolic link in one of several formats,
+ *     and the return value is the number of UTF-16LE characters that need to
+ *     be advanced to reach a simple "absolute" path starting with a backslash
+ *     (i.e. skip over \??\ and/or drive letter)
  * Negative integer:
  *     SUBST_NAME_IS_VOLUME_JUNCTION:
- *             The name is a volume junction.
+ *             The name is a volume junction.
  *     SUBST_NAME_IS_RELATIVE_LINK:
- *             The name is a relative symbolic link.
+ *             The name is a relative symbolic link.
  *     SUBST_NAME_IS_UNKNOWN:
  *             The name does not appear to be a valid symbolic link, junction,
  *             or mount point.
index a2664a1..f2bdba3 100644 (file)
@@ -252,11 +252,9 @@ execute_add_command(WIMStruct *wim,
        if (wim_target_path[0] == T('\0'))
                params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
        ret = (*capture_tree)(&branch, fs_source_path, &params);
-       if (ret) {
-               ERROR("Failed to build dentry tree for \"%"TS"\"",
-                     fs_source_path);
+       if (ret)
                goto out_destroy_sd_set;
-       }
+
        if (branch) {
                /* Use the target name, not the source name, for
                 * the root of each branch from a capture
index 9250b70..6f17b5b 100644 (file)
@@ -169,6 +169,10 @@ wimlib_vmsg(const tchar *tag, const tchar *format,
                                         T("unknown error (errno=%d)"),
                                         errno_save);
                        }
+               #ifdef WIN32
+                       if (errno_save == EBUSY)
+                               tstrcpy(buf, T("Resource busy"));
+               #endif
                        tfprintf(stderr, T(": %"TS), buf);
                }
                tputc(T('\n'), stderr);
index b6f2e84..adf605b 100644 (file)
@@ -271,7 +271,7 @@ win32_extract_encrypted_stream(file_spec_t file,
 
        err = OpenEncryptedFileRaw(path, CREATE_FOR_IMPORT, &file_ctx);
        if (err != ERROR_SUCCESS) {
-               errno = win32_error_to_errno(err);
+               set_errno_from_win32_error(err);
                ret = WIMLIB_ERR_OPEN;
                goto out;
        }
@@ -281,7 +281,7 @@ win32_extract_encrypted_stream(file_spec_t file,
        err = WriteEncryptedFileRaw(win32_encrypted_import_cb, &extract_ctx,
                                    file_ctx);
        if (err != ERROR_SUCCESS) {
-               errno = win32_error_to_errno(err);
+               set_errno_from_win32_error(err);
                ret = WIMLIB_ERR_WRITE;
                goto out_close;
        }
index 2a69009..2489d9b 100644 (file)
@@ -92,9 +92,8 @@ read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
 
        HANDLE hFile = win32_open_file_data_only(lte->file_on_disk);
        if (hFile == INVALID_HANDLE_VALUE) {
-               err = GetLastError();
-               ERROR("Failed to open \"%ls\"", lte->file_on_disk);
-               win32_error(err);
+               set_errno_from_GetLastError();
+               ERROR_WITH_ERRNO("Failed to open \"%ls\"", lte->file_on_disk);
                return WIMLIB_ERR_OPEN;
        }
 
@@ -111,9 +110,9 @@ read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
                if (!ReadFile(hFile, out_buf, bytesToRead, &bytesRead, NULL) ||
                    bytesRead != bytesToRead)
                {
-                       err = GetLastError();
-                       ERROR("Failed to read data from \"%ls\"", lte->file_on_disk);
-                       win32_error(err);
+                       set_errno_from_GetLastError();
+                       ERROR_WITH_ERRNO("Failed to read data from \"%ls\"",
+                                        lte->file_on_disk);
                        ret = WIMLIB_ERR_READ;
                        break;
                }
@@ -219,18 +218,18 @@ read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
 
        err = OpenEncryptedFileRawW(lte->file_on_disk, 0, &file_ctx);
        if (err != ERROR_SUCCESS) {
-               ERROR("Failed to open encrypted file \"%ls\" for raw read",
-                     lte->file_on_disk);
-               win32_error(err);
+               set_errno_from_win32_error(err);
+               ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
+                                "for raw read", lte->file_on_disk);
                ret = WIMLIB_ERR_OPEN;
                goto out_free_buf;
        }
        err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
                                   &export_ctx, file_ctx);
        if (err != ERROR_SUCCESS) {
-               ERROR("Failed to read encrypted file \"%ls\"",
-                     lte->file_on_disk);
-               win32_error(err);
+               set_errno_from_win32_error(err);
+               ERROR_WITH_ERRNO("Failed to read encrypted file \"%ls\"",
+                                lte->file_on_disk);
                ret = export_ctx.wimlib_err_code;
                if (ret == 0)
                        ret = WIMLIB_ERR_READ;
@@ -422,7 +421,7 @@ win32_get_security_descriptor(HANDLE hFile,
                        goto out_free_buf;
                default:
                fail:
-                       errno = win32_error_to_errno(err);
+                       set_errno_from_win32_error(err);
                        ERROR("Failed to read security descriptor of \"%ls\"", path);
                        ret = WIMLIB_ERR_READ;
                        goto out_free_buf;
@@ -490,8 +489,7 @@ win32_recurse_directory(HANDLE hDir,
                            status == STATUS_NO_MORE_MATCHES) {
                                ret = 0;
                        } else {
-                               errno = win32_error_to_errno(
-                                               RtlNtStatusToDosError(status));
+                               set_errno_from_nt_status(status);
                                ERROR_WITH_ERRNO("Failed to read directory "
                                                 "\"%ls\"", dir_path);
                                ret = WIMLIB_ERR_READ;
@@ -503,7 +501,7 @@ win32_recurse_directory(HANDLE hDir,
                for (;;) {
                        if (!(info->FileNameLength == 2 && info->FileName[0] == L'.') &&
                            !(info->FileNameLength == 4 && info->FileName[0] == L'.' &&
-                                                          info->FileName[1] == L'.'))
+                                                          info->FileName[1] == L'.'))
                        {
                                wchar_t *p;
                                struct wim_dentry *child;
@@ -558,8 +556,9 @@ out_free_buf:
                if (err == ERROR_FILE_NOT_FOUND) {
                        return 0;
                } else {
-                       ERROR("Failed to read directory \"%ls\"", dir_path);
-                       win32_error(err);
+                       set_errno_from_win32_error(err);
+                       ERROR_WITH_ERRNO("Failed to read directory \"%ls\"",
+                                        dir_path);
                        return WIMLIB_ERR_READ;
                }
        }
@@ -594,8 +593,8 @@ out_free_buf:
        } while (FindNextFileW(hFind, &dat));
        err = GetLastError();
        if (err != ERROR_NO_MORE_FILES) {
-               ERROR("Failed to read directory \"%ls\"", dir_path);
-               win32_error(err);
+               set_errno_from_win32_error(err);
+               ERROR_WITH_ERRNO("Failed to read directory \"%ls\"", dir_path);
                if (ret == 0)
                        ret = WIMLIB_ERR_READ;
        }
@@ -780,9 +779,8 @@ win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
                             &bytesReturned,
                             NULL))
        {
-               DWORD err = GetLastError();
-               ERROR("Failed to get reparse data of \"%ls\"", path);
-               win32_error(err);
+               set_errno_from_GetLastError();
+               ERROR_WITH_ERRNO("Failed to get reparse data of \"%ls\"", path);
                return -WIMLIB_ERR_READ;
        }
        if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
@@ -827,15 +825,17 @@ win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
        *size_ret = 0;
        err = OpenEncryptedFileRawW(path, 0, &file_ctx);
        if (err != ERROR_SUCCESS) {
-               ERROR("Failed to open encrypted file \"%ls\" for raw read", path);
-               win32_error(err);
+               set_errno_from_win32_error(err);
+               ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
+                                "for raw read", path);
                return WIMLIB_ERR_OPEN;
        }
        err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
                                   size_ret, file_ctx);
        if (err != ERROR_SUCCESS) {
-               ERROR("Failed to read raw encrypted data from \"%ls\"", path);
-               win32_error(err);
+               set_errno_from_win32_error(err);
+               ERROR_WITH_ERRNO("Failed to read raw encrypted data from "
+                                "\"%ls\"", path);
                ret = WIMLIB_ERR_READ;
        } else {
                ret = 0;
@@ -1058,7 +1058,7 @@ win32_capture_streams(HANDLE hFile,
                        }
                        buf = newbuf;
                } else {
-                       errno = win32_error_to_errno(RtlNtStatusToDosError(status));
+                       set_errno_from_nt_status(status);
                        ERROR_WITH_ERRNO("Failed to read streams of %ls", path);
                        ret = WIMLIB_ERR_READ;
                        goto out_free_buf;
@@ -1122,9 +1122,9 @@ out_free_buf:
                                        path, capture_access_denied_msg);
                                return 0;
                        } else {
-                               ERROR("Failed to look up data streams "
-                                     "of \"%ls\"", path);
-                               win32_error(err);
+                               set_errno_from_win32_error(err);
+                               ERROR_WITH_ERRNO("Failed to look up data streams "
+                                                "of \"%ls\"", path);
                                return WIMLIB_ERR_READ;
                        }
                }
@@ -1139,8 +1139,9 @@ out_free_buf:
        } while (win32func_FindNextStreamW(hFind, &dat));
        err = GetLastError();
        if (err != ERROR_HANDLE_EOF) {
-               ERROR("Win32 API: Error reading data streams from \"%ls\"", path);
-               win32_error(err);
+               set_errno_from_win32_error(err);
+               ERROR_WITH_ERRNO("Error reading data streams from "
+                                "\"%ls\"", path);
                ret = WIMLIB_ERR_READ;
        }
 out_find_close:
@@ -1400,19 +1401,13 @@ win32_build_dentry_tree(struct wim_dentry **root_ret,
        if (path_nchars > WINDOWS_NT_MAX_PATH)
                return WIMLIB_ERR_INVALID_PARAM;
 
-       if (GetFileAttributesW(root_disk_path) == INVALID_FILE_ATTRIBUTES &&
-           GetLastError() == ERROR_FILE_NOT_FOUND)
-       {
-               ERROR("Capture directory \"%ls\" does not exist!",
-                     root_disk_path);
-               return WIMLIB_ERR_OPENDIR;
-       }
-
        ret = win32_get_file_and_vol_ids(root_disk_path,
                                         &params->capture_root_ino,
                                         &params->capture_root_dev);
-       if (ret)
+       if (ret) {
+               ERROR_WITH_ERRNO("Can't open %ls", root_disk_path);
                return ret;
+       }
 
        win32_get_vol_flags(root_disk_path, &vol_flags, NULL);
 
index e3dace3..d59796b 100644 (file)
 
 #include <errno.h>
 
-#include "wimlib/win32_common.h"
+#ifdef WITH_NTDLL
+#  include <winternl.h>
+#endif
 
+#include "wimlib/win32_common.h"
 #include "wimlib/assert.h"
 #include "wimlib/error.h"
 #include "wimlib/util.h"
 
-#ifdef ENABLE_ERROR_MESSAGES
-void
-win32_error(DWORD err_code)
-{
-       wchar_t *buffer;
-       DWORD nchars;
-       nchars = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
-                                   FORMAT_MESSAGE_ALLOCATE_BUFFER,
-                               NULL, err_code, 0,
-                               (wchar_t*)&buffer, 0, NULL);
-       if (nchars == 0) {
-               ERROR("Error printing error message! "
-                     "Computer will self-destruct in 3 seconds.");
-       } else {
-               ERROR("Win32 error: %ls", buffer);
-               LocalFree(buffer);
-       }
-}
-#endif /* ENABLE_ERROR_MESSAGES */
-
-int
+static int
 win32_error_to_errno(DWORD err_code)
 {
        /* This mapping is that used in Cygwin.
@@ -324,12 +307,27 @@ win32_error_to_errno(DWORD err_code)
        }
 }
 
+
+void
+set_errno_from_win32_error(DWORD err)
+{
+       errno = win32_error_to_errno(err);
+}
+
 void
 set_errno_from_GetLastError(void)
 {
-       errno = win32_error_to_errno(GetLastError());
+       set_errno_from_win32_error(GetLastError());
 }
 
+#ifdef WITH_NTDLL
+void
+set_errno_from_nt_status(DWORD status)
+{
+       set_errno_from_win32_error(RtlNtStatusToDosError(status));
+}
+#endif
+
 /* Given a Windows-style path, return the number of characters of the prefix
  * that specify the path to the root directory of a drive, or return 0 if the
  * drive is relative (or at least on the current drive, in the case of
@@ -428,9 +426,9 @@ win32_get_vol_flags(const wchar_t *path, unsigned *vol_flags_ret,
                        filesystem_name,                /* lpFileSystemNameBuffer */
                        ARRAY_LEN(filesystem_name));    /* nFileSystemNameSize */
        if (!bret) {
-               DWORD err = GetLastError();
-               WARNING("Failed to get volume information for path \"%ls\"", path);
-               win32_error(err);
+               set_errno_from_GetLastError();
+               WARNING_WITH_ERRNO("Failed to get volume information for "
+                                  "path \"%ls\"", path);
                vol_flags = 0xffffffff;
                goto out;
        }
index c9a59ba..d16936c 100644 (file)
@@ -92,7 +92,7 @@ realpath(const wchar_t *path, wchar_t *resolved_path)
        }
        goto out;
 fail_win32:
-       errno = win32_error_to_errno(err);
+       set_errno_from_win32_error(err);
 out:
        return resolved_path;
 }
@@ -146,7 +146,7 @@ fail_close_handle:
 fail:
        if (err == NO_ERROR)
                err = GetLastError();
-       errno = win32_error_to_errno(err);
+       set_errno_from_win32_error(err);
        return -1;
 }
 
@@ -260,26 +260,18 @@ ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
 int
 win32_get_file_and_vol_ids(const wchar_t *path, u64 *ino_ret, u64 *dev_ret)
 {
-       HANDLE hFile;
-       DWORD err;
+       HANDLE h;
        BY_HANDLE_FILE_INFORMATION file_info;
        int ret;
+       DWORD err;
 
-       hFile = win32_open_existing_file(path, FILE_READ_ATTRIBUTES);
-       if (hFile == INVALID_HANDLE_VALUE) {
-               err = GetLastError();
-               if (err != ERROR_FILE_NOT_FOUND) {
-                       WARNING("Failed to open \"%ls\" to get file "
-                               "and volume IDs", path);
-                       win32_error(err);
-               }
-               return WIMLIB_ERR_OPEN;
+       h = win32_open_existing_file(path, FILE_READ_ATTRIBUTES);
+       if (h == INVALID_HANDLE_VALUE) {
+               ret = WIMLIB_ERR_OPEN;
+               goto out;
        }
 
-       if (!GetFileInformationByHandle(hFile, &file_info)) {
-               err = GetLastError();
-               ERROR("Failed to get file information for \"%ls\"", path);
-               win32_error(err);
+       if (!GetFileInformationByHandle(h, &file_info)) {
                ret = WIMLIB_ERR_STAT;
        } else {
                *ino_ret = ((u64)file_info.nFileIndexHigh << 32) |
@@ -287,7 +279,12 @@ win32_get_file_and_vol_ids(const wchar_t *path, u64 *ino_ret, u64 *dev_ret)
                *dev_ret = file_info.dwVolumeSerialNumber;
                ret = 0;
        }
-       CloseHandle(hFile);
+out_close_handle:
+       err = GetLastError();
+       CloseHandle(h);
+       SetLastError(err);
+out:
+       set_errno_from_GetLastError();
        return ret;
 }