librazor/atomic.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Mar 23 20:23:36 2012 +0000 (2012-03-23)
changeset 431 4bfb52b2e07b
parent 416 d0aa9e0a6d04
child 439 f28bb31024b4
permissions -rw-r--r--
Start 0.5.5
     1 /*
     2  * Copyright (C) 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include "config.h"
    20 
    21 #include <stdlib.h>
    22 #include <stdio.h>
    23 #include <limits.h>
    24 #include <errno.h>
    25 #include <unistd.h>
    26 #include <sys/types.h>
    27 #include <sys/stat.h>
    28 #include <fcntl.h>
    29 #include <string.h>
    30 #include <assert.h>
    31 
    32 #include "razor.h"
    33 #include "razor-internal.h"
    34 
    35 /*
    36  * Atomic transactions
    37  */
    38 
    39 static int allow_all_root_names = 0;
    40 
    41 /*
    42  * Primarily intended for testing named roots under UNIX platforms.
    43  */
    44 RAZOR_EXPORT void
    45 razor_disable_root_name_checks(int disable)
    46 {
    47 	allow_all_root_names = disable;
    48 }
    49 
    50 int
    51 razor_allow_all_root_names(void)
    52 {
    53 	return allow_all_root_names;
    54 }
    55 
    56 RAZOR_EXPORT const char *
    57 razor_atomic_get_error_msg(struct razor_atomic *atomic)
    58 {
    59 	if (atomic->error)
    60 		return razor_error_get_msg(atomic->error);
    61 	else
    62 		return NULL;
    63 }
    64 
    65 RAZOR_EXPORT void
    66 razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg)
    67 {
    68 	if (!atomic->error)
    69 		atomic->error = razor_error_new_str(NULL, error_msg);
    70 }
    71 
    72 RAZOR_EXPORT int
    73 razor_atomic_in_error_state(struct razor_atomic *atomic)
    74 {
    75 	return atomic->error && !atomic->in_undo;
    76 }
    77 
    78 #if !HAVE_WINDOWS_KTM
    79 
    80 /*
    81  * Common code with atomic-none and atomic-emulate
    82  */
    83 
    84 #define RAZOR_ASCII_ISALPHA(c)	\
    85 			((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
    86 
    87 int
    88 razor_valid_root_name(const char *name)
    89 {
    90 	if (razor_allow_all_root_names()) {
    91 #ifdef MSWIN_API
    92 		return !strpbrk(name, "/\\");
    93 #else
    94 		return !strchr(name, '/');
    95 #endif
    96 	}
    97 
    98 #ifdef MSWIN_API
    99 	return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
   100 	       name[2] == '\0';
   101 #else
   102 	return name[0] == '\0';
   103 #endif
   104 }
   105 
   106 RAZOR_EXPORT int
   107 razor_atomic_write(struct razor_atomic *atomic, int fd, const void *data,
   108 		   size_t size)
   109 {
   110 	int written;
   111 
   112 	if (razor_atomic_in_error_state(atomic))
   113 		return -1;
   114 
   115 	while(size) {
   116 		written = write(fd, data, size);
   117 		if (written < 0) {
   118 			atomic->error = razor_error_new_str(NULL,
   119 							    strerror(errno));
   120 
   121 			(void)close(fd);
   122 
   123 			return -1;
   124 		}
   125 
   126 		data += written;
   127 		size -= written;
   128 	}
   129 
   130 	return 0;
   131 }
   132 
   133 RAZOR_EXPORT int
   134 razor_atomic_sync(struct razor_atomic *atomic, int handle)
   135 {
   136 	if (razor_atomic_in_error_state(atomic))
   137 		return -1;
   138 
   139 	if (fsync(handle) < 0) {
   140 		atomic->error = razor_error_new_str(NULL, strerror(errno));
   141 		return -1;
   142 	}
   143 
   144 	return 0;
   145 }
   146 
   147 RAZOR_EXPORT int
   148 razor_atomic_close(struct razor_atomic *atomic, int fd)
   149 {
   150 	if (razor_atomic_in_error_state(atomic))
   151 		return -1;
   152 
   153 	if (close(fd) < 0) {
   154 		atomic->error = razor_error_new_str(NULL, strerror(errno));
   155 		return -1;
   156 	}
   157 
   158 	return 0;
   159 }
   160 
   161 #endif	/* !HAVE_WINDOWS_KTM */