Deleting Millions of Files in /var/spool/clientmqueue/ folder

Last update on April 10, 2014.

In case you have to delete millions of files, like in clientmqueue folder.

Use this code, Save this as listdirdelete.c

Compile it with gcc: gcc listdirdelete.c -o listdirdelete


Then use ./listdirdelete /var/spool/clientmqueue


The code will list the files and delete them one by one. The problem with other methods I tired was that it was being killed because of memory constraints.

Hope you find this helpful.

getdents function man page here


#define _GNU_SOURCE
#include <dirent.h>     /* Defines DT_* constants */
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#define handle_error(msg) \
               do { perror(msg); exit(EXIT_FAILURE); } while (0)

       struct linux_dirent {
           long           d_ino;
           off_t          d_off;
           unsigned short d_reclen;
           char           d_name[];
       };

#define BUF_SIZE 1024*1024*5

void Signal_Handler(int sig){ /* signal handler function */
        int sig;       
                switch(sig){
                        case SIGHUP:                            /* rehash the server */
                                break;
                        case SIGTERM:                           /* finalize the server */
                                exit(0);
                                break;
                }
        }

       int main(int argc, char *argv[]){
           int fd, nread;
           char buf[BUF_SIZE];
           struct linux_dirent *d;
           int bpos;
           char d_type;
           int status;
           char file_name[255] = "/var/spool/clientmqueue/";

           pid_t pID = fork();
           signal(SIGHUP,Signal_Handler); /* hangup signal */
           signal(SIGTERM,Signal_Handler); /* software termination signal from kill */

           if (pID == 0)                // child
           {

           fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
           if (fd == -1)
               handle_error("open");

           for ( ; ; ) {
               nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
               if (nread == -1)
                   handle_error("getdents");

               if (nread == 0)
                   break;

               printf("--------------- nread=%d ---------------\n", nread);
               printf("i-node#  file type  d_reclen  d_off   d_name\n");
               for (bpos = 0; bpos < nread;) {
                   d = (struct linux_dirent *) (buf + bpos);

                   if (d->d_ino != 0) printf("%8ld  ", d->d_ino);
                   d_type = *(buf + bpos + d->d_reclen - 1);
                   if (d->d_ino != 0) printf("%-10s ", (d_type == DT_REG) ?  "regular" :
                                    (d_type == DT_DIR) ?  "directory" :
                                    (d_type == DT_FIFO) ? "FIFO" :
                                    (d_type == DT_SOCK) ? "socket" :
                                    (d_type == DT_LNK) ?  "symlink" :
                                    (d_type == DT_BLK) ?  "block dev" :
                                    (d_type == DT_CHR) ?  "char dev" : "???");
                   if (d->d_ino != 0) printf("%4d %10lld  %s\n", d->d_reclen,
                           (long long) d->d_off, d->d_name);

                   char file_name[255] = "/var/spool/clientmqueue/";

                   strcat(file_name,d->d_name);
                   status = remove(file_name);

                   if( status == 0 )
                     printf("%s file deleted successfully.\n",file_name);
                   else
                   {
                      printf("Unable to delete the file\n");
                      perror("Error");
                   }
                   bpos += d->d_reclen;
               }
           }
           exit(EXIT_SUCCESS);
           }
           else if (pID < 0)            // failed to fork
            {
                 printf( "Failed to fork " );
                exit(1);
                // Throw exception
            }
           else                                   // parent
            {
              // Code only executed by parent process

              printf( "Parent Process:");
              exit(EXIT_SUCCESS);
            }

       }

Next entry

Previous entry

Similar entries

Comments

Comments are closed.

Pingbacks

Pingbacks are open.

Trackbacks

  1. test on 05/03/2015 11:04 a.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

  2. https://shampooforcurlyhair.wordpress.com/ on 05/06/2015 8:49 p.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

  3. sneak a peek at this site on 05/14/2017 4:40 p.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

  4. usa jerseys soccer on 05/27/2017 2:58 a.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

  5. viagra on 06/15/2017 11:36 a.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

  6. dien dan phim sex on 06/19/2017 6:09 a.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

  7. the k park văn phú on 06/19/2017 7:15 a.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

  8. chung cu eurowindow river park sex on 06/21/2017 5:40 p.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

  9. trum phim sex on 06/25/2017 9:24 a.m. #

    Deleting Millions of Files in /var/spool/clientmqueue/ folder

Trackback URL