return &file->file;
}
+static void fs_posix_file_close(struct posix_fs_file *file)
+{
+ if (file->fd != -1 && file->file.output == NULL) {
+ if (close(file->fd) < 0) {
+ fs_set_critical(file->file.fs, "close(%s) failed: %m",
+ file->file.path);
+ }
+ }
+}
+
static void fs_posix_file_deinit(struct fs_file *_file)
{
struct posix_fs_file *file = (struct posix_fs_file *)_file;
break;
}
- if (file->fd != -1) {
- if (close(file->fd) < 0) {
- fs_set_critical(_file->fs, "close(%s) failed: %m",
- _file->path);
- }
- }
+ fs_posix_file_close(file);
i_free(file->temp_path);
i_free(file->file.path);
i_free(file);
ret = read(file->fd, buf, size);
if (ret < 0)
fs_set_error(_file->fs, "read(%s) failed: %m", _file->path);
+ fs_posix_file_close(file);
return ret;
}
struct posix_fs_file *file = (struct posix_fs_file *)_file;
struct istream *input;
- if (file->fd == -1) {
- if (fs_posix_open(file) < 0) {
- input = i_stream_create_error(errno);
- i_stream_set_name(input, _file->path);
- return input;
- }
+ if (file->fd == -1 && fs_posix_open(file) < 0) {
+ input = i_stream_create_error(errno);
+ i_stream_set_name(input, _file->path);
+ } else {
+ input = i_stream_create_fd(file->fd, max_buffer_size, FALSE);
}
- return i_stream_create_fd(file->fd, max_buffer_size, FALSE);
+ i_stream_add_destroy_callback(input, fs_posix_file_close, file);
+ return input;
}
static int fs_posix_write_finish(struct posix_fs_file *file)