Jelajahi Sumber

Open image as read-only if attribute set

A disk image will be opened as read-only if the read-only attribute
is set on the image file. If the SCSI disk is been written to a
read-only image, the "Write protected" sense code will be returned.
If that is somehow bypassed the ImageBackingStore write method will
log an error and return 0 bytes written.

Issue #124
Morio 2 tahun lalu
induk
melakukan
45255d6103
1 mengubah file dengan 19 tambahan dan 3 penghapusan
  1. 19 3
      src/ZuluSCSI_disk.cpp

+ 19 - 3
src/ZuluSCSI_disk.cpp

@@ -272,6 +272,7 @@ public:
     {
         m_israw = false;
         m_isrom = false;
+        m_isreadonly_attr = false;
         m_blockdev = nullptr;
         m_bgnsector = m_endsector = m_cursector = 0;
     }
@@ -319,7 +320,16 @@ public:
         }
         else
         {
-            m_fsfile = SD.open(filename, O_RDWR);
+            m_isreadonly_attr = !!(FS_ATTRIB_READ_ONLY & SD.attrib(filename));
+            if (m_isreadonly_attr)
+            {
+                m_fsfile = SD.open(filename, O_RDONLY);
+                azlog("---- Image file is read-only, writes disabled");
+            }
+            else
+            {
+                m_fsfile = SD.open(filename, O_RDWR);
+            }
 
             uint32_t sectorcount = m_fsfile.size() / SD_SECTOR_SIZE;
             uint32_t begin = 0, end = 0;
@@ -354,7 +364,7 @@ public:
 
     bool isWritable()
     {
-        return !m_isrom;
+        return !m_isrom && !m_isreadonly_attr;
     }
 
     bool isRom()
@@ -506,6 +516,11 @@ public:
             azlog("ERROR: attempted to write to ROM drive");
             return 0;
         }
+        else  if (m_isreadonly_attr)
+        {
+            azlog("ERROR: attempted to write to a read only image");
+            return 0;
+        }
         else
         {
             return m_fsfile.write(buf, count);
@@ -514,7 +529,7 @@ public:
 
     void flush()
     {
-        if (!m_israw && !m_isrom)
+        if (!m_israw && !m_isrom && !m_isreadonly_attr)
         {
             m_fsfile.flush();
         }
@@ -523,6 +538,7 @@ public:
 private:
     bool m_israw;
     bool m_isrom;
+    bool m_isreadonly_attr;
     romdrive_hdr_t m_romhdr;
     FsFile m_fsfile;
     SdCard *m_blockdev;