1.1 --- a/librazor/atomic-ktm.c Thu Feb 09 20:42:08 2012 +0000
1.2 +++ b/librazor/atomic-ktm.c Sat Feb 11 23:50:26 2012 +0000
1.3 @@ -131,45 +131,6 @@
1.4 return atomic;
1.5 }
1.6
1.7 -void
1.8 -razor_atomic_set_error_str(struct razor_atomic *atomic, const wchar_t *path,
1.9 - const char *str)
1.10 -{
1.11 - assert(!atomic->error_str);
1.12 -
1.13 - free(atomic->error_path);
1.14 -
1.15 - if (path)
1.16 - atomic->error_path = razor_utf16_to_utf8(path, -1);
1.17 - else
1.18 - atomic->error_path = NULL;
1.19 -
1.20 - atomic->error_str = strdup(str);
1.21 -}
1.22 -
1.23 -static void
1.24 -razor_atomic_set_error(struct razor_atomic *atomic, const wchar_t *path,
1.25 - DWORD error)
1.26 -{
1.27 - wchar_t *buf;
1.28 -
1.29 - assert(!atomic->error_str);
1.30 -
1.31 - free(atomic->error_path);
1.32 -
1.33 - if (path)
1.34 - atomic->error_path = razor_utf16_to_utf8(path, -1);
1.35 - else
1.36 - atomic->error_path = NULL;
1.37 -
1.38 - FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|
1.39 - FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
1.40 - NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
1.41 - (LPWSTR)&buf, 0, NULL);
1.42 - atomic->error_str = razor_utf16_to_utf8(buf, -1);
1.43 - LocalFree(buf);
1.44 -}
1.45 -
1.46 RAZOR_EXPORT int
1.47 razor_atomic_commit(struct razor_atomic *atomic)
1.48 {
1.49 @@ -181,7 +142,7 @@
1.50 retval = !CommitTransaction(atomic->transaction);
1.51
1.52 if (retval) {
1.53 - razor_atomic_set_error(atomic, NULL, GetLastError());
1.54 + atomic->error = razor_error_new_mswin(NULL, GetLastError());
1.55 RollbackTransaction(atomic->transaction);
1.56 }
1.57
1.58 @@ -207,9 +168,8 @@
1.59 RollbackTransaction(atomic->transaction);
1.60 CloseHandle(atomic->transaction);
1.61 }
1.62 - free(atomic->error_path);
1.63 - free(atomic->error_str);
1.64 - free(atomic->error_msg);
1.65 + if (atomic->error)
1.66 + razor_error_free(atomic->error);
1.67 free(atomic);
1.68 }
1.69
1.70 @@ -250,16 +210,15 @@
1.71 if (err == ERROR_FILE_NOT_FOUND) {
1.72 creating = 1;
1.73 } else {
1.74 - razor_atomic_set_error(atomic,
1.75 - buffer->str,
1.76 - err);
1.77 + atomic->error = razor_error_new_mswin(buffer->str,
1.78 + err);
1.79 razor_wstr_destroy(buffer);
1.80 return -1;
1.81 }
1.82 } else if (!(fa.dwFileAttributes&
1.83 FILE_ATTRIBUTE_DIRECTORY)) {
1.84 - razor_atomic_set_error_str(atomic, buffer->str,
1.85 - "Not a directory");
1.86 + atomic->error = razor_error_new_str(buffer->str,
1.87 + "Not a directory");
1.88 razor_wstr_destroy(buffer);
1.89 return -1;
1.90 }
1.91 @@ -267,8 +226,8 @@
1.92 if (creating) {
1.93 if (!CreateDirectoryTransactedW(NULL, buffer->str, NULL,
1.94 atomic->transaction)) {
1.95 - razor_atomic_set_error(atomic, buffer->str,
1.96 - GetLastError());
1.97 + atomic->error = razor_error_new_mswin(buffer->str,
1.98 + GetLastError());
1.99 razor_wstr_destroy(buffer);
1.100 return -1;
1.101 }
1.102 @@ -328,7 +287,7 @@
1.103 * same path, this is likely to cause more problems than it solves.
1.104 */
1.105
1.106 - razor_atomic_set_error(atomic, buf, err);
1.107 + atomic->error = razor_error_new_mswin(buf, err);
1.108 free(buf);
1.109 return -1;
1.110 }
1.111 @@ -356,12 +315,12 @@
1.112
1.113 if (!MoveFileTransactedW(oldbuf, newbuf, NULL, NULL, flags,
1.114 atomic->transaction))
1.115 - razor_atomic_set_error(atomic, newbuf, GetLastError());
1.116 + atomic->error = razor_error_new_mswin(newbuf, GetLastError());
1.117
1.118 free(newbuf);
1.119 free(oldbuf);
1.120
1.121 - return !!atomic->error_str;
1.122 + return razor_atomic_in_error_state(atomic);
1.123 }
1.124
1.125 RAZOR_EXPORT int
1.126 @@ -381,7 +340,7 @@
1.127 err = GetLastError();
1.128 if (err != ERROR_FILE_EXISTS && err != ERROR_ALREADY_EXISTS) {
1.129 abort:
1.130 - razor_atomic_set_error(atomic, buf, err);
1.131 + atomic->error = razor_error_new_mswin(buf, err);
1.132 free(buf);
1.133 return -1;
1.134 }
1.135 @@ -421,8 +380,9 @@
1.136 * and we don't always know that at the time when the
1.137 * link is created, so it's a convienent lie for now.
1.138 */
1.139 - razor_atomic_set_error_str(atomic, NULL, "Symbolic links not supported "
1.140 - "on this platform");
1.141 + atomic->error = razor_error_new_str(NULL,
1.142 + "Symbolic links not supported "
1.143 + "on this platform");
1.144
1.145 return -1;
1.146 }
1.147 @@ -441,7 +401,7 @@
1.148 files = realloc(atomic->files,
1.149 (atomic->n_files+1) * sizeof(struct razor_atomic_file));
1.150 if (!files) {
1.151 - razor_atomic_set_error_str(atomic, NULL, "Not enough memory");
1.152 + atomic->error = razor_error_new_str(NULL, "Not enough memory");
1.153 return -1;
1.154 }
1.155 atomic->n_files++;
1.156 @@ -468,7 +428,8 @@
1.157 NULL);
1.158
1.159 if (files[i].h == INVALID_HANDLE_VALUE) {
1.160 - razor_atomic_set_error(atomic, files[i].path, GetLastError());
1.161 + atomic->error = razor_error_new_mswin(files[i].path,
1.162 + GetLastError());
1.163 free(files[i].path);
1.164 atomic->n_files--;
1.165 return -1;
1.166 @@ -492,9 +453,8 @@
1.167 while(size) {
1.168 if (!WriteFile(atomic->files[handle].h, data, size, &written,
1.169 NULL)) {
1.170 - razor_atomic_set_error(atomic,
1.171 - atomic->files[handle].path,
1.172 - GetLastError());
1.173 + atomic->error = razor_error_new_mswin(atomic->files[handle].path,
1.174 + GetLastError());
1.175
1.176 (void)CloseHandle(atomic->files[handle].h);
1.177 free(atomic->files[handle].path);
1.178 @@ -523,8 +483,8 @@
1.179 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
1.180
1.181 if (!CloseHandle(atomic->files[handle].h)) {
1.182 - razor_atomic_set_error(atomic, atomic->files[handle].path,
1.183 - GetLastError());
1.184 + atomic->error = razor_error_new_mswin(atomic->files[handle].path,
1.185 + GetLastError());
1.186 free(atomic->files[handle].path);
1.187 atomic->files[handle].path = NULL;
1.188 atomic->files[handle].h = INVALID_HANDLE_VALUE;
1.189 @@ -537,14 +497,14 @@
1.190 atomic->files[handle].h = h;
1.191
1.192 if (atomic->files[handle].h == INVALID_HANDLE_VALUE) {
1.193 - razor_atomic_set_error(atomic, atomic->files[handle].path,
1.194 - GetLastError());
1.195 + atomic->error = razor_error_new_mswin(atomic->files[handle].path,
1.196 + GetLastError());
1.197 free(atomic->files[handle].path);
1.198 atomic->files[handle].path = NULL;
1.199 return -1;
1.200 }
1.201
1.202 - return !!atomic->error_str;
1.203 + return razor_atomic_in_error_state(atomic);
1.204 }
1.205
1.206 RAZOR_EXPORT int
1.207 @@ -557,8 +517,8 @@
1.208 assert(atomic->files[handle].h != INVALID_HANDLE_VALUE);
1.209
1.210 if (!CloseHandle(atomic->files[handle].h))
1.211 - razor_atomic_set_error(atomic, atomic->files[handle].path,
1.212 - GetLastError());
1.213 + atomic->error = razor_error_new_mswin(atomic->files[handle].path,
1.214 + GetLastError());
1.215
1.216 free(atomic->files[handle].path);
1.217 atomic->files[handle].path = NULL;
1.218 @@ -568,7 +528,7 @@
1.219 atomic->files[atomic->n_files-1].h == INVALID_HANDLE_VALUE)
1.220 atomic->n_files--;
1.221
1.222 - return !!atomic->error_str;
1.223 + return razor_atomic_in_error_state(atomic);
1.224 }
1.225
1.226 #endif /* HAVE_WINDOWS_KTM */