static int file_set_contents(const char *filename, const char *contents, size_t len) { FILE *fd; char *ptr; size_t nb, tot_nb; if ((fd = file_get_fd(filename, 1)) == NULL) return (-1); printf("writing %d bytes to file %s\n", len, filename); tot_nb = 0; ptr = (char *)contents; while (contents && tot_nb < len) { nb = fwrite(ptr, 1, len, fd); if (ferror(fd) || nb == 0) { fclose(fd); return (-1); } tot_nb += nb; ptr += nb; } fclose(fd); return (0); }
FILE * wiki_save_generated_fd(const char *page) { char filename[PATH_MAX]; snprintf(filename, sizeof filename, "%s/%s/generated.html", config.contents_dir, page); return file_get_fd(filename, 1); }
static struct security_descriptor *file_get_sd( struct object *obj ) { struct file *file = (struct file *)obj; struct security_descriptor *sd; struct fd *fd; assert( obj->ops == &file_ops ); fd = file_get_fd( obj ); sd = get_file_sd( obj, fd, &file->mode, &file->uid ); release_object( fd ); return sd; }
static int file_set_sd( struct object *obj, const struct security_descriptor *sd, unsigned int set_info ) { struct fd *fd; int ret; assert( obj->ops == &file_ops ); fd = file_get_fd( obj ); ret = set_file_sd( obj, fd, sd, set_info ); release_object( fd ); return ret; }
static int file_get_contents(const char *filename, char **result) { FILE *fd; char *contents; char *ptr; size_t nb, tot_nb; *result = NULL; if ((fd = file_get_fd(filename, 0)) == NULL) return (-1); if ((contents = malloc(BUF_SIZE)) == NULL) { fclose(fd); return (-1); } nb = 0; tot_nb = 0; ptr = contents; while(!feof(fd)) { nb = fread(ptr, 1, BUF_SIZE, fd); if (ferror(fd)) { free(contents); fclose(fd); return (-1); } tot_nb += nb; if (nb < BUF_SIZE) break; contents = realloc(contents, tot_nb + BUF_SIZE); if (contents == NULL) { fclose(fd); return (-1); } ptr = contents + tot_nb; } ptr[tot_nb] = '\0'; fclose(fd); *result = contents; return tot_nb; }