csync: Pass the errno to csync_errno_to_status().

This is needed to be sure we are thread-safe. See also the manpage of
strerror_r(3).
This commit is contained in:
Andreas Schneider 2013-04-06 18:45:06 +02:00
parent b701bf3d9b
commit 7934cde2db
5 changed files with 58 additions and 42 deletions

View file

@ -117,8 +117,8 @@ enum csync_status_codes_e {
CSYNC_STATUS_READDIR_ERROR, CSYNC_STATUS_READDIR_ERROR,
CSYNC_STATUS_OPEN_ERROR, CSYNC_STATUS_OPEN_ERROR,
}; };
typedef enum csync_status_codes_e CSYNC_STATUS_CODE;
typedef enum csync_status_codes_e CSYNC_STATUS;
enum csync_instructions_e { enum csync_instructions_e {
CSYNC_INSTRUCTION_NONE = 0x00000000, CSYNC_INSTRUCTION_NONE = 0x00000000,

View file

@ -155,7 +155,7 @@ int csync_fnmatch(__const char *__pattern, __const char *__name, int __flags) {
#endif /* HAVE_FNMATCH */ #endif /* HAVE_FNMATCH */
CSYNC_STATUS_CODE csync_errno_to_csync_status(CSYNC_STATUS_CODE default_err) CSYNC_STATUS csync_errno_to_status(int error, CSYNC_STATUS default_status)
{ {
/* /*
@ -196,68 +196,68 @@ CSYNC_STATUS_CODE csync_errno_to_csync_status(CSYNC_STATUS_CODE default_err)
CSYNC_ERR_UNSPEC CSYNC_ERR_UNSPEC
*/ */
CSYNC_STATUS_CODE csync_err = CSYNC_STATUS_OK; CSYNC_STATUS status = CSYNC_STATUS_OK;
switch( errno ) { switch (error) {
case 0: case 0:
csync_err = CSYNC_STATUS_OK; status = CSYNC_STATUS_OK;
break; break;
/* The custom errnos first. */ /* The custom errnos first. */
case ERRNO_GENERAL_ERROR: case ERRNO_GENERAL_ERROR:
csync_err = CSYNC_STATUS_UNSUCCESSFUL; status = CSYNC_STATUS_UNSUCCESSFUL;
break; break;
case ERRNO_LOOKUP_ERROR: /* In Neon: Server or proxy hostname lookup failed */ case ERRNO_LOOKUP_ERROR: /* In Neon: Server or proxy hostname lookup failed */
csync_err = CSYNC_STATUS_LOOKUP_ERROR; status = CSYNC_STATUS_LOOKUP_ERROR;
break; break;
case ERRNO_USER_UNKNOWN_ON_SERVER: /* Neon: User authentication on server failed. */ case ERRNO_USER_UNKNOWN_ON_SERVER: /* Neon: User authentication on server failed. */
csync_err = CSYNC_STATUS_SERVER_AUTH_ERROR; status = CSYNC_STATUS_SERVER_AUTH_ERROR;
break; break;
case ERRNO_PROXY_AUTH: case ERRNO_PROXY_AUTH:
csync_err = CSYNC_STATUS_PROXY_AUTH_ERROR; /* Neon: User authentication on proxy failed */ status = CSYNC_STATUS_PROXY_AUTH_ERROR; /* Neon: User authentication on proxy failed */
break; break;
case ERRNO_CONNECT: case ERRNO_CONNECT:
csync_err = CSYNC_STATUS_CONNECT_ERROR; /* Network: Connection error */ status = CSYNC_STATUS_CONNECT_ERROR; /* Network: Connection error */
break; break;
case ERRNO_TIMEOUT: case ERRNO_TIMEOUT:
csync_err = CSYNC_STATUS_TIMEOUT; /* Network: Timeout error */ status = CSYNC_STATUS_TIMEOUT; /* Network: Timeout error */
break; break;
case ERRNO_QUOTA_EXCEEDED: case ERRNO_QUOTA_EXCEEDED:
csync_err = CSYNC_STATUS_QUOTA_EXCEEDED; /* Quota exceeded */ status = CSYNC_STATUS_QUOTA_EXCEEDED; /* Quota exceeded */
break; break;
case ERRNO_SERVICE_UNAVAILABLE: case ERRNO_SERVICE_UNAVAILABLE:
csync_err = CSYNC_STATUS_SERVICE_UNAVAILABLE; /* Service temporarily down */ status = CSYNC_STATUS_SERVICE_UNAVAILABLE; /* Service temporarily down */
break; break;
case EFBIG: case EFBIG:
csync_err = CSYNC_STATUS_FILE_SIZE_ERROR; /* File larger than 2MB */ status = CSYNC_STATUS_FILE_SIZE_ERROR; /* File larger than 2MB */
break; break;
case ERRNO_PRECONDITION: case ERRNO_PRECONDITION:
case ERRNO_RETRY: case ERRNO_RETRY:
case ERRNO_REDIRECT: case ERRNO_REDIRECT:
case ERRNO_WRONG_CONTENT: case ERRNO_WRONG_CONTENT:
csync_err = CSYNC_STATUS_HTTP_ERROR; status = CSYNC_STATUS_HTTP_ERROR;
break; break;
case ERRNO_TIMEDELTA: case ERRNO_TIMEDELTA:
csync_err = CSYNC_STATUS_TIMESKEW; status = CSYNC_STATUS_TIMESKEW;
break; break;
case EPERM: /* Operation not permitted */ case EPERM: /* Operation not permitted */
case EACCES: /* Permission denied */ case EACCES: /* Permission denied */
csync_err = CSYNC_STATUS_PERMISSION_DENIED; status = CSYNC_STATUS_PERMISSION_DENIED;
break; break;
case ENOENT: /* No such file or directory */ case ENOENT: /* No such file or directory */
csync_err = CSYNC_STATUS_NOT_FOUND; status = CSYNC_STATUS_NOT_FOUND;
break; break;
case EAGAIN: /* Try again */ case EAGAIN: /* Try again */
csync_err = CSYNC_STATUS_TIMEOUT; status = CSYNC_STATUS_TIMEOUT;
break; break;
case EEXIST: /* File exists */ case EEXIST: /* File exists */
csync_err = CSYNC_STATUS_FILE_EXISTS; status = CSYNC_STATUS_FILE_EXISTS;
break; break;
case EINVAL: case EINVAL:
csync_err = CSYNC_STATUS_PARAM_ERROR; status = CSYNC_STATUS_PARAM_ERROR;
break; break;
case ENOSPC: case ENOSPC:
csync_err = CSYNC_STATUS_OUT_OF_SPACE; status = CSYNC_STATUS_OUT_OF_SPACE;
break; break;
/* All the remaining basic errnos: */ /* All the remaining basic errnos: */
@ -292,8 +292,8 @@ CSYNC_STATUS_CODE csync_errno_to_csync_status(CSYNC_STATUS_CODE default_err)
case ERRNO_ERROR_STRING: case ERRNO_ERROR_STRING:
default: default:
csync_err = default_err; status = default_status;
} }
return csync_err; return status;
} }

View file

@ -28,6 +28,6 @@ char *csync_get_local_username(void);
int csync_fnmatch(__const char *__pattern, __const char *__name, int __flags); int csync_fnmatch(__const char *__pattern, __const char *__name, int __flags);
CSYNC_STATUS_CODE csync_errno_to_csync_status(CSYNC_STATUS_CODE default_err); CSYNC_STATUS csync_errno_to_status(int error, CSYNC_STATUS default_status);
#endif /* _CSYNC_MISC_H */ #endif /* _CSYNC_MISC_H */

View file

@ -130,7 +130,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
#endif #endif
sfp = csync_vio_open(ctx, suri, flags, 0); sfp = csync_vio_open(ctx, suri, flags, 0);
if (sfp == NULL) { if (sfp == NULL) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
if (errno == ENOMEM) { if (errno == ENOMEM) {
rc = -1; rc = -1;
} else { } else {
@ -174,7 +175,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
ctx->replica = drep; ctx->replica = drep;
while ((dfp = csync_vio_open(ctx, turi, O_CREAT|O_EXCL|O_WRONLY|O_NOCTTY, while ((dfp = csync_vio_open(ctx, turi, O_CREAT|O_EXCL|O_WRONLY|O_NOCTTY,
C_FILE_MODE)) == NULL) { C_FILE_MODE)) == NULL) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case EEXIST: case EEXIST:
if (count++ > 10) { if (count++ > 10) {
@ -202,7 +204,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
} }
if (csync_vio_mkdirs(ctx, tdir, C_DIR_MODE) < 0) { if (csync_vio_mkdirs(ctx, tdir, C_DIR_MODE) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
strerror_r(errno, errbuf, sizeof(errbuf)); strerror_r(errno, errbuf, sizeof(errbuf));
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN,
"dir: %s, command: mkdirs, error: %s", "dir: %s, command: mkdirs, error: %s",
@ -236,7 +239,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
if (bread < 0) { if (bread < 0) {
/* read error */ /* read error */
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
strerror_r(errno, errbuf, sizeof(errbuf)); strerror_r(errno, errbuf, sizeof(errbuf));
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
"file: %s, command: read, error: %s", "file: %s, command: read, error: %s",
@ -252,7 +256,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
bwritten = csync_vio_write(ctx, dfp, buf, bread); bwritten = csync_vio_write(ctx, dfp, buf, bread);
if (bwritten < 0 || bread != bwritten) { if (bwritten < 0 || bread != bwritten) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
strerror_r(errno, errbuf, sizeof(errbuf)); strerror_r(errno, errbuf, sizeof(errbuf));
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
"file: %s, command: write, error: bread = %zu, bwritten = %zu - %s", "file: %s, command: write, error: bread = %zu, bwritten = %zu - %s",
@ -267,7 +272,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
ctx->replica = srep; ctx->replica = srep;
if (csync_vio_close(ctx, sfp) < 0) { if (csync_vio_close(ctx, sfp) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
strerror_r(errno, errbuf, sizeof(errbuf)); strerror_r(errno, errbuf, sizeof(errbuf));
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
"file: %s, command: close, error: %s", "file: %s, command: close, error: %s",
@ -279,7 +285,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
ctx->replica = drep; ctx->replica = drep;
if (csync_vio_close(ctx, dfp) < 0) { if (csync_vio_close(ctx, dfp) < 0) {
dfp = NULL; dfp = NULL;
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
/* stop if no space left or quota exceeded */ /* stop if no space left or quota exceeded */
case ENOSPC: case ENOSPC:
@ -314,7 +321,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
} }
if (csync_vio_stat(ctx, turi, tstat) < 0) { if (csync_vio_stat(ctx, turi, tstat) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
rc = -1; rc = -1;
@ -344,7 +352,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
/* override original file */ /* override original file */
ctx->replica = drep; ctx->replica = drep;
if (csync_vio_rename(ctx, turi, duri) < 0) { if (csync_vio_rename(ctx, turi, duri) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
rc = -1; rc = -1;
@ -365,7 +374,8 @@ static int _csync_push_file(CSYNC *ctx, csync_file_stat_t *st) {
/* set mode only if it is not the default mode */ /* set mode only if it is not the default mode */
if ((st->mode & 07777) != C_FILE_MODE) { if ((st->mode & 07777) != C_FILE_MODE) {
if (csync_vio_chmod(ctx, duri, st->mode) < 0) { if (csync_vio_chmod(ctx, duri, st->mode) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
rc = -1; rc = -1;
@ -521,7 +531,8 @@ static int _csync_backup_file(CSYNC *ctx, csync_file_stat_t *st, char **duri) {
/* rename the older file to conflict */ /* rename the older file to conflict */
ctx->replica = drep; ctx->replica = drep;
if (csync_vio_rename(ctx, suri, *duri) < 0) { if (csync_vio_rename(ctx, suri, *duri) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
rc = -1; rc = -1;
@ -632,7 +643,8 @@ static int _csync_remove_file(CSYNC *ctx, csync_file_stat_t *st) {
} }
if (csync_vio_unlink(ctx, uri) < 0) { if (csync_vio_unlink(ctx, uri) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
rc = -1; rc = -1;
@ -698,7 +710,8 @@ static int _csync_new_dir(CSYNC *ctx, csync_file_stat_t *st) {
ctx->replica = dest; ctx->replica = dest;
if (csync_vio_mkdirs(ctx, uri, C_DIR_MODE) < 0) { if (csync_vio_mkdirs(ctx, uri, C_DIR_MODE) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
rc = -1; rc = -1;
@ -718,7 +731,8 @@ static int _csync_new_dir(CSYNC *ctx, csync_file_stat_t *st) {
/* chmod is if it is not the default mode */ /* chmod is if it is not the default mode */
if ((st->mode & 07777) != C_DIR_MODE) { if ((st->mode & 07777) != C_DIR_MODE) {
if (csync_vio_chmod(ctx, uri, st->mode) < 0) { if (csync_vio_chmod(ctx, uri, st->mode) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
rc = -1; rc = -1;
@ -798,7 +812,8 @@ static int _csync_sync_dir(CSYNC *ctx, csync_file_stat_t *st) {
/* chmod is if it is not the default mode */ /* chmod is if it is not the default mode */
if ((st->mode & 07777) != C_DIR_MODE) { if ((st->mode & 07777) != C_DIR_MODE) {
if (csync_vio_chmod(ctx, uri, st->mode) < 0) { if (csync_vio_chmod(ctx, uri, st->mode) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
rc = -1; rc = -1;
@ -868,7 +883,8 @@ static int _csync_remove_dir(CSYNC *ctx, csync_file_stat_t *st) {
} }
if (csync_vio_rmdir(ctx, uri) < 0) { if (csync_vio_rmdir(ctx, uri) < 0) {
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_PROPAGATE_ERROR); ctx->status_code = csync_errno_to_status(errno,
CSYNC_STATUS_PROPAGATE_ERROR);
switch (errno) { switch (errno) {
case ENOMEM: case ENOMEM:
strerror_r(errno, errbuf, sizeof(errbuf)); strerror_r(errno, errbuf, sizeof(errbuf));

View file

@ -226,7 +226,7 @@ int csync_ftw(CSYNC *ctx, const char *uri, csync_walker_fn fn,
if ((dh = csync_vio_opendir(ctx, uri)) == NULL) { if ((dh = csync_vio_opendir(ctx, uri)) == NULL) {
/* permission denied */ /* permission denied */
ctx->status_code = csync_errno_to_csync_status(CSYNC_STATUS_OPENDIR_ERROR); ctx->status_code = csync_errno_to_status(errno, CSYNC_STATUS_OPENDIR_ERROR);
if (errno == EACCES) { if (errno == EACCES) {
return 0; return 0;
} else { } else {