2012-07-07 22 views
3

我試圖安裝在C++程序的ISO文件在Linux安裝的ISO使用SYS/mount.h

我知道Linux的命令來實現這一點,即安裝鄰環〜/ Test.iso到/ mnt/myISO

但安裝(2)手冊頁狀態的原型如下安裝:

int mount(const char *source, const char *target, 
const char *filesystemtype, unsigned long mountflags, 
const void *data); 

我怎麼在這裏指定了循環選項?

-

此外,有良好的(/可接受)實踐一般,在Linux的編程使用系統調用殼由C++實現的任務,如這些?

+0

在某些情況下,腳本是更好,但它實際上取決於你的目標是什麼。 – 2012-07-07 20:32:41

+0

如果內存服務,循環選項會傳遞給'losetup'。 – user7116 2012-07-07 20:34:31

+0

@JesusRamos在我的問題的後半部分,我的意思是問,使用C++ system()函數調用相關的linux命令(不是創建shell腳本)是一種很好/可接受的做法。在這種情況下, m創建一個用於安裝ISO的GUI。 – flak37 2012-07-07 20:44:50

回答

4

小例子

#include <sys/mount.h> 
#include <linux/loop.h> 
#include <fcntl.h> 

int main() 
{ 
    int file_fd, device_fd; 

    file_fd = open("./TVM_TOMI1.iso", O_RDWR); 
    if (file_fd < -1) { 
     perror("open backing file failed"); 
     return 1; 
    } 
    device_fd = open("/dev/loop0", O_RDWR); 
    if (device_fd < -1) { 
     perror("open loop device failed"); 
     close(file_fd); 
     return 1; 
    } 
    if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0) { 
     perror("ioctl LOOP_SET_FD failed"); 
     close(file_fd); 
     close(device_fd); 
     return 1; 
    } 
    close(file_fd); 
    close(device_fd); 
    mount("/dev/loop0","/mnt/iso","iso9660",MS_RDONLY,""); 
} 

UPD: 卸載後,你需要無環路:

device_fd = open("/dev/loop0", O_RDWR); 
... 
if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) { 
    perror("ioctl LOOP_CLR_FD failed"); 
    return 1; 
} 
+0

謝謝!這就解釋了這個過程,正是我需要的。另一件事,我怎樣才能知道.iso文件是否具有iso9660格式或UDF格式? – flak37 2012-07-09 15:12:20

+0

或我錯過了什麼? – flak37 2012-07-09 15:58:06

+0

@ flak37嘗試「自動」,或通過偏移量檢查格式或使用libmagic。 http://wiki.it46.se/doku.php/myhacks/tunning_asterisknow_cd – askovpen 2012-07-09 19:08:32