1 /*****************************************************************************/
2 /* Software Testing Automation Framework (STAF) */
3 /* (C) Copyright IBM Corp. 2001 */
4 /* */
5 /* This software is licensed under the Eclipse Public License (EPL) V1.0. */
6 /*****************************************************************************/
7
8 #ifndef STAF_FileSystemInlImpl
9 #define STAF_FileSystemInlImpl
10
11 #include "STAF.h"
12 #include "STAFFileSystem.h"
13 #include "STAFString.h"
14 #include "STAFTimestamp.h"
15 #include "STAFRefPtr.h"
16 #include "STAFException.h"
17 #include <deque>
18
19
20 /**************************/
21 /* STAFFSPath definitions */
22 /**************************/
23
STAFFSPath()24 STAF_INLINE STAFFSPath::STAFFSPath() : fPathUpToDate(1), fPiecesUpToDate(1)
25 { /* Do Nothing */ }
26
27
STAFFSPath(const STAFString & path)28 STAF_INLINE STAFFSPath::STAFFSPath(const STAFString &path)
29 : fPathUpToDate(1), fPiecesUpToDate(1), fPath(path)
30 {
31 updatePieces();
32 }
33
34
setRoot(const STAFString & root)35 STAF_INLINE STAFFSPath &STAFFSPath::setRoot(const STAFString &root)
36 {
37 fRoot = root;
38 fPathUpToDate = 0;
39 return *this;
40 }
41
42
addDir(const STAFString & dir)43 STAF_INLINE STAFFSPath &STAFFSPath::addDir(const STAFString &dir)
44 {
45 fDirs.push_back(dir);
46 fPathUpToDate = 0;
47 return *this;
48 }
49
50
setName(const STAFString & name)51 STAF_INLINE STAFFSPath &STAFFSPath::setName(const STAFString &name)
52 {
53 fName = name;
54 fPathUpToDate = 0;
55 return *this;
56 }
57
58
setExtension(const STAFString & extension)59 STAF_INLINE STAFFSPath &STAFFSPath::setExtension(const STAFString &extension)
60 {
61 fExtension = extension;
62 fPathUpToDate = 0;
63 return *this;
64 }
65
66
clearDirList()67 STAF_INLINE STAFFSPath &STAFFSPath::clearDirList()
68 {
69 fDirs = std::deque<STAFString>();
70 fPathUpToDate = 0;
71 return *this;
72 }
73
74
asString() const75 STAF_INLINE STAFString STAFFSPath::asString() const
76 {
77 if (!fPathUpToDate) const_cast<STAFFSPath *>(this)->updatePath();
78
79 return fPath;
80 }
81
82
root() const83 STAF_INLINE STAFString STAFFSPath::root() const
84 {
85 if (!fPiecesUpToDate) const_cast<STAFFSPath *>(this)->updatePieces();
86
87 return fRoot;
88 }
89
90
numDirs() const91 STAF_INLINE unsigned int STAFFSPath::numDirs() const
92 {
93 if (!fPiecesUpToDate) const_cast<STAFFSPath *>(this)->updatePieces();
94
95 return fDirs.size();
96 }
97
98
dir(unsigned int index) const99 STAF_INLINE STAFString STAFFSPath::dir(unsigned int index) const
100 {
101 if (!fPiecesUpToDate) const_cast<STAFFSPath *>(this)->updatePieces();
102
103 if (index >= fDirs.size())
104 {
105 STAFString message(STAFString("STAFFSPath::dir(") + index +
106 STAFString(" of ") + fDirs.size());
107 STAFOutOfBoundsException error(message.toCurrentCodePage()->buffer());
108 THROW_STAF_EXCEPTION(error);
109 }
110
111 return fDirs[index];
112 }
113
114
name() const115 STAF_INLINE STAFString STAFFSPath::name() const
116 {
117 if (!fPiecesUpToDate) const_cast<STAFFSPath *>(this)->updatePieces();
118
119 return fName;
120 }
121
122
extension() const123 STAF_INLINE STAFString STAFFSPath::extension() const
124 {
125 if (!fPiecesUpToDate) const_cast<STAFFSPath *>(this)->updatePieces();
126
127 return fExtension;
128 }
129
130
exists() const131 STAF_INLINE unsigned int STAFFSPath::exists() const
132 {
133 unsigned int doesExist = 0;
134 unsigned int osRC = 0;
135 STAFRC_t rc = STAFFSExists(asString().getImpl(), &doesExist, &osRC);
136
137 STAFFSException::checkRC(rc, "STAFFSExists", osRC);
138
139 return doesExist;
140 }
141
142
getEntry() const143 STAF_INLINE STAFFSEntryPtr STAFFSPath::getEntry() const
144 {
145 STAFFSEntry_t entry = 0;
146 unsigned int osRC = 0;
147 STAFRC_t rc = STAFFSGetEntry(asString().getImpl(), &entry, &osRC);
148
149 STAFFSException::checkRC(rc, "STAFFSGetEntry", osRC);
150
151 return STAFFSEntryPtr(new STAFFSEntry(entry), STAFFSEntryPtr::INIT);
152 }
153
154
getEntry(unsigned int * osRC) const155 STAF_INLINE STAFFSEntryRC STAFFSPath::getEntry(unsigned int *osRC) const
156 {
157 STAFFSEntry_t entry = 0;
158 STAFRC_t rc = STAFFSGetEntry(asString().getImpl(), &entry, osRC);
159 STAFFSEntryPtr entryPtr;
160
161 if (rc == kSTAFOk)
162 entryPtr = STAFFSEntryPtr(new STAFFSEntry(entry), STAFFSEntryPtr::INIT);
163
164 return STAFFSEntryRC(rc, entryPtr);
165 }
166
167
createDirectory(STAFFSDirectoryCreateMode_t mode) const168 STAF_INLINE STAFFSEntryPtr STAFFSPath::createDirectory(
169 STAFFSDirectoryCreateMode_t mode) const
170 {
171 unsigned int osRC = 0;
172 STAFRC_t rc = STAFFSCreateDirectory(asString().getImpl(), mode, &osRC);
173
174 STAFFSException::checkRC(rc, "STAFFSCreateDirectory", osRC);
175
176 return getEntry();
177 }
178
179
createDirectory(unsigned int * osRC,STAFFSDirectoryCreateMode_t mode) const180 STAF_INLINE STAFFSEntryRC STAFFSPath::createDirectory(unsigned int *osRC,
181 STAFFSDirectoryCreateMode_t mode) const
182 {
183 STAFRC_t rc = STAFFSCreateDirectory(asString().getImpl(), mode, osRC);
184
185 if (rc == kSTAFOk) return getEntry(osRC);
186
187 return STAFFSEntryRC(rc, STAFFSEntryPtr());
188 }
189
190
updatePieces()191 STAF_INLINE void STAFFSPath::updatePieces()
192 {
193 STAFString_t theRoot = 0;
194 STAFString_t theName = 0;
195 STAFString_t theExtension = 0;
196 STAFString_t *theDirs = 0;
197 unsigned int numDirs = 0;
198 STAFRC_t rc = STAFFSDisassemblePath(fPath.getImpl(), &theRoot, &numDirs,
199 &theDirs, &theName, &theExtension);
200
201 STAFFSException::checkRC(rc, "STAFFSDisassemblePath");
202
203 fRoot = STAFString(theRoot, STAFString::kShallow);
204 fName = STAFString(theName, STAFString::kShallow);
205 fExtension = STAFString(theExtension, STAFString::kShallow);
206 fDirs = std::deque<STAFString>();
207
208 for (unsigned int i = 0; i < numDirs; ++i)
209 fDirs.push_back(STAFString(theDirs[i], STAFString::kShallow));
210
211 STAFFSFreePathDirs(theDirs);
212
213 fPiecesUpToDate = 1;
214 }
215
216
updatePath()217 STAF_INLINE void STAFFSPath::updatePath()
218 {
219 STAFStringConst_t *theDirs = new STAFStringConst_t[fDirs.size()];
220 STAFRefPtr<STAFStringConst_t> theDirsPtr =
221 STAFRefPtr<STAFStringConst_t>(theDirs,
222 STAFRefPtr<STAFStringConst_t>::INIT,
223 STAFRefPtr<STAFStringConst_t>::ARRAY);
224
225 int i = 0;
226
227 for (std::deque<STAFString>::iterator iter = fDirs.begin();
228 iter != fDirs.end(); ++iter)
229 { theDirs[i++] = iter->getImpl(); }
230
231 STAFString_t thePath = 0;
232 STAFRC_t rc = STAFFSAssemblePath(&thePath, fRoot.getImpl(), i, theDirs,
233 fName.getImpl(), fExtension.getImpl());
234
235 STAFFSException::checkRC(rc, "STAFFSAssemblePath");
236
237 fPath = STAFString(thePath, STAFString::kShallow);
238 fPathUpToDate = 1;
239 }
240
241
242 /***************************/
243 /* STAFFSEntry definitions */
244 /***************************/
245
path() const246 STAF_INLINE STAFFSPath STAFFSEntry::path() const
247 {
248 unsigned int osRC = 0;
249 STAFStringConst_t pathString;
250 STAFRC_t rc = STAFFSEntryGetPathString(fEntry, &pathString, &osRC);
251
252 STAFFSException::checkRC(rc, "STAFFSEntryGetPathString", osRC);
253
254 return STAFFSPath(pathString);
255 }
256
257
type() const258 STAF_INLINE STAFFSEntryType_t STAFFSEntry::type() const
259 {
260 unsigned int osRC = 0;
261 STAFFSEntryType_t theType = kSTAFFSFile;
262 STAFRC_t rc = STAFFSEntryGetType(fEntry, &theType, &osRC);
263
264 STAFFSException::checkRC(rc, "STAFFSEntryGetType", osRC);
265
266 return theType;
267 }
268
269
size() const270 STAF_INLINE STAFFSEntry::FileSize STAFFSEntry::size() const
271 {
272 unsigned int osRC = 0;
273 unsigned int upperSize = 0;
274 unsigned int lowerSize = 0;
275 STAFRC_t rc = STAFFSEntryGetSize(fEntry, &upperSize, &lowerSize, &osRC);
276
277 STAFFSException::checkRC(rc, "STAFFSEntryGetSize", osRC);
278
279 return std::make_pair(upperSize, lowerSize);
280 }
281
282
size64() const283 STAF_INLINE STAFUInt64_t STAFFSEntry::size64() const
284 {
285 unsigned int osRC = 0;
286 STAFUInt64_t size = 0;
287 STAFRC_t rc = STAFFSEntryGetSize64(fEntry, &size, &osRC);
288
289 STAFFSException::checkRC(rc, "STAFFSEntryGetSize", osRC);
290
291 return size;
292 }
293
294
modTime() const295 STAF_INLINE STAFTimestamp STAFFSEntry::modTime() const
296 {
297 unsigned int osRC = 0;
298 time_t modTime = 0;
299 STAFRC_t rc = STAFFSEntryGetModTime(fEntry, &modTime, &osRC);
300
301 STAFFSException::checkRC(rc, "STAFFSEntryGetModTime", osRC);
302
303 return modTime;
304 }
305
306
isLink() const307 STAF_INLINE unsigned int STAFFSEntry::isLink() const
308 {
309 unsigned int osRC = 0;
310 unsigned int isALink = 0;
311 STAFRC_t rc = STAFFSEntryGetIsLink(fEntry, &isALink, &osRC);
312
313 STAFFSException::checkRC(rc, "STAFFSEntryGetIsLink", osRC);
314
315 return isALink;
316 }
317
318
linkTarget() const319 STAF_INLINE STAFString STAFFSEntry::linkTarget() const
320 {
321 unsigned int osRC = 0;
322 STAFString_t linkTargetString;
323
324 STAFRC_t rc = STAFFSEntryGetLinkTarget(fEntry, &linkTargetString, &osRC);
325
326 STAFFSException::checkRC(rc, "STAFFSEntryGetLinkTarget", osRC);
327
328 return STAFString(linkTargetString);
329 }
330
331
readLock()332 STAF_INLINE void STAFFSEntry::readLock()
333 {
334 unsigned int osRC = 0;
335 STAFRC_t rc = STAFFSEntryReadLock(fEntry, &osRC);
336
337 STAFFSException::checkRC(rc, "STAFFSEntryReadLock", osRC);
338 }
339
340
readUnlock()341 STAF_INLINE void STAFFSEntry::readUnlock()
342 {
343 unsigned int osRC = 0;
344 STAFRC_t rc = STAFFSEntryReadUnlock(fEntry, &osRC);
345
346 STAFFSException::checkRC(rc, "STAFFSEntryReadUnlock", osRC);
347 }
348
349
writeLock()350 STAF_INLINE void STAFFSEntry::writeLock()
351 {
352 unsigned int osRC = 0;
353 STAFRC_t rc = STAFFSEntryWriteLock(fEntry, &osRC);
354
355 STAFFSException::checkRC(rc, "STAFFSEntryWriteLock", osRC);
356 }
357
358
writeUnlock()359 STAF_INLINE void STAFFSEntry::writeUnlock()
360 {
361 unsigned int osRC = 0;
362 STAFRC_t rc = STAFFSEntryWriteUnlock(fEntry, &osRC);
363
364 STAFFSException::checkRC(rc, "STAFFSEntryWriteUnlock", osRC);
365 }
366
367
remove()368 STAF_INLINE void STAFFSEntry::remove()
369 {
370 unsigned int osRC = 0;
371 STAFRC_t rc = STAFFSDeleteEntry(fEntry, &osRC);
372
373 STAFFSException::checkRC(rc, "STAFFSDeleteEntry", osRC);
374 }
375
376
remove(unsigned int * osRC)377 STAF_INLINE STAFRC_t STAFFSEntry::remove(unsigned int *osRC)
378 {
379 return STAFFSDeleteEntry(fEntry, osRC);
380 }
381
382
copy(const STAFString & toName)383 STAF_INLINE void STAFFSEntry::copy(const STAFString &toName)
384 {
385 unsigned int osRC = 0;
386 STAFRC_t rc = STAFFSCopyEntry(fEntry, toName.getImpl(), &osRC);
387
388 STAFFSException::checkRC(rc, "STAFFSCopyEntry", osRC);
389 }
390
391
move(const STAFString & toName)392 STAF_INLINE void STAFFSEntry::move(const STAFString &toName)
393 {
394 unsigned int osRC = 0;
395
396 STAFRC_t rc = STAFFSMoveEntry(fEntry, toName.getImpl(), &osRC);
397
398 STAFFSException::checkRC(rc, "STAFFSMoveEntry", osRC);
399 }
400
401
rename(const STAFString & toName)402 STAF_INLINE void STAFFSEntry::rename(const STAFString &toName)
403 {
404 unsigned int osRC = 0;
405
406 STAFRC_t rc = STAFFSRenameEntry(fEntry, toName.getImpl(), &osRC);
407
408 STAFFSException::checkRC(rc, "STAFFSRenameEntry", osRC);
409 }
410
411
enumerate(const STAFString & namePattern,const STAFString & extPattern,STAFFSEntryType_t types,STAFFSSortBy_t sortBy,STAFFSCaseSensitive_t casing) const412 STAF_INLINE STAFFSEnumPtr STAFFSEntry::enumerate(const STAFString &namePattern,
413 const STAFString &extPattern, STAFFSEntryType_t types,
414 STAFFSSortBy_t sortBy, STAFFSCaseSensitive_t casing) const
415 {
416 STAFFSEnumHandle_t handle = 0;
417 unsigned int osRC = 0;
418 STAFRC_t rc = STAFFSEnumOpen(&handle, fEntry, namePattern.getImpl(),
419 extPattern.getImpl(), casing, types, sortBy,
420 &osRC);
421
422 STAFFSException::checkRC(rc, "STAFFSEnumOpen", osRC);
423
424 return STAFFSEnumPtr(new STAFFSEnumeration(handle), STAFFSEnumPtr::INIT);
425 }
426
427
getImpl() const428 STAF_INLINE STAFFSEntry_t STAFFSEntry::getImpl() const
429 {
430 return fEntry;
431 }
432
433
adoptImpl()434 STAF_INLINE STAFFSEntry_t STAFFSEntry::adoptImpl()
435 {
436 STAFFSEntry_t tempEntry = fEntry;
437 fEntry = 0;
438 return tempEntry;
439 }
440
441
~STAFFSEntry()442 STAF_INLINE STAFFSEntry::~STAFFSEntry()
443 {
444 STAFFSFreeEntry(&fEntry);
445 }
446
447
448 /*********************************/
449 /* STAFFSEnumeration definitions */
450 /*********************************/
451
STAFFSEnumeration(STAFFSEnumHandle_t handle)452 STAF_INLINE STAFFSEnumeration::STAFFSEnumeration(STAFFSEnumHandle_t handle)
453 : fHandle(handle), fIsValid(1)
454 { next(); }
455
isValid() const456 STAF_INLINE unsigned int STAFFSEnumeration::isValid() const
457 { return fIsValid; }
458
entry() const459 STAF_INLINE STAFFSEntryPtr STAFFSEnumeration::entry() const
460 { return fCurrEntry; }
461
next()462 STAF_INLINE STAFFSEnumeration &STAFFSEnumeration::next()
463 {
464 STAFFSEntry_t entry = 0;
465 unsigned int osRC = 0;
466 STAFRC_t rc = STAFFSEnumNext(fHandle, &entry, &osRC);
467
468 if ((rc == 0) && (entry == 0))
469 {
470 fIsValid = 0;
471 fCurrEntry = STAFFSEntryPtr();
472 }
473 else
474 {
475 STAFFSException::checkRC(rc, "STAFFSDirectoryEnumNext", osRC);
476 fCurrEntry = STAFFSEntryPtr(new STAFFSEntry(entry),
477 STAFFSEntryPtr::INIT);
478 }
479
480 return *this;
481 }
482
~STAFFSEnumeration()483 STAF_INLINE STAFFSEnumeration::~STAFFSEnumeration()
484 {
485 STAFFSEnumClose(&fHandle, 0);
486 }
487
488
489 /******************************************/
490 /* STAFFSCurrentDirectoryLock definitions */
491 /******************************************/
492
STAFFSCurrentDirectoryLock()493 STAF_INLINE STAFFSCurrentDirectoryLock::STAFFSCurrentDirectoryLock()
494 {
495 STAFRC_t rc = STAFFSRequestCurrentDirectoryLock();
496
497 STAFFSException::checkRC(rc, "STAFFSRequestCurrentDirectoryLock");
498 }
499
~STAFFSCurrentDirectoryLock()500 STAF_INLINE STAFFSCurrentDirectoryLock::~STAFFSCurrentDirectoryLock()
501 {
502 STAFFSReleaseCurrentDirectoryLock();
503 }
504
505
506 /******************************/
507 /* STAFFileSystem definitions */
508 /******************************/
509
getInfo(STAFFSInfoType_t type)510 STAF_INLINE STAFString STAFFileSystem::getInfo(STAFFSInfoType_t type)
511 {
512 STAFString_t info = 0;
513 STAFRC_t rc = STAFFSInfo(&info, type);
514
515 STAFFSException::checkRC(rc, "STAFFSInfo");
516
517 return STAFString(info, STAFString::kShallow);
518 }
519
matchesWildcards(const STAFString & stringToCheck,const STAFString & wildcardString,STAFFSCaseSensitive_t sensitive)520 STAF_INLINE unsigned int STAFFileSystem::matchesWildcards(
521 const STAFString &stringToCheck, const STAFString &wildcardString,
522 STAFFSCaseSensitive_t sensitive)
523 {
524 unsigned int matches= 0;
525 STAFRC_t rc = STAFFSStringMatchesWildcards(stringToCheck.getImpl(),
526 wildcardString.getImpl(),
527 sensitive, &matches);
528
529 STAFFSException::checkRC(rc, "STAFFSStringMatchesWildcards");
530
531 return matches;
532 }
533
534
comparePaths(const STAFString & path1,const STAFString & path2,STAFFSCaseSensitive_t sensitive)535 STAF_INLINE STAFFSComparePathResult_t STAFFileSystem::comparePaths(
536 const STAFString &path1, const STAFString &path2,
537 STAFFSCaseSensitive_t sensitive)
538 {
539 STAFFSComparePathResult_t result;
540 STAFRC_t rc = STAFFSComparePaths(path1.getImpl(), path2.getImpl(),
541 sensitive, &result);
542
543 STAFFSException::checkRC(rc, "STAFFSComparePaths");
544
545 return result;
546 }
547
548
getCurrentDirectory()549 STAF_INLINE STAFString STAFFileSystem::getCurrentDirectory()
550 {
551 STAFString_t currDir = 0;
552 unsigned int osRC = 0;
553 STAFRC_t rc = STAFFSGetCurrentDirectory(&currDir, &osRC);
554
555 STAFFSException::checkRC(rc, "STAFFSGetCurrentDirectory", osRC);
556
557 return STAFString(currDir, STAFString::kShallow);
558 }
559
560
setCurrentDirectory(const STAFString & dirName)561 STAF_INLINE void STAFFileSystem::setCurrentDirectory(const STAFString &dirName)
562 {
563 unsigned int osRC = 0;
564 STAFRC_t rc = STAFFSSetCurrentDirectory(dirName.getImpl(), &osRC);
565
566 STAFFSException::checkRC(rc, "STAFFSSetCurrentDirectory", osRC);
567 }
568
569 #endif
570