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