There were several memory leaks inside jfsck(), they've probably been there for a...
[libjio.git] / unix.c
blob03bd9b68a971a052bdf51a10173d9219a769aa15
2 /*
3 * libjio - A library for Journaled I/O
4 * Alberto Bertogli (albertogli@telpin.com.ar)
6 * UNIX API wrappers
7 */
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <unistd.h>
14 #include "libjio.h"
15 #include "common.h"
18 /* read() family wrappers */
20 /* read wrapper */
21 ssize_t jread(struct jfs *fs, void *buf, size_t count)
23 int rv;
24 off_t pos;
26 pthread_mutex_lock(&(fs->lock));
28 pos = lseek(fs->fd, 0, SEEK_CUR);
30 plockf(fs->fd, F_LOCKR, pos, count);
31 rv = spread(fs->fd, buf, count, pos);
32 plockf(fs->fd, F_UNLOCK, pos, count);
34 if (rv == count) {
35 /* if success, advance the file pointer */
36 lseek(fs->fd, count, SEEK_CUR);
39 pthread_mutex_unlock(&(fs->lock));
41 return rv;
44 /* pread wrapper */
45 ssize_t jpread(struct jfs *fs, void *buf, size_t count, off_t offset)
47 int rv;
49 plockf(fs->fd, F_LOCKR, offset, count);
50 rv = spread(fs->fd, buf, count, offset);
51 plockf(fs->fd, F_UNLOCK, offset, count);
53 return rv;
56 /* readv wrapper */
57 ssize_t jreadv(struct jfs *fs, struct iovec *vector, int count)
59 int rv, i;
60 size_t sum;
61 off_t pos;
63 sum = 0;
64 for (i = 0; i < count; i++)
65 sum += vector[i].iov_len;
67 pthread_mutex_lock(&(fs->lock));
68 pos = lseek(fs->fd, 0, SEEK_CUR);
69 plockf(fs->fd, F_LOCKR, pos, count);
70 rv = readv(fs->fd, vector, count);
71 plockf(fs->fd, F_UNLOCK, pos, count);
72 pthread_mutex_unlock(&(fs->lock));
74 return rv;
78 /* write family wrappers */
80 /* write wrapper */
81 ssize_t jwrite(struct jfs *fs, const void *buf, size_t count)
83 int rv;
84 off_t pos;
85 struct jtrans ts;
87 pthread_mutex_lock(&(fs->lock));
89 jtrans_init(fs, &ts);
90 pos = lseek(fs->fd, 0, SEEK_CUR);
91 jtrans_add(&ts, buf, count, pos);
93 rv = jtrans_commit(&ts);
95 if (rv >= 0) {
96 /* if success, advance the file pointer */
97 lseek(fs->fd, count, SEEK_CUR);
100 pthread_mutex_unlock(&(fs->lock));
102 jtrans_free(&ts);
104 return rv;
107 /* pwrite wrapper */
108 ssize_t jpwrite(struct jfs *fs, const void *buf, size_t count, off_t offset)
110 int rv;
111 struct jtrans ts;
113 jtrans_init(fs, &ts);
114 jtrans_add(&ts, buf, count, offset);
116 rv = jtrans_commit(&ts);
118 jtrans_free(&ts);
120 return rv;
123 /* writev wrapper */
124 ssize_t jwritev(struct jfs *fs, const struct iovec *vector, int count)
126 int rv, i;
127 size_t sum;
128 off_t ipos, t;
129 struct jtrans ts;
131 pthread_mutex_lock(&(fs->lock));
133 jtrans_init(fs, &ts);
134 ipos = lseek(fs->fd, 0, SEEK_CUR);
135 t = ipos;
137 sum = 0;
138 for (i = 0; i < count; i++) {
139 jtrans_add(&ts, vector[i].iov_base, vector[i].iov_len, t);
140 sum += vector[i].iov_len;
141 t += vector[i].iov_len;
144 rv = jtrans_commit(&ts);
146 if (rv >= 0) {
147 /* if success, advance the file pointer */
148 lseek(fs->fd, sum, SEEK_CUR);
151 pthread_mutex_unlock(&(fs->lock));
153 jtrans_free(&ts);
155 return rv;
159 /* truncate a file - be careful with this */
160 int jtruncate(struct jfs *fs, off_t length)
162 int rv;
164 /* lock from length to the end of file */
165 plockf(fs->fd, F_LOCKW, length, 0);
166 rv = ftruncate(fs->fd, length);
167 plockf(fs->fd, F_UNLOCK, length, 0);
169 return rv;
172 /* lseek wrapper */
173 off_t jlseek(struct jfs *fs, off_t offset, int whence)
175 int rv;
177 pthread_mutex_lock(&(fs->lock));
178 rv = lseek(fs->fd, offset, whence);
179 pthread_mutex_unlock(&(fs->lock));
181 return rv;