From 08d9cbb9306a032d706a6ded0bf576fd0c58ca8d Mon Sep 17 00:00:00 2001
From: vyacheslav-shubin <shubin-vv@krista.ru>
Date: Sat, 4 Feb 2023 10:36:07 +0300
Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Add=20SD=20?=
 =?UTF-8?q?Card=20'hide'=20method=20for=20dev=20usage=20(#22425)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Marlin/src/sd/SdBaseFile.cpp | 22 ++++++++++++++++++++++
 Marlin/src/sd/SdBaseFile.h   |  5 +++++
 2 files changed, 27 insertions(+)

diff --git a/Marlin/src/sd/SdBaseFile.cpp b/Marlin/src/sd/SdBaseFile.cpp
index 1c1e0c7d145..e3f95623acb 100644
--- a/Marlin/src/sd/SdBaseFile.cpp
+++ b/Marlin/src/sd/SdBaseFile.cpp
@@ -1668,6 +1668,28 @@ bool SdBaseFile::remove(SdBaseFile *dirFile, const char *path) {
   return file.open(dirFile, path, O_WRITE) ? file.remove() : false;
 }
 
+bool SdBaseFile::hide(const bool hidden) {
+  if (ENABLED(SDCARD_READONLY)) return false;
+  // must be an open file or subdirectory
+  if (!(isFile() || isSubDir())) return false;
+  // sync() and cache directory entry
+  sync();
+  dir_t *d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
+  if (!d) return false;
+  uint8_t a = d->attributes;
+  if (hidden)
+    a |= DIR_ATT_HIDDEN;
+  else
+    a &= ~DIR_ATT_HIDDEN;
+
+  if (a != d->attributes) {
+    d->attributes = a;
+    return vol_->cacheFlush();
+  }
+
+  return true;
+}
+
 /**
  * Rename a file or subdirectory.
  *
diff --git a/Marlin/src/sd/SdBaseFile.h b/Marlin/src/sd/SdBaseFile.h
index bda44c6bd5c..dd8e2aff4bd 100644
--- a/Marlin/src/sd/SdBaseFile.h
+++ b/Marlin/src/sd/SdBaseFile.h
@@ -310,6 +310,11 @@ class SdBaseFile {
   bool rmdir();
   bool rmRfStar();
 
+  /**
+   * Set or clear DIR_ATT_HIDDEN attribute for directory entry
+   */
+  bool hide(const bool hidden);
+
   /**
    * Set the files position to current position + \a pos. See seekSet().
    * \param[in] offset The new position in bytes from the current position.