103544bf0SAugustin Cavalier/*
203544bf0SAugustin Cavalier * Copyright 2005-2008, Ingo Weinhold, ingo_weinhold@gmx.de.
303544bf0SAugustin Cavalier * Distributed under the terms of the MIT License.
403544bf0SAugustin Cavalier */
503544bf0SAugustin Cavalier// no header guards: must be included at appropriate part of .cpp
603544bf0SAugustin Cavalier
703544bf0SAugustin Cavalier
803544bf0SAugustin Cavalierclass LocalFD {
903544bf0SAugustin Cavalierpublic:
1003544bf0SAugustin Cavalier	LocalFD()
1103544bf0SAugustin Cavalier	{
1203544bf0SAugustin Cavalier	}
1303544bf0SAugustin Cavalier
1403544bf0SAugustin Cavalier	~LocalFD()
1503544bf0SAugustin Cavalier	{
1603544bf0SAugustin Cavalier	}
1703544bf0SAugustin Cavalier
1803544bf0SAugustin Cavalier	status_t Init(int fd)
1903544bf0SAugustin Cavalier	{
2003544bf0SAugustin Cavalier#ifndef BUILDING_FS_SHELL
2103544bf0SAugustin Cavalier		Descriptor* descriptor = get_descriptor(fd);
2203544bf0SAugustin Cavalier		if (descriptor && !descriptor->IsSystemFD()) {
2303544bf0SAugustin Cavalier			// we need to get a path
2403544bf0SAugustin Cavalier			fFD = -1;
2503544bf0SAugustin Cavalier			return descriptor->GetPath(fPath);
2603544bf0SAugustin Cavalier		}
2703544bf0SAugustin Cavalier#endif
2803544bf0SAugustin Cavalier
2903544bf0SAugustin Cavalier		fFD = fd;
3003544bf0SAugustin Cavalier		fPath = "";
3103544bf0SAugustin Cavalier		return B_OK;
3203544bf0SAugustin Cavalier	}
3303544bf0SAugustin Cavalier
3403544bf0SAugustin Cavalier	int FD() const
3503544bf0SAugustin Cavalier	{
3603544bf0SAugustin Cavalier		return fFD;
3703544bf0SAugustin Cavalier	}
3803544bf0SAugustin Cavalier
3903544bf0SAugustin Cavalier	const char* Path() const
4003544bf0SAugustin Cavalier	{
4103544bf0SAugustin Cavalier		return (fFD < 0 ? fPath.c_str() : NULL);
4203544bf0SAugustin Cavalier	}
4303544bf0SAugustin Cavalier
4403544bf0SAugustin Cavalier	bool IsSymlink() const
4503544bf0SAugustin Cavalier	{
4603544bf0SAugustin Cavalier		struct stat st;
4703544bf0SAugustin Cavalier		int result;
4803544bf0SAugustin Cavalier		if (Path())
4903544bf0SAugustin Cavalier			result = lstat(Path(), &st);
5003544bf0SAugustin Cavalier		else
5103544bf0SAugustin Cavalier			result = fstat(fFD, &st);
5203544bf0SAugustin Cavalier
5303544bf0SAugustin Cavalier		return (result == 0 && S_ISLNK(st.st_mode));
5403544bf0SAugustin Cavalier	}
5503544bf0SAugustin Cavalier
5603544bf0SAugustin Cavalierprivate:
5703544bf0SAugustin Cavalier	string	fPath;
5803544bf0SAugustin Cavalier	int		fFD;
5903544bf0SAugustin Cavalier};