当前位置: 首页>>代码示例>>C++>>正文


C++ posix_fallocate函数代码示例

本文整理汇总了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;
}
开发者ID:rcook,项目名称:emscripten,代码行数:26,代码来源:src.c

示例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("..");
}
开发者ID:Thomas-Kim,项目名称:dcp,代码行数:60,代码来源:benchmarks.c

示例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"));
	}
}
开发者ID:rudimeier,项目名称:util-linux,代码行数:7,代码来源:fallocate.c

示例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;
}
开发者ID:Werkov,项目名称:systemd,代码行数:33,代码来源:journald-kmsg.c

示例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;
}
开发者ID:BauerBox,项目名称:hiphop-php,代码行数:35,代码来源:shared_store_base.cpp

示例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;
}
开发者ID:mpartel,项目名称:usbmooc-scripts,代码行数:28,代码来源:fallocator.c

示例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;
}
开发者ID:farrellpeng,项目名称:MX283Linux,代码行数:29,代码来源:fallocate.c

示例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;
}
开发者ID:cooljeanius,项目名称:babeltrace,代码行数:26,代码来源:event-fields.c

示例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;
}
开发者ID:nyorain,项目名称:iro,代码行数:33,代码来源:os.cpp

示例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
		}
	}
开发者ID:dreamsxin,项目名称:libktorrent,代码行数:31,代码来源:fileops.cpp

示例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);
}
开发者ID:jpmens,项目名称:diablo,代码行数:25,代码来源:filealloc.c

示例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;
}
开发者ID:ChandKV,项目名称:nvml,代码行数:63,代码来源:file.c

示例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;
}
开发者ID:binaryf,项目名称:LIFL,代码行数:31,代码来源:lifl.c

示例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
}
开发者ID:iSCGroup,项目名称:libres3,代码行数:27,代码来源:netsys_c_fallocate.c

示例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));
        }
    }
}
开发者ID:heuripedes,项目名称:sf,代码行数:27,代码来源:sf.c


注:本文中的posix_fallocate函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。