Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6b711cf

Browse files
committed
In pg_upgrade, simplify function copy_file() by using pg_malloc() and
centralizing error/shutdown code.
1 parent 16e1ae7 commit 6b711cf

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

contrib/pg_upgrade/file.c

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ copy_file(const char *srcfile, const char *dstfile, bool force)
133133
int src_fd;
134134
int dest_fd;
135135
char *buffer;
136+
int ret = 0;
137+
int save_errno = 0;
136138

137139
if ((srcfile == NULL) || (dstfile == NULL))
138140
return -1;
@@ -150,37 +152,16 @@ copy_file(const char *srcfile, const char *dstfile, bool force)
150152

151153
buffer = (char *) pg_malloc(COPY_BUF_SIZE);
152154

153-
if (buffer == NULL)
154-
{
155-
if (src_fd != 0)
156-
close(src_fd);
157-
158-
if (dest_fd != 0)
159-
close(dest_fd);
160-
161-
return -1;
162-
}
163-
164155
/* perform data copying i.e read src source, write to destination */
165156
while (true)
166157
{
167158
ssize_t nbytes = read(src_fd, buffer, COPY_BUF_SIZE);
168159

169160
if (nbytes < 0)
170161
{
171-
int save_errno = errno;
172-
173-
if (buffer != NULL)
174-
pg_free(buffer);
175-
176-
if (src_fd != 0)
177-
close(src_fd);
178-
179-
if (dest_fd != 0)
180-
close(dest_fd);
181-
182-
errno = save_errno;
183-
return -1;
162+
save_errno = errno;
163+
ret = -1;
164+
break;
184165
}
185166

186167
if (nbytes == 0)
@@ -190,33 +171,24 @@ copy_file(const char *srcfile, const char *dstfile, bool force)
190171

191172
if (write(dest_fd, buffer, nbytes) != nbytes)
192173
{
193-
/* if write didn't set errno, assume problem is no disk space */
194-
int save_errno = errno ? errno : ENOSPC;
195-
196-
if (buffer != NULL)
197-
pg_free(buffer);
198-
199-
if (src_fd != 0)
200-
close(src_fd);
201-
202-
if (dest_fd != 0)
203-
close(dest_fd);
204-
205-
errno = save_errno;
206-
return -1;
174+
save_errno = errno;
175+
ret = -1;
176+
break;
207177
}
208178
}
209179

210-
if (buffer != NULL)
211-
pg_free(buffer);
180+
pg_free(buffer);
212181

213182
if (src_fd != 0)
214183
close(src_fd);
215184

216185
if (dest_fd != 0)
217186
close(dest_fd);
218187

219-
return 1;
188+
if (save_errno != 0)
189+
errno = save_errno;
190+
191+
return ret;
220192
}
221193
#endif
222194

0 commit comments

Comments
 (0)