⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 auto_mount_usb_cdrom.c

📁 自动挂载 USB CDROM的程序代码,很好的东西哦
💻 C
📖 第 1 页 / 共 2 页
字号:

                     }

              }

       }

       else

       {

              perror(FILE_MOUNT_CHECK);

       }

       return 0;

}

static int do_mount(SCSI_USB_DEV * dev)

{//自动mount 有进行vcd 和 cd的检查并用 cdfs mount

       int i = 0;

       

       char fstype[10];

       unsigned long mountflags=0;

       char mount_data[30];

       char tmpdir[50];

       

       int mount_ok = 0;

       int is_vcd = 0;

       char check_cmd[50];

       

       if(dev->type == 1 && is_manual_umount == 0)

       {

              //mkdir(dev->mount_path[0], 0777);

              strcpy(fstype, "iso9660");

              mountflags= 0xc0ed0000 |MS_RDONLY ;

              strcpy(mount_data,"codepage=936,iocharset=gb2312");

              if (mount(dev->devfile_parts[0], dev->mount_path[0], fstype ,mountflags, mount_data)== 0)

              {

                     mount_ok = 1;

                     DBG_PRINT2(("mount -t %s %s %s success\n", fstype, dev->devfile_parts[0], dev->mount_path[0]));

 

                     //check is vcd

                     sprintf(check_cmd,"ls %s/vcd/*.vcd", dev->mount_path[0]);

                     is_vcd = (system(check_cmd) == 0);

                     if (is_vcd)

                     {

                            if (umount(dev->mount_path[0]) == 0)

                            {

                                   DBG_PRINT2(("umount %s success(vcd iso9660)\n", dev->devfile_parts[0]));

                            }

                            else

                            {

                                   is_vcd = 0;

                                   DBG_PRINT2(("umount %s failed (vcd iso9660)\n", dev->devfile_parts[0]));

                            }

                     }

              }

              else

              {

                     mount_ok = 0;

              }

 

              if (mount_ok == 0 || is_vcd)

              {

                     DBG_PRINT2(("mount -t %s %s %s failed, try cdfs\n", fstype, dev->devfile_parts[0], dev->mount_path[0]));

                     

                     strcpy(fstype, "cdfs");

                     if (mount(dev->devfile_parts[0], dev->mount_path[0], fstype ,mountflags, mount_data)== 0)

                     {

                            DBG_PRINT2(("mount -t %s %s %s success\n", fstype, dev->devfile_parts[0], dev->mount_path[0]));

                            return 1;

                     }

                     else

                     {

                            DBG_PRINT2(("mount -t %s %s %s failed, try cdfs\n", fstype, dev->devfile_parts[0], dev->mount_path[0]));

                            return 0;

                     }

              }

       }

       else if (dev->type == 2)

       {

              sprintf(tmpdir, USB_DISK_MP"/disk%d", dev->index);

              mkdir(tmpdir, 0777);

              

              strcpy(fstype, "vfat");

              mountflags= 0xc0ed0000;

              strcpy(mount_data,"codepage=936,iocharset=gb2312");

              

              if (dev->part_num > 1)

                     i ++; /*if disk ignore first part disc*/

              for(; i<dev->part_num; i++) /*if disk ignore first part disc*/

              {

                     mkdir(dev->mount_path[i], 0777);

                     if (mount(dev->devfile_parts[i], dev->mount_path[i], fstype ,mountflags, mount_data)== 0)

                     {

                            DBG_PRINT2(("mount %s %s success\n", dev->devfile_parts[i], dev->mount_path[i]));

                            Do_USB_Disk_Update(dev->mount_path[i]);

                     }

                     else

                     {

                            rmdir(dev->mount_path[i]);

                            DBG_PRINT2(("mount %s %s failed\n", dev->devfile_parts[i], dev->mount_path[i]));

                     }

              }

       }

       else

       {

              return 0;

       }

       return 1;

}

static int do_umount(SCSI_USB_DEV * dev)

{

       int i = 0;

       char tmpdir[50];

 

       if (dev->type == 1)

       {//cdrom

              if (umount(dev->mount_path[0]) == 0)

              {

                     DBG_PRINT2(("umount %s success\n", dev->devfile_parts[0]));

                     return 1;

              }

              else

              {

                     DBG_PRINT2(("umount %s failed\n", dev->devfile_parts[0]));

                     return 0;

              }

       }

       else if(dev->type == 2) 

       {

              if (dev->part_num > 1)

                     i ++; /*if disk ignore first part disc*/

              

              for(; i<dev->part_num; i++)

              {

                     if (umount(dev->mount_path[i]) == 0)

                     {

                            DBG_PRINT2(("umount %s success\n", dev->devfile_parts[i]));

                            remove(dev->mount_path[i]);

                     }

                     else

                     {

                            DBG_PRINT2(("umount %s failed\n", dev->devfile_parts[i]));

                     }

              }

 

              sprintf(tmpdir, USB_DISK_MP"/disk%d", dev->index);

              remove(tmpdir);

       }

       return 1;

}

static int process_dev(SCSI_USB_DEV * dev)

{

       if (check_attach(dev))//检测设备是否插上

       {

              if (!check_mount(dev))//检测设备是否mount上了

              {

                     do_mount(dev);

              }

       }

       else

       {

              if (check_mount(dev))

              {

                     do_umount(dev);

              }

       }

       return 1;

}

 

void manual_umount(void);

void manual_umount()//争对光驱进行umount

{

       /* now only umount cdrom*/

       SCSI_USB_DEV * cur_dev = NULL;

 

       cur_dev = f_first_dev;

       while (cur_dev)

       {

              if (cur_dev->type == 1 ) break;

              cur_dev = cur_dev->next_dev;

       }

 

       if (cur_dev != NULL)

       {

              is_manual_umount = 1;

              if (check_mount(cur_dev))

              {

                     if (do_umount(cur_dev))

                     {

                            set_ALERT_Msg("光驱卸载成功");

                     }

              }

       }

}

void manual_mount(void);

void manual_mount()//争对光驱进行mount

{

       /* now only umount cdrom*/

       SCSI_USB_DEV * cur_dev = NULL;

 

       cur_dev = f_first_dev;

       while (cur_dev)

       {

              if (cur_dev->type == 1 ) break;

              cur_dev = cur_dev->next_dev;

       }

 

       if (cur_dev != NULL)

       {

              is_manual_umount = 0;

              if (!check_mount(cur_dev))

              {

                     do_mount(cur_dev);

              }

       }

}

       

#ifndef DBG

int Init_AutoMounter(void);

int Init_AutoMounter()

{

       int ret;

       pthread_t thread_automounter;

       

       ret = pthread_create(&thread_automounter, NULL, (void *) main_thread, NULL);

       if(ret != 0){

              printf ("Create pthread error!\n");

       }

       return ret;

}

static void * main_thread(void)

#else

int main()

#endif 

{

       SCSI_USB_DEV * cur_dev = NULL;

       mkdir(USB_DISK_MP, 0777);

       mkdir(USB_CDROM_MP, 0777);

       while (1)

       {

              find_device();//查找设备 初始化设备列表

              cur_dev = f_first_dev;

              while (cur_dev)

              {

                     process_dev(cur_dev);//对每个设备进行处理

                     cur_dev = cur_dev->next_dev;

              }

              sleep(10);//10秒钟检测一次

                     //对于u盘和移动硬盘定时检测并自动mount和umount

       }

}

 

/*

Attached devices:

Host: scsi0 Channel: 00 Id: 00 Lun: 00

  Vendor: BENQ     Model: DVD-ROM 16X      Rev: A.DD

  Type:   CD-ROM                           ANSI SCSI revision: 02

Host: scsi1 Channel: 00 Id: 00 Lun: 00

  Vendor: FUJITSU  Model: MHT2040AH        Rev: 0000

  Type:   Direct-Access                    ANSI SCSI revision: 02

 */

 

//char ** USB_DEVS_STATU= { "/proc/scsi/usb-storage-0/0",""};

///proc/scsi/usb-storage-1/1

///dev/scsi/host0/bus0/target0/lun0/cd 

///dev/scsi/host1/bus0/target0/lun0/   disc   part1  part2  part5

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -