direct-io.hg

view tools/libfsimage/common/fsimage.c @ 12351:ac10e3047f36

[LIBFSIMAGE] Don't leak FDs or RAM when closing filesystems
Signed-off-by: Tim Deegan <Tim.Deegan@xensource.com>
author Tim Deegan <Tim.Deegan@xensource.com>
date Fri Nov 10 12:27:56 2006 +0000 (2006-11-10)
parents f54e29a3be1f
children 2ff7dedde1e3
line source
1 /*
2 * Permission is hereby granted, free of charge, to any person obtaining a copy
3 * of this software and associated documentation files (the "Software"), to
4 * deal in the Software without restriction, including without limitation the
5 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 * sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 * DEALINGS IN THE SOFTWARE.
19 *
20 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
21 * Use is subject to license terms.
22 */
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <strings.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <pthread.h>
34 #include "fsimage_plugin.h"
35 #include "fsimage_priv.h"
37 static pthread_mutex_t fsi_lock = PTHREAD_MUTEX_INITIALIZER;
39 fsi_t *fsi_open_fsimage(const char *path, uint64_t off)
40 {
41 fsi_t *fsi = NULL;
42 int fd;
43 int err;
45 if ((fd = open(path, O_RDONLY)) == -1)
46 goto fail;
48 if ((fsi = malloc(sizeof(*fsi))) == NULL)
49 goto fail;
51 fsi->f_fd = fd;
52 fsi->f_off = off;
53 fsi->f_data = NULL;
55 pthread_mutex_lock(&fsi_lock);
56 err = find_plugin(fsi, path);
57 pthread_mutex_unlock(&fsi_lock);
58 if (err != 0)
59 goto fail;
61 return (fsi);
63 fail:
64 err = errno;
65 if (fd != -1)
66 (void) close(fd);
67 free(fsi);
68 errno = err;
69 return (NULL);
70 }
72 void fsi_close_fsimage(fsi_t *fsi)
73 {
74 pthread_mutex_lock(&fsi_lock);
75 fsi->f_plugin->fp_ops->fpo_umount(fsi);
76 (void) close(fsi->f_fd);
77 fsip_fs_free(fsi);
78 pthread_mutex_unlock(&fsi_lock);
79 }
81 int fsi_file_exists(fsi_t *fsi, const char *path)
82 {
83 fsi_file_t *ffi;
85 if ((ffi = fsi_open_file(fsi, path)) == NULL)
86 return (0);
88 fsi_close_file(ffi);
89 return (1);
90 }
92 fsi_file_t *fsi_open_file(fsi_t *fsi, const char *path)
93 {
94 fsi_plugin_ops_t *ops;
95 fsi_file_t *ffi;
97 pthread_mutex_lock(&fsi_lock);
98 ops = fsi->f_plugin->fp_ops;
99 ffi = ops->fpo_open(fsi, path);
100 pthread_mutex_unlock(&fsi_lock);
102 return (ffi);
103 }
105 int fsi_close_file(fsi_file_t *ffi)
106 {
107 fsi_plugin_ops_t *ops;
108 int err;
110 pthread_mutex_lock(&fsi_lock);
111 ops = ffi->ff_fsi->f_plugin->fp_ops;
112 err = ops->fpo_close(ffi);
113 pthread_mutex_unlock(&fsi_lock);
115 return (err);
116 }
118 ssize_t fsi_read_file(fsi_file_t *ffi, void *buf, size_t nbytes)
119 {
120 fsi_plugin_ops_t *ops;
121 ssize_t ret;
123 pthread_mutex_lock(&fsi_lock);
124 ops = ffi->ff_fsi->f_plugin->fp_ops;
125 ret = ops->fpo_read(ffi, buf, nbytes);
126 pthread_mutex_unlock(&fsi_lock);
128 return (ret);
129 }
131 ssize_t fsi_pread_file(fsi_file_t *ffi, void *buf, size_t nbytes, uint64_t off)
132 {
133 fsi_plugin_ops_t *ops;
134 ssize_t ret;
136 pthread_mutex_lock(&fsi_lock);
137 ops = ffi->ff_fsi->f_plugin->fp_ops;
138 ret = ops->fpo_pread(ffi, buf, nbytes, off);
139 pthread_mutex_unlock(&fsi_lock);
141 return (ret);
142 }