]> git.saurik.com Git - apt.git/commitdiff
support very long mtab entries in mountpoint discovery
authorDavid Kalnischkies <david@kalnischkies.de>
Mon, 10 Mar 2014 00:49:37 +0000 (01:49 +0100)
committerDavid Kalnischkies <david@kalnischkies.de>
Thu, 13 Mar 2014 12:58:46 +0000 (13:58 +0100)
Old code limited lines to 250 characters which is probably enough for
everybody, but who knows… It also takes care of device nodes which start
with the same prefix.

apt-pkg/contrib/cdromutl.cc
test/libapt/cdromfindmountpointfordevice_test.cc [new file with mode: 0644]
test/libapt/makefile
test/libapt/run-tests

index f850e08a5ff7c7c395e9e9c70af8b9a9f41ab04b..936e377fbfe6af2777bc2c6133e4eb945a6d552a 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <iostream>
 #include <string>
 #include <string.h>
 #include <iostream>
 #include <string>
+#include <vector>
 #include <sys/statvfs.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <sys/statvfs.h>
 #include <dirent.h>
 #include <fcntl.h>
@@ -262,37 +263,34 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version)
    return true;   
 }
                                                                        /*}}}*/
    return true;   
 }
                                                                        /*}}}*/
-
 // FindMountPointForDevice - Find mountpoint for the given device      /*{{{*/
 string FindMountPointForDevice(const char *devnode)
 {
 // FindMountPointForDevice - Find mountpoint for the given device      /*{{{*/
 string FindMountPointForDevice(const char *devnode)
 {
-   char buf[255];
-   char *out[10];
-   int i=0;
-
    // this is the order that mount uses as well
    // this is the order that mount uses as well
-   const char *mount[] = { "/etc/mtab", 
-                           "/proc/mount", 
-                           NULL };
+   std::vector<std::string> const mounts = _config->FindVector("Dir::state::MountPoints", "/etc/mtab,/proc/mount");
 
 
-   for (i=0; mount[i] != NULL; i++) {
-      if (FileExists(mount[i])) {
-         FILE *f=fopen(mount[i], "r");
-         while ( fgets(buf, sizeof(buf), f) != NULL) {
-            if (strncmp(buf, devnode, strlen(devnode)) == 0) {
-               if(TokSplitString(' ', buf, out, 10))
-               {
-                  fclose(f);
-                  // unescape the \0XXX chars in the path
-                  string mount_point = out[1];
-                  return DeEscapeString(mount_point);
-               }
-            }
-         }
-         fclose(f);
+   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];
+           return DeEscapeString(mount_point);
+        }
+        fclose(f);
       }
       }
-   }
-   
+
    return string();
 }
                                                                        /*}}}*/
    return string();
 }
                                                                        /*}}}*/
diff --git a/test/libapt/cdromfindmountpointfordevice_test.cc b/test/libapt/cdromfindmountpointfordevice_test.cc
new file mode 100644 (file)
index 0000000..26dcd14
--- /dev/null
@@ -0,0 +1,26 @@
+#include <config.h>
+
+#include <apt-pkg/cdromutl.h>
+#include <apt-pkg/configuration.h>
+
+#include <string>
+#include <vector>
+
+#include "assert.h"
+
+int main(int argc, char const *argv[]) {
+       if (argc != 2) {
+               std::cout << "One parameter expected - given " << argc << std::endl;
+               return 100;
+       }
+
+       _config->Set("Dir::state::Mountpoints", argv[1]);
+       equals("/", FindMountPointForDevice("rootfs"));
+       equals("/", FindMountPointForDevice("/dev/disk/by-uuid/fadcbc52-6284-4874-aaaa-dcee1f05fe21"));
+       equals("/sys", FindMountPointForDevice("sysfs"));
+       equals("/sys0", FindMountPointForDevice("sysfs0"));
+       equals("/boot/efi", FindMountPointForDevice("/dev/sda1"));
+       equals("/tmp", FindMountPointForDevice("tmpfs"));
+
+       return 0;
+}
index a8e053d6e3f5934139aa421cb706264a0abd8dbf..66d6ea783bb3d0e485ccab8ec2e91023e3597373 100644 (file)
@@ -94,6 +94,12 @@ SLIBS = -lapt-pkg
 SOURCE = cdromreducesourcelist_test.cc
 include $(PROGRAM_H)
 
 SOURCE = cdromreducesourcelist_test.cc
 include $(PROGRAM_H)
 
+# test cdroms FindMountPointForDevice for udev autodetection
+PROGRAM = CdromFindMountPointForDevice${BASENAME}
+SLIBS = -lapt-pkg
+SOURCE = cdromfindmountpointfordevice_test.cc
+include $(PROGRAM_H)
+
 # test IndexCopy::ConvertToSourceList
 PROGRAM = IndexCopyToSourceList${BASENAME}
 SLIBS = -lapt-pkg
 # test IndexCopy::ConvertToSourceList
 PROGRAM = IndexCopyToSourceList${BASENAME}
 SLIBS = -lapt-pkg
index a056f31f98d1bc4a83344f80bdfc4abb4df77306..0baedcf9ece3b9d6a0814e5995d5820f63ef035c 100755 (executable)
@@ -108,6 +108,14 @@ do
                        "${tmppath}/dists/unstable/InRelease" \
                        "${tmppath}/dists/broken/Release.gpg"
                ln -s "${tmppath}/dists/unstable" "${tmppath}/dists/sid"
                        "${tmppath}/dists/unstable/InRelease" \
                        "${tmppath}/dists/broken/Release.gpg"
                ln -s "${tmppath}/dists/unstable" "${tmppath}/dists/sid"
+       elif [ $name = "CdromFindMountPointForDevice${EXT}" ]; then
+               tmppath=$(mktemp)
+               echo 'rootfs / rootfs rw 0 0
+sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
+sysfs0 /sys0 sysfs rw,nosuid,nodev,noexec,relatime 0 0
+/dev/disk/by-uuid/fadcbc52-6284-4874-aaaa-dcee1f05fe21 / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0
+/dev/sda1 /boot/efi vfat rw,nosuid,nodev,noexec,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=lower,quiet,utf8,errors=remount-ro,rw,nosuid,nodev,noexec,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=lower,quiet,utf8,errors=remount-ro,rw,nosuid,nodev,noexec,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=lower,quiet,utf8,errors=remount-ro,rw,nosuid,nodev,noexec,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=lower,quiet,utf8,errors=remount-ro 0 0
+tmpfs /tmp tmpfs rw,nosuid,nodev,relatime 0 0' > $tmppath
        fi
 
        echo -n "Testing with ${NAME} "
        fi
 
        echo -n "Testing with ${NAME} "