The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
NFS file handle question prathamesh Linux 0 06-01-2008 11:25 PM
How to handle multiple rows in a file ksmbabu Shell Programming and Scripting 2 05-14-2008 09:44 PM
Stale NFS file handle alphasahoo UNIX and Linux Applications 3 05-12-2008 09:31 AM
Stale NFS file handle rein UNIX for Advanced & Expert Users 5 01-30-2005 09:30 AM
NFS file handle kazimir UNIX for Advanced & Expert Users 2 07-24-2002 09:11 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-2006
Registered User
 

Join Date: Oct 2004
Posts: 215
deleting a file name by its handle

All,

I am having three function

1.open
2.process
3.close.


Open will open a file and process will process a file and close will close the file ..

I can able to close the file by its filehandler.Is there is anyway that i can get the file name by filehandle and remove it after closing the file .Please help ....

Thanks,
Arun.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-14-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
See 'man 2 stat'.
Reply With Quote
  #3 (permalink)  
Old 11-14-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,459
there is no simple way to get a filename from a file descriptor (handle).

If the file in question is a temporary file look into man tmpfile - the file is automatically deleted on close.

Otherwise, it is easier to have the shell script that passed the filename (to the C process) delete the file.

As last suggestion - pass the filename around as an argument

Code:
char global_filename[128]={0x0};

int openit(char *filename)
{
    int filedesc=open(filename, O_RDONLY);
    if(filesdesc < 0) { 
        perror("error opening file");
        exit(EXIT_FAILURE);
    }
    return filedesc;
}

void process(int fd)
{

}

void closeit(int fd)
{
     if(close(fd) <0) {

        perror("error closing file");
        exit(EXIT_FAILURE);
     }     
}

void deleteit(char *filename)
{
     remove(filename);
}

int main(int argc, char *argv[])
{
     int fd=openit(argv[1]);
     process(fd);
     closeit(fd);
     deleteit(argv[1]);
     return 0;
}
Reply With Quote
  #4 (permalink)  
Old 11-14-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,333
You can open the file and immediately remove it. Then simply use the file...because it is open by a process, the OS will not actually remove it until the final close. When you are finished with it, a simple close() will remove it from existence. And an exit() is good enough too, all opened files are closed on exit.
Reply With Quote
  #5 (permalink)  
Old 11-14-2006
Registered User
 

Join Date: Oct 2004
Posts: 215
Thanks all for your answer .

As i saw jim told in delete function it takes argument as filename. But i want to know whether it is possible to get the filehandle as argument and then by using it get the file name and delete the file. Since in design i am having the control of filehandle only . Please let me know.

Thanks Again,
Arun Kumar.
Reply With Quote
  #6 (permalink)  
Old 11-21-2006
Registered User
 

Join Date: Oct 2003
Posts: 69
I don't think you'll be able to get the file name. As suggested above, you can get information about the file (with the fstat() function call), but that doesn't help you because it won't give you a name.

What it will give you, however, is the device and id information about where the file is stored. My guess is that this information should be sufficient to unlink the file from the file system, however, if you *really* need to do this you'll have to start looking for the source code to "unlink" for your system. Then you'll need to piece together an unlink that can take the data from a file handle rather than a file name, and this will certainly be non-portable.

Regardless, methinks this is a tough nut to crack without having the file name available to you when you want to delete it.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0