~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/shared/CLucene/util/dirent.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------------
 
2
* Copyright (C) 2003-2006 Matt J. Weinstein 
 
3
 
4
* Distributable under the terms of either the Apache License (Version 2.0) or 
 
5
* the GNU Lesser General Public License, as specified in the COPYING file.
 
6
------------------------------------------------------------------------------*/
 
7
#include "CLucene/_ApiHeader.h"
 
8
 
 
9
#if !defined(_CL_HAVE_DIRENT_H) && !defined(_CL_HAVE_SYS_NDIR_H) && !defined(_CL_HAVE_SYS_DIR_H) && !defined(_CL_HAVE_NDIR_H)
 
10
#include "dirent.h"
 
11
#include <sys/stat.h>
 
12
#include <errno.h>
 
13
 
 
14
 
 
15
DIR * 
 
16
opendir (const char *szPath)
 
17
{
 
18
        DIR *nd;
 
19
        char szFullPath[CL_MAX_PATH];
 
20
 
 
21
        errno = 0;
 
22
 
 
23
        if (!szPath)
 
24
        {
 
25
                errno = EFAULT;
 
26
                return NULL;
 
27
        }
 
28
 
 
29
        if (szPath[0] == '\0')
 
30
        {
 
31
                errno = ENOTDIR;
 
32
                return NULL;
 
33
        }
 
34
 
 
35
        /* Attempt to determine if the given path really is a directory. */
 
36
        struct cl_stat_t rcs;
 
37
        if ( fileStat(szPath,&rcs) == -1)
 
38
        {
 
39
                /* call GetLastError for more error info */
 
40
                errno = ENOENT;
 
41
                return NULL;
 
42
        }
 
43
        if (!(rcs.st_mode & _S_IFDIR))
 
44
        {
 
45
                /* Error, entry exists but not a directory. */
 
46
                errno = ENOTDIR;
 
47
                return NULL;
 
48
        }
 
49
 
 
50
        /* Make an absolute pathname.  */
 
51
        _realpath(szPath,szFullPath);
 
52
 
 
53
        /* Allocate enough space to store DIR structure and the complete
 
54
        * directory path given. */
 
55
        //nd = (DIR *) malloc (sizeof (DIR) + _tcslen (szFullPath) + _tcslen (DIRENT_SLASH) +
 
56
        //                                      _tcslen (DIRENT_SEARCH_SUFFIX)+1);
 
57
        nd = new DIR;
 
58
 
 
59
        if (!nd)
 
60
        {
 
61
                /* Error, out of memory. */
 
62
                errno = ENOMEM;
 
63
                return NULL;
 
64
        }
 
65
 
 
66
        /* Create the search expression. */
 
67
        strcpy (nd->dd_name, szFullPath);
 
68
 
 
69
        /* Add on a slash if the path does not end with one. */
 
70
        if (nd->dd_name[0] != '\0' &&
 
71
                nd->dd_name[strlen (nd->dd_name) - 1] != '/' &&
 
72
                nd->dd_name[strlen (nd->dd_name) - 1] != '\\')
 
73
        {
 
74
                strcat (nd->dd_name, DIRENT_SLASH);
 
75
        }
 
76
 
 
77
        /* Add on the search pattern */
 
78
        strcat (nd->dd_name, DIRENT_SEARCH_SUFFIX);
 
79
 
 
80
        /* Initialize handle to -1 so that a premature closedir doesn't try
 
81
        * to call _findclose on it. */
 
82
        nd->dd_handle = -1;
 
83
 
 
84
        /* Initialize the status. */
 
85
        nd->dd_stat = 0;
 
86
 
 
87
        /* Initialize the dirent structure. ino and reclen are invalid under
 
88
        * Win32, and name simply points at the appropriate part of the
 
89
        * findfirst_t structure. */
 
90
        //nd->dd_dir.d_ino = 0;
 
91
        //nd->dd_dir.d_reclen = 0;
 
92
        nd->dd_dir.d_namlen = 0;
 
93
        nd->dd_dir.d_name = nd->dd_dta.name;
 
94
 
 
95
        return nd;
 
96
}
 
97
 
 
98
 
 
99
struct dirent * readdir (DIR * dirp)
 
100
{
 
101
        errno = 0;
 
102
 
 
103
        /* Check for valid DIR struct. */
 
104
        if (!dirp)
 
105
        {
 
106
                errno = EFAULT;
 
107
                return NULL;
 
108
        }
 
109
 
 
110
        if (dirp->dd_dir.d_name != dirp->dd_dta.name)
 
111
        {
 
112
                /* The structure does not seem to be set up correctly. */
 
113
                errno = EINVAL;
 
114
                return NULL;
 
115
        }
 
116
 
 
117
        bool bCallFindNext = true;
 
118
 
 
119
        if (dirp->dd_stat < 0)
 
120
        {
 
121
                /* We have already returned all files in the directory
 
122
                * (or the structure has an invalid dd_stat). */
 
123
                return NULL;
 
124
        }
 
125
        else if (dirp->dd_stat == 0)
 
126
        {
 
127
                /* We haven't started the search yet. */
 
128
                /* Start the search */
 
129
                dirp->dd_handle = _findfirst (dirp->dd_name, &(dirp->dd_dta));
 
130
 
 
131
                if (dirp->dd_handle == -1)
 
132
                {
 
133
                        /* Whoops! Seems there are no files in that
 
134
                        * directory. */
 
135
                        dirp->dd_stat = -1;
 
136
                }
 
137
                else
 
138
                {
 
139
                        dirp->dd_stat = 1;
 
140
                }
 
141
 
 
142
                /* Dont call _findnext first time. */
 
143
                bCallFindNext = false;
 
144
        }
 
145
 
 
146
        while (dirp->dd_stat > 0)
 
147
        {
 
148
                if (bCallFindNext)
 
149
                {
 
150
                        /* Get the next search entry. */
 
151
                        if (_findnext (dirp->dd_handle, &(dirp->dd_dta)))
 
152
                        {
 
153
                                /* We are off the end or otherwise error. */
 
154
                                _findclose (dirp->dd_handle);
 
155
                                dirp->dd_handle = -1;
 
156
                                dirp->dd_stat = -1;                      
 
157
                                return NULL;
 
158
                        }
 
159
                        else
 
160
                        {
 
161
                                /* Update the status to indicate the correct
 
162
                                * number. */
 
163
                                dirp->dd_stat++;
 
164
                        }
 
165
                }
 
166
 
 
167
                /* Successfully got an entry. Everything about the file is
 
168
                * already appropriately filled in except the length of the
 
169
                * file name. */
 
170
                dirp->dd_dir.d_namlen = strlen (dirp->dd_dir.d_name);
 
171
 
 
172
                bool bThisFolderOrUpFolder = dirp->dd_dir.d_name[0] == '.' &&
 
173
                        (dirp->dd_dir.d_name[1] == 0 || (dirp->dd_dir.d_name[1] == '.' && dirp->dd_dir.d_name[2] == 0));
 
174
 
 
175
                if (!bThisFolderOrUpFolder)
 
176
                {
 
177
                        struct cl_stat_t buf;
 
178
                        char buffer[CL_MAX_DIR];
 
179
                        size_t bl = strlen(dirp->dd_name)-strlen(DIRENT_SEARCH_SUFFIX);
 
180
                        strncpy(buffer,dirp->dd_name,bl);
 
181
                        buffer[bl]=0;
 
182
                        strcat(buffer, dirp->dd_dir.d_name);     
 
183
                        if ( fileStat(buffer,&buf) == 0 )
 
184
                        {
 
185
                                /* Finally we have a valid entry. */
 
186
                                return &dirp->dd_dir;
 
187
                        }
 
188
                }
 
189
 
 
190
                /* Allow to find next file. */
 
191
                bCallFindNext = true;
 
192
        }
 
193
 
 
194
        return NULL;
 
195
}
 
196
 
 
197
 
 
198
 
 
199
int32_t
 
200
closedir (DIR * dirp)
 
201
{
 
202
        int32_t rc;
 
203
 
 
204
        errno = 0;
 
205
        rc = 0;
 
206
 
 
207
        if (!dirp)
 
208
        {
 
209
                errno = EFAULT;
 
210
                return -1;
 
211
        }
 
212
 
 
213
        if (dirp->dd_handle != -1)
 
214
        {
 
215
                rc = _findclose (dirp->dd_handle);
 
216
        }
 
217
 
 
218
        /* Delete the dir structure. */
 
219
        _CLVDELETE(dirp);
 
220
 
 
221
        return rc;
 
222
}
 
223
#endif //HAVE_DIRENT_H
 
224