本文整理汇总了C++中posix_fallocate函数的典型用法代码示例。如果您正苦于以下问题:C++ posix_fallocate函数的具体用法?C++ posix_fallocate怎么用?C++ posix_fallocate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了posix_fallocate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
struct stat s;
int f = open("/test", O_RDWR, 0777);
assert(f);
printf("posix_fadvise: %d\n", posix_fadvise(f, 3, 2, POSIX_FADV_DONTNEED));
printf("errno: %d\n", errno);
printf("\n");
errno = 0;
printf("posix_fallocate: %d\n", posix_fallocate(f, 3, 2));
printf("errno: %d\n", errno);
stat("/test", &s);
printf("st_size: %d\n", s.st_size);
memset(&s, 0, sizeof s);
printf("\n");
errno = 0;
printf("posix_fallocate2: %d\n", posix_fallocate(f, 3, 7));
printf("errno: %d\n", errno);
stat("/test", &s);
printf("st_size: %d\n", s.st_size);
memset(&s, 0, sizeof s);
return 0;
}
示例2: make_level
static void make_level(
const char* target,
uint32_t depth,
uint32_t width,
uint32_t files,
uint32_t file_size
) {
if (chdir(target) == -1) {
perror(target);
return;
}
if (depth) {
mode_t mode = 0775;
for (size_t i = 0; i < width; i++) {
char filename[18];
snprintf(filename, sizeof(filename), "d%u", i);
if (mkdir(filename, mode) == -1) {
if (errno != EEXIST) {
perror(filename);
goto up;
}
}
make_level(filename, depth - 1, width, files, file_size);
}
}
FILE* rand = fopen("/dev/urandom", "r");
if (rand == NULL) {
perror("/dev/urandom");
goto up;
}
for (size_t i = 0; i < files; i++) {
char filename[17];
snprintf(filename, sizeof(filename), "%u", i);
FILE* f = fopen(filename, "w");
if (f == NULL) {
perror("fopen");
goto up;
}
int fd = fileno(f);
// if fallocate fails nbd
posix_fallocate(fd, 0, file_size);
const size_t chunk_size = 1000;
char* buffer = malloc(chunk_size);
for (size_t i = 0; i < file_size; i++) {
// don't care if fread fails
fread(buffer, 1, chunk_size, rand);
size_t written = fwrite(buffer, 1, chunk_size, f);
if (written < chunk_size) {
perror(filename);
goto cleanup;
}
}
cleanup:
free(buffer);
fclose(f);
}
fclose(rand);
up:
chdir("..");
}
示例3: xposix_fallocate
static void xposix_fallocate(int fd, off_t offset, off_t length)
{
int error = posix_fallocate(fd, offset, length);
if (error < 0) {
err(EXIT_FAILURE, _("fallocate failed"));
}
}
示例4: server_open_kernel_seqnum
int server_open_kernel_seqnum(Server *s) {
_cleanup_close_ int fd;
uint64_t *p;
int r;
assert(s);
/* We store the seqnum we last read in an mmaped file. That
* way we can just use it like a variable, but it is
* persistent and automatically flushed at reboot. */
fd = open("/run/systemd/journal/kernel-seqnum", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
if (fd < 0) {
log_error_errno(errno, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
return 0;
}
r = posix_fallocate(fd, 0, sizeof(uint64_t));
if (r != 0) {
log_error_errno(r, "Failed to allocate sequential number file, ignoring: %m");
return 0;
}
p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
log_error_errno(errno, "Failed to map sequential number file, ignoring: %m");
return 0;
}
s->kernel_seqnum = p;
return 0;
}
示例5: snprintf
bool SharedStoreFileStorage::addFile() {
if ((int64)m_chunks.size() * m_chunkSize >= m_maxSize) {
m_state = StateFull;
return false;
}
char name[PATH_MAX];
snprintf(name, sizeof(name), "%s.XXXXXX", m_prefix.c_str());
int fd = mkstemp(name);
if (fd < 0) {
Logger::Error("Failed to open temp file");
return false;
}
if (posix_fallocate(fd, 0, m_chunkSize)) {
Logger::Error("Failred to posix_fallocate of size %llu", m_chunkSize);
close(fd);
return false;
}
if (RuntimeOption::ApcFileStorageKeepFileLinked) {
m_fileNames.push_back(std::string(name));
} else {
unlink(name);
}
char *addr = (char *)mmap(NULL, m_chunkSize, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (addr == (char *)-1) {
Logger::Error("Failed to mmap of size %llu", name, m_chunkSize);
close(fd);
return false;
}
m_current = addr;
m_chunkRemain = m_chunkSize - PaddingSize;
m_chunks.push_back(addr);
close(fd);
return true;
}
示例6: main
int main(int argc, const char *argv[])
{
if (argc != 4) {
fprintf(stderr, "Usage: fallocator <file> <offset> <len>\n");
return 1;
}
int fd = open(argv[1], O_RDWR | O_CREAT, 0666);
if (fd == -1) {
perror("failed to open file");
return 1;
}
uint64_t offset;
uint64_t length;
sscanf(argv[2], "%"SCNu64, &offset);
sscanf(argv[3], "%"SCNu64, &length);
errno = posix_fallocate(fd, offset, length);
if (errno != 0) {
perror("failed to fallocate");
close(fd);
return 1;
}
close(fd);
return 0;
}
示例7: fallocate_main
int fallocate_main(int argc UNUSED_PARAM, char **argv)
{
const char *str_l;
const char *str_o = "0";
off_t ofs, len;
unsigned opts;
int fd;
/* exactly one non-option arg */
opt_complementary = "=1";
opts = getopt32(argv, "l:o:", &str_l, &str_o);
if (!(opts & 1))
bb_show_usage();
ofs = xatoull_sfx(str_o, kmg_i_suffixes);
len = xatoull_sfx(str_l, kmg_i_suffixes);
argv += optind;
fd = xopen3(*argv, O_RDWR | O_CREAT, 0666);
/* posix_fallocate has unusual method of returning error */
/* maybe use Linux-specific fallocate(int fd, int mode, off_t offset, off_t len) instead? */
if ((errno = posix_fallocate(fd, ofs, len)) != 0)
bb_perror_msg_and_die("fallocate '%s'", *argv);
/* util-linux also performs fsync(fd); */
return EXIT_SUCCESS;
}
示例8: increase_packet_size
static
int increase_packet_size(struct ctf_stream_pos *pos)
{
int ret;
assert(pos);
ret = munmap_align(pos->base_mma);
if (ret) {
goto end;
}
pos->packet_size += PACKET_LEN_INCREMENT;
ret = posix_fallocate(pos->fd, pos->mmap_offset,
pos->packet_size / CHAR_BIT);
if (ret) {
goto end;
}
pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
pos->flags, pos->fd, pos->mmap_offset);
if (pos->base_mma == MAP_FAILED) {
ret = -1;
}
end:
return ret;
}
示例9: os_create_anonymous_file
int os_create_anonymous_file(std::size_t size)
{
static const char tmplate[] = "/wlc-shared-XXXXXX";
std::string path = getenv("XDG_RUNTIME_DIR");
if (path.empty())
return -1;
std::string name = path;
if(path.back() != '/') name.append("/");
name.append(tmplate);
int fd = create_tmpfile_cloexec(name.c_str());
if (fd < 0)
return -1;
int ret;
#if HAVE_POSIX_FALLOCATE
if ((ret = posix_fallocate(fd, 0, size)) != 0) {
close(fd);
errno = ret;
return -1;
}
#else
if ((ret = ftruncate(fd, size)) < 0) {
close(fd);
return -1;
}
#endif
return fd;
}
示例10: TruncateFile
void TruncateFile(int fd,Uint64 size,bool quick)
{
if (FileSize(fd) == size)
return;
if (quick)
{
#ifdef HAVE_FTRUNCATE64
if (ftruncate64(fd,size) == -1)
#else
if (ftruncate(fd,size) == -1)
#endif
throw Error(i18n("Cannot expand file: %1",strerror(errno)));
}
else
{
#ifdef HAVE_POSIX_FALLOCATE64
if (posix_fallocate64(fd,0,size) != 0)
throw Error(i18n("Cannot expand file: %1",strerror(errno)));
#elif HAVE_POSIX_FALLOCATE
if (posix_fallocate(fd,0,size) != 0)
throw Error(i18n("Cannot expand file: %1",strerror(errno)));
#elif HAVE_FTRUNCATE64
if (ftruncate64(fd,size) == -1)
throw Error(i18n("Cannot expand file: %1",strerror(errno)));
#else
if (ftruncate(fd,size) == -1)
throw Error(i18n("Cannot expand file: %1",strerror(errno)));
#endif
}
}
示例11: FileAllocSpace
int
FileAllocSpace(int fd, off_t off, off_t len)
{
#if (LINUX_SYS_FALLOCATE) && defined(SYS_fallocate)
int e;
static int fallocate_ok = 1;
static int posix_fallocate_ok = 1;
if (fallocate_ok) {
e = syscall(SYS_fallocate, fd, 0, (loff_t)off, (loff_t)len);
if (e == 0)
return 0;
if (e < 0 && (errno == ENOSYS || errno == EOPNOTSUPP))
fallocate_ok = 0;
}
if (posix_fallocate_ok) {
e = posix_fallocate(fd, off, len);
if (e == 0)
return 0;
if (e < 0 && (errno == ENOSYS || errno == EOPNOTSUPP))
posix_fallocate_ok = 0;
}
#endif
return ftruncate(fd, off + len);
}
示例12: util_file_create
/*
* util_file_create -- create a new memory pool file
*/
int
util_file_create(const char *path, size_t size, size_t minsize)
{
LOG(3, "path %s size %zu minsize %zu", path, size, minsize);
ASSERTne(size, 0);
if (size < minsize) {
ERR("size %zu smaller than %zu", size, minsize);
errno = EINVAL;
return -1;
}
if (((off_t)size) < 0) {
ERR("invalid size (%zu) for off_t", size);
errno = EFBIG;
return -1;
}
int fd;
int mode;
int flags = O_RDWR | O_CREAT | O_EXCL;
#ifndef _WIN32
mode = 0;
#else
mode = S_IWRITE | S_IREAD;
flags |= O_BINARY;
#endif
/*
* Create file without any permission. It will be granted once
* initialization completes.
*/
if ((fd = open(path, flags, mode)) < 0) {
ERR("!open %s", path);
return -1;
}
if ((errno = posix_fallocate(fd, 0, (off_t)size)) != 0) {
ERR("!posix_fallocate");
goto err;
}
/* for windows we can't flock until after we fallocate */
if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
ERR("!flock");
goto err;
}
return fd;
err:
LOG(4, "error clean up");
int oerrno = errno;
if (fd != -1)
(void) close(fd);
unlink(path);
errno = oerrno;
return -1;
}
示例13: xmp_fallocate
static int xmp_fallocate(const char *path, int mode,
off_t offset, off_t length, struct fuse_file_info *fi)
{
if(conf.syscall_fallocate) sql_write(path,"fallocate");
int fd;
int res;
(void) fi;
if (mode)
{
if(conf.syscall_fallocate && conf.enable_error_messages) sql_write_err();
return -EOPNOTSUPP;
}
char *rpath;
rpath=get_rel_path(path);
fd = open(rpath, O_WRONLY);
free(rpath);
if (fd == -1)
{
if(conf.syscall_fallocate && conf.enable_error_messages) sql_write_err();
return -errno;
}
res = -posix_fallocate(fd, offset, length);
close(fd);
return res;
}
示例14: netsys_fallocate
CAMLprim value netsys_fallocate(value fd, value start, value len) {
#ifdef HAVE_POSIX_FALLOCATE
int r;
int64 start_int, len_int;
off_t start_off, len_off;
/* Att: off_t might be 64 bit even on 32 bit systems! */
start_int = Int64_val(start);
len_int = Int64_val(len);
if ( ((int64) ((off_t) start_int)) != start_int )
failwith("Netsys.fadvise: large files not supported on this OS");
if ( ((int64) ((off_t) len_int)) != len_int )
failwith("Netsys.fadvise: large files not supported on this OS");
start_off = start_int;
len_off = len_int;
r = posix_fallocate(Int_val(fd), start_off, len_off);
/* does not set errno! */
if (r != 0)
unix_error(r, "posix_fallocate64", Nothing);
return Val_unit;
#else
invalid_argument("Netsys.fallocate not available");
#endif
}
示例15: open_file
static void open_file() {
if (file)
return;
if (!file_name[0] && !guess_filename()) {
die("could not determine the filename, consider using the -o option.\n");
}
file = fopen(file_name, "wb+");
if (!file)
die("failed to open '%s': %s\n", file_name, strerror(errno));
if (file_size) {
int fd = fileno(file);
int err;
if ((err = posix_fallocate(fd, 0, file_size))) {
warn("failed to preallocate '%s': %s\n", file_name, strerror(err));
}
if ((err = posix_fadvise(fd, 0, file_size, POSIX_FADV_RANDOM))) {
warn("failed to set '%s's access policy: %s\n", file_name, strerror(err));
}
}
}