Ticket #12376: deletefile.diff

File deletefile.diff, 2.7 KB (added by lomion@…, 10 years ago)
  • mythtv/programs/mythbackend/mainserver.cpp

    diff --git a/mythtv/programs/mythbackend/mainserver.cpp b/mythtv/programs/mythbackend/mainserver.cpp
    index 8813652..436c711 100644
    a b void MainServer::DoDeleteInDB(DeleteStruct *ds) 
    22352235    }
    22362236}
    22372237
     2238
     2239bool MainServer::removeDir(const QString &dirname)
     2240{
     2241    QDir dir(dirname);
     2242
     2243    if (!dir.exists())
     2244        return false;
     2245
     2246    dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
     2247    QFileInfoList list = dir.entryInfoList();
     2248    QFileInfoList::const_iterator it = list.begin();
     2249    const QFileInfo *fi;
     2250
     2251    while (it != list.end())
     2252    {
     2253        fi = &(*it++);
     2254        if (fi->isFile() && !fi->isSymLink())
     2255        {
     2256            QFile::remove(fi->absoluteFilePath());
     2257        }
     2258        else if (fi->isDir() && !fi->isSymLink())
     2259        {
     2260            if(!removeDir(fi->absoluteFilePath())) return false;
     2261        }
     2262    }
     2263
     2264    dir.rmdir(dirname);
     2265        return true;
     2266}
     2267
    22382268/**
    22392269 *  \brief Deletes links and unlinks the main file and returns the descriptor.
    22402270 *
    int MainServer::DeleteFile(const QString &filename, bool followLinks, 
    22852315            return -2; // valid result, not an error condition
    22862316    }
    22872317
    2288     if (fd < 0)
     2318    if (fd < 0 && errno != EISDIR)
    22892319        LOG(VB_GENERAL, LOG_ERR, errmsg + ENO);
    22902320
    22912321    return fd;
    22922322}
    22932323
     2324
     2325
    22942326/** \fn MainServer::OpenAndUnlink(const QString&)
    22952327 *  \brief Opens a file, unlinks it and returns the file descriptor.
    22962328 *
    int MainServer::OpenAndUnlink(const QString &filename) 
    23082340
    23092341    if (fd == -1)
    23102342    {
    2311         LOG(VB_GENERAL, LOG_ERR, msg + " could not open " + ENO);
    2312         return -1;
    2313     }
    2314 
    2315     if (unlink(fname.constData()))
     2343        if (errno == EISDIR)
     2344        {
     2345                if(!removeDir(filename))
     2346                {
     2347                        LOG(VB_GENERAL, LOG_ERR, msg + " could not delete directory " + ENO);
     2348                        return -1;
     2349                }
     2350        }
     2351        else
     2352        {
     2353                LOG(VB_GENERAL, LOG_ERR, msg + " could not open " + ENO);
     2354                return -1;
     2355        }
     2356    }
     2357    else if (unlink(fname.constData()))
    23162358    {
    23172359        LOG(VB_GENERAL, LOG_ERR, msg + " could not unlink " + ENO);
    23182360        close(fd);
  • mythtv/programs/mythbackend/mainserver.h

    diff --git a/mythtv/programs/mythbackend/mainserver.h b/mythtv/programs/mythbackend/mainserver.h
    index 32084f9..9b5bf23 100644
    a b class MainServer : public QObject, public MythSocketCBs 
    256256
    257257    void SetExitCode(int exitCode, bool closeApplication);
    258258
     259    static bool removeDir(const QString &dirname);
    259260    static int  DeleteFile(const QString &filename, bool followLinks,
    260261                           bool deleteBrokenSymlinks = false);
    261262    static int  OpenAndUnlink(const QString &filename);