Data-SortedSet-Shared

 view release on metacpan or  search on metacpan

sortedset.h  view on Meta::CPAN

    *index_slots   = ss_next_pow2((uint32_t)want);
    *node_capacity = (uint32_t)((uint64_t)max_entries / (SS_MIN - 1) + 64); /* worst-case fill + slack */
    return 1;
}

/* Securely obtain a fd: create exclusively (O_CREAT|O_EXCL|O_NOFOLLOW at mode,
 * default 0600), or attach an existing file (O_RDWR|O_NOFOLLOW, no O_CREAT). */
static int ss_secure_open(const char *path, mode_t mode, char *errbuf) {
    for (int attempt = 0; attempt < 100; attempt++) {
        int fd = open(path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, mode);
        if (fd >= 0) { (void)fchmod(fd, mode); return fd; }   /* exact mode: umask narrowed the O_EXCL create */
        if (errno != EEXIST) { SS_ERR("create %s: %s", path, strerror(errno)); return -1; }
        fd = open(path, O_RDWR|O_NOFOLLOW|O_CLOEXEC);
        if (fd >= 0) return fd;
        if (errno == ENOENT) continue;   /* creator unlinked between our two opens; retry */
        SS_ERR("open %s: %s", path, strerror(errno));  /* ELOOP => symlink rejected */
        return -1;
    }
    SS_ERR("open %s: create/attach kept racing", path);
    return -1;
}

t/01-basic.t  view on Meta::CPAN

    is $r->max_entries, 100, 'reopen: stored max_entries wins';
    is $r->count, 0, 'reopen count == 0';
}

# validation
ok !eval { Data::SortedSet::Shared->new(undef, 0); 1 }, 'max_entries 0 rejected';
{ open my $fh, '>', $path or die $!; print $fh "junk"; close $fh; }
ok !eval { Data::SortedSet::Shared->new($path, 100); 1 }, 'too-small/corrupt file rejected';
unlink $path;

# opt-in file mode: applied exactly at create (fchmod after the O_EXCL create,
# so an explicit mode is honored regardless of umask); ignored when attaching
{
    my $mp = "/tmp/ss-mode-$$.bin";
    unlink $mp;
    { my $w = Data::SortedSet::Shared->new($mp, 10, 0660); }
    is +((stat $mp)[2] & 07777), 0660, 'explicit mode honored at create';
    { my $r = Data::SortedSet::Shared->new($mp, 10, 0644); }
    is +((stat $mp)[2] & 07777), 0660, 'mode ignored when attaching an existing file';
    unlink $mp;
    { my $d = Data::SortedSet::Shared->new($mp, 10); }



( run in 0.762 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )