Skip to content

Commit f0d7a12

Browse files
author
itagaki.takahiro
committed
Add missing headers and portable flock() implemented with fcntl() for Solaris.
git-svn-id: http://pg-rman.googlecode.com/svn/trunk@19 182aca00-e38e-11de-a668-6fd11605f5ce
1 parent 6545c64 commit f0d7a12

File tree

4 files changed

+77
-13
lines changed

4 files changed

+77
-13
lines changed

catalog.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
#include "pg_rman.h"
1111

12-
#include <stdlib.h>
13-
#include <libgen.h>
14-
#include <unistd.h>
1512
#include <dirent.h>
16-
#include <time.h>
13+
#include <fcntl.h>
14+
#include <libgen.h>
1715
#include <sys/stat.h>
1816
#include <sys/types.h>
1917
#include <sys/file.h>
18+
#include <stdlib.h>
19+
#include <time.h>
20+
#include <unistd.h>
2021

2122
#include "pgut/pgut-port.h"
2223

@@ -96,7 +97,7 @@ IsDir(const char *dirpath, const DIR *dir, const struct dirent *ent)
9697
return ent->d_type == DT_DIR;
9798
#elif defined(_finddata_t)
9899
return (dir->dd_dta.attrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
99-
#else
100+
#elif defined(WIN32)
100101
char path[MAXPGPATH];
101102
DWORD attr;
102103

@@ -106,6 +107,8 @@ IsDir(const char *dirpath, const DIR *dir, const struct dirent *ent)
106107
strlcat(path, ent->d_name, MAXPGPATH);
107108
attr = GetFileAttributes(path);
108109
return attr != (DWORD) -1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
110+
#else
111+
#error IsDir: this platform is not supported.
109112
#endif
110113
}
111114

pgut/pgut-port.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include "c.h"
1111
#include "pgut-port.h"
1212

13+
#undef flock
14+
15+
#include <unistd.h>
16+
#include <fcntl.h>
17+
1318
#ifdef WIN32
1419

1520
#include <winioctl.h>
@@ -119,13 +124,18 @@ readlink(const char *path, char *target, size_t size)
119124
return r;
120125
}
121126

127+
#endif
128+
129+
#ifdef PGUT_FLOCK
130+
131+
#ifdef WIN32
122132
int
123-
flock(int fd, int operation)
133+
pgut_flock(int fd, int operation)
124134
{
125135
BOOL ret;
126136
HANDLE handle = (HANDLE) _get_osfhandle(fd);
127137
DWORD lo = 0;
128-
DWORD hi = 1;
138+
DWORD hi = 0;
129139

130140
if (operation & LOCK_UN)
131141
{
@@ -150,4 +160,34 @@ flock(int fd, int operation)
150160
return 0;
151161
}
152162

163+
#else
164+
165+
int
166+
pgut_flock(int fd, int operation)
167+
{
168+
struct flock lck;
169+
int cmd;
170+
171+
memset(&lck, 0, sizeof(lck));
172+
lck.l_whence = SEEK_SET;
173+
lck.l_start = 0;
174+
lck.l_len = 0;
175+
lck.l_pid = getpid();
176+
177+
if (operation & LOCK_UN)
178+
lck.l_type = F_UNLCK;
179+
else if (operation & LOCK_EX)
180+
lck.l_type = F_WRLCK;
181+
else
182+
lck.l_type = F_RDLCK;
183+
184+
if (operation & LOCK_NB)
185+
cmd = F_SETLK;
186+
else
187+
cmd = F_SETLKW;
188+
189+
return fcntl(fd, cmd, &lck);
190+
}
191+
#endif
192+
153193
#endif

pgut/pgut-port.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,41 @@
1010
#ifndef PGUT_PORT_H
1111
#define PGUT_PORT_H
1212

13+
/*
14+
* readlink ports
15+
*/
1316
#ifdef WIN32
1417

15-
#define LOCK_SH 1 /* Shared lock. */
16-
#define LOCK_EX 2 /* Exclusive lock. */
17-
#define LOCK_UN 8 /* Unlock. */
18-
#define LOCK_NB 4 /* Don't block when locking. */
19-
2018
#define S_IFLNK (0)
2119
#define S_IRWXG (0)
2220
#define S_IRWXO (0)
2321
#define S_ISLNK(mode) (0)
2422

25-
extern int flock(int fd, int operation);
2623
extern ssize_t readlink(const char *path, char *target, size_t size);
2724

2825
#endif
2926

27+
/*
28+
* flock ports
29+
*/
30+
#ifndef LOCK_EX
31+
32+
#define PGUT_FLOCK
33+
34+
#undef LOCK_SH
35+
#undef LOCK_EX
36+
#undef LOCK_UN
37+
#undef LOCK_NB
38+
39+
#define LOCK_SH 1 /* Shared lock. */
40+
#define LOCK_EX 2 /* Exclusive lock. */
41+
#define LOCK_UN 8 /* Unlock. */
42+
#define LOCK_NB 4 /* Don't block when locking. */
43+
44+
extern int pgut_flock(int fd, int operation);
45+
46+
#define flock pgut_flock
47+
48+
#endif
49+
3050
#endif /* PGUT_PORT_H */

restore.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <fcntl.h>
1313
#include <sys/stat.h>
14+
#include <sys/types.h>
1415
#include <unistd.h>
1516

1617
#include "catalog/pg_control.h"

0 commit comments

Comments
 (0)