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