Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cerberus, guard folder for added files or directories, execute commands on them
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <string.h>
- #include <ctype.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <ftw.h>
- #define workdirectory "compressed"
- #define compressionprogram "zip"
- #define compressiondirectoryoption "-r"
- #define compressionextension ".zip"
- #define skipdirectorystring "tmp"
- #define movefilesleeptime 1
- #define movedirectorysleeptime 30
- #define MAXNAME 100
- // global variables, enumerations
- enum ENTRIES { FILEENTRY=1, DIRECTORYENTRY };
- typedef unsigned long ul;
- static ul dirtotal;
- // functions declarations
- void workonfile(char *filename, int type);
- int isFile(char* name);
- off_t filesize(char *filename);
- int isEntryEligible(char *filename);
- int findsimple(char text[], const char token[]);
- int dirsum(const char *fpath, const struct stat *sb, int typeflag);
- int main()
- {
- DIR *dir = opendir(".");
- struct dirent *result;
- unsigned long filesno=0;
- int i;
- if ((isFile(workdirectory))) {
- printf("work directory not accessible\n");
- exit(EXIT_FAILURE); }
- printf("Cerberus started..\n");
- while (1) {
- rewinddir(dir);
- result=readdir(dir);
- while (!(i=isEntryEligible(result->d_name))) {
- result=readdir(dir);
- if (result==NULL)
- break; }
- sleep(1); // don't overtire the cpu
- if (result!=NULL) { // skip NULL entry
- ++filesno;
- printf("\n%ld.", filesno);
- if (i==FILEENTRY)
- printf("file");
- if (i==DIRECTORYENTRY)
- printf("directory");
- printf(" name: %s; i-number: %ld\n", result->d_name, (long)result->d_ino);
- workonfile(result->d_name, i); } }
- return 0;
- }
- // move file to defined work directory, daemonize compress and remove
- void workonfile(char *filename, int type)
- {
- char command[MAXNAME*3];
- int i;
- off_t tsize[2];
- if (type==FILEENTRY) { // file
- while (1) { // give time to the file to be copied, downloaded
- tsize[0]=filesize(filename);
- sleep(movefilesleeptime);
- tsize[1]=filesize(filename);
- if (tsize[0]==tsize[1])
- break; }
- sprintf(command, "mv %s %s", filename, workdirectory); }
- else { // directory
- while (1) {
- ftw(filename, &dirsum, 1);
- tsize[0]=dirtotal;
- sleep(movedirectorysleeptime);
- ftw(filename, &dirsum, 1);
- tsize[1]=dirtotal;
- if (tsize[0]==tsize[1])
- break; }
- sprintf(command, "cp -r %s %s", filename, workdirectory);
- system(command);
- sprintf(command, "rm -rf %s", filename); }
- system(command);
- i=fork();
- if (i<0) exit(EXIT_FAILURE); // fork error, unlikely
- if (i>0) return; // parent returns
- // compress file and unlink
- // child continues
- i=fork();
- if (i<0) exit(EXIT_FAILURE);
- if (i>0) { // parent waits for child to compress
- waitpid(i, NULL, 0);
- if (type==1) {
- sprintf(command, "%s/%s", workdirectory, filename);
- unlink(command); }
- else {
- sprintf(command, "rm -rf %s/%s", workdirectory, filename);
- system(command);
- }
- exit(EXIT_SUCCESS); } // close process
- // second child continues to compression
- chdir(workdirectory);
- if (type==FILEENTRY)
- sprintf(command, "%s %s%s %s", compressionprogram, filename, compressionextension, filename);
- else
- sprintf(command, "%s %s %s%s %s", compressionprogram, compressiondirectoryoption, filename, compressionextension, filename);
- system(command);
- exit(EXIT_SUCCESS); // close process
- }
- // is file or directory
- int isFile(char* name)
- {
- DIR *directory=opendir(name);
- if (directory!=NULL) {
- closedir(directory);
- return 0; }
- if (errno==ENOTDIR)
- return 1;
- return -1; // nothing found
- }
- // return filesize in bytes
- off_t filesize(char *filename)
- {
- struct stat st;
- stat(filename, &st);
- return st.st_size;
- }
- // is entry eligible for processing
- int isEntryEligible(char *filename)
- {
- int i;
- i=isFile(filename);
- if (i==1) // file
- return FILEENTRY;
- if (!i && !findsimple(filename, skipdirectorystring))
- if (strcmp(filename, ".") && strcmp(filename, "..") && strcmp(filename, workdirectory))
- return DIRECTORYENTRY;
- return 0;
- }
- // find command in text
- int findsimple(char text[], const char token[])
- {
- int i, n, hit=0;
- char ttoken[MAXNAME];
- for (i=0;i<strlen(text);i++) {
- if (tolower(text[i])==token[0]) {
- hit=i+1;
- for (n=0;n<strlen(token) && (i+n)<strlen(text);n++)
- ttoken[n]=tolower(text[i+n]);
- ttoken[n]='\0';
- if (strcmp(ttoken, token))
- hit=0;
- else
- break; } }
- return hit;
- }
- // directory entries size summary
- int dirsum(const char *fpath, const struct stat *sb, int typeflag)
- {
- dirtotal=0;
- dirtotal += sb->st_size;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement