bmaptool: warn if destination file is suspecious
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 30 Sep 2013 11:25:00 +0000 (14:25 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 30 Sep 2013 11:25:00 +0000 (14:25 +0300)
Warn a user if he/she writes to a file under /dev, but it is not a special
device file, but just a regular file. This should improve user-friendliness.

Change-Id: I70e49d1c34ade667008dc606eb555e924b864ade
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptool

index e4df75e65872c8a6859f8a9f0a8609b7eab0f239..162fee5bb0b332160b048aa82a35f6b75b0f04eb 100755 (executable)
--- a/bmaptool
+++ b/bmaptool
@@ -381,6 +381,21 @@ def open_files(args, log):
         bmap_path = None
         args.nobmap = True
 
+    # If the destination file is under "/dev", but does not exist, print a
+    # warning. This is done in order to be more user-friendly, because
+    # sometimes users mean to write to a block device, them misspell its name.
+    # We just create the "/dev/misspelled" file, write the data there, and
+    # report success. Later on the user finds out that the image was not really
+    # written to the device, and gets confused. Similar confusion may happen if
+    # the destination file is not a special device for some reasons.
+    if os.path.normpath(args.dest).startswith("/dev/"):
+        if not os.path.exists(args.dest):
+            log.warning("\"%s\" does not exist, creating a regular file " \
+                        "\"%s\"" % (args.dest, args.dest))
+        elif stat.S_ISREG(os.stat(args.dest).st_mode):
+            log.warning("\"%s\" is under \"/dev\", but it is a regular file, " \
+                        "not a device node" % args.dest)
+
     # Try to open the destination file. If it does not exist, a new regular
     # file will be created. If it exists and it is a regular file - it'll be
     # truncated. If this is a block device, it'll just be opened.