-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathdm.c
More file actions
256 lines (219 loc) · 6.34 KB
/
Copy pathdm.c
File metadata and controls
256 lines (219 loc) · 6.34 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* Copyright (C) 2014 InfiniDB, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
/**
* $Id: dm.c 414 2011-02-10 14:23:02Z rdempsey $
*
* @file dm.c
* @author Robert King <rking@calpont.com>
*
* @brief Implementation file for the Disk Manager (dm) Library.
*
* This file contains the implementation for the following Disk Manager
* Library functions:
*
* - dmOid2FPath()
* - _doDir()
* - _doFile()
*/
#include <stdlib.h>
#include <stdio.h>
#include "dm.h"
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
#define STATIC static
STATIC int _doDir(char* pBuffer, int blen, unsigned int val); //dmc-Branch
STATIC int _doFile(char* pBuffer, int blen, unsigned char val);
/** @brief Converts an OID into a group if directories and a filename.
*
*/
int dmFPath2Oid(dmFilePathArgs_t* pArgs, UINT32* OID)
{
unsigned part;
*OID = 0;
part = atoi(pArgs->pDirA);
if (part > 255) return -1;
*OID |= (part << 24);
part = atoi(pArgs->pDirB);
if (part > 255) return -1;
*OID |= (part << 16);
part = atoi(pArgs->pDirC);
if (part > 255) return -1;
*OID |= (part << 8);
part = atoi(&pArgs->pFName[4]);
if (part > 255) return -1;
*OID |= (part << 0);
return 0;
}
/** @brief Converts an OID into a group if directories and a filename.
*
* This function takes a 32-bit Object ID (OID). If DLen is 0, then the
* OID is converted into 3 hierarchical directory names and a filename.
* If DLen is >0 then the OID is converted into 5 hierarchical directory
* names and a filename (using the partition and segment as additional
* input into the filepath. The actual location
* of the file is <DBRoot>/<DirA>/<DirB>/<DirC>/<FName>, or
* <DBRoot>/<DirA>/<DirB>/<DirC>/<DirD>/<part#>/<segFName>. The <DBRoot>
* entry must be pre-pended by the calling application after calling
* this function. The value for <DBRoot> is stored in the Calpont.xml
* configuration file.
*
* @param oid INPUT -- The Object Id.
* @param partition INPUT -- partition to be included in filepath.
* @param segment INPUT -- segment to be included in filepath.
* @param dmFilePathArgs* I/O -- Points to a buffer structure
*
* @return 0 if everything went OK. -1 if an error occured. Two
* kinds of errors are possible:
*
* - a null pointer was passed in
* - truncation occured.
*
* If a null buffer pointer is passed in, a return code
* of -1 will be returned FOR THAT BUFFER.
*
* Truncation can occur if the buffer length specified in
* dmFilePathArgs is too small.
*
* If a buffer's return code is not zero, the appropriate
* return code in dmfilePathArgs can be examined. If a
* buffer's return code is be less than zero, the
* corresponding buffer pointer was NULL. If it is greater
* or equal to the buffer's length argument, length is too small
*.
*/
int dmOid2FPath(UINT32 oid, UINT32 partition, UINT32 segment,
dmFilePathArgs_t* pArgs) //dmc-Branch
{
pArgs->Arc = _doDir(
pArgs->pDirA,
pArgs->ALen,
(unsigned int)oid>>24);
pArgs->Brc = _doDir(
pArgs->pDirB,
pArgs->BLen,
(unsigned int)(oid&0x00ff0000)>>16);
pArgs->Crc = _doDir(
pArgs->pDirC,
pArgs->CLen,
(unsigned int)(oid&0x0000ff00)>>8);
//dmc-Branch-Begin
// include partition and segment number in the file path if they are present
if (pArgs->DLen > 0)
{
pArgs->Drc = _doDir(
pArgs->pDirD,
pArgs->DLen,
(unsigned int)(oid&0x000000ff));
pArgs->Erc = _doDir(
pArgs->pDirE,
pArgs->ELen,
partition);
pArgs->FNrc = _doFile(
pArgs->pFName,
pArgs->FNLen,
segment);
if (
(pArgs->Drc < 0) ||
(pArgs->Erc < 0)
)
return -1;
if (
(pArgs->Drc >= pArgs->ALen) ||
(pArgs->Erc >= pArgs->ALen)
)
return -1;
}
else
{
pArgs->FNrc = _doFile(
pArgs->pFName,
pArgs->FNLen,
(unsigned int)(oid&0x000000ff));
}
//dmc-Branch-End
if (
(pArgs->Arc < 0) ||
(pArgs->Brc < 0) ||
(pArgs->Crc < 0) ||
(pArgs->FNrc < 0)
)
return -1;
if (
(pArgs->Arc >= pArgs->ALen) ||
(pArgs->Brc >= pArgs->BLen) ||
(pArgs->Crc >= pArgs->CLen) ||
(pArgs->FNrc >= pArgs->FNLen)
)
return -1;
else
return 0;
}
/** @brief Formats a directory name.
*
* This function takes a 8-bit value and converts it into a directory
* name.
*
* @param pBuffer OUPUT - A pointer to the output buffer.
* @param blen INPUT - The length of the output buffer, in bytes.
* @param val INPUT - value to be used in the formatted name.
*
* @return The number of characters in the output buffer is returned
* (not including the NULL terminator). -1 of the input buffer
* pointer is NULL.
* @note If the input pointer is invalid (points to unmapped memoty,
* for example,) this function can raise a segmentation violation
* or a bus error.
*
*/
STATIC int _doDir(char* pBuffer, int blen, unsigned int val) //dmc-Branch
{
int rc;
if (!pBuffer)
{
rc = -1;
} else {
rc = snprintf(pBuffer, blen, "%03u.dir", val); //dmc-Branch
pBuffer[blen-1] = (char)0;
}
return rc;
}
/** @brief Formats a file name.
*
* This function takes a 8-bit value and converts it into a file name.
*
* @param pBuffer OUPUT - A pointer to the output buffer.
* @param blen INPUT - The length of the output buffer, in bytes.
* @param val INPUT - value to be used in the formatted name.
*
* @return The number of characters in the output buffer is returned
* (not including the NULL terminator). -1 of the input buffer
* pointer is NULL.
* @note If the input pointer is invalid (points to unmapped memoty,
* for example,) this function can raise a segmentation violation
* or a bus error.
*
*/
STATIC int _doFile(char* pBuffer, int blen, unsigned char val)
{
int rc;
if (!pBuffer)
{
rc = -1;
} else {
rc = snprintf(pBuffer, blen, "FILE%03d.cdf", val);
pBuffer[blen-1] = (char)0;
}
return rc;
}