fopen? fails w/ mounted dvd

This is a discussion on fopen? fails w/ mounted dvd within the Linux Programming forums, part of the Platform Specific Boards category; So the fseek/ftell method seems more reliable than stat.st_size (for acquiring file size) albeit much slower. ...except i get irregular ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    fopen? fails w/ mounted dvd

    So the fseek/ftell method seems more reliable than stat.st_size (for acquiring file size) albeit much slower.

    ...except i get irregular results calling on mounted dvd's (burnt by me on redhat7). Sometimes this is a null pointer from fopen, but usually the program completes without reporting an error, but also without collecting any file sizes. I haven't had any other problems with the dvd's.

    Here's the code -- it works almost exactly like du -h DIR (the code for which I haven't looked at). du b/t/w does work where this doesn't:

    Code:
    #include <stdio.h>
    #include <dirent.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h>
    
    int sw1=0;
    float totalsize; // kb count
    
    int dodir (char *doname) {
            long int bytes, testbytes;      // "testbytes" is needed 
            float dividit;                  // for the return value error check 
            char nextfile[256];             // since "bytes" is +=
            struct dirent *dcon;
            FILE *fh;
        
            if (sw1 != 0) printf ("\t%s ", doname);
        
            DIR *dir = opendir (doname); 
            if (dir == NULL ) { 
                    printf ("For Directory %s ERROR: %s\n", doname, strerror(errno)); 
                    exit(1);
            }
        
            bytes = 0;
            while (dcon = readdir(dir)) {
                    if (dcon->d_type == DT_REG) {   
                            strcpy(nextfile, doname);
                            strcat(nextfile, "/");
                            strcat(nextfile, dcon->d_name);
                            fh = fopen (nextfile, "r");
                            if (fh == NULL) 
                                    printf ("\t\tFor regular file %s ERROR: %s\n", nextfile, strerror(errno));
                            if ((fseek(fh,0,SEEK_END)) != 0)  
                                    printf ("\t\tfseek ERROR (%s)\n", nextfile);
                            if ((testbytes = ftell(fh)) == -1) 
                                    printf ("ftell ERROR (%s)\n", nextfile);
                            else bytes += testbytes; 
                            fclose(fh);
                    }
            }
            dividit = bytes; 
            dividit /= 1024; // gives kb 
            totalsize += dividit;
            if (sw1 == 0) printf (" (%.2f kb):\n", dividit);
            else if (sw1 >= 1) printf ("\t%.2f kb\n", dividit);
    
            sw1++;
            rewinddir(dir);
    
            while (dcon = readdir(dir)) {
                    if ((strcmp (dcon->d_name, "..")) == 0 || (strcmp(dcon->d_name, ".")) == 0)     
                         continue;
                    strcpy(nextfile, doname);
                    strcat(nextfile, "/");
                    strcat(nextfile, dcon->d_name);
                    if (dcon->d_type == DT_DIR) dodir(nextfile);
            }
            closedir(dir);
    }
    
    int main (int argc, char *argv[]) {
            char unit[6];
    
            if (argc < 2) 
                    puts ("Directory pathname required on command-line.");
                    
            strcpy(unit, "kb");
            printf ("In %s", argv[1]);
    
            dodir(argv[1]);
    
            if (totalsize >= 1024) { totalsize /= 1024; strcpy(unit, "Mb"); }
            if (totalsize >= 1024) { totalsize /= 1024; strcpy(unit, "GB"); }
            printf ("Total size of regular files: %.2f %s\n", totalsize,unit);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    Do you have large file support enabled ?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "Do you have large file support enabled ?"

    No, but none of the files are bigger than a gigabyte.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    But what about the sum of all the sizes?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The sum of all sizes!!!

    Depends on the size of the tree. The code will add up over 4 GB fine (as long as no individual file is more than 4). I can sit and watch it do a whole harddrive, so one dvd shouldn't present a problem.

    Of course, all the sizes are floats...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Since you are printing sterror(errno) when the fopen() fails... Why not tell us which error code is reported?

    The fixed-size buffer for filenames is suspicious as well. 256 seems big but may not be big enough. You can avoid the whole issue by chdir()'ing into the directory and opening by relative path, then chdir()'ing back out again.

    Large file support may not be the problem... But in any case, you should probably turn it on. Why artificially restrict your utility when it is easy to do otherwise? Compile with this:

    -D_FILE_OFFSET_BITS=64

    And use the fseeko() and ftello() functions with off_t instead of fseek() and ftell() with long.

    Also, why not just stat() the file to get its size?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 08:35 AM
  3. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 06:28 AM
  4. Mounting & detecting DVD ROM
    By g4j31a5 in forum Linux Programming
    Replies: 2
    Last Post: 07-18-2007, 09:16 PM
  5. Watching a dvd film.
    By adrianxw in forum Tech Board
    Replies: 5
    Last Post: 03-11-2003, 02:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21