summaryrefslogtreecommitdiffstats
path: root/apt-pkg/contrib/cdromutl.cc
blob: f4020176afa6dd8748099405821c186ad2383af3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
/* ######################################################################
   
   CDROM Utilities - Some functions to manipulate CDROM mounts.
   
   These are here for the cdrom method and apt-cdrom.
   
   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include <config.h>

#include <apt-pkg/cdromutl.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/hashes.h>
#include <apt-pkg/strutl.h>

#include <iostream>
#include <string>
#include <vector>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <unistd.h>

#include <apti18n.h>
									/*}}}*/

using std::string;

// IsMounted - Returns true if the mount point is mounted		/*{{{*/
// ---------------------------------------------------------------------
/* This is a simple algorithm that should always work, we stat the mount point
   and the '..' file in the mount point and see if they are on the same device.
   By definition if they are the same then it is not mounted. This should 
   account for symlinked mount points as well. */
bool IsMounted(string &Path)
{
   if (Path.empty() == true)
      return false;

   // Need that trailing slash for directories
   if (Path[Path.length() - 1] != '/')
      Path += '/';

   // if the path has a ".disk" directory we treat it as mounted
   // this way even extracted copies of disks are recognized
   if (DirectoryExists(Path + ".disk/") == true)
      return true;

   /* First we check if the path is actually mounted, we do this by
      stating the path and the previous directory (careful of links!)
      and comparing their device fields. */
   struct stat Buf,Buf2;
   if (stat(Path.c_str(),&Buf) != 0 || 
       stat((Path + "../").c_str(),&Buf2) != 0)
      return _error->Errno("stat",_("Unable to stat the mount point %s"),Path.c_str());

   if (Buf.st_dev == Buf2.st_dev)
      return false;
   return true;
}
									/*}}}*/
// UnmountCdrom - Unmount a cdrom					/*{{{*/
// ---------------------------------------------------------------------
/* Forking umount works much better than the umount syscall which can 
   leave /etc/mtab inconsistent. We drop all messages this produces. */
bool UnmountCdrom(string Path)
{
   // do not generate errors, even if the mountpoint does not exist
   // the mountpoint might be auto-created by the mount command
   // and a non-existing mountpoint is surely not mounted
   _error->PushToStack();
   bool const mounted = IsMounted(Path);
   _error->RevertToStack();
   if (mounted == false)
      return true;

   for (int i=0;i<3;i++)
   {
   
      int Child = ExecFork();

      // The child
      if (Child == 0)
      {
	 // Make all the fds /dev/null
	 int const null_fd = open("/dev/null",O_RDWR);
	 for (int I = 0; I != 3; ++I)
	    dup2(null_fd, I);

	 if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true)
	 {
	    if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0)
	       _exit(100);
	    _exit(0);	 	 
	 }
	 else
	 {
	    const char * const Args[] = {
	       "umount",
	       Path.c_str(),
	       nullptr
	    };
	    execvp(Args[0], const_cast<char **>(Args));
	    _exit(100);
	 }
      }

      // if it can not be umounted, give it a bit more time
      // this can happen when auto-mount magic or fs/cdrom prober attack
      if (ExecWait(Child,"umount",true) == true)
	 return true;
      sleep(1);
   }

   return false;
}
									/*}}}*/
// MountCdrom - Mount a cdrom						/*{{{*/
// ---------------------------------------------------------------------
/* We fork mount and drop all messages */
bool MountCdrom(string Path, string DeviceName)
{
   // do not generate errors, even if the mountpoint does not exist
   // the mountpoint might be auto-created by the mount command
   _error->PushToStack();
   bool const mounted = IsMounted(Path);
   _error->RevertToStack();
   if (mounted == true)
      return true;

   int Child = ExecFork();

   // The child
   if (Child == 0)
   {
      // Make all the fds /dev/null
      int const null_fd = open("/dev/null",O_RDWR);
      for (int I = 0; I != 3; ++I)
	 dup2(null_fd, I);

      if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
      {
	 if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
	    _exit(100);
	 _exit(0);	 
      }
      else
      {
	 const char *Args[10];
	 Args[0] = "mount";
	 if (DeviceName == "") 
	 {
	    Args[1] = Path.c_str();
	    Args[2] = 0;
	 } else {
	    Args[1] = DeviceName.c_str();
	    Args[2] = Path.c_str();
	    Args[3] = 0;
	 }
	 execvp(Args[0],(char **)Args);      
	 _exit(100);
      }      
   }

   // Wait for mount
   return ExecWait(Child,"mount",true);
}
									/*}}}*/
// IdentCdrom - Generate a unique string for this CD			/*{{{*/
// ---------------------------------------------------------------------
/* We convert everything we hash into a string, this prevents byte size/order
   from effecting the outcome. */
bool IdentCdrom(string CD,string &Res,unsigned int Version)
{
   Hashes Hash(Hashes::MD5SUM);
   bool writable_media = false;

   int dirfd = open(CD.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
   if (dirfd == -1)
      return _error->Errno("open",_("Unable to read %s"),CD.c_str());

   // if we are on a writable medium (like a usb-stick) that is just
   // used like a cdrom don't use "." as it will constantly change,
   // use .disk instead
   if (faccessat(dirfd, ".", W_OK, 0) == 0)
   {
      int diskfd = openat(dirfd, "./.disk", O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0);
      if (diskfd != -1)
      {
	 close(dirfd);
	 dirfd = diskfd;
	 writable_media = true;
	 CD = CD.append("/.disk");
	 if (_config->FindB("Debug::aptcdrom",false) == true)
	    std::clog << "Found writable cdrom, using alternative path: " << CD
	       << std::endl;
      }
   }

   DIR * const D = fdopendir(dirfd);
   if (D == nullptr)
      return _error->Errno("opendir",_("Unable to read %s"),CD.c_str());

   /* Run over the directory, we assume that the reader order will never
      change as the media is read-only. In theory if the kernel did
      some sort of wacked caching this might not be true.. */
   for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
   {
      // Skip some files..
      if (strcmp(Dir->d_name,".") == 0 ||
	  strcmp(Dir->d_name,"..") == 0)
	 continue;

      std::string S;
      if (Version <= 1)
	 S = std::to_string(Dir->d_ino);
      else
      {
	 struct stat Buf;
	 if (fstatat(dirfd, Dir->d_name, &Buf, 0) != 0)
	    continue;
	 S = std::to_string(Buf.st_mtime);
      }

      Hash.Add(S.c_str());
      Hash.Add(Dir->d_name);
   }

   // Some stats from the fsys
   std::string S;
   if (_config->FindB("Debug::identcdrom",false) == false)
   {
      struct statvfs Buf;
      if (fstatvfs(dirfd, &Buf) != 0)
	 return _error->Errno("statfs",_("Failed to stat the cdrom"));

      // We use a kilobyte block size to avoid overflow
      S = std::to_string(Buf.f_blocks * (Buf.f_bsize / 1024));
      if (writable_media == false)
	 S.append(" ").append(std::to_string(Buf.f_bfree * (Buf.f_bsize / 1024)));
      Hash.Add(S.c_str(), S.length());
      strprintf(S, "-%u", Version);
   }
   else
      strprintf(S, "-%u.debug", Version);

   closedir(D);
   Res = Hash.GetHashString(Hashes::MD5SUM).HashValue().append(std::move(S));
   return true;
}
									/*}}}*/
// FindMountPointForDevice - Find mountpoint for the given device	/*{{{*/
string FindMountPointForDevice(const char *devnode)
{
   // this is the order that mount uses as well
   std::vector<std::string> const mounts = _config->FindVector("Dir::state::MountPoints", "/etc/mtab,/proc/mount");

   for (std::vector<std::string>::const_iterator m = mounts.begin(); m != mounts.end(); ++m)
      if (FileExists(*m) == true)
      {
	 char * line = NULL;
	 size_t line_len = 0;
	 FILE * f = fopen(m->c_str(), "r");
	 while(getline(&line, &line_len, f) != -1)
	 {
	    char * out[] = { NULL, NULL, NULL };
	    TokSplitString(' ', line, out, 3);
	    if (out[2] != NULL || out[1] == NULL || out[0] == NULL)
	       continue;
	    if (strcmp(out[0], devnode) != 0)
	       continue;
	    fclose(f);
	    // unescape the \0XXX chars in the path
	    string mount_point = out[1];
	    free(line);
	    return DeEscapeString(mount_point);
	 }
	 fclose(f);
	 free(line);
      }

   return string();
}
									/*}}}*/