I've implemented a method as below:
long getSize(const char *d_name)
{
struct stat buff {};
// stat(const char* var) always returns -1
int exists = stat(d_name, &buff);
long totalSize = 0;
// So totalSize never increases
if (exists > -1)
totalSize += buff.st_size;
return totalSize;
}
Also I have a structure:
struct Entity
{
string name;
string type;
long size;
string created_at;
string modofied_at; // equivalence to "modified" phrase
bool isHidden;
};
I want to iterate over files in a specific path and locate their data (size, name, etc) into a vector containing structures of each entity (file or dir). So I implemented this:
vector<Entity> getEntities(const char *path)
{
vector<Entity> entities;
DIR *dir;
struct dirent *ent;
/** if path exists **/
if ((dir = opendir(path)) == nullptr)
{
/* could not open directory */
perror("path_invalid");
exit(1);
}
/** loop over entities till encounters nullptr **/
while ((ent = readdir(dir)) != nullptr)
{
Entity entity;
entity.name = ent->d_name;
// This member is always 0
entity.size = this->getSize(ent->d_name);
entity.isHidden = this->isHidden(ent->d_name);
entities.push_back(entity);
}
closedir(dir);
return entities;
}
The problem is that stat
always returns -1
. So Entity's size always will be assigned unexpectedly to 0.