1 mike 1.2 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 mike 1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 #include "Dir.h"
35 #include "InternalException.h"
36
37 #include <iostream>
|
38 kumpf 1.7 #include <errno.h>
|
39 mike 1.2
40 PEGASUS_NAMESPACE_BEGIN
41
42 // Clone the string to a plain old C-String and null out
43 // trailing slash (if any).
44
45 static CString _clonePath(const String& path)
46 {
47 String clone = path;
48
49 if (clone.size() && clone[clone.size()-1] == '/')
50 clone.remove(clone.size()-1);
51
52 return clone.getCString();
53 }
54
55 Dir::Dir(const String& path)
56 : _path(path)
57 {
|
58 ouyang.jian 1.4 _dirRep.dir = opendir(_clonePath(_path));
|
59 mike 1.2
60 if (_dirRep.dir)
61 {
|
62 kumpf 1.7 #if defined(PEGASUS_OS_ZOS) || defined(PEGASUS_OS_VMS)
63 // ATTN: DO NOT REMOVE THE errno = 0 assignment.
|
64 marek 1.6 // Reason: On some platforms readdir_r is a wrapper around
65 // readdir. Without errno set to 0, readdir reports a bad return
66 // code even in the case that just the end of directory was reached.
|
67 kumpf 1.7 errno = 0;
68 #endif
|
69 kumpf 1.3 // Need to use readdir_r since we are multithreaded
70 if (readdir_r(_dirRep.dir, &_dirRep.buffer, &_dirRep.entry) != 0)
|
71 mike 1.2 {
|
72 kumpf 1.3 _more = false;
|
73 mike 1.2 closedir(_dirRep.dir);
|
74 kumpf 1.3 throw CannotOpenDirectory(_path);
|
75 mike 1.2 }
|
76 kumpf 1.3 _more = _dirRep.entry != NULL;
|
77 mike 1.2 }
78 else
79 {
|
80 kumpf 1.3 _more = false;
81 throw CannotOpenDirectory(_path);
|
82 mike 1.2 }
83 }
84
85 Dir::~Dir()
86 {
87 if (_dirRep.dir)
|
88 kumpf 1.3 closedir(_dirRep.dir);
|
89 mike 1.2
90 }
91
92
93 const char* Dir::getName() const
94 {
95 return _more ? _dirRep.entry->d_name : "";
96 }
97
98 void Dir::next()
99 {
100 if (_more)
101 {
|
102 kumpf 1.7 #if defined(PEGASUS_OS_ZOS) || defined(PEGASUS_OS_VMS)
103 // ATTN: DO NOT REMOVE THE errno = 0 assignment.
|
104 marek 1.6 // Reason: On some platforms readdir_r is a wrapper around
105 // readdir. Without errno set to 0, readdir reports a bad return
106 // code even in the case that just the end of directory was reached.
|
107 kumpf 1.7 errno = 0;
108 #endif
|
109 kumpf 1.3 // Need to use readdir_r since we are multithreaded
|
110 kumpf 1.5 if (readdir_r(_dirRep.dir, &_dirRep.buffer, &_dirRep.entry) != 0)
|
111 mike 1.2 {
|
112 kumpf 1.3 _more = false;
113 throw CannotOpenDirectory(_path);
|
114 mike 1.2 }
|
115 kumpf 1.3 _more = _dirRep.entry != NULL;
|
116 mike 1.2 }
117 }
118
119 PEGASUS_NAMESPACE_END
|