summaryrefslogtreecommitdiff
path: root/lib/common/LinuxWorkaround.cpp
blob: 7900fa6e5b4b37b5e545c95518ecf3e86dc06630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// --------------------------------------------------------------------------
//
// File
//		Name:    LinuxWorkaround.cpp
//		Purpose: Workarounds for Linux
//		Created: 2003/10/31
//
// --------------------------------------------------------------------------

#include "Box.h"

#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>

#include <string>

#include "LinuxWorkaround.h"
#include "CommonException.h"

#include "MemLeakFindOn.h"

#ifdef PLATFORM_LINUX

// --------------------------------------------------------------------------
//
// Function
//		Name:    LinuxWorkaround_FinishDirentStruct(struct dirent *, const char *)
//		Purpose: Finishes off filling in a dirent structure, which Linux leaves incomplete.
//		Created: 2003/10/31
//
// --------------------------------------------------------------------------
void LinuxWorkaround_FinishDirentStruct(struct dirent *entry, const char *DirectoryName)
{
	// From man readdir under Linux:
	//
	// BUGS
    //   Field d_type is not implemented as  of  libc6  2.1  and  will  always  return
    //   DT_UNKNOWN (0).
	//
	// What kind of an OS is this?
	
	
	// Build filename of this entry
	std::string fn(DirectoryName);
	fn += '/';
	fn += entry->d_name;
	
	// Do a stat on it
	struct stat st;
	if(::lstat(fn.c_str(), &st) != 0)
	{
		THROW_EXCEPTION(CommonException, OSFileError)
	}
	
	// Fill in the d_type field.
	if(S_ISREG(st.st_mode))
	{
		entry->d_type = DT_REG;	
	}
	else if(S_ISDIR(st.st_mode))
	{
		entry->d_type = DT_DIR;
	}
	else if(S_ISLNK(st.st_mode))
	{
		entry->d_type = DT_LNK;
	}
	// otherwise leave it as we found it
}

#endif // PLATFORM_LINUX