maheshg
maheshg

Reputation: 339

is there a way to find the oldest file using just the c++

On Linux.

I want to build a buffer of files. A new file is saved every 30 minutes . But the total number of files allowed are 'n'.

so when 'n+1'th file is created, the oldest one has to be deleted.

I found stuff like 'dirent.h' and 'struct stat' that can help access directory, list all files and get its properties.

struct stat does not however the give time of creation, but just - last modified,last accessed, time of last status changed http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html

Please Help.

P.S: boost is not available right now.

Upvotes: 1

Views: 4698

Answers (3)

Tim Cheng
Tim Cheng

Reputation: 33

@Onur A.'s code will run failed when directory have too many files(maybe more than 125?). Since the pointer oldestFile's value will changed on after some loops. The reason I still not found, maybe readdir() reuse that output buffer when input files more than a certain amount? I prefer use strncpy to store oldest file's path in that code. Here's my code

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

void directoryManager(char *dir, int maxNumberOfFiles) {
    DIR           *dp;
    struct dirent *entry;
    char oldestFile[128];
    struct stat    statbuf;
    int            numberOfEntries = 0;
    time_t         t_oldest;
    double         sec;

    time(&t_oldest);
    // printf("now:%s\n", ctime(&t_oldest));
    if ((dp = opendir(dir)) != NULL) {
        chdir(dir);
        while ((entry = readdir(dp)) != NULL) {
            lstat(entry->d_name, &statbuf);
            if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) continue;
            printf("%s\t%s", entry->d_name, ctime(&statbuf.st_mtime));
            numberOfEntries++;
            if (difftime(statbuf.st_mtime, t_oldest) < 0) {
                t_oldest   = statbuf.st_mtime;
                strncpy(oldestFile, entry->d_name, sizeof(oldestFile));
            }
        }
    } else {
        printf("open %s failed\n", dir);
        return;
    }

    printf("\n\n\n%s\n", oldestFile);
    if (numberOfEntries >= maxNumberOfFiles) remove(oldestFile);
    closedir(dp);
    
}

int main() {
    directoryManager("./myFile", 5);
}

Upvotes: 0

mehmet riza oz
mehmet riza oz

Reputation: 559

I needed a function that deletes the oldest file in a given directory. I used system callback functions and wrote the code in C. You can call it from C++.

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

void directoryManager(char *dir, int maxNumberOfFiles){
DIR *dp;
struct dirent *entry, *oldestFile;
struct stat statbuf;
int numberOfEntries=0;
time_t t_oldest;
double sec;

time(&t_oldest);
//printf("now:%s\n", ctime(&t_oldest));
if((dp = opendir(dir)) != NULL) {
   chdir(dir);
   while((entry = readdir(dp)) != NULL) {
      lstat(entry->d_name, &statbuf);       
      if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
         continue;
      printf("%s\t%s", entry->d_name, ctime(&statbuf.st_mtime));
         numberOfEntries++;
      if(difftime(statbuf.st_mtime, t_oldest) < 0){
            t_oldest = statbuf.st_mtime;
         oldestFile = entry;
      }
  } 
}       

//printf("\n\n\n%s", oldestFile->d_name);
if(numberOfEntries >= maxNumberOfFiles)
   remove(oldestFile->d_name);

//printf("\noldest time:%s", ctime(&t_oldest));
closedir(dp);
}

int main(){
    directoryManager("/home/myFile", 5);
}

Upvotes: 2

sirgeorge
sirgeorge

Reputation: 6541

On Linux there is no such thing as file creation time saved with file in file system(s) metadata. There is something close to it, but not the same: inode modification time (this is the st_ctime member of struct stat). From stat man page:

The field st_ctime is changed by writing or by setting inode information (i.e., owner, group group,link count, mode, etc.).

As long as you do not modify these properties and you do not write (more than zero bytes) to a file(s) - the st_ctime is your "file creation time".

Upvotes: 3

Related Questions