Advertisement
Guest User

Untitled

a guest
Apr 11th, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. /*IDE : Nano Text Editor
  2. **OS : Debian 9
  3. **
  4. **AUTHOR : xxx
  5. ** Mail: xxx
  6. **
  7. **BRIEF : This program is to sync the files in one folder to another folder.
  8. **
  9. **NOT : This program for my Operating System Lab. class homework.
  10. */
  11.  
  12.  
  13. #include <unistd.h>
  14. #include <iostream>
  15. #include <sys/wait.h>
  16. #include <time.h>
  17. #include <sys/stat.h>
  18. #include <pthread.h>
  19. #include <string>
  20. #include <sstream>
  21. #include <fstream>
  22.  
  23. using namespace std;
  24.  
  25. //GLOBAL VARIABLES//
  26. struct pathStruct{ //Can pass thread arguments needed
  27. string rep;
  28. string sub;
  29. };
  30. 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.
  31. int runningTime = 120; //This is the determine program running time. 1 = 1 second
  32. ///////////////////
  33.  
  34. /*Brief: To clear the main I checked arguments in a function.
  35. **params: Standart main arguments
  36. */
  37. void argControl(int argc, char* argv[]);
  38.  
  39. /*Brief: Continously printing date on the screen and if changes a file, writing logs to file and sync the folders.
  40. **params: struct pathStruct - Taking one struct that containing two file path info.
  41. */
  42. void* time_work(void *pargs);
  43.  
  44. /*Brief: Just checking last modified time and if modified time changed, notify the time_work thread
  45. **params: struct pathStruct - Taking one struct that containing two file path info.
  46. */
  47. void* checkChanging(void *pargs);
  48.  
  49. /*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.
  50. **Params: String - taking string variable that contains you wanna run on system.
  51. */
  52. string systemRead(string command);
  53.  
  54. /*Brief: Print logs to screen and giving order to write logs file.
  55. **Params: Repasitory and submission path
  56. */
  57. void printLogs(string pathRep, string pathSub);
  58.  
  59. /*Brief: Just writing to file the txt
  60. **Params: string txt - Text to writing file, string pathT - writing which file
  61. */
  62. void fileWrite(string txt, string pathT);
  63.  
  64. int main(int argc, char* argv[])
  65. {
  66. 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;
  67. argControl(argc, argv);
  68.  
  69. struct pathStruct pargs;
  70. pargs.rep = argv[1];
  71. pargs.sub = argv[2];
  72.  
  73. pthread_t timeThread, changeThread;
  74.  
  75. pthread_create(&timeThread, NULL, time_work, (void *)&pargs);
  76. pthread_create(&changeThread, NULL, &checkChanging, (void *)&pargs);
  77.  
  78. void *result;
  79. pthread_join(timeThread, &result);
  80. pthread_join(changeThread, &result);
  81.  
  82. cout << endl << "Parent funciton is terminating..." << endl;
  83.  
  84. return 0;
  85. }
  86.  
  87. void argControl(int argc, char* argv[])
  88. {
  89. cout << endl; //Terminalde ilk satırı üstünde boşluk bırakarak daha estetik görünmesi için.
  90.  
  91. if (argc != 3)
  92. {
  93. cout<< "Program arguments count 3! (./uyg3 PathRep PathD), please correct and try again" << endl
  94. << "Example : ./sync path/to/repository path/to/submission" << endl << endl;
  95. exit(-1);
  96. }
  97.  
  98. struct stat buffer;
  99. string pathT1(argv[1]);
  100. pathT1 += "/";
  101. if(stat(pathT1.c_str(), &buffer))
  102. {
  103. cout<< "There is no existing directory named as '" << argv[1] << "'" << endl
  104. << "Please correctly enter paths..." << endl << endl;
  105. exit(-1);
  106. }
  107.  
  108. string pathT2(argv[2]);
  109. pathT2 += "/";
  110. if(stat(pathT2.c_str(), &buffer))
  111. {
  112. cout<< "There is no existing directory named as '" << argv[2] << "'" << endl
  113. << "Please correctly enter paths..." << endl << endl;
  114. exit(-1);
  115. }
  116.  
  117. if(pathT1 == pathT2)
  118. {
  119. cout << "Repository and submission directory cannot have same path." << endl << endl;
  120. exit(-1);
  121. }
  122.  
  123. }
  124.  
  125. void* time_work(void *pargs) //timeThread function
  126. {
  127. struct pathStruct *paths = (struct pathStruct *)pargs;
  128. string pathRep = paths->rep;
  129. string pathSub = paths->sub;
  130.  
  131. int i = runningTime*10;
  132. while(i--)
  133. {
  134. cout << "\r\033[1A" << systemRead("date") << flush;
  135. if(changeCount)
  136. {
  137. systemRead("rsync -a " + pathSub + " " + pathRep);
  138. printLogs(pathRep, pathSub);
  139. changeCount--;
  140. }
  141. usleep(100000);
  142. }
  143. }
  144.  
  145. void* checkChanging(void *pargs) //changeThread function
  146. {
  147. struct pathStruct *paths = (struct pathStruct *)pargs;
  148. string pathRep = paths->rep;
  149. string pathSub = paths->sub;
  150.  
  151. string command = "date -r " + pathSub;
  152. string oldTime = systemRead(command.c_str());
  153. string tmpTime;
  154.  
  155. int i = runningTime;
  156. while(i--)
  157. {
  158. tmpTime = systemRead(command.c_str());
  159. if(oldTime.compare(tmpTime))
  160. {
  161. changeCount++;
  162. oldTime = systemRead(command.c_str());
  163. }
  164. sleep(1);
  165. }
  166. }
  167.  
  168. string systemRead(string command)
  169. {
  170. string temp = "";
  171. char buffalo[100] = "";
  172. FILE *fp;
  173.  
  174. fp = popen(command.c_str(), "r");
  175. while(fgets(buffalo, sizeof(buffalo), fp)) temp += buffalo;
  176. pclose(fp);
  177. return temp;
  178. }
  179.  
  180. void fileWrite(string txt, string pathT)
  181. {
  182. struct stat buffer;
  183. if(stat(pathT.c_str(), &buffer))
  184. {
  185. systemRead("touch "+ pathT);
  186. }
  187. ofstream fileT;
  188. fileT.open(pathT, ios_base::app);
  189. fileT << txt;
  190. fileT.close();
  191. return;
  192. }
  193.  
  194. void printLogs(string pathRep, string pathSub)
  195. {
  196. //Printing file list to log file
  197. string logFileStr = "\n\n##############################################\n";
  198. logFileStr += "Log File Updated : " + systemRead("date");
  199. logFileStr += "\n" + systemRead("find " + pathSub);
  200. fileWrite(logFileStr, pathRep + "/logs.log");
  201.  
  202. //Printing submission folder
  203. cout<< "\r\033[1A" << flush
  204. << "######################################################################" << endl
  205. << "Last modified time: " << systemRead("date -r " + pathSub)
  206. << endl << "SUB DIRECTORY : " << systemRead("du -sh " + pathSub)
  207. << "-----------------------------------------------" << endl
  208. << systemRead("du -sh " + pathSub)
  209. << systemRead("du -sh " + pathSub + "/*") << endl;
  210.  
  211. //Printing subfolders of submission folder
  212. stringstream listSub(systemRead("realpath $(ls -d " + pathSub + "/*/)"));
  213. string tokenPath;
  214.  
  215. while(listSub >> tokenPath)
  216. {
  217. cout << endl << "SUB DIRECTORY : "<< systemRead("du -sh " + tokenPath);
  218. if(systemRead("du -sh " + tokenPath).substr(0,1)[0] == '0')
  219. {
  220. cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
  221. continue;
  222. }
  223. cout << "-----------------------------------------------" << endl;
  224. cout << systemRead("du -sh " + tokenPath + "/*") << endl;
  225. }
  226. cout << "######################################################################" << endl;
  227. cout << endl << endl;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement