1

I am trying to make a simple daemon for continuously deleting files from purple/icons ( the pidgin dir ). However, remove() and unlink() are not deleting files. Here is my code, please, tell me where is my error.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <dirent.h>
#include <time.h>

int remove_files(const char* directory);
int remove_files(const char* directory) {

    DIR *dp;
    struct dirent *dirp;
    int files_deleted = 0;
    int count=0;
    dp = opendir(directory);
    while ( (dirp = readdir(dp)) != 0x00 ) {
        files_deleted++; 
        remove(dirp->d_name);
    }
    closedir(dp);
    return files_deleted;
}

int main(int argc, char *argv[]) {
    time_t sec;
    time(&sec);
    //dir handlers
    DIR *dp;
    struct dirent *ep;
    //
    FILE *fp = 0x00;
    pid_t process_id = 0;
    pid_t sid = 0;

    process_id = fork();
    if ( process_id < 0 ) {
        fprintf(stderr, "fork() failed \n");
        exit(1);
    } else if ( process_id > 0 ){
        fprintf(stdout, "process_id of child process %d\n", process_id);
        exit(0);
    }

    umask(0);

    if ( (sid=setsid() ) < 0 ) {
        exit(1);
    }

    chdir("/home/ilian/");

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    fp = fopen("Log.txt", "w+");
    if ( fp == 0x00 ) exit(1);
    fprintf(fp, "Started logging at %.24s \n", ctime(&sec));
    fflush(fp);
    while ( 1 ) {
        fprintf(fp, "%d files deleted \n", remove_files("/home/ilian/.purple/icons/"));
        fflush(fp);
        sleep(10);
    }
    fclose(fp);
    return (0);

} //END OF MAIN

Nothing is deleted, but the daemon is running. The log shows how much files are but the files are untouched.

  • Have you considered rm -r ~/.purple/icons; ln -s /dev/null ~/.purple/icons ? In that way, you do not even have to delete anything! – Lekensteyn Jun 18 '13 at 9:28
  • @Lekensteyn I had problems before with Pidgin simply overriding any symlinks and creating directories, so I don't think that'll work. – l0b0 Jun 18 '13 at 9:35
  • Did you try reporting this bug to Pidgin? You could save yourself (and others) some work... – l0b0 Jun 18 '13 at 9:36
  • 1
    coding questions are off topic for SU, I'm guessing this would be a better fit for SO. In your shoes, I'd just use inocron, and skip the coding. – Journeyman Geek Jun 18 '13 at 9:41
  • @l0b0 With other programs, yes. I have tried it on pidgin and it works. Illian, instead of a busy loop, consider using inotify. – Lekensteyn Jun 18 '13 at 12:21
8

Two things:

  • You should always check return values from system and library functions. If you had

    if(remove(dirp->d_name)<0)
        perror(dirp->d_name);
    else
        files_deleted++;
    

    then you'd see what was happening.

  • The reason your code doesn't work is because remove() and unlink() require the full path to the file in question, whereas readdir() only populates d_name with the filename.

    The easiest thing to do is chdir() into the directory first:

    chdir(directory);
    dp = opendir(".");
    ...
    
| improve this answer | |
  • Thanks, I just didnt figure it.. Never knew that d_name did not give the full path :( Thanks again, youve been of great help. – Ilian Zapryanov Jun 18 '13 at 9:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.