Skip to content

VFSFileImpl.exists() always returns true (IDFGH-1106) #3427

@judge2005

Description

@judge2005

Environment

  • Development Kit: ESP32-PICO-Kit|
  • Kit version: v1
  • Module or chip used: ESP32-PICO-D4
  • IDF version: Arduino 1.0.2 (so v3.2)
  • Build System: Make
  • Compiler version: xtensa-esp32-elf-gcc.exe (crosstool-NG crosstool-ng-1.22.0-80-g6c4433a5) 5.2.0
  • Operating System: Windows
  • Power Supply: USB

Problem Description

I'm using the SPIFFS file system. If a reference to the SPIFFS file system is passed to a function that expects a reference to a FS, the FS implementation of exists() is called. This always returns true, even for files that don't exist.

Expected Behavior

I should be able to use an FS& for a SPIFF& - SPIFF is a sub-class of FS. So exists() on the FS class should work for a SPIFFS filing system.

Actual Behavior

I can't!

Steps to reproduce

Build the included code and run it.

Code to reproduce this issue

#include "dirent.h"
#include "Arduino.h"
#include "SPIFFS.h"

void testSPIFFS(FS &fs, String path) {
	// Should print out "fs.exists(/somerandomfile) : 0". It doesn't
	Serial.printf("fs.exists(%s) : %d\n", path.c_str(), fs.exists(path));
}

void setup() {
	Serial.begin(115200);
	SPIFFS.begin();
	String path = "/somerandomfile";

	Serial.printf("SPIFFS.exists(%s) : %d\n", path.c_str(), SPIFFS.exists(path));
	testSPIFFS(SPIFFS, path);

	// The /spiffs part is prepended by one of the classes above, which is why it is used here.
	DIR *dir = opendir("/spiffs/somerandomfile2");
	
	// Should print out "opendir(/spiffs/somerandomfile2) : 0", but it doesn't
	Serial.printf("opendir(%s) : %d\n", "/spiffs/somerandomfile2", (int)dir);
}

void loop() {
}

Other items if possible

In the end, FS.exists() calls VFSFileImpl.exists() which ends up calling opendir(). opendir() always returns a DIR* for a file on the SPIFFS filing system if the file does not exist. All of this could be avoided if the methods on FS were virtual...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions