//unix C
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/vfs.h>
//network
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <aio.h>
#include <netinet/tcp.h>
#include <netdb.h>
//thread
#include <pthread.h>
//signal
#include <signal.h>
//err and std
#include <errno.h>
#define DEVICE_NAME "/dev/sdb"
#define PARTITION_NAME "/dev/sdb1"
#define MNT_DIR "/mnt/ramfs/u/"
#define TMP_MOUNT "/mnt/ramfs/mnttmp"
#define FS_TYPE_NTFS 1
#define FS_TYPE_UNKOWN 2
#define FS_TYPE_FAILED 0
#define PROC_MOUNTS "/proc/mounts"
#define HDISK_TYPE_UNKOWN 0
#define HDISK_TYPE_SATA 1 //
#define HDISK_TYPE_USB 2
int GetHdiskType(char *pcDevName)
{
char acBuf[512];
char acBlockName[128];
if (NULL == pcDevName)
{
return HDISK_TYPE_UNKOWN;
}
memset(acBuf, 0, sizeof(acBuf));
memset(acBlockName, 0, sizeof(acBlockName));
sprintf(acBlockName, "/sys/block/%s", pcDevName);
readlink(acBlockName, acBuf, sizeof(acBuf));
if (strstr(acBuf, "ahci"))
{
return HDISK_TYPE_SATA;
}
else if (strstr(acBuf, "usb"))
{
return HDISK_TYPE_USB;
}
return HDISK_TYPE_UNKOWN;
}
static int FindHdiskNew(int iDiskType, char *pcDevName, int iLen)
{
FILE* pFile = 0;
int major, minor, size;
int iDiskCnt = 0;
char buf[256];
char devname[20], name[20];
int iType = 0;
pFile = fopen("/proc/partitions", "rb");
if (NULL == pFile)
{
perror(":open");
return(-1);
}
while (fgets(buf, sizeof(buf), pFile))
{
if (strstr(buf, "sd") != NULL)
{
sscanf(buf, " %d %d %d %s", &major, &minor, &size, name);
if (minor%16 == 0)
{
//USB设备名
snprintf(devname, 20, "/dev/%s", name);
//if ((size / 1000000) < 100)
{
iType = GetHdiskType(name);
if (iDiskType == iType)
{
//printf("Find disk,\"%s\", size:%d \n", devname, size);
snprintf(pcDevName, iLen, "%s", devname);
iDiskCnt++;
break;
}
}
}
}
}
fclose(pFile);
return iDiskCnt;
}
int MonitorMountFsType(char *pcMountPath)
{
FILE *fp = NULL;
char acBuf[512];
fp = fopen(PROC_MOUNTS, "rb");
if (NULL == fp)
{
perror("open proc mounts");
return FS_TYPE_UNKOWN;
}
while (fgets(acBuf, sizeof(acBuf), fp))
{
if (strstr(acBuf, pcMountPath) != NULL)
{
fclose(fp);
usleep(100000);
if (strstr(acBuf, "ntfs") != NULL)
{
return FS_TYPE_NTFS;
}
return FS_TYPE_UNKOWN;
}
}
fclose(fp);
usleep(10000);
return FS_TYPE_FAILED;
}
int IsPartitionTypeNtfs(char *pcPartName)
{
char acTmpMount[128];
char acCmd[128];
int iRet = 0;
mkdir(TMP_MOUNT, 0777);
memset(acTmpMount, 0, sizeof(acTmpMount));
strcpy(acTmpMount, TMP_MOUNT);
sprintf(acCmd, "mount %s %s", pcPartName, acTmpMount);
system(acCmd);
usleep(100000);
iRet = MonitorMountFsType(TMP_MOUNT);
sprintf(acCmd, "umount %s", acTmpMount);
system(acCmd);
usleep(100000);
rmdir(TMP_MOUNT);
return iRet;
}
int main()
{
char acMntCmd[128];
char acUnmnt[128];
char acMountPath[128];
char acDevName[20];
char acPartitionName[20];
int iSysType = 0;
int iPartitionFlag = 0;
int iMntFlag=0;
int i = 0;
int iRet = 0;
memset(acDevName, 0, sizeof(acDevName));
sprintf(acUnmnt, "umount -l %s", MNT_DIR);
while (1)
{
if (iMntFlag)
{
if (0==access(acDevName, 0))
{
usleep(500000);
continue;
}
else
{
// usb is removed
system(acUnmnt);
iMntFlag=0;
memset(acDevName, 0, sizeof(acDevName));
rmdir(MNT_DIR);
printf("umount usb disk.\n");
usleep(500000);
continue;
}
}
else
{
memset(acDevName, 0, sizeof(acDevName));
iRet = FindHdiskNew(HDISK_TYPE_USB, acDevName, sizeof(acDevName));
if (iRet > 0)
{
// usb is insert
usleep(500000);
}
else
{
sleep(1);
continue;
}
}
iPartitionFlag = 0;
for (i = 1; i < 8;i ++)
{
sprintf(acPartitionName, "%s%d", acDevName, i);
if (0==access(acPartitionName, 0))
{
if (!iMntFlag)
{
system(acUnmnt);
//iSysType = ReadPartitionType(DEVICE_NAME);
iSysType = IsPartitionTypeNtfs(acPartitionName);
printf("systype %x\n", iSysType);
strcpy(acMountPath, MNT_DIR);
if (-1 == access(acMountPath, 0))
{
if (mkdir(acMountPath, 777) < 0)
{
printf("create dir %s failure\n", acMountPath);
return(-1);
}
printf("[%s] Create mount path %s\n", __FUNCTION__, acMountPath);
}
if (FS_TYPE_NTFS == iSysType)
{
printf("The device fs is ntfs \n");
sprintf(acMntCmd, "ntfs-3g %s %s", acPartitionName, acMountPath);
system(acMntCmd);
system("sync");
}
else if (FS_TYPE_UNKOWN == iSysType)
{
printf("The device fs is fat32 \n");
sprintf(acMntCmd, "mount %s %s", acPartitionName, acMountPath);
system(acMntCmd);
system("sync");
}
else
{
// 分区无文件系统
continue;
}
iMntFlag=1;
printf("mount %s to usb disk .\n", acPartitionName);
iPartitionFlag = 1;
}
else
{
iPartitionFlag = 1;
}
break;
}
}
if (1 == iPartitionFlag )
{
;
}
else if (0==access(acDevName, 0))
{
if (!iMntFlag)
{
system(acUnmnt);
if (-1 == access(MNT_DIR, 0))
{
if (mkdir(MNT_DIR, 777) < 0)
{
printf("create dir %s failure\n", MNT_DIR);
return(-1);
}
printf("[%s] Create mount path %s\n", __FUNCTION__, MNT_DIR);
}
sprintf(acMntCmd, "mount %s %s", acDevName, MNT_DIR);
system(acMntCmd);
iMntFlag=1;
printf("mount /dev/sdb usb disk.\n");
}
}
usleep(500000);
}
return 0;
}