~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/src/BSUtil.cc

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007 SNAP Innovation GmbH
 
2
 *
 
3
 * BLOB Streaming for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Paul McCullagh
 
20
 *
 
21
 * 2007-05-25
 
22
 *
 
23
 * H&G2JCtL
 
24
 *
 
25
 * Network interface.
 
26
 *
 
27
 */
 
28
 
 
29
/* I must do this in order to get the BS config.h, which
 
30
 * will give us the correct value for VERSION!
 
31
 */
 
32
#ifdef MYSQL_SERVER
 
33
#undef MYSQL_SERVER
 
34
#endif
 
35
 
 
36
#include "CSConfig.h"
 
37
 
 
38
#include <string.h>
 
39
#include <ctype.h>
 
40
 
 
41
#include "CSGlobal.h"
 
42
#include "CSStrUtil.h"
 
43
 
 
44
#include "BSEngine.h"
 
45
#include "BSUtil.h"
 
46
 
 
47
/*
 
48
 * A file name has the form:
 
49
 * <text>-<number>[.<ext>]
 
50
 * This function return the number part as a
 
51
 * u_int.
 
52
 */
 
53
u_int bs_file_to_table_id(const char *file_name, const char *name_part)
 
54
{
 
55
        u_int value = 0;
 
56
 
 
57
        if (file_name) {
 
58
                const char *num = file_name +  strlen(file_name) - 1;
 
59
                
 
60
                while (num >= file_name && *num != '-')
 
61
                        num--;
 
62
                if (name_part) {
 
63
                        /* Check the name part of the file: */
 
64
                        int len = strlen(name_part);
 
65
                        
 
66
                        if (len != num - file_name)
 
67
                                return 0;
 
68
                        if (strncmp(file_name, name_part, len) != 0)
 
69
                                return 0;
 
70
                }
 
71
                num++;
 
72
                if (isdigit(*num))
 
73
                        sscanf(num, "%lu", &value);
 
74
        }
 
75
        return value;
 
76
}
 
77
 
 
78
u_int bs_file_to_table_id(const char *file_name)
 
79
{
 
80
        return bs_file_to_table_id(file_name, NULL);
 
81
}
 
82
 
 
83
const char *bs_file_to_table_name(size_t size, char *tab_name, const char *file_name)
 
84
{
 
85
        const char      *cptr;
 
86
        size_t          len;
 
87
 
 
88
        file_name = cs_last_name_of_path(file_name);
 
89
        cptr = file_name + strlen(file_name) - 1;
 
90
        while (cptr > file_name && *cptr != '.')
 
91
                cptr--;
 
92
        if (cptr > file_name && *cptr == '.') {
 
93
                if (strncmp(cptr, ".bs", 2) == 0) {
 
94
                        cptr--;
 
95
                        while (cptr > file_name && isdigit(*cptr))
 
96
                                cptr--;
 
97
                }
 
98
        }
 
99
 
 
100
        ret_name:
 
101
        len = cptr - file_name;
 
102
        if (len > size-1)
 
103
                len = size-1;
 
104
 
 
105
        memcpy(tab_name, file_name, len);
 
106
        tab_name[len] = 0;
 
107
 
 
108
        /* Return a pointer to what was removed! */
 
109
        return file_name + len;
 
110
}
 
111
 
 
112
bool bs_parse_blob_url(BSBlobURLPtr blob, const char *url, bool allow_old_urls)
 
113
{
 
114
        const char      *ptr = cs_last_name_of_path(url);
 
115
        u_long          tab_id = 0;
 
116
        u_llong         blob_id = 0;
 
117
        u_long          auth_code = 0;
 
118
        u_long          server_id = 0;
 
119
        char            type;
 
120
 
 
121
        if (ptr == url)
 
122
                return false;
 
123
 
 
124
        if (strncmp(url, BS_URL_PREFIX, BS_URL_PREFIX_LEN) == 0) {
 
125
                if (allow_old_urls)
 
126
                        url += BS_URL_PREFIX_LEN;
 
127
                else
 
128
                        goto deprecated_url;
 
129
        }
 
130
        else if (strncmp(url, BS_URL_NEW_PREFIX, BS_URL_PREFIX_LEN) == 0)
 
131
                url += BS_URL_PREFIX_LEN;
 
132
        else if (strncmp(url, BS_URL_ENC_PREFIX, BS_URL_ENC_PREFIX_LEN) == 0) {
 
133
                if (allow_old_urls)
 
134
                        url += BS_URL_ENC_PREFIX_LEN;
 
135
                else
 
136
                        goto deprecated_url;
 
137
        }
 
138
        cs_strcpy(BS_DATABASE_NAME_SIZE, blob->bu_database, url, ptr - url - 1);
 
139
        if (*ptr == '%' && *(ptr + 1) == '5' &&  *(ptr + 2) == 'E') {
 
140
                ptr += 3;
 
141
                sscanf(ptr, "%lu-%llu-%lx-%lu", &tab_id, &blob_id, &auth_code, &server_id);
 
142
                type = BS_URL_NEW_TYPE_REPO;
 
143
        }
 
144
        else if (isdigit(*ptr)) {
 
145
                sscanf(ptr, "%lu-%llu-%lx-%lu", &tab_id, &blob_id, &auth_code, &server_id);
 
146
                type = BS_URL_NEW_TYPE_BLOB;
 
147
        }
 
148
        else {
 
149
                sscanf(ptr, "%c%lu-%llu-%lx-%lu", &type, &tab_id, &blob_id, &auth_code, &server_id);
 
150
                switch (type) {
 
151
                        case BS_URL_TYPE_BLOB:
 
152
                                type = BS_URL_NEW_TYPE_BLOB;
 
153
                                break;
 
154
                        case BS_URL_TYPE_REPO:
 
155
                                type = BS_URL_NEW_TYPE_REPO;
 
156
                                break;
 
157
                        case BS_URL_NEW_TYPE_BLOB:
 
158
                        case BS_URL_NEW_TYPE_REPO:
 
159
                                break;
 
160
                        default:
 
161
                                return false;
 
162
                }
 
163
        }
 
164
        blob->bu_type = type;
 
165
        blob->bu_tab_id = tab_id;
 
166
        blob->bu_blob_id = blob_id;
 
167
        blob->bu_auth_code = auth_code;
 
168
        blob->bu_server_id = server_id;
 
169
        return *blob->bu_database && tab_id && blob_id;
 
170
 
 
171
        deprecated_url:
 
172
        char buffer[CS_EXC_MESSAGE_SIZE];
 
173
 
 
174
        cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Deprecated URL format: ");
 
175
        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, url);
 
176
        cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, ", please update the URLs in this table using: UPDATE blob_table SET blob_col = REPLACE(REPLACE(blob_col, '~>', '~*'), '/:', '/~')");
 
177
        CSException::throwException(CS_CONTEXT, BS_ERR_INCORRECT_URL, buffer);
 
178
}
 
179
 
 
180
const char *bs_version()
 
181
{
 
182
        return VERSION;
 
183
}
 
184