static int plugin_log__data(struct cork_stream_consumer *vself, const void *buf, size_t size, bool is_first) { size_t bytes_written = fwrite(buf, 1, size, stderr); /* If there was an error writing to the file, then signal this * to the producer */ if (bytes_written == size) { return 0; } else { cork_system_error_set(); return -1; } }
END_TEST START_TEST(test_system_error) { DESCRIBE_TEST; /* Artificially flag a system error and make sure we can detect it */ errno = ENOMEM; cork_error_clear(); cork_system_error_set(); fail_unless(cork_error_code() == ENOMEM, "Expected a system error"); printf("Got error: %s\n", cork_error_message()); cork_error_clear(); }
int bz_redhat_is_present(bool *dest) { int rc; struct stat info; rc = stat("/etc/redhat-release", &info); if (rc == 0) { *dest = true; return 0; } else if (errno == ENOENT) { *dest = false; return 0; } else { cork_system_error_set(); return -1; } }
static int cork_read_pipe_read(struct cork_read_pipe *p, char *buf, bool *progress) { if (p->fds[0] == -1) { return 0; } do { DEBUG("[read] Reading from pipe %d\n", p->fds[0]); ssize_t bytes_read = read(p->fds[0], buf, BUF_SIZE); if (bytes_read == -1) { if (errno == EAGAIN) { /* We've exhausted all of the data currently available. */ DEBUG("[read] No more bytes without blocking\n"); return 0; } else if (errno == EINTR) { /* Interrupted by a signal; return so that our wait loop can * catch that. */ DEBUG("[read] Interrupted by signal\n"); return 0; } else { /* An actual error */ cork_system_error_set(); DEBUG("[read] Error: %s\n", cork_error_message()); return -1; } } else if (bytes_read == 0) { DEBUG("[read] End of stream\n"); *progress = true; rii_check(cork_stream_consumer_eof(p->consumer)); rii_check_posix(close(p->fds[0])); p->fds[0] = -1; return 0; } else { DEBUG("[read] Got %zd bytes\n", bytes_read); *progress = true; rii_check(cork_stream_consumer_data (p->consumer, buf, bytes_read, p->first)); p->first = false; } } while (true); }
int cork_consume_file(struct cork_stream_consumer *consumer, FILE *fp) { char buf[BUFFER_SIZE]; size_t bytes_read; bool first = true; while (true) { while ((bytes_read = fread(buf, 1, BUFFER_SIZE, fp)) > 0) { rii_check(cork_stream_consumer_data (consumer, buf, bytes_read, first)); first = false; } if (feof(fp)) { return cork_stream_consumer_eof(consumer); } else if (errno != EINTR) { cork_system_error_set(); return -1; } } }
int cork_consume_fd(struct cork_stream_consumer *consumer, int fd) { char buf[BUFFER_SIZE]; ssize_t bytes_read; bool first = true; while (true) { while ((bytes_read = read(fd, buf, BUFFER_SIZE)) > 0) { rii_check(cork_stream_consumer_data (consumer, buf, bytes_read, first)); first = false; } if (bytes_read == 0) { return cork_stream_consumer_eof(consumer); } else if (errno != EINTR) { cork_system_error_set(); return -1; } } }
static int cork_walk_one_directory(struct cork_dir_walker *w, struct cork_buffer *path, size_t root_path_size) { DIR *dir = NULL; struct dirent *entry; size_t dir_path_size; rip_check_posix(dir = opendir(path->buf)); cork_buffer_append(path, "/", 1); dir_path_size = path->size; errno = 0; while ((entry = readdir(dir)) != NULL) { struct stat info; /* Skip the "." and ".." entries */ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } /* Stat the directory entry */ cork_buffer_append_string(path, entry->d_name); ei_check_posix(stat(path->buf, &info)); /* If the entry is a subdirectory, recurse into it. */ if (S_ISDIR(info.st_mode)) { int rc = cork_dir_walker_enter_directory (w, path->buf, path->buf + root_path_size, path->buf + dir_path_size); if (rc != CORK_SKIP_DIRECTORY) { ei_check(cork_walk_one_directory(w, path, root_path_size)); ei_check(cork_dir_walker_leave_directory (w, path->buf, path->buf + root_path_size, path->buf + dir_path_size)); } } else if (S_ISREG(info.st_mode)) { ei_check(cork_dir_walker_file (w, path->buf, path->buf + root_path_size, path->buf + dir_path_size)); } /* Remove this entry name from the path buffer. */ cork_buffer_truncate(path, dir_path_size); /* We have to reset errno to 0 because of the ambiguous way * readdir uses a return value of NULL. Other functions may * return normally yet set errno to a non-zero value. dlopen * on Mac OS X is an ogreish example. Since an error readdir * is indicated by returning NULL and setting errno to indicate * the error, then we need to reset it to zero before each call. * We shall assume, perhaps to our great misery, that functions * within this loop do proper error checking and act accordingly. */ errno = 0; } /* Check errno immediately after the while loop terminates */ if (CORK_UNLIKELY(errno != 0)) { cork_system_error_set(); goto error; } /* Remove the trailing '/' from the path buffer. */ cork_buffer_truncate(path, dir_path_size - 1); rii_check_posix(closedir(dir)); return 0; error: if (dir != NULL) { rii_check_posix(closedir(dir)); } return -1; }