#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);
* 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.
if (wim_target_path[0] == T('\0'))
params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
ret = (*capture_tree)(&branch, fs_source_path, ¶ms);
- 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
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);
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;
}
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;
}
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;
}
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;
}
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;
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;
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;
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;
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;
}
}
} 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;
}
&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) {
*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;
}
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;
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;
}
}
} 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:
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,
¶ms->capture_root_ino,
¶ms->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);
#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.
}
}
+
+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
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;
}
}
goto out;
fail_win32:
- errno = win32_error_to_errno(err);
+ set_errno_from_win32_error(err);
out:
return resolved_path;
}
fail:
if (err == NO_ERROR)
err = GetLastError();
- errno = win32_error_to_errno(err);
+ set_errno_from_win32_error(err);
return -1;
}
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) |
*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;
}