Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <pthread.h>
- #define MAX_PATH_LENGTH 4096
- #define MAX_FILENAME_LENGTH 256
- #define MAX_CHILD_PROCESSES 10
- typedef struct {
- char filename[MAX_FILENAME_LENGTH];
- off_t size;
- const char *destPath; // Добавлено поле для хранения destPath
- } FileInfo;
- int compareBySize(const void *a, const void *b) {
- return ((FileInfo *)a)->size - ((FileInfo *)b)->size;
- }
- int compareByName(const void *a, const void *b) {
- return strcmp(((FileInfo *)a)->filename, ((FileInfo *)b)->filename);
- }
- void copyFile(const char *sourcePath, const char *destPath) {
- int sourceFile = open(sourcePath, O_RDONLY);
- int destFile = open(destPath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- char buffer[4096];
- ssize_t bytesRead, bytesWritten;
- while ((bytesRead = read(sourceFile, buffer, sizeof(buffer))) > 0) {
- bytesWritten = write(destFile, buffer, bytesRead);
- if (bytesWritten != bytesRead) {
- perror("Write error");
- exit(EXIT_FAILURE);
- }
- }
- close(sourceFile);
- close(destFile);
- }
- void *threadFunction(void *arg) {
- FileInfo *fileInfo = (FileInfo *)arg;
- printf("Thread ID: %lu, Filename: %s, Size: %ld bytes\n", pthread_self(), fileInfo->filename, (long)fileInfo->size);
- // Construct destination file path
- char destFilePath[MAX_PATH_LENGTH];
- snprintf(destFilePath, sizeof(destFilePath), "%s/%s", fileInfo->destPath, fileInfo->filename);
- copyFile(fileInfo->filename, destFilePath);
- return NULL;
- }
- void sortAndCopyFiles(const char *dirPath, int sortBy, const char *destPath, int maxThreads) {
- DIR *dir = opendir(dirPath);
- if (dir == NULL) {
- perror("Error opening directory");
- exit(EXIT_FAILURE);
- }
- struct dirent *entry;
- struct stat fileInfo;
- int numThreads = 0;
- int numFiles = 0;
- FileInfo *files = malloc(MAX_CHILD_PROCESSES * sizeof(FileInfo));
- while ((entry = readdir(dir)) != NULL) {
- if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
- continue;
- char filePath[MAX_PATH_LENGTH];
- snprintf(filePath, sizeof(filePath), "%s/%s", dirPath, entry->d_name);
- if (lstat(filePath, &fileInfo) == -1) {
- perror("Error getting file info");
- exit(EXIT_FAILURE);
- }
- if (S_ISREG(fileInfo.st_mode)) {
- strncpy(files[numFiles].filename, entry->d_name, MAX_FILENAME_LENGTH - 1);
- files[numFiles].size = fileInfo.st_size;
- files[numFiles].destPath = destPath; // Передаем destPath в структуру FileInfo
- numFiles++;
- if (numFiles >= MAX_CHILD_PROCESSES) {
- qsort(files, numFiles, sizeof(FileInfo), (sortBy == 1) ? compareBySize : compareByName);
- for (int i = 0; i < numFiles; i++) {
- pthread_t thread;
- if (pthread_create(&thread, NULL, threadFunction, &files[i]) != 0) {
- perror("Error creating thread");
- exit(EXIT_FAILURE);
- }
- if (pthread_join(thread, NULL) != 0) {
- perror("Error joining thread");
- exit(EXIT_FAILURE);
- }
- numThreads++;
- if (numThreads >= maxThreads) {
- usleep(1000); // Sleep for 1 millisecond to allow threads to finish
- numThreads--;
- }
- }
- numFiles = 0;
- }
- }
- }
- closedir(dir);
- free(files);
- }
- int main(int argc, char *argv[]) {
- if (argc != 4) {
- fprintf(stderr, "Usage: %s <source_directory> <sort_criteria> <destination_directory>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- const char *dirPath = argv[1];
- int sortBy = atoi(argv[2]);
- const char *destPath = argv[3];
- if (sortBy != 1 && sortBy != 2) {
- fprintf(stderr, "Invalid sort criteria. Use 1 for sorting by size or 2 for sorting by name.\n");
- exit(EXIT_FAILURE);
- }
- int maxThreads;
- printf("Enter the maximum number of threads: ");
- scanf("%d", &maxThreads);
- sortAndCopyFiles(dirPath, sortBy, destPath, maxThreads);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement