Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/cs/vcs_src/ifs_func.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
/*
21
* File: ifs_func.c
22
*/
23
24
#include "ifs_agent.h"
25
#include <ls.h>
26
27
struct DataEntry {
28
struct DataEntry *next;
29
char *fpath;
30
char *key;
31
void* data;
32
int len;
33
};
34
35
struct DataEntry *DataSpool;
36
37
/*
38
*name: MallocZero
39
* Allocation a block of memory and initiate to 0
40
* rtn: NULL if error
41
*/
42
void *
43
MallocZero( nSize )
44
int nSize;
45
{
46
void *pBuf;
47
48
if( nSize <= 0 )
49
return NULL;
50
pBuf = (void *)malloc( nSize );
51
if( pBuf == NULL )
52
return NULL;
53
memset( pBuf, 0, nSize );
54
return pBuf;
55
}
56
57
/*
58
*name: SecurityDataAccess
59
* read/write the security data
60
* rtn: the offset of file
61
*/
62
int
63
SecurityDataAccess( pos, data, len )
64
int pos;
65
void *data;
66
int len;
67
{
68
static int FileNum = 0;
69
char fname[ STRLEN ];
70
71
if( FileNum == 0 ) { /* create a security file */
72
sfsprintf( fname, sizeof(fname), "/tmp/ifsdata.%d", getpid() );
73
if( (FileNum = open( fname, O_RDWR|O_CREAT|O_TRUNC, 0600 )) < 0 )
74
return -1;
75
#ifndef DEBUG
76
unlink( fname );
77
#endif
78
write( FileNum, "ifs_data\n", 9 );
79
}
80
if( pos <= 0 ) { /* write security data */
81
pos = lseek( FileNum, 0, SEEK_END );
82
if( write( FileNum, data, len ) != len )
83
return -1;
84
} else {
85
if( lseek( FileNum, pos, SEEK_SET ) == -1 )
86
return -1;
87
if( read( FileNum, data, len ) != len )
88
return -1;
89
}
90
return pos;
91
}
92
93
/*
94
*name: DataEntryInsert
95
*/
96
int
97
DataEntryInsert( fpath, key, data, len )
98
char *fpath;
99
char *key;
100
void *data;
101
int len;
102
{
103
struct DataEntry *ent = DataSpool;
104
105
while( ent != NULL ) {
106
if( strcmp( ent->fpath, fpath ) == 0 &&
107
strcmp( ent->key, key ) == 0 ) {
108
free( ent->data );
109
break;
110
}
111
ent = ent->next;
112
}
113
if( ent == NULL ) {
114
ent = (struct DataEntry *) MallocZero( sizeof(*ent) );
115
ent->next = DataSpool;
116
DataSpool = ent;
117
ent->fpath = strdup( fpath );
118
ent->key = strdup( key );
119
}
120
ent->data = (void*)SecurityDataAccess( 0, data, len );
121
ent->len = len;
122
return 0;
123
}
124
125
/*
126
*name: DataEntryDelete
127
*/
128
int
129
DataEntryDelete( fpath, key )
130
char *fpath;
131
char *key;
132
{
133
struct DataEntry *ent = DataSpool;
134
struct DataEntry *last = NULL;
135
136
while( ent != NULL ) {
137
if( strcmp( ent->fpath, fpath ) == 0 &&
138
strcmp( ent->key, key ) == 0 ) {
139
if( last == NULL ) {
140
DataSpool = ent->next;
141
} else {
142
last->next = ent->next;
143
}
144
free( ent->fpath );
145
free( ent->key );
146
free( ent->data );
147
free( ent );
148
return 0;
149
}
150
last = ent;
151
ent = ent->next;
152
}
153
return -1;
154
}
155
156
/*
157
*name: DataEntryQuery
158
*/
159
int
160
DataEntryQuery( fpath, key, buffer, bsize )
161
char *fpath;
162
char *key;
163
void *buffer;
164
int bsize;
165
{
166
struct DataEntry *ent = DataSpool;
167
168
while( ent != NULL ) {
169
if( strncmp( fpath, ent->fpath, strlen(ent->fpath) ) == 0 &&
170
strcmp( ent->key, key ) == 0 ) {
171
if( ent->len > bsize )
172
return -1;
173
SecurityDataAccess( integralof(ent->data), buffer, ent->len );
174
return ent->len;
175
}
176
ent = ent->next;
177
}
178
return -1;
179
}
180
181
/*
182
*name: DashF
183
* Test if the path is a regular file
184
* rtn: TRUE or FALSE
185
*/
186
int
187
DashF( pPath )
188
char *pPath;
189
{
190
struct stat St;
191
192
return( stat( pPath, &St ) == 0 && S_ISREG( St.st_mode ) );
193
}
194
195
/*
196
*name: DashD
197
* Test if the path is a directory
198
* rtn: TRUE or FALSE
199
*/
200
int
201
DashD( pPath )
202
char *pPath;
203
{
204
struct stat St;
205
206
return( stat( pPath, &St ) == 0 && S_ISDIR( St.st_mode ) );
207
}
208
209
/*
210
*Name: CopyFile
211
* Duplicate a file
212
* rtn: -1 if error, 0 if O.K.
213
*/
214
int
215
CopyFile( pSrc, pDst )
216
char *pSrc, *pDst;
217
{
218
char buf[ 1024 ];
219
int fs, fd, len;
220
221
if( (fs = open( pSrc, O_RDONLY, 0 )) < 0 )
222
return -1;
223
if( (fd = open( pDst, O_WRONLY|O_CREAT, 0644 )) > 0 ) {
224
while( (len = read( fs, buf, sizeof(buf) )) > 0 )
225
write( fd, buf, len );
226
close( fd );
227
}
228
close( fs );
229
return 0;
230
}
231
232
/*
233
*name: MakePath
234
* Recursive create the directories of a file.
235
*/
236
void
237
MakePath( fpath )
238
char *fpath;
239
{
240
char *ptr;
241
242
if( (ptr = strrchr( fpath, '/' )) != NULL ) {
243
*ptr = '\0';
244
if( !DashD( fpath ) ) {
245
MakePath( fpath );
246
mkdir( fpath, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH );
247
}
248
*ptr = '/';
249
}
250
}
251
252
/*
253
*name: MakeTmpFile
254
* Recursive find the valid directory for tmpfile
255
*/
256
void
257
MakeTmpFile( fpath, buf, bsize )
258
char *fpath;
259
char *buf;
260
int bsize;
261
{
262
char *ptr;
263
264
if( (ptr = strrchr( fpath, '/' )) != NULL ) {
265
*ptr = '\0';
266
if( DashD( fpath ) ) {
267
sfsprintf( buf, bsize, "%s/._tmp.%d", fpath, getpid() );
268
} else {
269
MakeTmpFile( fpath, buf, bsize );
270
}
271
*ptr = '/';
272
}
273
}
274
275
/*
276
*name: GetUserFile
277
*/
278
char *
279
GetUserFile( fname, buf, size )
280
char *fname;
281
char *buf;
282
int size;
283
{
284
sfsprintf( buf, size, "/tmp/ifs.%s", fmtuid(getuid()) );
285
mkdir( buf, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH );
286
strcat( buf, "/" );
287
strcat( buf, fname );
288
return buf;
289
}
290
291
/*
292
*name: logit
293
* Write the message to logfile
294
*/
295
void
296
logit( msg )
297
char *msg;
298
{
299
static int flog;
300
301
if( flog == 0 ) {
302
char logfile[ 256 ];
303
304
GetUserFile( "vcs.log", logfile, sizeof(logfile) );
305
flog = open( logfile, O_WRONLY|O_APPEND, 0600 );
306
}
307
if( flog > 0 ) {
308
write( flog, msg, strlen(msg) );
309
}
310
}
311
312
/*
313
*name: SplitFields
314
* Parse a text string and split into fields by tokens
315
* rtn: fields number
316
*/
317
int
318
SplitFields( arg, asize, str, tok )
319
char *arg[];
320
int asize;
321
char *str;
322
char tok;
323
{
324
char *ptr;
325
int n, fields;
326
327
fields = 0;
328
for( n = 0; n < asize; n++ ) {
329
arg[ n ] = str;
330
if( str != NULL ) {
331
fields++;
332
ptr = strchr( str, tok );
333
if( ptr != NULL ) *ptr++ = '\0';
334
str = ptr;
335
}
336
}
337
return fields;
338
}
339
340
/*
341
*name: MakeImageFile
342
* create an empty image file
343
*/
344
int
345
MakeImageFile( fname, size )
346
char *fname;
347
int size;
348
{
349
static time_t actime;
350
static time_t modtime;
351
int fd;
352
353
if( actime == 0 ) {
354
actime = modtime = cs.time - 86400 * (365 * 4 + 1);
355
}
356
fd = open( fname, O_WRONLY|O_CREAT|O_EXCL, 0600 );
357
if( fd > 0 ) {
358
write( fd, "-invalid-\n", 10 );
359
if( size > 10 ) {
360
lseek( fd, size-1, 0 );
361
write( fd, "\n", 1 );
362
}
363
close( fd );
364
touch( fname, actime, modtime, 1);
365
}
366
return 0;
367
}
368
369