PostgreSQL Source Code: src/port/pgmkdirp.c Source File (original) (raw)

Go to the documentation of this file.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33#include "c.h"

34

35#include <sys/stat.h>

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56int

58{

59 struct stat sb;

60 mode_t numask,

61 oumask;

62 int last,

63 retval;

64 char *p;

65

66 retval = 0;

67 p = path;

68

69#ifdef WIN32

70

71 if (strlen(p) >= 2)

72 {

73 if (p[0] == '/' && p[1] == '/')

74 {

75

76 p = strstr(p + 2, "/");

77 if (p == NULL)

78 {

79 errno = EINVAL;

80 return -1;

81 }

82 }

83 else if (p[1] == ':' &&

84 ((p[0] >= 'a' && p[0] <= 'z') ||

85 (p[0] >= 'A' && p[0] <= 'Z')))

86 {

87

88 p += 2;

89 }

90 }

91#endif

92

93

94

95

96

97

98

99

100

101

102

103 oumask = umask(0);

105 (void) umask(numask);

106

107 if (p[0] == '/')

108 ++p;

109 for (last = 0; !last; ++p)

110 {

111 if (p[0] == '\0')

112 last = 1;

113 else if (p[0] != '/')

114 continue;

115 *p = '\0';

116 if (!last && p[1] == '\0')

117 last = 1;

118

119 if (last)

120 (void) umask(oumask);

121

122

123 if (stat(path, &sb) == 0)

124 {

126 {

127 if (last)

128 errno = EEXIST;

129 else

130 errno = ENOTDIR;

131 retval = -1;

132 break;

133 }

134 }

136 {

137 retval = -1;

138 break;

139 }

140 if (!last)

141 *p = '/';

142 }

143

144

145 (void) umask(oumask);

146

147 return retval;

148}

int pg_mkdir_p(char *path, int omode)