deleting a file name by its handle


 
Thread Tools Search this Thread
Top Forums Programming deleting a file name by its handle
# 1  
Old 11-14-2006
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.
# 2  
Old 11-14-2006
See 'man 2 stat'.
# 3  
Old 11-14-2006
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;
}

# 4  
Old 11-14-2006
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.
# 5  
Old 11-15-2006
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.
# 6  
Old 11-21-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Difference between handle to the thread HANDLE and thread identifier pthread_t

This question might be silly but its confusing me a bit: What is the difference between handle to the thread HANDLE and thread identifier pthread_t? ---------- Post updated at 01:52 PM ---------- Previous update was at 01:48 PM ---------- Sorry I saw details and HANDLE is in windows and... (0 Replies)
Discussion started by: rupeshkp728
0 Replies

2. UNIX for Advanced & Expert Users

How to decode nfs file handle in HP-UX?

Hi Experts, Any idea how to decode file handle in HP-UX? I am getting the following error continously in my HP-UX 11.31 box :mad: Apr 26 07:15:00 host62 su: + tty?? root-bb Apr 26 07:15:00 host62 su: + tty?? root-abcadm Apr 26 07:15:01 host62 vmunix: NFS write error on host peq9vs:... (1 Reply)
Discussion started by: vipinable
1 Replies

3. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

4. Shell Programming and Scripting

handle file by month

Hi, I want to write a shell script which handle an oracle alert log so that when it changes month, the script generates a new file with month in the filename. Thanks in advance. Leim (4 Replies)
Discussion started by: leim
4 Replies

5. AIX

how to handle potential file contention

I need to change how a posting procedure currently works in order to improve load balancing but I am hitting a potential file contention problem that I was wondering if someone here could assist me with... In a directory called FilePool I would have a bunch of files that are constantly coming in... (3 Replies)
Discussion started by: philplasma
3 Replies

6. Linux

NFS file handle question

Hello All, I have a small question regarding the NFS file handles. Suppose I have a NFS client who has requested for a particular file from the NFS server.Now lets assume that I am using a NFS v2 server. So I get the filehandle for that file and I can use it. Suppose later I upgrade the server to... (0 Replies)
Discussion started by: prathamesh
0 Replies

7. Shell Programming and Scripting

How to handle multiple rows in a file

I have a FILE a.txt which has the following data 113901.94,113901.94,56950.97,56950.97,NOT MATCHING,NOT MATCHING 10693.04,10693.04,5346.52,5346.52,NOT MATCHING,NOT MATCHING 1901.94,1901.94,550.97,550.97,NOT MATCHING,NOT MATCHING 103.04,103.04,53.52,53.52,NOT MATCHING,NOT MATCHING #### This... (2 Replies)
Discussion started by: ksmbabu
2 Replies

8. UNIX and Linux Applications

Stale NFS file handle

There are a filesystem /GWD/appbase/projects/GRIDDB Under this filesystem there is a directory called backup. But When I am trying to access the backup directory ,it is showing me the following error: # cd /GWD/appbase/projects/GRIDDB # cd backup -bash: cd: backup: Stale NFS file handle ... (3 Replies)
Discussion started by: alphasahoo
3 Replies

9. UNIX for Advanced & Expert Users

Stale NFS file handle

Hi, I get an error saying "Stale NFS file handle" how can I solve this? Is it possible to do this with a umount/ mount command? (5 Replies)
Discussion started by: rein
5 Replies

10. UNIX for Advanced & Expert Users

NFS file handle

How can I kill mount connection(NFS , made by automount) if remote filesystem is down? I tried: fuser -ku /auto /auto: umount /auto nfs umount: ERROR: /auto is busy If I try cd /auto - I get - /auto: Stale remote file handle. I know that reboot will help but I cannot reboot this... (2 Replies)
Discussion started by: kazimir
2 Replies
Login or Register to Ask a Question