--- /dev/null
+#! /usr/bin/perl
+
+use strict;
+
+my $mode = shift;
+my %devs;
+if ($mode eq "find-label"){
+ my $targetPath = shift;
+ if (! -d $targetPath){
+ &Usage("not a directory: $targetPath");
+ }
+ %devs = &LabelOfDevs;
+ open($CMD, "mount|") || die "cannot execute mount: $!";
+ my ($label, $relPath);
+ while(<$CMD>){
+ # /dev/sdb1 on /media/src type vfat (rw,...)
+ if(/(\S+) on (\S+)/){
+ my ($dev, $path) = ($1, $2);
+ if (index($targetPath, $path) == 0){
+ if ($devs{$dev} ne ""){
+ $label = $devs{$dev};
+ $relPath = substr($targetPath, length($path) + 1);
+ }
+ }
+ }
+ }
+ close $CMD;
+ if ($label eq ""){
+ &Usage("no mounted device found");
+ } else {
+ print "L=\"$label\"\n$relPath\n";
+ }
+} elsif ($mode eq "search-target"){
+ my $label = shift;
+ my $relPath = shift;
+ &Usage("missing label") unless $label;
+ &Usage("missing relative path") unless $relPath;
+
+ %devs = &LabelOfDevs;
+ if (
+ # /dev/sdb1: LABEL="EFI system" UUID="80F7-47BB" TYPE="vfat" ...
+ open(my $CMD, "blkid|") || die "cannot execute blkid: $!";
+ while(<$CMD>){
+ if(/(\S+): LABEL="(.*?)"/){
+ $devs{$1} = $2;
+ }
+ }
+close $CMD;
+} else {
+ &Usage("unknown mode");
+}
+
+
+sub Usage{
+ my $msg = shift;
+ print <<EOS;
+Usage:
+osconnect find-label <path>
+ Search the label of the device containing <path>.
+ Output:
+ L=<label>
+ <relative_path>
+osconnect mount-target <label> <rel_path>
+ Search the device with a given label and mount it (if not mounted)
+ Output:
+ <mount-directory>
+ <flag>
+
+Example:
+osconnect.pl find-label /media/USB-black/backup
+
+EOS
+ print "+++", $msg, "\n";
+ exit 1;
+}
+
+# Returns an associative array with (dev, label) entries
+# @return an associative array with (dev, label) entries
+sub LabelOfDev{
+
+ my %devs;
+ # /dev/sdb1: LABEL="EFI system" UUID="80F7-47BB" TYPE="vfat" ...
+ open(my $CMD, "blkid|") || die "cannot execute blkid: $!";
+ while(<$CMD>){
+ if(/(\S+): LABEL="(.*?)"/){
+ $devs{$1} = $2;
+ }
+ }
+ close $CMD;
+ return %devs;
+}
+
+# Search the device of a given label.
+# @param $label label to search
+# @return: "": not found
+# otherwise: the device with the given label
+sub DeviceOfLabel{
+ my $label = shift;
+ my $dev;
+ for (keys %devs){
+ if ($devs{$_} eq $label){
+ $dev = $_;
+ last;
+ }
+ return $rc;
+}