Alien-Libjio
view release on metacpan or search on metacpan
libjio/libjio/check.c view on Meta::CPAN
/*
* Recovery functions
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <sys/mman.h>
#include "libjio.h"
#include "common.h"
#include "journal.h"
#include "trans.h"
/** Remove the journal directory (if it's clean).
*
* @param name path to the file
* @param jdir path to the journal directory
* @returns 0 on success, < 0 on error
*/
static int jfsck_cleanup(const char *name, const char *jdir)
{
char tfile[PATH_MAX*3];
DIR *dir;
struct dirent *dent;
dir = opendir(jdir);
if (dir == NULL && errno == ENOENT)
/* it doesn't exist, so it's clean */
return 0;
else if (dir == NULL)
return -1;
for (errno = 0, dent = readdir(dir); dent != NULL;
errno = 0, dent = readdir(dir)) {
/* We only care about files we know, and ignore everything
* else. Note that transactions should have been removed by
* jfsck(), we will not do it to prevent accidental misuse */
if (strcmp(dent->d_name, "lock"))
continue;
/* build the full path to the transaction file */
memset(tfile, 0, PATH_MAX * 3);
strcat(tfile, jdir);
strcat(tfile, "/");
strcat(tfile, dent->d_name);
if (strlen(tfile) > PATH_MAX) {
closedir(dir);
return -1;
}
if (unlink(tfile) != 0) {
closedir(dir);
return -1;
}
}
if (errno) {
closedir(dir);
return -1;
}
if (closedir(dir) != 0)
return -1;
if (rmdir(jdir) != 0)
return -1;
return 0;
}
/* Check the journal and fix the incomplete transactions */
enum jfsck_return jfsck(const char *name, const char *jdir,
struct jfsck_result *res, unsigned int flags)
{
int tfd, rv, i, ret;
unsigned int maxtid;
char jlockfile[PATH_MAX], tname[PATH_MAX], brokenname[PATH_MAX];
struct stat sinfo;
struct jfs fs;
struct jtrans *curts;
struct operation *tmpop;
DIR *dir;
struct dirent *dent;
unsigned char *map;
off_t filelen, lr;
tfd = -1;
filelen = 0;
dir = NULL;
fs.fd = -1;
fs.jfd = -1;
fs.jdir = NULL;
fs.jdirfd = -1;
fs.jmap = MAP_FAILED;
map = NULL;
ret = 0;
res->total = 0;
res->invalid = 0;
res->in_progress = 0;
res->broken = 0;
res->corrupt = 0;
res->reapplied = 0;
fs.fd = open(name, O_RDWR | O_SYNC);
if (fs.fd < 0) {
ret = J_EIO;
if (errno == ENOENT)
ret = J_ENOENT;
goto exit;
}
fs.name = (char *) name;
/* Locking the whole file protect us from concurrent runs, but it's
* not to be trusted nor assumed (lingering transactions break it): it
* just helps prevent some accidents. */
lr = plockf(fs.fd, F_LOCKW, 0, 0);
if (lr == -1) {
/* In the future, we may want to differentiate this case from
* a normal I/O error. */
ret = J_EIO;
goto exit;
}
if (jdir == NULL) {
fs.jdir = (char *) malloc(PATH_MAX);
if (fs.jdir == NULL) {
ret = J_ENOMEM;
goto exit;
}
if (!get_jdir(name, fs.jdir)) {
ret = J_ENOMEM;
goto exit;
}
} else {
fs.jdir = strdup(jdir);
if (fs.jdir == NULL) {
ret = J_ENOMEM;
goto exit;
}
}
rv = lstat(fs.jdir, &sinfo);
if (rv < 0) {
ret = J_EIO;
if (errno == ENOENT)
ret = J_ENOJOURNAL;
goto exit;
}
if (!S_ISDIR(sinfo.st_mode)) {
ret = J_ENOJOURNAL;
goto exit;
}
fs.jdirfd = open(fs.jdir, O_RDONLY);
if (fs.jdirfd < 0) {
ret = J_EIO;
if (errno == ENOENT)
ret = J_ENOJOURNAL;
goto exit;
}
/* open the lock file, which is only used to complete the jfs
* structure */
snprintf(jlockfile, PATH_MAX, "%s/%s", fs.jdir, "lock");
rv = open(jlockfile, O_RDWR | O_CREAT, 0600);
if (rv < 0) {
ret = J_EIO;
if (errno == ENOENT)
ret = J_ENOJOURNAL;
goto exit;
}
fs.jfd = rv;
fs.jmap = (unsigned int *) mmap(NULL, sizeof(unsigned int),
( run in 0.559 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )