Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LittleFS missing availableForWrite() #8384

Closed
6 tasks done
CapnBry opened this issue Nov 24, 2021 · 2 comments · Fixed by #8386
Closed
6 tasks done

LittleFS missing availableForWrite() #8384

CapnBry opened this issue Nov 24, 2021 · 2 comments · Fixed by #8386

Comments

@CapnBry
Copy link

CapnBry commented Nov 24, 2021

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: [ESP-12F]
  • Core Version: [3.0.2]
  • Development Env: [VSMicro]
  • Operating System: [Windows]

Settings in IDE

  • Module: [LOLIN(WEMOS) D1 D2 & mini]
  • Flash Mode: [DIO]
  • Flash Size: [4MB]
  • lwip Variant: [Higher Bandwidth]
  • Reset Method: [nodemcu]
  • Flash Frequency: [40Mhz]
  • CPU Frequency: [80Mhz]
  • Upload Using: [SERIAL]
  • Upload Speed: [921600] (serial upload only)

Problem Description

Attempting to use HTTPClient to stream directly to a LittleFS File times out with error code -11. This is due to the LittleFSFileImpl not implementing availableForWrite(), so the base implementation returns 0 and the streaming can't write and times out. I added this implementation but I do not know if this is the right thing to return. It probably isn't but hey it works! It would probably work even if I returned 1 so I'm not going to throw myself a parade or anything for getting it working.

// in LittleFS.h LittleFSFileImpl
    int availableForWrite() override {
      if (!_opened || !_fd) {
        return 0;
      }
      return _fs->_lfs_cfg.block_size;
    }

MCVE Sketch

void taskcbIcsCheck(void)
{
  Serial.println(F("Performing ICS check..."));

  WiFiClient *client;
  if (strncmp(ICS_URL, "https://", 8) == 0)
  {
    client = new BearSSL::WiFiClientSecure();
    ((BearSSL::WiFiClientSecure *)client)->setInsecure();
  }
  else
    client = new WiFiClient();

  HTTPClient *https = new HTTPClient();
  int httpCode = -1;
  if (https->begin(*client, ICS_URL))
  {
    https->setUserAgent("Google-Calendar-Importer");
    https->setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
    httpCode = https->GET();
    Serial.print(F("Response code ")); Serial.println(httpCode, DEC);

    if (httpCode == HTTP_CODE_OK) {
      File f = LittleFS.open("cal.ics", "w");
      int n = https->writeToStream(&f);
      f.close();
      Serial.print("Download complete n="); Serial.println(n, DEC);

      g_IcsLastCheck = time(nullptr);
      taskIcsRead.restartDelayed(100);
    }
    https->end();
  }
  else
    Serial.print("Connect failed");

  delete https;
  delete client;
}

Debug Messages

[HTTP-Client][handleHeaderResponse] RX: ''
[HTTP-Client][handleHeaderResponse] code: 200
[HTTP-Client][handleHeaderResponse] size: 8718
// and then nothing, return code -11
@d-a-v
Copy link
Collaborator

d-a-v commented Nov 25, 2021

@CapnBry thanks for reporting this issue to us.
LittleFS wrapper indeed lacks of this implementation.
Returning block_size unlocks Stream*Send() methods but this may be a little too rough.
We'll address a fix very soon.

@d-a-v d-a-v self-assigned this Nov 25, 2021
@d-a-v d-a-v added this to the 3.1 milestone Nov 25, 2021
@CapnBry
Copy link
Author

CapnBry commented Nov 25, 2021

Awesome, thanks for the reply. Yeah I wasn't sure what value to return considering it could be up to the remaining space of the entire partition! Or be really inefficient and return as low as 1 haha.

Appreciate you putting this on your radar for a proper fox and thanks so much for all the other tons of code that makes my projects just an include and a few lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants