Implementing the RM () function to delete files or directories under Linux

Source: Internet
Author: User
Tags lstat strcmp

There are two functions under Linux that can be used to delete a file:

#include <unistd.h>int unlink (constchar *pathname);

The unlink function removes a name from the file system, and if the name is the last link of the file and the file is not opened by any process, delete the file. Otherwise, when the file is closed or the last link is deleted, the file is deleted and the space is freed.

#include <unistd.h>int rmdir (constchar *pathname);

Only if the directory is empty, rmdir can delete the directory.

Since rmdir can only delete empty directory files, it is necessary to delete all files in the directory before deleting the directory files.

First implement the Rm_dir (const string& path) function to delete all files in the directory, traverse each file in Rm_dir (), and recursively delete the directory file if it encounters a directory file.

//recursively Delete all of the file in the directory.intRm_dir (std::stringDir_full_path) {DIR* Dirp =Opendir (Dir_full_path.c_str ()); if(!dirp) {        return-1; }    structDirent *dir; structStat St;  while(dir = Readdir (DIRP))! =NULL) {        if(strcmp (Dir->d_name,".") ==0|| strcmp (Dir->d_name,"..") ==0)        {            Continue; } std::stringSub_path = Dir_full_path +'/'+ dir->D_name; if(Lstat (Sub_path.c_str (), &st) = =-1) {Log ("Rm_dir:lstat", Sub_path,"Error"); Continue; }            if(S_isdir (St.st_mode)) {if(Rm_dir (sub_path) = =-1)//if it is a directory file, delete it recursively{closedir (DIRP); return-1;        } rmdir (Sub_path.c_str ()); }        Else if(S_isreg (St.st_mode)) {unlink (Sub_path.c_str ()); //if it is a normal file, then unlink        }        Else{Log ("Rm_dir:st_mode", Sub_path,"Error"); Continue; }    }    if(RmDir (DIR_FULL_PATH.C_STR ()) = =-1)//Delete dir itself.{closedir (DIRP); return-1;    } closedir (DIRP); return 0;}

Implement RM () function, determine the file type, if the directory file is Rm_dir, ordinary file is unlink.

intRM (std::stringfile_name) {std::stringFile_path =file_name; structStat St; if(Lstat (File_path.c_str (), &st) = =-1)    {        return-1; }    if(S_isreg (St.st_mode)) {if(Unlink (FILE_PATH.C_STR ()) = =-1)        {            return-1; }        }    Else if(S_isdir (St.st_mode)) {if(file_name = ="."|| file_name = ="..")        {            return-1; }            if(Rm_dir (file_path) = =-1)//Delete all the files in dir.        {            return-1; }    }    return 0;}

Implementing the RM () function to delete files or directories under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.