librazor/atomic.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Aug 23 17:24:34 2014 +0100 (2014-08-23)
changeset 443 b2d6a8273459
parent 423 6112bcc5d1cf
child 447 0a5e583393e1
permissions -rw-r--r--
Update libtool versioning in preparation for release
     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 struct razor_error *
    57 razor_atomic_get_error(struct razor_atomic *atomic)
    58 {
    59 	return atomic->error;
    60 }
    61 
    62 RAZOR_EXPORT const char *
    63 razor_atomic_get_error_msg(struct razor_atomic *atomic)
    64 {
    65 	if (atomic->error)
    66 		return razor_error_get_msg(atomic->error);
    67 	else
    68 		return NULL;
    69 }
    70 
    71 RAZOR_EXPORT void
    72 razor_atomic_abort(struct razor_atomic *atomic, const char *error_msg)
    73 {
    74 	if (!atomic->error)
    75 		atomic->error = razor_error_new_str(NULL, error_msg);
    76 }
    77 
    78 RAZOR_EXPORT int
    79 razor_atomic_in_error_state(struct razor_atomic *atomic)
    80 {
    81 	return atomic->error && !atomic->in_undo;
    82 }
    83 
    84 #if !HAVE_WINDOWS_KTM
    85 
    86 /*
    87  * Common code with atomic-none and atomic-emulate
    88  */
    89 
    90 #define RAZOR_ASCII_ISALPHA(c)	\
    91 			((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
    92 
    93 int
    94 razor_valid_root_name(const char *name)
    95 {
    96 	if (razor_allow_all_root_names()) {
    97 #ifdef MSWIN_API
    98 		return !strpbrk(name, "/\\");
    99 #else
   100 		return !strchr(name, '/');
   101 #endif
   102 	}
   103 
   104 #ifdef MSWIN_API
   105 	return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
   106 	       name[2] == '\0';
   107 #else
   108 	return name[0] == '\0';
   109 #endif
   110 }
   111 
   112 RAZOR_EXPORT int
   113 razor_atomic_write(struct razor_atomic *atomic, int fd, const void *data,
   114 		   size_t size)
   115 {
   116 	int written;
   117 
   118 	if (razor_atomic_in_error_state(atomic))
   119 		return -1;
   120 
   121 	while(size) {
   122 		written = write(fd, data, size);
   123 		if (written < 0) {
   124 			atomic->error = razor_error_new_str(NULL,
   125 							    strerror(errno));
   126 
   127 			(void)close(fd);
   128 
   129 			return -1;
   130 		}
   131 
   132 		data += written;
   133 		size -= written;
   134 	}
   135 
   136 	return 0;
   137 }
   138 
   139 RAZOR_EXPORT int
   140 razor_atomic_sync(struct razor_atomic *atomic, int handle)
   141 {
   142 	if (razor_atomic_in_error_state(atomic))
   143 		return -1;
   144 
   145 	if (fsync(handle) < 0) {
   146 		atomic->error = razor_error_new_str(NULL, strerror(errno));
   147 		return -1;
   148 	}
   149 
   150 	return 0;
   151 }
   152 
   153 RAZOR_EXPORT int
   154 razor_atomic_close(struct razor_atomic *atomic, int fd)
   155 {
   156 	if (razor_atomic_in_error_state(atomic))
   157 		return -1;
   158 
   159 	if (close(fd) < 0) {
   160 		atomic->error = razor_error_new_str(NULL, strerror(errno));
   161 		return -1;
   162 	}
   163 
   164 	return 0;
   165 }
   166 
   167 #endif	/* !HAVE_WINDOWS_KTM */