libzypp: zypp-media/cdtools.cc Source File

libzypp 17.38.11

cdtools.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9
10#include "cdtools.h"
11
12extern "C"
13{
14#include <sys/ioctl.h>
15#include <linux/cdrom.h>
16}
17
18#include <fcntl.h>
19#include <unistd.h>
20#include <zypp-core/base/LogControl.h>
21#include <zypp-core/ExternalProgram.h>
22
23
24/*
25** If defined to the full path of the eject utility,
26** it will be used additionally to the eject-ioctl.
27*/
28#define EJECT_TOOL_PATH "/bin/eject"
29
30
31namespace zypp::media {
32
33 bool CDTools::openTray(const std::string &device_r)
34 {
35 int fd = ::open( device_r.c_str(), O_RDONLY|O_NONBLOCK|O_CLOEXEC );
36 int res = -1;
37
38 if ( fd != -1)
39 {
40 res = ::ioctl( fd, CDROMEJECT );
41 ::close( fd );
42 }
43
44 if ( res )
45 {
46 if( fd == -1)
47 {
48 WAR << "Unable to open '" << device_r
49 << "' (" << ::strerror( errno ) << ")" << std::endl;
50 }
51 else
52 {
53 WAR << "Eject " << device_r
54 << " failed (" << ::strerror( errno ) << ")" << std::endl;
55 }
56
57#if defined(EJECT_TOOL_PATH)
58 DBG << "Try to eject " << device_r << " using "
59 << EJECT_TOOL_PATH << " utility" << std::endl;
60
61 const char *cmd[3];
62 cmd[0] = EJECT_TOOL_PATH;
63 cmd[1] = device_r.c_str();
64 cmd[2] = NULL;
65 ExternalProgram eject(cmd, ExternalProgram::Stderr_To_Stdout);
66
67 for(std::string out( eject.receiveLine());
68 out.length(); out = eject.receiveLine())
69 {
70 DBG << " " << out;
71 }
72
73 if(eject.close() != 0)
74 {
75 WAR << "Eject of " << device_r << " failed." << std::endl;
76 return false;
77 }
78#else
79 return false;
80#endif
81 }
82 MIL << "Eject of " << device_r << " successful." << std::endl;
83 return true;
84 }
85
86 bool CDTools::closeTray(const std::string &device_r)
87 {
88 int fd = ::open( device_r.c_str(), O_RDONLY|O_NONBLOCK|O_CLOEXEC );
89 if ( fd == -1 ) {
90 WAR << "Unable to open '" << device_r << "' (" << ::strerror( errno ) << ")" << std::endl;
91 return false;
92 }
93 int res = ::ioctl( fd, CDROMCLOSETRAY );
94 ::close( fd );
95 if ( res ) {
96 WAR << "Close tray " << device_r << " failed (" << ::strerror( errno ) << ")" << std::endl;
97 return false;
98 }
99 DBG << "Close tray " << device_r << std::endl;
100 return true;
101 }
102
103}
ExternalProgram.h
LogControl.h
DBG
#define DBG
Definition Logger.h:102
MIL
#define MIL
Definition Logger.h:103
WAR
#define WAR
Definition Logger.h:104
EJECT_TOOL_PATH
#define EJECT_TOOL_PATH
Definition cdtools.cc:28
cdtools.h
zypp::ExternalProgram
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
Definition ExternalProgram.h:64
zypp::ExternalProgram::close
int close() override
Wait for the progamm to complete.
Definition ExternalProgram.cc:357
zypp::ExternalProgram::Stderr_To_Stdout
@ Stderr_To_Stdout
Definition ExternalProgram.h:77
zypp::externalprogram::ExternalDataSource::receiveLine
std::string receiveLine()
Read one line from the input stream.
Definition ExternalDataSource.cc:115
zypp::media::CDTools::openTray
static bool openTray(const std::string &device_r)
Definition cdtools.cc:33
zypp::media::CDTools::closeTray
static bool closeTray(const std::string &device_r)
Definition cdtools.cc:86
zypp::media
Definition MediaNetworkRequestExecutor.h:22