Сервер / Клиент на основе канала (не FIFO) проверки прав доступа к файлам C ++ - PullRequest
0 голосов
/ 28 мая 2019

Мне нужно создать программу клиент / сервер с конвейером (не FIFO), где пользователь вводит имя файла и восьмеричное разрешение, а сервер возвращает «YES», если восьмеричное разрешение файла совпадает с разрешением ввода, и «NO» в противном случае.

Я создал 2 канала, один для имени файла и один для восьмеричного.Я не знаю, как проверить свойство файла внутри C ++.Это моя первая программа, которую я создал, поэтому думаю, что у меня может быть много ошибок.Спасибо за вашу поддержку!

#include<stdio.h> 
#include<stdlib.h> 
#include<unistd.h> 
#include<sys/types.h> 
#include<string.h> 
#include<sys/wait.h> 
#include <sys/stat.h>

int main() 
{ 
    // We use two pipes 
    // First pipe to send input string from parent 
    // Second pipe to send concatenated string from child 

    int fd1[2];  // Used to store two ends of first pipe 
    int fd2[2];  // Used to store two ends of second pipe 

    char octal[7]; 
    char input_str[100]; 

    pid_t p; 

    if (pipe(fd1)==-1) 
    { 
        fprintf(stderr, "Pipe Failed" ); 
        return 1; 
    } 
    if (pipe(fd2)==-1) 
    { 
        fprintf(stderr, "Pipe Failed" ); 
        return 1; 
    } 

    scanf("%s", input_str); 
    scanf("%d", octal);
    p = fork(); 

    if (p < 0) 
    { 
        fprintf(stderr, "fork Failed" ); 
        return 1; 
    } 

    // Parent process 
    else if (p > 0) 
    { 
        char answear[100]; 

        close(fd1[0]);  // Close reading end of first pipe 

        // Write input string and close writing end of first 
        // pipe. 
        write(fd1[1], input_str, strlen(input_str)+1); 
    write(fd2[1], octal, 7); 
    close(fd2[1]);
        close(fd1[1]); 

        // Wait for child to send a string 
        wait(NULL); 

        // Read string from child, print it and close 
        // reading end. 
        read(fd2[0], answear, 100); 
        printf("The answear is %s\n", answear); 
        close(fd2[0]); 
    } 

    // child process 
    else
    { 
        close(fd1[1]);  // Close writing end of first pipe 
    close(fd2[1]);  // Close writing end of secound pipe
        // Read a string using first pipe 
        char file_name[100]; 
        read(fd1[0], file_name, 100); 
    // Read a int using secound pipe 
    int octal2[7];
    read(fd2[0], octal2, 7);

        // verify the octal permission of the file and give the result on the answear
        struct stat buf;  
    stat(file_name, &buf);
    int statchmod = buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    if (statchmod == octal2[0])
        char answear[]="Totul e OK!"; //OK
    else
        chmod(file_name, S_IRWXU);
        char answear[]="Drepturile au fost modificate"; //not OK
        // Close both reading ends 
        close(fd1[0]); 
        close(fd2[0]); 


        // Write concatenated string and close writing end 
        write(fd2[1], answear, 100); 
        close(fd2[1]); 

        exit(0); 
    } 
} 

Теперь результат не отображается, если что-то изменилось.Он показывает просто «Результат есть».

...