diff -r d0aa9e0a6d04 -r 6112bcc5d1cf librazor/atomic-ktm.c --- a/librazor/atomic-ktm.c Thu Feb 09 20:42:08 2012 +0000 +++ b/librazor/atomic-ktm.c Sat Feb 11 23:50:26 2012 +0000 @@ -131,45 +131,6 @@ return atomic; } -void -razor_atomic_set_error_str(struct razor_atomic *atomic, const wchar_t *path, - const char *str) -{ - assert(!atomic->error_str); - - free(atomic->error_path); - - if (path) - atomic->error_path = razor_utf16_to_utf8(path, -1); - else - atomic->error_path = NULL; - - atomic->error_str = strdup(str); -} - -static void -razor_atomic_set_error(struct razor_atomic *atomic, const wchar_t *path, - DWORD error) -{ - wchar_t *buf; - - assert(!atomic->error_str); - - free(atomic->error_path); - - if (path) - atomic->error_path = razor_utf16_to_utf8(path, -1); - else - atomic->error_path = NULL; - - FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER| - FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), - (LPWSTR)&buf, 0, NULL); - atomic->error_str = razor_utf16_to_utf8(buf, -1); - LocalFree(buf); -} - RAZOR_EXPORT int razor_atomic_commit(struct razor_atomic *atomic) { @@ -181,7 +142,7 @@ retval = !CommitTransaction(atomic->transaction); if (retval) { - razor_atomic_set_error(atomic, NULL, GetLastError()); + atomic->error = razor_error_new_mswin(NULL, GetLastError()); RollbackTransaction(atomic->transaction); } @@ -207,9 +168,8 @@ RollbackTransaction(atomic->transaction); CloseHandle(atomic->transaction); } - free(atomic->error_path); - free(atomic->error_str); - free(atomic->error_msg); + if (atomic->error) + razor_error_free(atomic->error); free(atomic); } @@ -250,16 +210,15 @@ if (err == ERROR_FILE_NOT_FOUND) { creating = 1; } else { - razor_atomic_set_error(atomic, - buffer->str, - err); + atomic->error = razor_error_new_mswin(buffer->str, + err); razor_wstr_destroy(buffer); return -1; } } else if (!(fa.dwFileAttributes& FILE_ATTRIBUTE_DIRECTORY)) { - razor_atomic_set_error_str(atomic, buffer->str, - "Not a directory"); + atomic->error = razor_error_new_str(buffer->str, + "Not a directory"); razor_wstr_destroy(buffer); return -1; } @@ -267,8 +226,8 @@ if (creating) { if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL, atomic->transaction)) { - razor_atomic_set_error(atomic, buffer->str, - GetLastError()); + atomic->error = razor_error_new_mswin(buffer->str, + GetLastError()); razor_wstr_destroy(buffer); return -1; } @@ -328,7 +287,7 @@ * same path, this is likely to cause more problems than it solves. */ - razor_atomic_set_error(atomic, buf, err); + atomic->error = razor_error_new_mswin(buf, err); free(buf); return -1; } @@ -356,12 +315,12 @@ if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags, atomic->transaction)) - razor_atomic_set_error(atomic, newbuf, GetLastError()); + atomic->error = razor_error_new_mswin(newbuf, GetLastError()); free(newbuf); free(oldbuf); - return !!atomic->error_str; + return razor_atomic_in_error_state(atomic); } RAZOR_EXPORT int @@ -381,7 +340,7 @@ err = GetLastError(); if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) { abort: - razor_atomic_set_error(atomic, buf, err); + atomic->error = razor_error_new_mswin(buf, err); free(buf); return -1; } @@ -421,8 +380,9 @@ * and we don't always know that at the time when the * link is created, so it's a convienent lie for now. */ - razor_atomic_set_error_str(atomic, NULL, "Symbolic links not supported " - "on this platform"); + atomic->error = razor_error_new_str(NULL, + "Symbolic links not supported " + "on this platform"); return -1; } @@ -441,7 +401,7 @@ files = realloc(atomic->files, (atomic->n_files+1) * sizeof(struct razor_atomic_file)); if (!files) { - razor_atomic_set_error_str(atomic, NULL, "Not enough memory"); + atomic->error = razor_error_new_str(NULL, "Not enough memory"); return -1; } atomic->n_files++; @@ -468,7 +428,8 @@ NULL); if (files[i].h == INVALID_HANDLE_VALUE) { - razor_atomic_set_error(atomic, files[i].path, GetLastError()); + atomic->error = razor_error_new_mswin(files[i].path, + GetLastError()); free(files[i].path); atomic->n_files--; return -1; @@ -492,9 +453,8 @@ while(size) { if (!WriteFile(atomic->files[handle].h, data, size, &written, NULL)) { - razor_atomic_set_error(atomic, - atomic->files[handle].path, - GetLastError()); + atomic->error = razor_error_new_mswin(atomic->files[handle].path, + GetLastError()); (void)CloseHandle(atomic->files[handle].h); free(atomic->files[handle].path); @@ -523,8 +483,8 @@ assert(atomic->files[handle].h != INVALID_HANDLE_VALUE); if (!CloseHandle(atomic->files[handle].h)) { - razor_atomic_set_error(atomic, atomic->files[handle].path, - GetLastError()); + atomic->error = razor_error_new_mswin(atomic->files[handle].path, + GetLastError()); free(atomic->files[handle].path); atomic->files[handle].path = NULL; atomic->files[handle].h = INVALID_HANDLE_VALUE; @@ -537,14 +497,14 @@ atomic->files[handle].h = h; if (atomic->files[handle].h == INVALID_HANDLE_VALUE) { - razor_atomic_set_error(atomic, atomic->files[handle].path, - GetLastError()); + atomic->error = razor_error_new_mswin(atomic->files[handle].path, + GetLastError()); free(atomic->files[handle].path); atomic->files[handle].path = NULL; return -1; } - return !!atomic->error_str; + return razor_atomic_in_error_state(atomic); } RAZOR_EXPORT int @@ -557,8 +517,8 @@ assert(atomic->files[handle].h != INVALID_HANDLE_VALUE); if (!CloseHandle(atomic->files[handle].h)) - razor_atomic_set_error(atomic, atomic->files[handle].path, - GetLastError()); + atomic->error = razor_error_new_mswin(atomic->files[handle].path, + GetLastError()); free(atomic->files[handle].path); atomic->files[handle].path = NULL; @@ -568,7 +528,7 @@ atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE) atomic->n_files--; - return !!atomic->error_str; + return razor_atomic_in_error_state(atomic); } #endif /* HAVE_WINDOWS_KTM */