本文整理汇总了C++中perrorf函数的典型用法代码示例。如果您正苦于以下问题:C++ perrorf函数的具体用法?C++ perrorf怎么用?C++ perrorf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了perrorf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: do_fusermount
static int
do_fusermount (guestfs_h *g, const char *localmountpoint, int error_fd)
{
pid_t pid;
int status;
pid = fork ();
if (pid == -1) {
perrorf (g, "fork");
return -1;
}
if (pid == 0) { /* child */
/* Ensure stdout and stderr point to the error_fd. */
dup2 (error_fd, STDOUT_FILENO);
dup2 (error_fd, STDERR_FILENO);
close (error_fd);
execlp ("fusermount", "fusermount", "-u", localmountpoint, NULL);
perror ("exec: fusermount");
_exit (EXIT_FAILURE);
}
/* Parent. */
if (waitpid (pid, &status, 0) == -1) {
perrorf (g, "waitpid");
return -1;
}
if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS)
return 0; /* it failed to unmount the mountpoint */
return 1; /* unmount was successful */
}
示例2: rlc_insert
static int
rlc_insert (guestfs_h *g,
const char *path, const char *name, time_t now,
char *link)
{
struct rlc_entry *entry;
size_t len;
entry = malloc (sizeof *entry);
if (entry == NULL) {
perrorf (g, "malloc");
return -1;
}
len = strlen (path) + strlen (name) + 2;
entry->c.pathname = malloc (len);
if (entry->c.pathname == NULL) {
perrorf (g, "malloc");
free (entry);
return -1;
}
if (STREQ (path, "/"))
snprintf (entry->c.pathname, len, "/%s", name);
else
snprintf (entry->c.pathname, len, "%s/%s", path, name);
entry->link = link;
entry->c.timeout = now + g->ml_dir_cache_timeout;
return gen_replace (g, g->rlc_ht, (struct entry_common *) entry, rlc_free);
}
示例3: filecopy
int filecopy(FILE *in, FILE *out, char *outname, size_t insiz)
{
char buffer[BSIZ];
size_t nread, total = 0;
int ret = 0, lastprogress = 0;
while((nread = fread(buffer, sizeof buffer[0], BSIZ, in)) > 0){
if(fwrite(buffer, sizeof buffer[0], nread, out) <= 0){
perrorf("fwrite()");
ret = 1;
goto bail;
}
if(++lastprogress > PROGRESS_COUNT){
progress(outname, total += nread, insiz);
lastprogress = 0;
}
}
progressdone(outname, insiz);
if(ferror(in)){
perrorf("fread()");
ret = 1;
}
bail:
return ret;
}
示例4: guestfs_impl_read_file
char *
guestfs_impl_read_file (guestfs_h *g, const char *path, size_t *size_r)
{
int fd = -1;
size_t size;
CLEANUP_UNLINK_FREE char *tmpfile = NULL;
char *ret = NULL;
struct stat statbuf;
tmpfile = guestfs_int_make_temp_path (g, "cat", NULL);
if (tmpfile == NULL)
goto err;
if (guestfs_download (g, path, tmpfile) == -1)
goto err;
fd = open (tmpfile, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
perrorf (g, "open: %s", tmpfile);
goto err;
}
/* Read the whole file into memory. */
if (fstat (fd, &statbuf) == -1) {
perrorf (g, "stat: %s", tmpfile);
goto err;
}
/* Don't use safe_malloc, because we want to return an errno to the caller. */
size = statbuf.st_size;
ret = malloc (size + 1);
if (!ret) {
perrorf (g, "malloc: %zu bytes", size + 1);
goto err;
}
if (full_read (fd, ret, size) != size) {
perrorf (g, "full-read: %s: %zu bytes", tmpfile, size + 1);
goto err;
}
ret[size] = '\0';
if (close (fd) == -1) {
perrorf (g, "close: %s", tmpfile);
goto err;
}
/* Mustn't touch *size_r until we are sure that we won't return any
* error (RHBZ#589039).
*/
*size_r = size;
return ret;
err:
free (ret);
if (fd >= 0)
close (fd);
return NULL;
}
示例5: set_abs_path
/* We need to make all tmpdir paths absolute because lots of places in
* the code assume this. Do it at the time we set the path or read
* the environment variable (RHBZ#882417).
*/
static int
set_abs_path (guestfs_h *g, const char *tmpdir, char **tmpdir_ret)
{
char *ret;
struct stat statbuf;
/* Free the old path, and set it to NULL so that if we fail below
* we don't end up with a pointer to freed memory.
*/
free (*tmpdir_ret);
*tmpdir_ret = NULL;
if (tmpdir == NULL)
return 0;
ret = realpath (tmpdir, NULL);
if (ret == NULL) {
perrorf (g, _("failed to set temporary directory: %s"), tmpdir);
return -1;
}
if (stat (ret, &statbuf) == -1) {
perrorf (g, _("failed to set temporary directory: %s"), tmpdir);
return -1;
}
if (!S_ISDIR (statbuf.st_mode)) {
error (g, _("temporary directory '%s' is not a directory"), tmpdir);
return -1;
}
*tmpdir_ret = ret;
return 0;
}
示例6: copy_xattr_list
static struct guestfs_xattr_list *
copy_xattr_list (guestfs_h *g, const struct guestfs_xattr *first, size_t num)
{
struct guestfs_xattr_list *xattrs;
size_t i;
xattrs = malloc (sizeof *xattrs);
if (xattrs == NULL) {
perrorf (g, "malloc");
return NULL;
}
xattrs->len = num;
xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
if (xattrs->val == NULL) {
perrorf (g, "malloc");
free (xattrs);
return NULL;
}
for (i = 0; i < num; ++i) {
xattrs->val[i].attrname = strdup (first[i].attrname);
xattrs->val[i].attrval_len = first[i].attrval_len;
xattrs->val[i].attrval = malloc (first[i].attrval_len);
memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
}
return xattrs;
}
示例7: lazy_make_tmpdir
static int
lazy_make_tmpdir (guestfs_h *g, char *(*getdir) (guestfs_h *g), char **dest)
{
if (!*dest) {
CLEANUP_FREE char *tmpdir = getdir (g);
char *tmppath = safe_asprintf (g, "%s/libguestfsXXXXXX", tmpdir);
if (mkdtemp (tmppath) == NULL) {
perrorf (g, _("%s: cannot create temporary directory"), tmppath);
free (tmppath);
return -1;
}
/* Allow qemu (which may be running as qemu.qemu) to read in this
* temporary directory; we are storing either sockets, or temporary
* disks which qemu needs to access to. (RHBZ#610880).
* We do this only for root, as for normal users qemu will be run
* under the same user.
*/
if (geteuid () == 0 && chmod (tmppath, 0755) == -1) {
perrorf (g, "chmod: %s", tmppath);
free (tmppath);
return -1;
}
*dest = tmppath;
}
return 0;
}
示例8: guestfs_int_new_conn_socket_connected
/* Create a new socket connection, connected.
*
* As above, but the caller passes us a connected daemon_sock
* and promises not to call accept_connection.
*/
struct connection *
guestfs_int_new_conn_socket_connected (guestfs_h *g,
int daemon_sock,
int console_sock)
{
struct connection_socket *conn;
assert (daemon_sock >= 0);
if (fcntl (daemon_sock, F_SETFL, O_NONBLOCK) == -1) {
perrorf (g, "new_conn_socket_connected: fcntl");
return NULL;
}
if (console_sock >= 0) {
if (fcntl (console_sock, F_SETFL, O_NONBLOCK) == -1) {
perrorf (g, "new_conn_socket_connected: fcntl");
return NULL;
}
}
conn = safe_malloc (g, sizeof *conn);
/* Set the operations. */
conn->ops = &ops;
/* Set the internal state. */
conn->console_sock = console_sock;
conn->daemon_sock = daemon_sock;
conn->daemon_accept_sock = -1;
return (struct connection *) conn;
}
示例9: write_or_append
static int
write_or_append (guestfs_h *g, const char *path,
const char *content, size_t size,
int append)
{
CLEANUP_UNLINK_FREE char *tmpfile = NULL;
int fd = -1;
int64_t filesize;
/* If the content is small enough, use guestfs_internal_write{,_append}
* since that call is more efficient.
*/
if (size <= 2*1024*1024)
return
(!append ? guestfs_internal_write : guestfs_internal_write_append)
(g, path, content, size);
if (guestfs_int_lazy_make_tmpdir (g) == -1)
goto err;
/* Write the content out to a temporary file. */
tmpfile = safe_asprintf (g, "%s/write%d", g->tmpdir, ++g->unique);
fd = open (tmpfile, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0600);
if (fd == -1) {
perrorf (g, "open: %s", tmpfile);
goto err;
}
if (full_write (fd, content, size) != size) {
perrorf (g, "write: %s", tmpfile);
goto err;
}
if (close (fd) == -1) {
perrorf (g, "close: %s", tmpfile);
goto err;
}
fd = -1;
if (!append) {
if (guestfs_upload (g, tmpfile, path) == -1)
goto err;
}
else {
/* XXX Should have an 'upload-append' call to make this atomic. */
filesize = guestfs_filesize (g, path);
if (filesize == -1)
goto err;
if (guestfs_upload_offset (g, tmpfile, path, filesize) == -1)
goto err;
}
return 0;
err:
if (fd >= 0)
close (fd);
return -1;
}
示例10: guestfs___download_to_tmp
/* Download a guest file to a local temporary file. The file is
* cached in the temporary directory, and is not downloaded again.
*
* The name of the temporary (downloaded) file is returned. The
* caller must free the pointer, but does *not* need to delete the
* temporary file. It will be deleted when the handle is closed.
*
* Refuse to download the guest file if it is larger than max_size.
* On this and other errors, NULL is returned.
*
* There is actually one cache per 'struct inspect_fs *' in order
* to handle the case of multiple roots.
*/
char *
guestfs___download_to_tmp (guestfs_h *g, struct inspect_fs *fs,
const char *filename,
const char *basename, uint64_t max_size)
{
char *r;
int fd;
char devfd[32];
int64_t size;
/* Make the basename unique by prefixing it with the fs number.
* This also ensures there is one cache per filesystem.
*/
if (asprintf (&r, "%s/%td-%s", g->tmpdir, fs - g->fses, basename) == -1) {
perrorf (g, "asprintf");
return NULL;
}
/* If the file has already been downloaded, return. */
if (access (r, R_OK) == 0)
return r;
/* Check size of remote file. */
size = guestfs_filesize (g, filename);
if (size == -1)
/* guestfs_filesize failed and has already set error in handle */
goto error;
if ((uint64_t) size > max_size) {
error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
filename, size);
goto error;
}
fd = open (r, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC, 0600);
if (fd == -1) {
perrorf (g, "open: %s", r);
goto error;
}
snprintf (devfd, sizeof devfd, "/dev/fd/%d", fd);
if (guestfs_download (g, filename, devfd) == -1) {
unlink (r);
close (fd);
goto error;
}
if (close (fd) == -1) {
perrorf (g, "close: %s", r);
unlink (r);
goto error;
}
return r;
error:
free (r);
return NULL;
}
示例11: guestfs___send_file
/* Send a file.
* Returns:
* 0 OK
* -1 error
* -2 daemon cancelled (we must read the error message)
*/
int
guestfs___send_file (guestfs_h *g, const char *filename)
{
char buf[GUESTFS_MAX_CHUNK_SIZE];
int fd, r = 0, err;
g->user_cancel = 0;
fd = open (filename, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
perrorf (g, "open: %s", filename);
send_file_cancellation (g);
return -1;
}
fadvise_sequential (fd);
/* Send file in chunked encoding. */
while (!g->user_cancel) {
r = read (fd, buf, sizeof buf);
if (r == -1 && (errno == EINTR || errno == EAGAIN))
continue;
if (r <= 0) break;
err = send_file_data (g, buf, r);
if (err < 0) {
if (err == -2) /* daemon sent cancellation */
send_file_cancellation (g);
close (fd);
return err;
}
}
if (r == -1) {
perrorf (g, "read: %s", filename);
send_file_cancellation (g);
close (fd);
return -1;
}
if (g->user_cancel) {
error (g, _("operation cancelled by user"));
g->last_errnum = EINTR;
send_file_cancellation (g);
close (fd);
return -1;
}
/* End of file, but before we send that, we need to close
* the file and check for errors.
*/
if (close (fd) == -1) {
perrorf (g, "close: %s", filename);
send_file_cancellation (g);
return -1;
}
return send_file_complete (g);
}
示例12: get_umask_from_fork
/**
* Fallback method of getting the umask using fork.
*/
static int
get_umask_from_fork (guestfs_h *g)
{
pid_t pid;
int fd[2], r;
int mask;
int status;
r = pipe2 (fd, O_CLOEXEC);
if (r == -1) {
perrorf (g, "pipe2");
return -1;
}
pid = fork ();
if (pid == -1) {
perrorf (g, "fork");
close (fd[0]);
close (fd[1]);
return -1;
}
if (pid == 0) {
/* The child process must ONLY call async-safe functions. */
close (fd[0]);
/* umask can't fail. */
mask = umask (0);
if (write (fd[1], &mask, sizeof mask) != sizeof mask)
_exit (EXIT_FAILURE);
if (close (fd[1]) == -1)
_exit (EXIT_FAILURE);
_exit (EXIT_SUCCESS);
}
/* Parent. */
close (fd[1]);
/* Read the umask. */
if (read (fd[0], &mask, sizeof mask) != sizeof mask) {
perrorf (g, "read");
close (fd[0]);
guestfs_int_waitpid_noerror (pid);
return -1;
}
close (fd[0]);
if (guestfs_int_waitpid (g, pid, &status, "umask") == -1)
return -1;
else if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
guestfs_int_external_command_failed (g, status, "umask", NULL);
return -1;
}
return mask;
}
示例13: copy
int copy(char *dest, char *src)
{
FILE *in, *out;
int ret;
char *actualdest = dest;
if(samefile(dest, src)){
eprintf("`%s' and `%s' are the same file", dest, src);
return 1;
}
if(!(in = fopen(src, "r"))){
perrorf("open (for read): `%s'", src);
return 1;
}
/* TODO: make dir if it doesn't exist */
if(!(out = fopen(dest, "w"))){
if(errno == EISDIR){
char *srcbase = strrchr(src, '/');
if(!srcbase)
srcbase = src;
actualdest = alloca(strlen(dest) + strlen(srcbase) + 2);
sprintf(actualdest, "%s/%s", dest, srcbase);
if(samefile(actualdest, src)){
eprintf("`%s' and `%s' are the same file", actualdest, src);
fclose(in);
return 1;
}
out = fopen(actualdest, "w");
if(!out){
perrorf("open (for write): `%s'", actualdest);
fclose(in);
return 1;
}
}else{
perrorf("open (for write): `%s'", dest);
fclose(in);
return 1;
}
}
ret = filecopy(in, out, actualdest, filelen(src));
fclose(in);
fclose(out);
if(!i_am_cp && remove(src))
perrorf("non-fatal: remove: `%s'", src);
return ret;
}
示例14: read_osinfo_db_three_levels
static int
read_osinfo_db_three_levels (guestfs_h *g, const char *directory)
{
DIR *dir;
int r;
dir = opendir (directory);
if (!dir) {
debug (g, "osinfo: %s: %s", directory, strerror (errno));
return 0; /* This is not an error: RHBZ#948324. */
}
debug (g, "osinfo: loading 3-level-directories database from %s", directory);
for (;;) {
struct dirent *d;
CLEANUP_FREE char *pathname = NULL;
struct stat sb;
errno = 0;
d = readdir (dir);
if (!d) break;
pathname = safe_asprintf (g, "%s/%s", directory, d->d_name);
/* Iterate only on directories. */
if (stat (pathname, &sb) == 0 && S_ISDIR (sb.st_mode)) {
r = read_osinfo_db_directory (g, pathname);
if (r == -1)
goto error;
}
}
/* Check for failure in readdir. */
if (errno != 0) {
perrorf (g, "readdir: %s", directory);
goto error;
}
/* Close the directory handle. */
r = closedir (dir);
dir = NULL;
if (r == -1) {
perrorf (g, "closedir: %s", directory);
goto error;
}
return 1;
error:
if (dir)
closedir (dir);
return -1;
}
示例15: read_whole_file
/* Read the whole file into a memory buffer and return it. The file
* should be a regular, local, trusted file.
*/
static int
read_whole_file (guestfs_h *g, const char *filename,
char **data_r, size_t *size_r)
{
int fd;
char *data;
off_t size;
off_t n;
ssize_t r;
struct stat statbuf;
fd = open (filename, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
perrorf (g, "open: %s", filename);
return -1;
}
if (fstat (fd, &statbuf) == -1) {
perrorf (g, "stat: %s", filename);
close (fd);
return -1;
}
size = statbuf.st_size;
data = safe_malloc (g, size);
n = 0;
while (n < size) {
r = read (fd, &data[n], size - n);
if (r == -1) {
perrorf (g, "read: %s", filename);
free (data);
close (fd);
return -1;
}
if (r == 0) {
error (g, _("read: %s: unexpected end of file"), filename);
free (data);
close (fd);
return -1;
}
n += r;
}
if (close (fd) == -1) {
perrorf (g, "close: %s", filename);
free (data);
return -1;
}
*data_r = data;
*size_r = size;
return 0;
}