|
diff --git a/cache.c b/cache.c index add647e..6847202 100644 --- a/ cache.c+++ b/ cache.c |
|
@@ -65,14 +65,18 @@ static int open_slot(struct cache_slot *slot) |
65 | return 0; |
65 | return 0; |
66 | } |
66 | } |
67 | |
67 | |
68 | /* Close the active cache slot */ |
68 | /* Close the active cache slot */ |
69 | static void close_slot(struct cache_slot *slot) |
69 | static int close_slot(struct cache_slot *slot) |
70 | { |
70 | { |
| |
71 | int err = 0; |
71 | if (slot->cache_fd > 0) { |
72 | if (slot->cache_fd > 0) { |
72 | close(slot->cache_fd); |
73 | if (close(slot->cache_fd)) |
73 | slot->cache_fd = -1; |
74 | err = errno; |
| |
75 | else |
| |
76 | slot->cache_fd = -1; |
74 | } |
77 | } |
| |
78 | return err; |
75 | } |
79 | } |
76 | |
80 | |
77 | /* Print the content of the active cache slot (but skip the key). */ |
81 | /* Print the content of the active cache slot (but skip the key). */ |
78 | static int print_slot(struct cache_slot *slot) |
82 | static int print_slot(struct cache_slot *slot) |
@@ -115,14 +119,18 @@ static int is_modified(struct cache_slot *slot) |
115 | st.st_size != slot->cache_st.st_size); |
119 | st.st_size != slot->cache_st.st_size); |
116 | } |
120 | } |
117 | |
121 | |
118 | /* Close an open lockfile */ |
122 | /* Close an open lockfile */ |
119 | static void close_lock(struct cache_slot *slot) |
123 | static int close_lock(struct cache_slot *slot) |
120 | { |
124 | { |
| |
125 | int err = 0; |
121 | if (slot->lock_fd > 0) { |
126 | if (slot->lock_fd > 0) { |
122 | close(slot->lock_fd); |
127 | if (close(slot->lock_fd)) |
123 | slot->lock_fd = -1; |
128 | err = errno; |
| |
129 | else |
| |
130 | slot->lock_fd = -1; |
124 | } |
131 | } |
| |
132 | return err; |
125 | } |
133 | } |
126 | |
134 | |
127 | /* Create a lockfile used to store the generated content for a cache |
135 | /* Create a lockfile used to store the generated content for a cache |
128 | * slot, and write the slot key + \0 into it. |
136 | * slot, and write the slot key + \0 into it. |
@@ -133,9 +141,10 @@ static int lock_slot(struct cache_slot *slot) |
133 | slot->lock_fd = open(slot->lock_name, O_RDWR|O_CREAT|O_EXCL, |
141 | slot->lock_fd = open(slot->lock_name, O_RDWR|O_CREAT|O_EXCL, |
134 | S_IRUSR|S_IWUSR); |
142 | S_IRUSR|S_IWUSR); |
135 | if (slot->lock_fd == -1) |
143 | if (slot->lock_fd == -1) |
136 | return errno; |
144 | return errno; |
137 | write(slot->lock_fd, slot->key, slot->keylen + 1); |
145 | if (write(slot->lock_fd, slot->key, slot->keylen + 1) < 0) |
| |
146 | return errno; |
138 | return 0; |
147 | return 0; |
139 | } |
148 | } |
140 | |
149 | |
141 | /* Release the current lockfile. If `replace_old_slot` is set the |
150 | /* Release the current lockfile. If `replace_old_slot` is set the |
@@ -149,9 +158,13 @@ static int unlock_slot(struct cache_slot *slot, int replace_old_slot) |
149 | if (replace_old_slot) |
158 | if (replace_old_slot) |
150 | err = rename(slot->lock_name, slot->cache_name); |
159 | err = rename(slot->lock_name, slot->cache_name); |
151 | else |
160 | else |
152 | err = unlink(slot->lock_name); |
161 | err = unlink(slot->lock_name); |
153 | return err; |
162 | |
| |
163 | if (err) |
| |
164 | return errno; |
| |
165 | |
| |
166 | return 0; |
154 | } |
167 | } |
155 | |
168 | |
156 | /* Generate the content for the current cache slot by redirecting |
169 | /* Generate the content for the current cache slot by redirecting |
157 | * stdout to the lock-fd and invoking the callback function |
170 | * stdout to the lock-fd and invoking the callback function |
@@ -176,9 +189,11 @@ static int fill_slot(struct cache_slot *slot) |
176 | if (dup2(tmp, STDOUT_FILENO) == -1) |
189 | if (dup2(tmp, STDOUT_FILENO) == -1) |
177 | return errno; |
190 | return errno; |
178 | |
191 | |
179 | /* Close the temporary filedescriptor */ |
192 | /* Close the temporary filedescriptor */ |
180 | close(tmp); |
193 | if (close(tmp)) |
| |
194 | return errno; |
| |
195 | |
181 | return 0; |
196 | return 0; |
182 | } |
197 | } |
183 | |
198 | |
184 | /* Crude implementation of 32-bit FNV-1 hash algorithm, |
199 | /* Crude implementation of 32-bit FNV-1 hash algorithm, |
|