Thread: unable to find file from inode

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    16

    unable to find file from inode

    The code I'm using works fine however when i get to my while loop to search the directory i cant seem to find the file based on its inode. I check to see what it is in terminal (ls -i) and then choose desired inode to pass as an argument to my program.

    Ive narrowed down the issue down to the following two pieces of code but cant figure out where i messed up.

    Code:
        ino_t inodeNum = strtol(argv[1], &endPtr, 10);
    Code:
    //traverse directory looking for file with matching inode
        while((dirStream = readdir(dirPtr)) != NULL && cont == 0)
        {
            printf(" Searching. \n");
            if(dirStream->d_ino == inodeNum)
            {
                //copy the file name
                strncpy(fileName, dirStream->d_name, fileBuff);
                //close directory
                closedir(dirPtr);
                //exit loop
                cont = 666;
            }
            if((dirStream = readdir(dirPtr)) == NULL)
            {
                printf(" Error finding file with matching inode.\n");
                printf(" Failed to remove directory\n");
                return 0;
            }
        }
    there are thee items in the directory so when it searches it loops 3 times before displaying the error messages once NULL is reached.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <sys/types.h>
    #include <dirent.h>
    
    int main(int argc, char **argv) {
        if (argc != 3) {
            fprintf(stderr, "Usage: ino DIRECTORY INODE\n");
            return EXIT_FAILURE;
        }
    
        ino_t inode = atoi(argv[2]);
    
        DIR *dir = opendir(argv[1]);
        if (!dir) {
            perror("opendir");
            return EXIT_FAILURE;
        }
    
        bool found = false;
        struct dirent *ent;
        while ((ent = readdir(dir)) != NULL) {
            if (ent->d_ino == inode) {
                found = true;
                break;
            }
        }
        closedir(dir);
    
        if (found)
            printf("Found: %s\n", ent->d_name);
        else
            printf("Not found.\n");
    
        return 0;
    }
    Last edited by john.c; 07-16-2018 at 02:09 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    16
    The code i had is very similar to yours. Can you point out where i messed up? Also the program im writing assumes you are already in the desired directory. You are given an inode number and want to pass that so i only have 2 arguments.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Calling readdir twice per loop is bad.
    Especially after you closed your dirstream.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    16
    Ohhhhh wow. Now i see it. Thank you so much! the code now works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to find values
    By camateur in forum C Programming
    Replies: 1
    Last Post: 06-08-2017, 06:52 AM
  2. Replies: 7
    Last Post: 03-19-2010, 12:16 AM
  3. Windows 7: Program unable to find glut/glfw dll?
    By Jake.c in forum Game Programming
    Replies: 1
    Last Post: 10-29-2009, 03:59 PM
  4. How to obtain Inode statistics for a file
    By NuNn in forum C Programming
    Replies: 1
    Last Post: 02-11-2009, 11:41 PM
  5. Performing File operations using File Inode number
    By rak1986 in forum C Programming
    Replies: 4
    Last Post: 09-22-2008, 02:43 AM

Tags for this Thread