libcamera: base: file_descriptor: Return UniqueFD from dup()
The dup() function returns a duplicate of the file descriptor. Wrapping it in a FileDescriptor isn't wrong as such, but it prevents from using it in contexts where a UniqueFD is needed. As the duplicate is guaranteed to have a single owner when created, return it as a UniqueFD instead. A FileDescriptor can easily be created from the UniqueFD if desired. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Hirokazu Honda <hiroh@chromium.org> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
parent
1d0dbc0da8
commit
fcf98514cb
2 changed files with 15 additions and 9 deletions
|
@ -28,7 +28,7 @@ public:
|
||||||
|
|
||||||
bool isValid() const { return fd_ != nullptr; }
|
bool isValid() const { return fd_ != nullptr; }
|
||||||
int fd() const { return fd_ ? fd_->fd() : -1; }
|
int fd() const { return fd_ ? fd_->fd() : -1; }
|
||||||
FileDescriptor dup() const;
|
UniqueFD dup() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class Descriptor
|
class Descriptor
|
||||||
|
|
|
@ -222,17 +222,23 @@ FileDescriptor &FileDescriptor::operator=(FileDescriptor &&other)
|
||||||
* \brief Duplicate a FileDescriptor
|
* \brief Duplicate a FileDescriptor
|
||||||
*
|
*
|
||||||
* Duplicating a FileDescriptor creates a duplicate of the wrapped file
|
* Duplicating a FileDescriptor creates a duplicate of the wrapped file
|
||||||
* descriptor and returns a new FileDescriptor instance that wraps the
|
* descriptor and returns a UniqueFD that owns the duplicate. The fd() function
|
||||||
* duplicate. The fd() function of the original and duplicate instances will
|
* of the original and the get() function of the duplicate will return different
|
||||||
* return different values. The duplicate instance will not be affected by
|
* values. The duplicate instance will not be affected by destruction of the
|
||||||
* destruction of the original instance or its copies.
|
* original instance or its copies.
|
||||||
*
|
*
|
||||||
* \return A new FileDescriptor instance wrapping a duplicate of the original
|
* \return A UniqueFD owning a duplicate of the original file descriptor
|
||||||
* file descriptor
|
|
||||||
*/
|
*/
|
||||||
FileDescriptor FileDescriptor::dup() const
|
UniqueFD FileDescriptor::dup() const
|
||||||
{
|
{
|
||||||
return FileDescriptor(fd());
|
UniqueFD dupFd(::dup(fd()));
|
||||||
|
if (!dupFd.isValid()) {
|
||||||
|
int ret = -errno;
|
||||||
|
LOG(FileDescriptor, Error)
|
||||||
|
<< "Failed to dup() fd: " << strerror(-ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dupFd;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileDescriptor::Descriptor::Descriptor(int fd, bool duplicate)
|
FileDescriptor::Descriptor::Descriptor(int fd, bool duplicate)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue