0169d2f9396b51b18fd4058fba11d63ce1e3796f
[platform/upstream/libzypp.git] / zypp / media / MediaDIR.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaDIR.cc
10  *
11 */
12
13 #include <iostream>
14
15 #include "zypp/base/Logger.h"
16 #include "zypp/media/MediaDIR.h"
17
18 #include <sys/mount.h>
19 #include <errno.h>
20
21 using namespace std;
22
23 namespace zypp {
24   namespace media {
25
26     ///////////////////////////////////////////////////////////////////
27     //
28     //  CLASS NAME : MediaDIR
29     //
30     ///////////////////////////////////////////////////////////////////
31
32     ///////////////////////////////////////////////////////////////////
33     //
34     //
35     //  METHOD NAME : MediaDIR::MediaDIR
36     //  METHOD TYPE : Constructor
37     //
38     //  DESCRIPTION : Attach point is always url_r.getPathName(),
39     //                as files are not copied.
40     //                Thus attach_point_hint_r is ignored.
41     //
42     MediaDIR::MediaDIR( const Url &      url_r,
43                         const Pathname & /*attach_point_hint_r*/ )
44         : MediaHandler( url_r, url_r.getPathName(),
45                     "/",    // urlpath below attachpoint
46                     false ) // does_download
47     {
48         MIL << "MediaDIR::MediaDIR(" << url_r << ")" << endl;
49         if( !url_r.getHost().empty())
50         {
51           ZYPP_THROW(MediaBadUrlException(url_r,
52             "Hostname not allowed in the Url"
53           ));
54         }
55     }
56
57     ///////////////////////////////////////////////////////////////////
58     //
59     //
60     //  METHOD NAME : MediaDIR::attachTo
61     //  METHOD TYPE : PMError
62     //
63     //  DESCRIPTION : Asserted that not already attached, and attachPoint is a directory.
64     //
65     void MediaDIR::attachTo(bool next)
66     {
67       if(next)
68         ZYPP_THROW(MediaNotSupportedException(url()));
69
70       // fetch attach point from url again if needed ...
71       // it may happen that attachPointHint (and attachPoint())
72       // does not contain any path, because the directory has
73       // not existed while the handler was constructed.
74       if( attachPoint().empty() && !url().getPathName().empty())
75       {
76         Pathname real( getRealPath(url().getPathName()));
77
78         PathInfo adir( real);
79         if( adir.isDir())
80         {
81           // set attachpoint only if the dir exists
82           setAttachPoint( real, false);
83         }
84         else
85         {
86           ZYPP_THROW(MediaBadUrlException(url(),
87             "Specified path '" + url().getPathName() + "' is not a directory"
88           ));
89         }
90       }
91
92       // attach point is same as source path... we do not mount here
93       if(attachPoint().empty())
94       {
95         ZYPP_THROW(MediaBadUrlException(url(),
96           "The media URL does not provide any useable directory path"
97         ));
98       }
99       else
100       if(!PathInfo(attachPoint()).isDir())
101       {
102         ZYPP_THROW(MediaBadUrlException(url(),
103           "Specified path '" + attachPoint().asString() + "' is not a directory"
104         ));
105       }
106
107       MediaSourceRef media(new MediaSource("dir", attachPoint().asString()));
108       setMediaSource(media);
109     }
110
111
112     ///////////////////////////////////////////////////////////////////
113     //
114     //
115     //  METHOD NAME : MediaDIR::releaseFrom
116     //  METHOD TYPE : void
117     //
118     //  DESCRIPTION : Asserted that media is attached.
119     //
120     void MediaDIR::releaseFrom( const std::string & ejectDev )
121     {
122       return;
123     }
124
125     ///////////////////////////////////////////////////////////////////
126     //
127     //
128     //  METHOD NAME : MediaDIR::getFile
129     //  METHOD TYPE : PMError
130     //
131     //  DESCRIPTION : Asserted that media is attached.
132     //
133     void MediaDIR::getFile(const Pathname & filename , const ByteCount &expectedFileSize_r) const
134     {
135       MediaHandler::getFile( filename, expectedFileSize_r );
136     }
137
138     ///////////////////////////////////////////////////////////////////
139     //
140     //  METHOD NAME : MediaDIR::getDir
141     //  METHOD TYPE : PMError
142     //
143     //  DESCRIPTION : Asserted that media is attached.
144     //
145     void MediaDIR::getDir( const Pathname & dirname, bool recurse_r ) const
146     {
147       MediaHandler::getDir( dirname, recurse_r );
148     }
149
150     ///////////////////////////////////////////////////////////////////
151     //
152     //
153     //  METHOD NAME : MediaDIR::getDirInfo
154     //  METHOD TYPE : PMError
155     //
156     //  DESCRIPTION : Asserted that media is attached and retlist is empty.
157     //
158     void MediaDIR::getDirInfo( std::list<std::string> & retlist,
159                                const Pathname & dirname, bool dots ) const
160     {
161       MediaHandler::getDirInfo( retlist, dirname, dots );
162     }
163
164     ///////////////////////////////////////////////////////////////////
165     //
166     //
167     //  METHOD NAME : MediaDIR::getDirInfo
168     //  METHOD TYPE : PMError
169     //
170     //  DESCRIPTION : Asserted that media is attached and retlist is empty.
171     //
172     void MediaDIR::getDirInfo( filesystem::DirContent & retlist,
173                                const Pathname & dirname, bool dots ) const
174     {
175       MediaHandler::getDirInfo( retlist, dirname, dots );
176     }
177
178     bool MediaDIR::getDoesFileExist( const Pathname & filename ) const
179     {
180       return MediaHandler::getDoesFileExist( filename );
181     }
182
183   } // namespace media
184 } // namespace zypp