Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*IDE : Nano Text Editor
- **OS : Debian 9
- **
- **AUTHOR : xxx
- ** Mail: xxx
- **
- **BRIEF : This program is to sync the files in one folder to another folder.
- **
- **NOT : This program for my Operating System Lab. class homework.
- */
- #include <unistd.h>
- #include <iostream>
- #include <sys/wait.h>
- #include <time.h>
- #include <sys/stat.h>
- #include <pthread.h>
- #include <string>
- #include <sstream>
- #include <fstream>
- using namespace std;
- //GLOBAL VARIABLES//
- struct pathStruct{ //Can pass thread arguments needed
- string rep;
- string sub;
- };
- int changeCount = 0; //This variable increases when files change. When sync is done decreases. If sync work last too long, new changes added queue. Up to 2, 3, 4... To be sure all files be synced, I applied this method.
- int runningTime = 120; //This is the determine program running time. 1 = 1 second
- ///////////////////
- /*Brief: To clear the main I checked arguments in a function.
- **params: Standart main arguments
- */
- void argControl(int argc, char* argv[]);
- /*Brief: Continously printing date on the screen and if changes a file, writing logs to file and sync the folders.
- **params: struct pathStruct - Taking one struct that containing two file path info.
- */
- void* time_work(void *pargs);
- /*Brief: Just checking last modified time and if modified time changed, notify the time_work thread
- **params: struct pathStruct - Taking one struct that containing two file path info.
- */
- void* checkChanging(void *pargs);
- /*Brief: Program contains a lot of system call. And system calls' deafult is printing result to terminal. This is prevent that and just taking string parameters.
- **Params: String - taking string variable that contains you wanna run on system.
- */
- string systemRead(string command);
- /*Brief: Print logs to screen and giving order to write logs file.
- **Params: Repasitory and submission path
- */
- void printLogs(string pathRep, string pathSub);
- /*Brief: Just writing to file the txt
- **Params: string txt - Text to writing file, string pathT - writing which file
- */
- void fileWrite(string txt, string pathT);
- int main(int argc, char* argv[])
- {
- cout << "Because of this program a trial, it is set to run maximum two minutes. \n(You can change 'runningTime' variable at 'Global Variables' section.) " << endl;
- argControl(argc, argv);
- struct pathStruct pargs;
- pargs.rep = argv[1];
- pargs.sub = argv[2];
- pthread_t timeThread, changeThread;
- pthread_create(&timeThread, NULL, time_work, (void *)&pargs);
- pthread_create(&changeThread, NULL, &checkChanging, (void *)&pargs);
- void *result;
- pthread_join(timeThread, &result);
- pthread_join(changeThread, &result);
- cout << endl << "Parent funciton is terminating..." << endl;
- return 0;
- }
- void argControl(int argc, char* argv[])
- {
- cout << endl; //Terminalde ilk satırı üstünde boşluk bırakarak daha estetik görünmesi için.
- if (argc != 3)
- {
- cout<< "Program arguments count 3! (./uyg3 PathRep PathD), please correct and try again" << endl
- << "Example : ./sync path/to/repository path/to/submission" << endl << endl;
- exit(-1);
- }
- struct stat buffer;
- string pathT1(argv[1]);
- pathT1 += "/";
- if(stat(pathT1.c_str(), &buffer))
- {
- cout<< "There is no existing directory named as '" << argv[1] << "'" << endl
- << "Please correctly enter paths..." << endl << endl;
- exit(-1);
- }
- string pathT2(argv[2]);
- pathT2 += "/";
- if(stat(pathT2.c_str(), &buffer))
- {
- cout<< "There is no existing directory named as '" << argv[2] << "'" << endl
- << "Please correctly enter paths..." << endl << endl;
- exit(-1);
- }
- if(pathT1 == pathT2)
- {
- cout << "Repository and submission directory cannot have same path." << endl << endl;
- exit(-1);
- }
- }
- void* time_work(void *pargs) //timeThread function
- {
- struct pathStruct *paths = (struct pathStruct *)pargs;
- string pathRep = paths->rep;
- string pathSub = paths->sub;
- int i = runningTime*10;
- while(i--)
- {
- cout << "\r\033[1A" << systemRead("date") << flush;
- if(changeCount)
- {
- systemRead("rsync -a " + pathSub + " " + pathRep);
- printLogs(pathRep, pathSub);
- changeCount--;
- }
- usleep(100000);
- }
- }
- void* checkChanging(void *pargs) //changeThread function
- {
- struct pathStruct *paths = (struct pathStruct *)pargs;
- string pathRep = paths->rep;
- string pathSub = paths->sub;
- string command = "date -r " + pathSub;
- string oldTime = systemRead(command.c_str());
- string tmpTime;
- int i = runningTime;
- while(i--)
- {
- tmpTime = systemRead(command.c_str());
- if(oldTime.compare(tmpTime))
- {
- changeCount++;
- oldTime = systemRead(command.c_str());
- }
- sleep(1);
- }
- }
- string systemRead(string command)
- {
- string temp = "";
- char buffalo[100] = "";
- FILE *fp;
- fp = popen(command.c_str(), "r");
- while(fgets(buffalo, sizeof(buffalo), fp)) temp += buffalo;
- pclose(fp);
- return temp;
- }
- void fileWrite(string txt, string pathT)
- {
- struct stat buffer;
- if(stat(pathT.c_str(), &buffer))
- {
- systemRead("touch "+ pathT);
- }
- ofstream fileT;
- fileT.open(pathT, ios_base::app);
- fileT << txt;
- fileT.close();
- return;
- }
- void printLogs(string pathRep, string pathSub)
- {
- //Printing file list to log file
- string logFileStr = "\n\n##############################################\n";
- logFileStr += "Log File Updated : " + systemRead("date");
- logFileStr += "\n" + systemRead("find " + pathSub);
- fileWrite(logFileStr, pathRep + "/logs.log");
- //Printing submission folder
- cout<< "\r\033[1A" << flush
- << "######################################################################" << endl
- << "Last modified time: " << systemRead("date -r " + pathSub)
- << endl << "SUB DIRECTORY : " << systemRead("du -sh " + pathSub)
- << "-----------------------------------------------" << endl
- << systemRead("du -sh " + pathSub)
- << systemRead("du -sh " + pathSub + "/*") << endl;
- //Printing subfolders of submission folder
- stringstream listSub(systemRead("realpath $(ls -d " + pathSub + "/*/)"));
- string tokenPath;
- while(listSub >> tokenPath)
- {
- cout << endl << "SUB DIRECTORY : "<< systemRead("du -sh " + tokenPath);
- if(systemRead("du -sh " + tokenPath).substr(0,1)[0] == '0')
- {
- cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
- continue;
- }
- cout << "-----------------------------------------------" << endl;
- cout << systemRead("du -sh " + tokenPath + "/*") << endl;
- }
- cout << "######################################################################" << endl;
- cout << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement