Advertisement
Vladislav8653

Untitled

Dec 17th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <dirent.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/wait.h>
  10.  
  11. #define MAX_PATH_LENGTH 4096
  12. #define MAX_FILENAME_LENGTH 256
  13. #define MAX_CHILD_PROCESSES 10
  14.  
  15. typedef struct {
  16. char filename[MAX_FILENAME_LENGTH];
  17. off_t size;
  18. } FileInfo;
  19.  
  20. int compareBySize(const void *a, const void *b) {
  21. return ((FileInfo *)a)->size - ((FileInfo *)b)->size;
  22. }
  23.  
  24. int compareByName(const void *a, const void *b) {
  25. return strcmp(((FileInfo *)a)->filename, ((FileInfo *)b)->filename);
  26. }
  27.  
  28. void copyFile(const char *sourcePath, const char *destPath) {
  29. // ... (unchanged)
  30. }
  31.  
  32. void sortAndCopyFiles(const char *dirPath, int sortBy, const char *destPath, int maxProcesses) {
  33. DIR *dir = opendir(dirPath);
  34. if (dir == NULL) {
  35. perror("Error opening directory");
  36. exit(EXIT_FAILURE);
  37. }
  38.  
  39. struct dirent *entry;
  40. struct stat fileInfo;
  41.  
  42. int numProcesses = 0;
  43. int numFiles = 0;
  44. FileInfo *files = malloc(MAX_CHILD_PROCESSES * sizeof(FileInfo));
  45.  
  46. while ((entry = readdir(dir)) != NULL) {
  47. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  48. continue;
  49.  
  50. char filePath[MAX_PATH_LENGTH];
  51. snprintf(filePath, sizeof(filePath), "%s/%s", dirPath, entry->d_name);
  52.  
  53. if (lstat(filePath, &fileInfo) == -1) {
  54. perror("Error getting file info");
  55. exit(EXIT_FAILURE);
  56. }
  57.  
  58. if (S_ISREG(fileInfo.st_mode)) {
  59. strncpy(files[numFiles].filename, entry->d_name, MAX_FILENAME_LENGTH - 1);
  60. files[numFiles].size = fileInfo.st_size;
  61. numFiles++;
  62.  
  63. if (numFiles >= MAX_CHILD_PROCESSES) {
  64. qsort(files, numFiles, sizeof(FileInfo), (sortBy == 1) ? compareBySize : compareByName);
  65.  
  66. for (int i = 0; i < numFiles; i++) {
  67. pid_t childPid = fork();
  68. if (childPid == -1) {
  69. perror("Error forking process");
  70. exit(EXIT_FAILURE);
  71. }
  72.  
  73. if (childPid == 0) { // Child process
  74. printf("PID: %d, Path: %s, Filename: %s, Size: %ld bytes\n", getpid(), filePath, files[i].filename, files[i].size);
  75.  
  76. char destFilePath[MAX_PATH_LENGTH];
  77. snprintf(destFilePath, sizeof(destFilePath), "%s/%s", destPath, files[i].filename);
  78.  
  79. copyFile(filePath, destFilePath);
  80. exit(EXIT_SUCCESS);
  81. } else { // Parent process
  82. numProcesses++;
  83. if (numProcesses >= maxProcesses) {
  84. wait(NULL); // Wait for any child process to finish
  85. numProcesses--;
  86. }
  87. }
  88. }
  89.  
  90. numFiles = 0;
  91. }
  92. }
  93. }
  94.  
  95. closedir(dir);
  96. free(files);
  97. }
  98.  
  99. int main(int argc, char *argv[]) {
  100. // ... (unchanged)
  101.  
  102. int sortBy = atoi(argv[2]);
  103.  
  104. if (sortBy != 1 && sortBy != 2) {
  105. fprintf(stderr, "Invalid sort criteria. Use 1 for sorting by size or 2 for sorting by name.\n");
  106. exit(EXIT_FAILURE);
  107. }
  108.  
  109. // ... (unchanged)
  110.  
  111. sortAndCopyFiles(dirPath, sortBy, destPath, maxProcesses);
  112.  
  113. return 0;
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement