~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to contrib/pg_upgrade/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      util.c
 
3
 *
 
4
 *      utility functions
 
5
 *
 
6
 *      Copyright (c) 2010-2011, PostgreSQL Global Development Group
 
7
 *      contrib/pg_upgrade/util.c
 
8
 */
 
9
 
 
10
#include "pg_upgrade.h"
 
11
 
 
12
#include <signal.h>
 
13
 
 
14
 
 
15
LogOpts         log_opts;
 
16
 
 
17
/*
 
18
 * report_status()
 
19
 *
 
20
 *      Displays the result of an operation (ok, failed, error message,...)
 
21
 */
 
22
void
 
23
report_status(eLogType type, const char *fmt,...)
 
24
{
 
25
        va_list         args;
 
26
        char            message[MAX_STRING];
 
27
 
 
28
        va_start(args, fmt);
 
29
        vsnprintf(message, sizeof(message), fmt, args);
 
30
        va_end(args);
 
31
 
 
32
        pg_log(type, "%s\n", message);
 
33
}
 
34
 
 
35
 
 
36
/*
 
37
 * prep_status
 
38
 *
 
39
 *      Displays a message that describes an operation we are about to begin.
 
40
 *      We pad the message out to MESSAGE_WIDTH characters so that all of the "ok" and
 
41
 *      "failed" indicators line up nicely.
 
42
 *
 
43
 *      A typical sequence would look like this:
 
44
 *              prep_status("about to flarb the next %d files", fileCount );
 
45
 *
 
46
 *              if(( message = flarbFiles(fileCount)) == NULL)
 
47
 *                report_status(PG_REPORT, "ok" );
 
48
 *              else
 
49
 *                pg_log(PG_FATAL, "failed - %s", message );
 
50
 */
 
51
void
 
52
prep_status(const char *fmt,...)
 
53
{
 
54
        va_list         args;
 
55
        char            message[MAX_STRING];
 
56
 
 
57
        va_start(args, fmt);
 
58
        vsnprintf(message, sizeof(message), fmt, args);
 
59
        va_end(args);
 
60
 
 
61
        if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
 
62
                pg_log(PG_REPORT, "%s", message);
 
63
        else
 
64
                pg_log(PG_REPORT, "%-" MESSAGE_WIDTH "s", message);
 
65
}
 
66
 
 
67
 
 
68
void
 
69
pg_log(eLogType type, char *fmt,...)
 
70
{
 
71
        va_list         args;
 
72
        char            message[MAX_STRING];
 
73
 
 
74
        va_start(args, fmt);
 
75
        vsnprintf(message, sizeof(message), fmt, args);
 
76
        va_end(args);
 
77
 
 
78
        if (log_opts.fd != NULL)
 
79
        {
 
80
                fwrite(message, strlen(message), 1, log_opts.fd);
 
81
                /* if we are using OVERWRITE_MESSAGE, add newline */
 
82
                if (strchr(message, '\r') != NULL)
 
83
                        fwrite("\n", 1, 1, log_opts.fd);
 
84
                fflush(log_opts.fd);
 
85
        }
 
86
 
 
87
        switch (type)
 
88
        {
 
89
                case PG_INFO:
 
90
                        if (log_opts.verbose)
 
91
                                printf("%s", _(message));
 
92
                        break;
 
93
 
 
94
                case PG_REPORT:
 
95
                case PG_WARNING:
 
96
                        printf("%s", _(message));
 
97
                        break;
 
98
 
 
99
                case PG_FATAL:
 
100
                        printf("%s", "\n");
 
101
                        printf("%s", _(message));
 
102
                        printf("Failure, exiting\n");
 
103
                        exit(1);
 
104
                        break;
 
105
 
 
106
                case PG_DEBUG:
 
107
                        if (log_opts.debug)
 
108
                                fprintf(log_opts.debug_fd, "%s\n", _(message));
 
109
                        break;
 
110
 
 
111
                default:
 
112
                        break;
 
113
        }
 
114
        fflush(stdout);
 
115
}
 
116
 
 
117
 
 
118
void
 
119
check_ok(void)
 
120
{
 
121
        /* all seems well */
 
122
        report_status(PG_REPORT, "ok");
 
123
        fflush(stdout);
 
124
}
 
125
 
 
126
 
 
127
/*
 
128
 * quote_identifier()
 
129
 *              Properly double-quote a SQL identifier.
 
130
 *
 
131
 * The result should be pg_free'd, but most callers don't bother because
 
132
 * memory leakage is not a big deal in this program.
 
133
 */
 
134
char *
 
135
quote_identifier(const char *s)
 
136
{
 
137
        char       *result = pg_malloc(strlen(s) * 2 + 3);
 
138
        char       *r = result;
 
139
 
 
140
        *r++ = '"';
 
141
        while (*s)
 
142
        {
 
143
                if (*s == '"')
 
144
                        *r++ = *s;
 
145
                *r++ = *s;
 
146
                s++;
 
147
        }
 
148
        *r++ = '"';
 
149
        *r++ = '\0';
 
150
 
 
151
        return result;
 
152
}
 
153
 
 
154
 
 
155
/*
 
156
 * get_user_info()
 
157
 * (copied from initdb.c) find the current user
 
158
 */
 
159
int
 
160
get_user_info(char **user_name)
 
161
{
 
162
        int                     user_id;
 
163
 
 
164
#ifndef WIN32
 
165
        struct passwd *pw = getpwuid(geteuid());
 
166
 
 
167
        user_id = geteuid();
 
168
#else                                                   /* the windows code */
 
169
        struct passwd_win32
 
170
        {
 
171
                int                     pw_uid;
 
172
                char            pw_name[128];
 
173
        }                       pass_win32;
 
174
        struct passwd_win32 *pw = &pass_win32;
 
175
        DWORD           pwname_size = sizeof(pass_win32.pw_name) - 1;
 
176
 
 
177
        GetUserName(pw->pw_name, &pwname_size);
 
178
 
 
179
        user_id = 1;
 
180
#endif
 
181
 
 
182
        *user_name = pg_strdup(pw->pw_name);
 
183
 
 
184
        return user_id;
 
185
}
 
186
 
 
187
 
 
188
void *
 
189
pg_malloc(int n)
 
190
{
 
191
        void       *p = malloc(n);
 
192
 
 
193
        if (p == NULL)
 
194
                pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
 
195
 
 
196
        return p;
 
197
}
 
198
 
 
199
 
 
200
void
 
201
pg_free(void *p)
 
202
{
 
203
        if (p != NULL)
 
204
                free(p);
 
205
}
 
206
 
 
207
 
 
208
char *
 
209
pg_strdup(const char *s)
 
210
{
 
211
        char       *result = strdup(s);
 
212
 
 
213
        if (result == NULL)
 
214
                pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
 
215
 
 
216
        return result;
 
217
}
 
218
 
 
219
 
 
220
/*
 
221
 * getErrorText()
 
222
 *
 
223
 *      Returns the text of the error message for the given error number
 
224
 *
 
225
 *      This feature is factored into a separate function because it is
 
226
 *      system-dependent.
 
227
 */
 
228
const char *
 
229
getErrorText(int errNum)
 
230
{
 
231
#ifdef WIN32
 
232
        _dosmaperr(GetLastError());
 
233
#endif
 
234
        return strdup(strerror(errNum));
 
235
}
 
236
 
 
237
 
 
238
/*
 
239
 *      str2uint()
 
240
 *
 
241
 *      convert string to oid
 
242
 */
 
243
unsigned int
 
244
str2uint(const char *str)
 
245
{
 
246
        return strtoul(str, NULL, 10);
 
247
}