librazor/atomic.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Aug 23 11:13:48 2014 +0100 (2014-08-23)
changeset 440 48204dea0b9f
parent 423 6112bcc5d1cf
child 447 0a5e583393e1
permissions -rw-r--r--
Remove INTLLIBS from librazor_la_LIBADD.

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