Thread: copying files from a directory into another one

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    copying files from a directory into another one

    Hello there!
    I tried to make an exercise in which i had to COPY *regular* files which have *execution* rights from a source directory into a new one (to be created if it doesn't exist). The 2 directories have are passed in the command line.
    My solution seems to work, although I think the most elegant think would be to set the rights for the copy in the same way they're set for the original... I tried to do this with this line:

    Code:
       if ( (fdDest=open(path_copia,O_CREAT|O_TRUNC|O_WRONLY,(statbuff.st_mode &S_IRWXU)|(statbuff.st_mode &S_IRWXG)|(statbuff.st_mode &S_IRWXO) ) ) <0 )
    but it seems to have problem for the group rights which are not copied identically in the new file.. What is wrong in your opinion?
    many thanks! Bye

    Code:
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char *argv[])
    {
       if (argc!=3)
       {
          printf("USAGE: programname source destination\n");
          return 1;
       }
    
       DIR *srcPtr;
       
       if (mkdir(argv[2],0777)<0)
       {
          printf("Unable to create directory %s (probably it already exists)\n",argv[2]);
       }
       srcPtr=opendir(argv[1]);
       if (srcPtr==NULL)
       {
          printf("BAD OPENDIR %s\n",argv[1]);
          return 1;
       }
       
       struct dirent *dirPtr;
       struct stat statbuff;
       int fdSrc,fdDest;
       char *path_copia;
       unsigned int len_path_copia;
       unsigned char c;
    
       chdir(argv[1]);
       while ((dirPtr=readdir(srcPtr))!=NULL)
       {
          if (lstat(dirPtr->d_name,&statbuff)<0)
          {
             printf("BAD LSTAT\n");
             return 1;
          }
          if (S_ISREG(statbuff.st_mode) &&
             (statbuff.st_mode & S_IXUSR )) //IT's A REGULAR FILE WITH EXECUTION rights for the owner
          {
             //COPY----
             len_path_copia=strlen(argv[2]);
             len_path_copia+=1+strlen(dirPtr->d_name);
             path_copia=(char *)malloc(len_path_copia*sizeof(char));
             
             strcpy(path_copia,argv[2]);
             strcat(path_copia,dirPtr->d_name);
             printf("Copying %s in %s\n",dirPtr->d_name,path_copia);
             if ( (fdDest=open(path_copia,O_CREAT|O_TRUNC|O_WRONLY,(statbuff.st_mode &S_IRWXU)|(statbuff.st_mode &S_IRWXG)|(statbuff.st_mode &S_IRWXO) ) ) <0 )
             {
                printf("BAD OPEN %s\n",path_copia);
                return 1;
             }
             if ( (fdSrc=open(dirPtr->d_name,O_RDONLY))<0 )
             {
                printf("BAD OPEN %s\n",dirPtr->d_name);
                return 1;
             }
             while (read(fdSrc,&c,1)==1)
             {
                write(fdDest,&c,1);
             }
             close(fdSrc);
             close(fdDest);
             free(path_copia);
          }
          
       }
       chdir("..");
       closedir (srcPtr);
       return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    (statbuff.st_mode &S_IRWXU)|(statbuff.st_mode &S_IRWXG)|(statbuff.st_mode &S_IRWXO)
    The commutative rules of & and | states that this is the same thing, but much shorter and easier to read:
    Code:
    (statbuff.st_mode & S_IRWXU | S_IRWXG | S_IRWXO)
    I'm not sure why your file would have differnet rules than what you open it as - but perhaps your umask is affecting the mode?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If the file is read-only, you won't be able to write to it.
    Write the file, close it, then set the required permissions.

    Also check the http://www.research.att.com/~gsf/man/man1/umask.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    umask setting affected permission bits of the created files.
    You can either:
    umask(0);
    somewhere near start of your program,
    or, after file was created
    fchmod(filedes, permissions)

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by Salem View Post
    If the file is read-only, you won't be able to write to it.
    Write the file, close it, then set the required permissions.

    Also check the http://www.research.att.com/~gsf/man/man1/umask.html
    yes it's true i forgot to add
    Code:
    umask (0);
    at the beginning of the program...
    thanks!

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by smoking81 View Post
    yes it's true i forgot to add
    Code:
    umask (0);
    at the beginning of the program...
    thanks!
    That is not the best solution. The umask is designed to give the user a certain amount of control over the default permissions of files. Unless you have a very good reason, and this isn't one, you should not override the umask provided by the user's environment. The better solution is to explicitly chmod() the file to the proper mode.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    1
    Can't you give me pls an example of a program that copies even the files from a subdirectory ...
    I tryed to figure it out and somebody told me that i most do it recursivly and i don't know how to do it
    Pls help me!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from the fact that you are waking an old thread whichis against the forum rules, so I should not reply to this thread and make it worse, what is wrong with the posted code at the beginning of the thread?

    Edit: Ah sorry: The above program doesn't recurse through subdirectories. There is, however, a FAQ entry for "how to list files in a directory" which should help you solve that problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying all files in a directory
    By rak1986 in forum C Programming
    Replies: 2
    Last Post: 08-25-2008, 01:02 AM
  2. deleting all files in a directory using c..
    By ShadeS_07 in forum C Programming
    Replies: 6
    Last Post: 07-30-2008, 08:21 AM
  3. Replies: 4
    Last Post: 07-24-2008, 09:02 AM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  5. searching files in directory
    By lobo in forum Windows Programming
    Replies: 5
    Last Post: 10-05-2001, 03:00 AM