Ancient
view release on metacpan or search on metacpan
bench/file.pl view on Meta::CPAN
# ============================================
# Additional stat benchmarks
# ============================================
print "\n--- ATIME ---\n";
cmpthese(-2, {
'file::atime' => sub {
my $a = file::atime($small_file);
},
'Perl stat atime' => sub {
my $a = (stat($small_file))[8];
},
});
print "\n--- MODE ---\n";
cmpthese(-2, {
'file::mode' => sub {
my $m = file::mode($small_file);
},
'Perl stat mode' => sub {
my $m = (stat($small_file))[2] & 07777;
},
});
print "\n--- IS_LINK ---\n";
cmpthese(-2, {
'file::is_link' => sub {
my $l = file::is_link($small_file);
},
'Perl -l' => sub {
my $l = -l $small_file;
xs/file/file.c view on Meta::CPAN
int open_flags = O_RDONLY | O_BINARY;
#else
int open_flags = O_RDONLY;
#endif
fd = open(path, open_flags);
if (fd < 0) {
return &PL_sv_undef;
}
if (fstat(fd, &st) < 0) {
close(fd);
return &PL_sv_undef;
}
/* Pre-allocate exact size for regular files */
if (S_ISREG(st.st_mode) && st.st_size > 0) {
result = newSV(st.st_size + 1);
SvPOK_on(result);
buf = SvPVX(result);
xs/file/file.c view on Meta::CPAN
int open_flags = O_RDONLY | O_BINARY;
#else
int open_flags = O_RDONLY;
#endif
fd = open(path, open_flags);
if (fd < 0) {
return &PL_sv_undef;
}
if (fstat(fd, &st) < 0) {
close(fd);
return &PL_sv_undef;
}
if (S_ISREG(st.st_mode) && st.st_size > 0) {
result = newSV(st.st_size + 1);
SvPOK_on(result);
buf = SvPVX(result);
while (total < st.st_size) {
xs/file/file.c view on Meta::CPAN
int fd;
struct stat st;
int flags = writable ? O_RDWR : O_RDONLY;
int prot = writable ? (PROT_READ | PROT_WRITE) : PROT_READ;
fd = open(path, flags);
if (fd < 0) {
return -1;
}
if (fstat(fd, &st) < 0) {
close(fd);
return -1;
}
if (st.st_size == 0) {
/* Can't mmap empty file */
close(fd);
return -1;
}
xs/file/file.c view on Meta::CPAN
free_iter_slot(idx);
}
}
/* ============================================
Fast stat operations
============================================ */
static IV file_size_internal(const char *path) {
struct stat st;
if (stat(path, &st) < 0) {
return -1;
}
return st.st_size;
}
static int file_exists_internal(const char *path) {
struct stat st;
return stat(path, &st) == 0;
}
static int file_is_file_internal(const char *path) {
struct stat st;
if (stat(path, &st) < 0) return 0;
return S_ISREG(st.st_mode);
}
static int file_is_dir_internal(const char *path) {
struct stat st;
if (stat(path, &st) < 0) return 0;
return S_ISDIR(st.st_mode);
}
static int file_is_readable_internal(const char *path) {
return access(path, R_OK) == 0;
}
static int file_is_writable_internal(const char *path) {
return access(path, W_OK) == 0;
}
static IV file_mtime_internal(const char *path) {
struct stat st;
if (stat(path, &st) < 0) {
return -1;
}
return st.st_mtime;
}
static IV file_atime_internal(const char *path) {
struct stat st;
if (stat(path, &st) < 0) {
return -1;
}
return st.st_atime;
}
static IV file_ctime_internal(const char *path) {
struct stat st;
if (stat(path, &st) < 0) {
return -1;
}
return st.st_ctime;
}
static IV file_mode_internal(const char *path) {
struct stat st;
if (stat(path, &st) < 0) {
return -1;
}
return st.st_mode & 07777; /* Return permission bits only */
}
static int file_is_link_internal(const char *path) {
#ifdef _WIN32
/* Windows: check for reparse point */
DWORD attrs = GetFileAttributesA(path);
if (attrs == INVALID_FILE_ATTRIBUTES) return 0;
return (attrs & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
#else
struct stat st;
if (lstat(path, &st) < 0) return 0;
return S_ISLNK(st.st_mode);
#endif
}
static int file_is_executable_internal(const char *path) {
#ifdef _WIN32
/* Windows: check file extension */
const char *ext = strrchr(path, '.');
if (ext) {
if (_stricmp(ext, ".exe") == 0 || _stricmp(ext, ".bat") == 0 ||
xs/file/file.c view on Meta::CPAN
int open_flags_r = O_RDONLY | O_BINARY;
int open_flags_w = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
#else
int open_flags_r = O_RDONLY;
int open_flags_w = O_WRONLY | O_CREAT | O_TRUNC;
#endif
fd_src = open(src, open_flags_r);
if (fd_src < 0) return 0;
if (fstat(fd_src, &st) < 0) {
close(fd_src);
return 0;
}
fd_dst = open(dst, open_flags_w, st.st_mode & 07777);
if (fd_dst < 0) {
close(fd_src);
return 0;
}
( run in 2.474 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )