]> wimlib.net Git - wimlib/blobdiff - src/win32.c
add_image.c: Add debugging statements for branch attachment
[wimlib] / src / win32.c
index b3468a9a2b8d88062e63a528f306c68994a3a1e8..3ac8deca0ba2dd4934488e386ae17e3812224b4a 100644 (file)
@@ -29,18 +29,21 @@ void win32_error(u32 err_code)
 #define win32_error(err_code)
 #endif
 
-void *win32_open_file(const void *path)
+void *win32_open_file_readonly(const void *path)
 {
        return CreateFileW((const wchar_t*)path,
-                          GENERIC_READ | READ_CONTROL,
+                          FILE_READ_DATA |
+                              FILE_READ_ATTRIBUTES |
+                              READ_CONTROL |
+                              ACCESS_SYSTEM_SECURITY,
                           FILE_SHARE_READ,
                           NULL, /* lpSecurityAttributes */
                           OPEN_EXISTING,
                           FILE_FLAG_BACKUP_SEMANTICS |
-                                  FILE_FLAG_OPEN_REPARSE_POINT,
+                              FILE_FLAG_OPEN_REPARSE_POINT,
                           NULL /* hTemplateFile */);
 }
-               
+
 int win32_read_file(const char *filename,
                    void *handle, u64 offset, size_t size, u8 *buf)
 {
@@ -48,7 +51,7 @@ int win32_read_file(const char *filename,
        DWORD err;
        DWORD bytesRead;
        LARGE_INTEGER liOffset = {.QuadPart = offset};
-       
+
        wimlib_assert(size <= 0xffffffff);
 
        if (SetFilePointerEx(h, liOffset, NULL, FILE_BEGIN))
@@ -65,4 +68,57 @@ void win32_close_file(void *handle)
        CloseHandle((HANDLE)handle);
 }
 
+static bool win32_modify_privilege(const char *privilege, bool enable)
+{
+       HANDLE hToken;
+       LUID luid;
+       TOKEN_PRIVILEGES newState;
+       bool ret = false;
+
+       DEBUG("%s privilege %s",
+             enable ? "Enabling" : "Disabling", privilege);
+
+       if (!OpenProcessToken(GetCurrentProcess(),
+                             TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
+                             &hToken))
+       {
+               DEBUG("OpenProcessToken() failed");
+               goto out;
+       }
+
+       if (!LookupPrivilegeValue(NULL, privilege, &luid)) {
+               DEBUG("LookupPrivilegeValue() failed");
+               goto out;
+       }
+
+       newState.PrivilegeCount = 1;
+       newState.Privileges[0].Luid = luid;
+       newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
+       ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
+       if (!ret)
+               DEBUG("AdjustTokenPrivileges() failed");
+       CloseHandle(hToken);
+out:
+       if (!ret) {
+               DWORD err = GetLastError();
+               win32_error(err);
+               WARNING("Failed to %s privilege %s",
+                       enable ? "enable" : "disable", privilege);
+               WARNING("The program will continue, but if permission issues are "
+                       "encountered, you may need to run this program as the administrator");
+       }
+       return ret;
+}
+
+bool win32_acquire_privilege(const char *privilege)
+{
+       return win32_modify_privilege(privilege, true);
+}
+
+bool win32_release_privilege(const char *privilege)
+{
+       return win32_modify_privilege(privilege, false);
+}
+
+
 #endif /* __CYGWIN__ || __WIN32__ */