return true;
}
-static jmp_buf bus_error_restore;
+static sigjmp_buf bus_error_restore;
static volatile sig_atomic_t bus_error;
static void
handlerSIGBUS(int UNUSED num)
{
bus_error = 1;
- longjmp(bus_error_restore, 1);
+ siglongjmp(bus_error_restore, 1);
}
static bool
calculateHashFromFD(int fd, HashPath d_path, struct stat const * const st)
{
hashFunctionContext * const h_ctx = &global_info.hash_context;
- bool res = false;
- loff_t offset = 0;
- void *buf = 0;
- off_t size = st->st_size;
- loff_t cur_size = 0;
+ void const * volatile buf = 0;
+ loff_t volatile buf_size = 0;
+ bool volatile res = false;
if (hashFunctionContextReset(h_ctx)==-1 ||
return false;
bus_error = 0;
- if (setjmp(bus_error_restore)!=0) goto out;
+ if (sigsetjmp(bus_error_restore,1)==0) {
+ loff_t offset = 0;
+ off_t size = st->st_size;
- while (offset < size) {
- cur_size = size-offset;
- if (cur_size>HASH_BLOCKSIZE) cur_size = HASH_BLOCKSIZE;
+ while (offset < size) {
+ buf_size = size-offset;
+ if (buf_size>HASH_BLOCKSIZE) buf_size = HASH_BLOCKSIZE;
- buf = mmap(0, cur_size, PROT_READ, MAP_SHARED, fd, offset);
- if (buf==0) goto out;
-
- offset += cur_size;
- madvise(buf, cur_size, MADV_SEQUENTIAL); // ignore error...
+ if ((buf=mmap(0, buf_size, PROT_READ, MAP_SHARED, fd, offset))==0) {
+ perror("mmap(<hash>)");
+ goto out;
+ }
- if (hashFunctionContextUpdate(h_ctx, buf, cur_size)==-1) goto out;
+ offset += buf_size;
+ madvise(const_cast(void *)(buf), buf_size, MADV_SEQUENTIAL); // ignore error...
- munmap(buf, cur_size);
- buf = 0;
- }
+ if (hashFunctionContextUpdate(h_ctx, buf, buf_size)==-1) goto out;
- if (!convertDigest(d_path)) goto out;
-
- res = true;
+ munmap(const_cast(void *)(buf), buf_size);
+ buf = 0;
+ }
+
+ res = convertDigest(d_path);
+ }
out:
- if (buf!=0) munmap(buf, cur_size);
+ if (buf!=0) munmap(const_cast(void *)(buf), buf_size);
return res;
}
return res;
}
+static enum { mkdirFAIL, mkdirSUCCESS, mkdirSKIP }
+mkdirSingle(char const *path, char *end_ptr, int good_err)
+{
+ *end_ptr = '\0';
+ if (mkdir(path, 0700)!=-1 || errno==EEXIST) {
+ *end_ptr = '/';
+ return mkdirSUCCESS;
+ }
+ else if (errno==good_err) {
+ *end_ptr = '/';
+ return mkdirSKIP;
+ }
+ else {
+ int old_errno = errno;
+ WRITE_MSG(2, "mkdir('");
+ WRITE_STR(2, path);
+ errno = old_errno;
+ perror("')");
+ return mkdirFAIL;
+ }
+}
+
+static char *
+rstrchr(char *str, char c)
+{
+ while (*str!=c) --str;
+ return str;
+}
+
static bool
mkdirRecursive(char const *path)
{
- struct stat st;
-
- if (path[0]!='/') return false; // only absolute paths
- if (lstat(path,&st)!=-1) return true;
+ if (path[0]!='/') return false; // only absolute paths
char buf[strlen(path)+1];
- char * ptr = buf+1;
-
+ char * ptr = buf + sizeof(buf) - 2;
+
strcpy(buf, path);
- while ((ptr = strchr(ptr, '/'))!=0) {
- *ptr = '\0';
- if (mkdir(buf, 0700)==-1 && errno!=EEXIST) {
- int old_errno = errno;
- WRITE_MSG(2, "mkdir('");
- WRITE_STR(2, buf);
- errno = old_errno;
- perror("')");
- return false;
+ while (ptr>buf && (ptr = rstrchr(ptr, '/'))!=0) {
+ switch (mkdirSingle(buf, ptr, ENOENT)) {
+ case mkdirSUCCESS : break;
+ case mkdirSKIP : --ptr; continue;
+ case mkdirFAIL : return false;
+ }
+
+ break; // implied by mkdirSUCCESS
+ }
+
+ assert(ptr!=0);
+ ++ptr;
+
+ while ((ptr=strchr(ptr, '/'))!=0) {
+ switch (mkdirSingle(buf, ptr, 0)) {
+ case mkdirSKIP :
+ case mkdirFAIL : return false;
+ case mkdirSUCCESS : ++ptr; continue;
}
- *ptr = '/';
- ++ptr;
}
return true;
char buf[sizeof(int)*2 + 1];
size_t len;
- *ptr++ = '-';
- *ptr = '\0';
- ptr[sizeof(int)*2] = '\0';
-
- if (!global_args->dry_run &&
- !mkdirRecursive(result))
- return false;
+ *ptr = '-';
+ ptr[sizeof(int)*2+1] = '\0';
for (;; ++idx) {
len = utilvserver_fmt_xuint(buf, idx);
- memset(ptr, '0', sizeof(int)*2 - len);
- memcpy(ptr + sizeof(int)*2 - len, buf, len);
+ memset(ptr+1, '0', sizeof(int)*2 - len);
+ memcpy(ptr+1 + sizeof(int)*2 - len, buf, len);
if (lstat(result, hash_st)==-1) {
if (global_args->dry_run && errno!=ENOENT) {
break; // ok, we finish here
if (!global_args->dry_run) {
+ *ptr = '\0';
+ if (!mkdirRecursive(result))
+ return false;
+ *ptr = '-';
+
int fd = open(result, O_NOFOLLOW|O_EXCL|O_CREAT|O_WRONLY, 0200);
- if (global_args->dry_run && fd==-1) {
+ if (fd==-1) {
int old_errno = errno;
WRITE_MSG(2, "open('");
WRITE_STR(2, buf);