diff --git a/libraries/FS/src/vfs_api.cpp b/libraries/FS/src/vfs_api.cpp index ab7dccb7599..e65a537c03d 100644 --- a/libraries/FS/src/vfs_api.cpp +++ b/libraries/FS/src/vfs_api.cpp @@ -36,7 +36,8 @@ FileImplPtr VFSImpl::open(const char* fpath, const char* mode, const bool create return FileImplPtr(); } - sprintf(temp,"%s%s", _mountpoint, fpath); + strcpy(temp, _mountpoint); + strcat(temp, fpath); struct stat st; //file found @@ -136,19 +137,25 @@ bool VFSImpl::rename(const char* pathFrom, const char* pathTo) log_e("%s does not exists", pathFrom); return false; } - char * temp1 = (char *)malloc(strlen(pathFrom)+strlen(_mountpoint)+1); + size_t mountpointLen = strlen(_mountpoint); + char * temp1 = (char *)malloc(strlen(pathFrom)+mountpointLen+1); if(!temp1) { log_e("malloc failed"); return false; } - char * temp2 = (char *)malloc(strlen(pathTo)+strlen(_mountpoint)+1); + char * temp2 = (char *)malloc(strlen(pathTo)+mountpointLen+1); if(!temp2) { free(temp1); log_e("malloc failed"); return false; } - sprintf(temp1,"%s%s", _mountpoint, pathFrom); - sprintf(temp2,"%s%s", _mountpoint, pathTo); + + strcpy(temp1, _mountpoint); + strcat(temp1, pathFrom); + + strcpy(temp2, _mountpoint); + strcat(temp2, pathTo); + auto rc = ::rename(temp1, temp2); free(temp1); free(temp2); @@ -182,7 +189,10 @@ bool VFSImpl::remove(const char* fpath) log_e("malloc failed"); return false; } - sprintf(temp,"%s%s", _mountpoint, fpath); + + strcpy(temp, _mountpoint); + strcat(temp, fpath); + auto rc = unlink(temp); free(temp); return rc == 0; @@ -211,7 +221,10 @@ bool VFSImpl::mkdir(const char *fpath) log_e("malloc failed"); return false; } - sprintf(temp,"%s%s", _mountpoint, fpath); + + strcpy(temp, _mountpoint); + strcat(temp, fpath); + auto rc = ::mkdir(temp, ACCESSPERMS); free(temp); return rc == 0; @@ -244,7 +257,10 @@ bool VFSImpl::rmdir(const char *fpath) log_e("malloc failed"); return false; } - sprintf(temp,"%s%s", _mountpoint, fpath); + + strcpy(temp, _mountpoint); + strcat(temp, fpath); + auto rc = ::rmdir(temp); free(temp); return rc == 0; @@ -265,7 +281,9 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode) if(!temp) { return; } - sprintf(temp,"%s%s", _fs->_mountpoint, fpath); + + strcpy(temp, _fs->_mountpoint); + strcat(temp, fpath); _path = strdup(fpath); if(!_path) { @@ -362,7 +380,10 @@ void VFSFileImpl::_getStat() const if(!temp) { return; } - sprintf(temp,"%s%s", _fs->_mountpoint, _path); + + strcpy(temp, _fs->_mountpoint); + strcat(temp, _path); + if(!stat(temp, &_stat)) { _written = false; } @@ -466,14 +487,27 @@ FileImplPtr VFSFileImpl::openNextFile(const char* mode) if(file->d_type != DT_REG && file->d_type != DT_DIR) { return openNextFile(mode); } - String fname = String(file->d_name); - String name = String(_path); - if(!fname.startsWith("/") && !name.endsWith("/")) { - name += "/"; + + size_t pathLen = strlen(_path); + size_t fileNameLen = strlen(file->d_name); + char * name = (char *)malloc(pathLen+fileNameLen+2); + + if(name == NULL) { + return FileImplPtr(); } - name += fname; - return std::make_shared(_fs, name.c_str(), mode); + strcpy(name, _path); + + if ((file->d_name[0] != '/') && (_path[pathLen - 1] != '/')) + { + strcat(name, "/"); + } + + strcat(name, file->d_name); + + FileImplPtr fileImplPtr = std::make_shared(_fs, name, mode); + free(name); + return fileImplPtr; } boolean VFSFileImpl::seekDir(long position){