1/*
2 * Copyright 2005-2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5// no header guards: must be included at appropriate part of .cpp
6
7
8class LocalFD {
9public:
10	LocalFD()
11	{
12	}
13
14	~LocalFD()
15	{
16	}
17
18	status_t Init(int fd)
19	{
20#ifndef BUILDING_FS_SHELL
21		Descriptor* descriptor = get_descriptor(fd);
22		if (descriptor && !descriptor->IsSystemFD()) {
23			// we need to get a path
24			fFD = -1;
25			return descriptor->GetPath(fPath);
26		}
27#endif
28
29		fFD = fd;
30		fPath = "";
31		return B_OK;
32	}
33
34	int FD() const
35	{
36		return fFD;
37	}
38
39	const char* Path() const
40	{
41		return (fFD < 0 ? fPath.c_str() : NULL);
42	}
43
44	bool IsSymlink() const
45	{
46		struct stat st;
47		int result;
48		if (Path())
49			result = lstat(Path(), &st);
50		else
51			result = fstat(fFD, &st);
52
53		return (result == 0 && S_ISLNK(st.st_mode));
54	}
55
56private:
57	string	fPath;
58	int		fFD;
59};
60