From 097e05c083a912fd7c21106fb206e86e3b40d524 Mon Sep 17 00:00:00 2001 From: "[CST] Focus" Date: Wed, 5 Feb 2014 13:08:03 +0400 Subject: [PATCH] system/helpers.cpp: add code to get hdd standby/active status and flush hdd buffers --- src/system/helpers.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++ src/system/helpers.h | 3 ++ 2 files changed, 76 insertions(+) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index 01e76b513..0f00424a6 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -38,6 +38,9 @@ #include #include #include +#include +#include +#include #include #include @@ -611,3 +614,73 @@ bool CFileHelpers::removeDir(const char *Dir) errno = 0; return true; } + +static int hdd_open_dev(const char * fname) +{ + FILE * fp; + struct mntent * mnt; + dev_t dev; + struct stat st; + int fd = -1; + + if (stat(fname, &st) != 0) { + perror(fname); + return fd; + } + + dev = st.st_dev; + fp = setmntent("/proc/mounts", "r"); + if (fp == NULL) { + perror("setmntent"); + return fd; + } + + while ((mnt = getmntent(fp)) != NULL) { + if (stat(mnt->mnt_fsname, &st) != 0) + continue; + if (S_ISBLK(st.st_mode) && st.st_rdev == dev) { + printf("[hdd] file [%s] -> dev [%s]\n", fname, mnt->mnt_fsname); + fd = open(mnt->mnt_fsname, O_RDONLY|O_NONBLOCK); + if (fd < 0) + perror(mnt->mnt_fsname); + break; + } + } + endmntent(fp); + return fd; +} + +bool hdd_get_standby(const char * fname) +{ + bool standby = false; + + int fd = hdd_open_dev(fname); + if (fd >= 0) { + unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0}; + int ret = ioctl(fd, HDIO_DRIVE_CMD, args); + if (ret) { + args[0] = WIN_CHECKPOWERMODE2; + ret = ioctl(fd, HDIO_DRIVE_CMD, args); + } + if ((ret == 0) && (args[2] != 0xFF)) + standby = true; + + printf("[hdd] %s\n", standby ? "standby" : "active"); + close(fd); + } + return standby; +} + +void hdd_flush(const char * fname) +{ + int fd = hdd_open_dev(fname); + if (fd >= 0) { + printf("[hdd] flush buffers...\n"); + fsync(fd); + if (ioctl(fd, BLKFLSBUF, NULL)) + perror("BLKFLSBUF"); + else + ioctl(fd, HDIO_DRIVE_CMD, NULL); + close(fd); + } +} diff --git a/src/system/helpers.h b/src/system/helpers.h index ed9f191ff..b494a2500 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -43,6 +43,9 @@ void mySleep(int sec); std::string find_executable(const char *name); +bool hdd_get_standby(const char * fname); +void hdd_flush(const char * fname); + std::string getPathName(std::string &path); std::string getBaseName(std::string &path); std::string getFileName(std::string &file);