You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

841 lines
29 KiB

12 years ago
12 years ago
  1. /* test/libburner.c , API illustration of burning data or audio tracks to CD */
  2. /* Copyright (C) 2005 - 2016 Thomas Schmitt <scdbackup@gmx.net> */
  3. /* Provided under GPL, see also "License and copyright aspects" at file end */
  4. /** Overview
  5. libburner is a minimal demo application for the library libburn as provided
  6. on http://libburnia-project.org . It can list the available devices, can
  7. blank a CD-RW or DVD-RW, can format DVD-RW and BD, can burn to CD-R,
  8. CD-RW, DVD-R, DVD+R, DVD+R/DL, DVD+RW, DVD-RW, DVD-RAM, BD-R, BD-RE.
  9. Not supported yet: DVD-R/DL.
  10. It's main purpose, nevertheless, is to show you how to use libburn and also
  11. to serve the libburnia team as reference application. libburner.c does indeed
  12. define the standard way how above three gestures can be implemented and
  13. stay upward compatible for a good while.
  14. There is another demo program, test/telltoc.c, which inspects drive, media
  15. state, and media contents.
  16. Before you can do anything, you have to initialize libburn by
  17. burn_initialize()
  18. and provide some signal and abort handling, e.g. by the builtin handler, by
  19. burn_set_signal_handling("libburner : ", NULL, 0x0)
  20. as it is done in main() at the end of this file.
  21. Then you acquire a drive in an appropriate way conforming to the API. The two
  22. main approaches are shown here in application functions:
  23. libburner_aquire_by_adr() demonstrates usage as of cdrecord traditions
  24. libburner_aquire_by_driveno() demonstrates a scan-and-choose approach
  25. With that acquired drive you can blank a CD-RW or DVD-RW as shown in
  26. libburner_blank_disc()
  27. or you can format a DVD-RW to profile "Restricted Overwrite" (needed once)
  28. or an unused BD to default size with spare blocks
  29. libburner_format()
  30. With the acquired drive you can burn to CD, DVD, BD. See
  31. libburner_payload()
  32. These three functions switch temporarily to a non-fatal signal handler
  33. while they are waiting for the drive to become idle again:
  34. burn_set_signal_handling("libburner : ", NULL, 0x30)
  35. After the waiting loop ended, they check for eventual abort events by
  36. burn_is_aborting(0)
  37. The 0x30 handler will eventually execute
  38. burn_abort()
  39. but not wait for the drive to become idle and not call exit().
  40. This is needed because the worker threads might block as long as the signal
  41. handler has not returned. The 0x0 handler would wait for them to finish.
  42. Take this into respect when implementing own signal handlers.
  43. When everything is done, main() releases the drive and shuts down libburn:
  44. burn_drive_release();
  45. burn_finish()
  46. Applications must use 64 bit off_t. E.g. by defining
  47. #define _LARGEFILE_SOURCE
  48. #define _FILE_OFFSET_BITS 64
  49. or take special precautions to interface with the library by 64 bit integers
  50. where libburn/libburn.h prescribes off_t.
  51. This program gets fed with appropriate settings externally by libburn's
  52. autotools generated build system.
  53. */
  54. /** See this for the decisive API specs . libburn.h is The Original */
  55. /* For using the installed header file : #include <libburn/libburn.h> */
  56. /* This program insists in the own headerfile. */
  57. #include "../libburn/libburn.h"
  58. /* libburn works on Linux systems with kernel 2.4 or 2.6, FreeBSD, Solaris */
  59. #include <stdio.h>
  60. #include <ctype.h>
  61. #include <sys/types.h>
  62. #include <unistd.h>
  63. #include <string.h>
  64. #include <stdlib.h>
  65. #include <time.h>
  66. #include <errno.h>
  67. #include <sys/stat.h>
  68. #include <fcntl.h>
  69. /** For simplicity i use global variables to represent the drives.
  70. Drives are systemwide global, so we do not give away much of good style.
  71. */
  72. /** This list will hold the drives known to libburn. This might be all CD
  73. drives of the system and thus might impose severe impact on the system.
  74. */
  75. static struct burn_drive_info *drive_list;
  76. /** If you start a long lasting operation with drive_count > 1 then you are
  77. not friendly to the users of other drives on those systems. Beware. */
  78. static unsigned int drive_count;
  79. /** This variable indicates wether the drive is grabbed and must be
  80. finally released */
  81. static int drive_is_grabbed = 0;
  82. /** A number and a text describing the type of media in acquired drive */
  83. static int current_profile= -1;
  84. static char current_profile_name[80]= {""};
  85. /* Some in-advance definitions make possible a more comprehensive ordering
  86. of the functions and their explanations in here */
  87. int libburner_aquire_by_adr(char *drive_adr);
  88. int libburner_aquire_by_driveno(int *drive_no);
  89. /* ------------------------------- API gestures ---------------------------- */
  90. /** You need to acquire a drive before burning. The API offers this as one
  91. compact call and alternatively as application controllable gestures of
  92. whitelisting, scanning for drives and finally grabbing one of them.
  93. If you have a persistent address of the drive, then the compact call is
  94. to prefer because it only touches one drive. On modern Linux kernels,
  95. there should be no fatal disturbance of ongoing burns of other libburn
  96. instances with any of our approaches. We use open(O_EXCL) by default.
  97. On /dev/hdX it should cooperate with growisofs and some cdrecord variants.
  98. On /dev/sgN versus /dev/scdM expect it not to respect other programs.
  99. */
  100. int libburner_aquire_drive(char *drive_adr, int *driveno)
  101. {
  102. int ret;
  103. if(drive_adr != NULL && drive_adr[0] != 0)
  104. ret = libburner_aquire_by_adr(drive_adr);
  105. else
  106. ret = libburner_aquire_by_driveno(driveno);
  107. if (ret <= 0 || *driveno <= 0)
  108. return ret;
  109. burn_disc_get_profile(drive_list[0].drive, &current_profile,
  110. current_profile_name);
  111. if (current_profile_name[0])
  112. printf("Detected media type: %s\n", current_profile_name);
  113. return 1;
  114. }
  115. /** If the persistent drive address is known, then this approach is much
  116. more un-obtrusive to the systemwide livestock of drives. Only the
  117. given drive device will be opened during this procedure.
  118. */
  119. int libburner_aquire_by_adr(char *drive_adr)
  120. {
  121. int ret;
  122. char libburn_drive_adr[BURN_DRIVE_ADR_LEN];
  123. /* Some not-so-harmless drive addresses get blocked in this demo */
  124. if (strncmp(drive_adr, "stdio:/dev/fd/", 14) == 0 ||
  125. strcmp(drive_adr, "stdio:-") == 0) {
  126. fprintf(stderr, "Will not work with pseudo-drive '%s'\n",
  127. drive_adr);
  128. return 0;
  129. }
  130. /* This tries to resolve links or alternative device files */
  131. ret = burn_drive_convert_fs_adr(drive_adr, libburn_drive_adr);
  132. if (ret<=0) {
  133. fprintf(stderr, "Address does not lead to a CD burner: '%s'\n",
  134. drive_adr);
  135. return 0;
  136. }
  137. fprintf(stderr,"Aquiring drive '%s' ...\n", libburn_drive_adr);
  138. ret = burn_drive_scan_and_grab(&drive_list, libburn_drive_adr, 1);
  139. if (ret <= 0) {
  140. fprintf(stderr,"FAILURE with persistent drive address '%s'\n",
  141. libburn_drive_adr);
  142. } else {
  143. fprintf(stderr,"Done\n");
  144. drive_is_grabbed = 1;
  145. }
  146. return ret;
  147. }
  148. /** This method demonstrates how to use libburn without knowing a persistent
  149. drive address in advance. It has to make sure that after assessing the list
  150. of available drives, all unwanted drives get closed again. As long as they
  151. are open, no other libburn instance can see them. This is an intended
  152. locking feature. The application is responsible for giving up the locks
  153. by either burn_drive_release() (only after burn_drive_grab() !),
  154. burn_drive_info_forget(), burn_drive_info_free(), or burn_finish().
  155. @param driveno the index number in libburn's drive list. This will get
  156. set to 0 on success and will then be the drive index to
  157. use in the further dourse of processing.
  158. @return 1 success , <= 0 failure
  159. */
  160. int libburner_aquire_by_driveno(int *driveno)
  161. {
  162. char adr[BURN_DRIVE_ADR_LEN];
  163. int ret, i;
  164. printf("Beginning to scan for devices ...\n");
  165. while (!burn_drive_scan(&drive_list, &drive_count))
  166. usleep(100002);
  167. if (drive_count <= 0 && *driveno >= 0) {
  168. printf("FAILED (no drives found)\n");
  169. return 0;
  170. }
  171. printf("Done\n");
  172. /*
  173. Interactive programs may choose the drive number at this moment.
  174. drive[0] to drive[drive_count-1] are struct burn_drive_info
  175. as defined in libburn/libburn.h . This structure is part of API
  176. and thus will strive for future compatibility on source level.
  177. Have a look at the info offered.
  178. Caution: do not take .location for drive address. Always use
  179. burn_drive_get_adr() or you might become incompatible
  180. in future.
  181. Note: bugs with struct burn_drive_info - if any - will not be
  182. easy to fix. Please report them but also strive for
  183. workarounds on application level.
  184. */
  185. printf("\nOverview of accessible drives (%d found) :\n",
  186. drive_count);
  187. printf("-----------------------------------------------------------------------------\n");
  188. for (i = 0; i < (int) drive_count; i++) {
  189. if (burn_drive_get_adr(&(drive_list[i]), adr) <=0)
  190. strcpy(adr, "-get_adr_failed-");
  191. printf("%d --drive '%s' : '%s' '%s'\n",
  192. i,adr,drive_list[i].vendor,drive_list[i].product);
  193. }
  194. printf("-----------------------------------------------------------------------------\n\n");
  195. /*
  196. On multi-drive systems save yourself from sysadmins' revenge.
  197. Be aware that you hold reserved all available drives at this point.
  198. So either make your choice quick enough not to annoy other system
  199. users, or set free the drives for a while.
  200. The tested way of setting free all drives is to shutdown the library
  201. and to restart when the choice has been made. The list of selectable
  202. drives should also hold persistent drive addresses as obtained
  203. above by burn_drive_get_adr(). By such an address one may use
  204. burn_drive_scan_and_grab() to finally acquire exactly one drive.
  205. A not yet tested shortcut should be to call burn_drive_info_free()
  206. and to call either burn_drive_scan() or burn_drive_scan_and_grab()
  207. before accessing any drives again.
  208. In both cases you have to be aware that the desired drive might get
  209. acquired in the meantime by another user or libburn process.
  210. */
  211. /* We already made our choice via command line. (default is 0)
  212. So we just have to keep our desired drive and drop all others.
  213. No other libburn instance will have a chance to steal our drive.
  214. */
  215. if (*driveno < 0) {
  216. printf("Pseudo-drive \"-\" given : bus scanning done.\n");
  217. return 2; /* the program will end after this */
  218. }
  219. if ((int) drive_count <= *driveno) {
  220. fprintf(stderr,
  221. "Found only %d drives. Number %d not available.\n",
  222. drive_count, *driveno);
  223. return 0; /* the program will end after this */
  224. }
  225. /* Drop all drives which we do not want to use */
  226. for (i = 0; i < (int) drive_count; i++) {
  227. if (i == *driveno) /* the one drive we want to keep */
  228. continue;
  229. ret = burn_drive_info_forget(&(drive_list[i]),0);
  230. if (ret != 1)
  231. fprintf(stderr, "Cannot drop drive %d. Please report \"ret=%d\" to libburn-hackers@pykix.org\n",
  232. i, ret);
  233. else
  234. printf("Dropped unwanted drive %d\n",i);
  235. }
  236. /* Make the one we want ready for blanking or burning */
  237. ret= burn_drive_grab(drive_list[*driveno].drive, 1);
  238. if (ret != 1)
  239. return 0;
  240. drive_is_grabbed = 1;
  241. return 1;
  242. }
  243. /** Makes a previously used CD-RW or unformatted DVD-RW ready for thorough
  244. re-usal.
  245. To our knowledge it is hardly possible to abort an ongoing blank operation
  246. because after start it is entirely handled by the drive.
  247. So expect signal handling to wait the normal blanking timespan until it
  248. can allow the process to end. External kill -9 will not help the drive.
  249. */
  250. int libburner_blank_disc(struct burn_drive *drive, int blank_fast)
  251. {
  252. enum burn_disc_status disc_state;
  253. struct burn_progress p;
  254. double percent = 1.0;
  255. disc_state = burn_disc_get_status(drive);
  256. printf(
  257. "Drive media status: %d (see libburn/libburn.h BURN_DISC_*)\n",
  258. disc_state);
  259. if (current_profile == 0x13) {
  260. ; /* formatted DVD-RW will get blanked to sequential state */
  261. } else if (disc_state == BURN_DISC_BLANK) {
  262. fprintf(stderr,
  263. "IDLE: Blank media detected. Will leave it untouched\n");
  264. return 2;
  265. } else if (disc_state == BURN_DISC_FULL ||
  266. disc_state == BURN_DISC_APPENDABLE) {
  267. ; /* this is what libburner is willing to blank */
  268. } else if (disc_state == BURN_DISC_EMPTY) {
  269. fprintf(stderr,"FATAL: No media detected in drive\n");
  270. return 0;
  271. } else {
  272. fprintf(stderr,
  273. "FATAL: Unsuitable drive and media state\n");
  274. return 0;
  275. }
  276. if(!burn_disc_erasable(drive)) {
  277. fprintf(stderr,
  278. "FATAL : Media is not of erasable type\n");
  279. return 0;
  280. }
  281. /* Switch to asynchronous signal handling for the time of waiting */
  282. burn_set_signal_handling("libburner : ", NULL, 0x30);
  283. printf("Beginning to %s-blank media.\n", (blank_fast?"fast":"full"));
  284. burn_disc_erase(drive, blank_fast);
  285. sleep(1);
  286. while (burn_drive_get_status(drive, &p) != BURN_DRIVE_IDLE) {
  287. if(p.sectors>0 && p.sector>=0) /* display 1 to 99 percent */
  288. percent = 1.0 + ((double) p.sector+1.0)
  289. / ((double) p.sectors) * 98.0;
  290. printf("Blanking ( %.1f%% done )\n", percent);
  291. sleep(1);
  292. }
  293. if (burn_is_aborting(0) > 0)
  294. return -1;
  295. /* Back to synchronous handling */
  296. burn_set_signal_handling("libburner : ", NULL, 0x0);
  297. printf("Done\n");
  298. return 1;
  299. }
  300. /** Formats unformatted DVD-RW to profile 0013h "Restricted Overwrite"
  301. which needs no blanking for re-use but is not capable of multi-session.
  302. Expect a behavior similar to blanking with unusual noises from the drive.
  303. Formats unformatted BD-RE to default size. This will allocate some
  304. reserve space, test for bad blocks and make the media ready for writing.
  305. Expect a very long run time.
  306. Formats unformatted blank BD-R to hold a default amount of spare blocks
  307. for eventual mishaps during writing. If BD-R get written without being
  308. formatted, then they get no such reserve and will burn at full speed.
  309. */
  310. int libburner_format(struct burn_drive *drive)
  311. {
  312. struct burn_progress p;
  313. double percent = 1.0;
  314. int ret, status, num_formats, format_flag= 0;
  315. off_t size = 0;
  316. unsigned dummy;
  317. enum burn_disc_status disc_state;
  318. if (current_profile == 0x13) {
  319. fprintf(stderr, "IDLE: DVD-RW media is already formatted\n");
  320. return 2;
  321. } else if (current_profile == 0x41 || current_profile == 0x43) {
  322. disc_state = burn_disc_get_status(drive);
  323. if (disc_state != BURN_DISC_BLANK && current_profile == 0x41) {
  324. fprintf(stderr,
  325. "FATAL: BD-R is not blank. Cannot format.\n");
  326. return 0;
  327. }
  328. ret = burn_disc_get_formats(drive, &status, &size, &dummy,
  329. &num_formats);
  330. if (ret > 0 && status != BURN_FORMAT_IS_UNFORMATTED) {
  331. fprintf(stderr,
  332. "IDLE: BD media is already formatted\n");
  333. return 2;
  334. }
  335. size = 0; /* does not really matter */
  336. format_flag = 3<<1; /* format to default size, no quick */
  337. } else if (current_profile == 0x14) { /* sequential DVD-RW */
  338. size = 128 * 1024 * 1024;
  339. format_flag = 1; /* write initial 128 MiB */
  340. } else {
  341. fprintf(stderr, "FATAL: Can only format DVD-RW or BD\n");
  342. return 0;
  343. }
  344. burn_set_signal_handling("libburner : ", NULL, 0x30);
  345. printf("Beginning to format media.\n");
  346. burn_disc_format(drive, size, format_flag);
  347. sleep(1);
  348. while (burn_drive_get_status(drive, &p) != BURN_DRIVE_IDLE) {
  349. if(p.sectors>0 && p.sector>=0) /* display 1 to 99 percent */
  350. percent = 1.0 + ((double) p.sector+1.0)
  351. / ((double) p.sectors) * 98.0;
  352. printf("Formatting ( %.1f%% done )\n", percent);
  353. sleep(1);
  354. }
  355. if (burn_is_aborting(0) > 0)
  356. return -1;
  357. burn_set_signal_handling("libburner : ", NULL, 0x0);
  358. burn_disc_get_profile(drive_list[0].drive, &current_profile,
  359. current_profile_name);
  360. if (current_profile == 0x14 || current_profile == 0x13)
  361. printf("Media type now: %4.4xh \"%s\"\n",
  362. current_profile, current_profile_name);
  363. if (current_profile == 0x14) {
  364. fprintf(stderr,
  365. "FATAL: Failed to change media profile to desired value\n");
  366. return 0;
  367. }
  368. return 1;
  369. }
  370. /** Brings preformatted track images (ISO 9660, audio, ...) onto media.
  371. To make sure a data image is fully readable on any Linux machine, this
  372. function adds 300 kiB of padding to the (usualy single) track.
  373. Audio tracks get padded to complete their last sector.
  374. A fifo of 4 MB is installed between each track and its data source.
  375. Each of the 4 MB buffers gets allocated automatically as soon as a track
  376. begins to be processed and it gets freed as soon as the track is done.
  377. The fifos do not wait for buffer fill but writing starts immediately.
  378. In case of external signals expect abort handling of an ongoing burn to
  379. last up to a minute. Wait the normal burning timespan before any kill -9.
  380. */
  381. int libburner_payload(struct burn_drive *drive,
  382. char source_adr[][4096], int source_adr_count,
  383. int multi, int simulate_burn, int all_tracks_type)
  384. {
  385. struct burn_source *data_src = NULL, *fifo_src[99];
  386. struct burn_disc *target_disc = NULL;
  387. struct burn_session *session = NULL;
  388. struct burn_write_opts *burn_options = NULL;
  389. enum burn_disc_status disc_state;
  390. struct burn_track *track, *tracklist[99];
  391. struct burn_progress progress;
  392. time_t start_time;
  393. int last_sector = 0, padding = 0, trackno, unpredicted_size = 0, fd;
  394. int fifo_chunksize = 2352, fifo_chunks = 1783; /* ~ 4 MB fifo */
  395. int ret;
  396. off_t fixed_size;
  397. char *adr, reasons[BURN_REASONS_LEN];
  398. struct stat stbuf;
  399. for (trackno = 0 ; trackno < source_adr_count; trackno++) {
  400. fifo_src[trackno] = NULL;
  401. tracklist[trackno] = NULL;
  402. }
  403. if (all_tracks_type != BURN_AUDIO) {
  404. all_tracks_type = BURN_MODE1;
  405. /* a padding of 300 kiB helps to avoid the read-ahead bug */
  406. padding = 300*1024;
  407. fifo_chunksize = 2048;
  408. fifo_chunks = 2048; /* 4 MB fifo */
  409. }
  410. target_disc = burn_disc_create();
  411. session = burn_session_create();
  412. burn_disc_add_session(target_disc, session, BURN_POS_END);
  413. for (trackno = 0 ; trackno < source_adr_count; trackno++) {
  414. tracklist[trackno] = track = burn_track_create();
  415. burn_track_define_data(track, 0, padding, 1, all_tracks_type);
  416. /* Open file descriptor to source of track data */
  417. adr = source_adr[trackno];
  418. fixed_size = 0;
  419. if (adr[0] == '-' && adr[1] == 0) {
  420. fd = 0;
  421. } else {
  422. fd = open(adr, O_RDONLY);
  423. if (fd>=0)
  424. if (fstat(fd,&stbuf)!=-1)
  425. if((stbuf.st_mode&S_IFMT)==S_IFREG)
  426. fixed_size = stbuf.st_size;
  427. }
  428. if (fixed_size==0)
  429. unpredicted_size = 1;
  430. /* Convert this filedescriptor into a burn_source object */
  431. data_src = NULL;
  432. if (fd >= 0)
  433. data_src = burn_fd_source_new(fd, -1, fixed_size);
  434. if (data_src == NULL) {
  435. fprintf(stderr,
  436. "FATAL: Could not open data source '%s'.\n",adr);
  437. if(errno!=0)
  438. fprintf(stderr,"(Most recent system error: %s )\n",
  439. strerror(errno));
  440. {ret = 0; goto ex;}
  441. }
  442. /* Install a fifo object on top of that data source object */
  443. fifo_src[trackno] = burn_fifo_source_new(data_src,
  444. fifo_chunksize, fifo_chunks, 0);
  445. if (fifo_src[trackno] == NULL) {
  446. fprintf(stderr,
  447. "FATAL: Could not create fifo object of 4 MB\n");
  448. {ret = 0; goto ex;}
  449. }
  450. /* Use the fifo object as data source for the track */
  451. if (burn_track_set_source(track, fifo_src[trackno])
  452. != BURN_SOURCE_OK) {
  453. fprintf(stderr,
  454. "FATAL: Cannot attach source object to track object\n");
  455. {ret = 0; goto ex;}
  456. }
  457. burn_session_add_track(session, track, BURN_POS_END);
  458. printf("Track %d : source is '%s'\n", trackno+1, adr);
  459. /* Give up local reference to the data burn_source object */
  460. burn_source_free(data_src);
  461. data_src = NULL;
  462. } /* trackno loop end */
  463. /* Evaluate drive and media */
  464. disc_state = burn_disc_get_status(drive);
  465. if (disc_state != BURN_DISC_BLANK &&
  466. disc_state != BURN_DISC_APPENDABLE) {
  467. if (disc_state == BURN_DISC_FULL) {
  468. fprintf(stderr, "FATAL: Closed media with data detected. Need blank or appendable media.\n");
  469. if (burn_disc_erasable(drive))
  470. fprintf(stderr, "HINT: Try --blank_fast\n\n");
  471. } else if (disc_state == BURN_DISC_EMPTY)
  472. fprintf(stderr,"FATAL: No media detected in drive\n");
  473. else
  474. fprintf(stderr,
  475. "FATAL: Cannot recognize state of drive and media\n");
  476. {ret = 0; goto ex;}
  477. }
  478. burn_options = burn_write_opts_new(drive);
  479. burn_write_opts_set_perform_opc(burn_options, 0);
  480. burn_write_opts_set_multi(burn_options, !!multi);
  481. if(simulate_burn)
  482. printf("\n*** Will TRY to SIMULATE burning ***\n\n");
  483. burn_write_opts_set_simulate(burn_options, simulate_burn);
  484. burn_drive_set_speed(drive, 0, 0);
  485. burn_write_opts_set_underrun_proof(burn_options, 1);
  486. if (burn_write_opts_auto_write_type(burn_options, target_disc,
  487. reasons, 0) == BURN_WRITE_NONE) {
  488. fprintf(stderr, "FATAL: Failed to find a suitable write mode with this media.\n");
  489. fprintf(stderr, "Reasons given:\n%s\n", reasons);
  490. {ret = 0; goto ex;}
  491. }
  492. burn_set_signal_handling("libburner : ", NULL, 0x30);
  493. printf("Burning starts. With e.g. 4x media expect up to a minute of zero progress.\n");
  494. start_time = time(0);
  495. burn_disc_write(burn_options, target_disc);
  496. while (burn_drive_get_status(drive, NULL) == BURN_DRIVE_SPAWNING)
  497. usleep(100002);
  498. while (burn_drive_get_status(drive, &progress) != BURN_DRIVE_IDLE) {
  499. if (progress.sectors <= 0 ||
  500. (progress.sector >= progress.sectors - 1 &&
  501. !unpredicted_size) ||
  502. (unpredicted_size && progress.sector == last_sector))
  503. printf(
  504. "Thank you for being patient since %d seconds.",
  505. (int) (time(0) - start_time));
  506. else if(unpredicted_size)
  507. printf("Track %d : sector %d", progress.track+1,
  508. progress.sector);
  509. else
  510. printf("Track %d : sector %d of %d",progress.track+1,
  511. progress.sector, progress.sectors);
  512. last_sector = progress.sector;
  513. if (progress.track >= 0 && progress.track < source_adr_count) {
  514. int size, free_bytes, ret;
  515. char *status_text;
  516. ret = burn_fifo_inquire_status(
  517. fifo_src[progress.track], &size, &free_bytes,
  518. &status_text);
  519. if (ret >= 0 )
  520. printf(" [fifo %s, %2d%% fill]", status_text,
  521. (int) (100.0 - 100.0 *
  522. ((double) free_bytes) /
  523. (double) size));
  524. }
  525. printf("\n");
  526. sleep(1);
  527. }
  528. printf("\n");
  529. if (burn_is_aborting(0) > 0)
  530. {ret = -1; goto ex;}
  531. if (multi && current_profile != 0x1a && current_profile != 0x13 &&
  532. current_profile != 0x12 && current_profile != 0x43)
  533. /* not with DVD+RW, formatted DVD-RW, DVD-RAM, BD-RE */
  534. printf("NOTE: Media left appendable.\n");
  535. if (simulate_burn)
  536. printf("\n*** Did TRY to SIMULATE burning ***\n\n");
  537. ret = 1;
  538. ex:;
  539. /* Dispose objects */
  540. if (burn_options != NULL)
  541. burn_write_opts_free(burn_options);
  542. for (trackno = 0 ; trackno < source_adr_count; trackno++) {
  543. if (fifo_src[trackno] != NULL)
  544. burn_source_free(fifo_src[trackno]);
  545. if (tracklist[trackno])
  546. burn_track_free(tracklist[trackno]);
  547. }
  548. if (data_src != NULL)
  549. burn_source_free(data_src);
  550. if (session != NULL)
  551. burn_session_free(session);
  552. if (target_disc != NULL)
  553. burn_disc_free(target_disc);
  554. return ret;
  555. }
  556. /** The setup parameters of libburner */
  557. static char drive_adr[BURN_DRIVE_ADR_LEN] = {""};
  558. static int driveno = 0;
  559. static int do_blank = 0;
  560. static char source_adr[99][4096];
  561. static int source_adr_count = 0;
  562. static int do_multi = 0;
  563. static int simulate_burn = 0;
  564. static int all_tracks_type = BURN_MODE1;
  565. /** Converts command line arguments into above setup parameters.
  566. */
  567. int libburner_setup(int argc, char **argv)
  568. {
  569. int i, insuffient_parameters = 0, print_help = 0;
  570. for (i = 1; i < argc; ++i) {
  571. if (!strcmp(argv[i], "--audio")) {
  572. all_tracks_type = BURN_AUDIO;
  573. } else if (!strcmp(argv[i], "--blank_fast")) {
  574. do_blank = 1;
  575. } else if (!strcmp(argv[i], "--blank_full")) {
  576. do_blank = 2;
  577. } else if (!strcmp(argv[i], "--burn_for_real")) {
  578. simulate_burn = 0;
  579. } else if (!strcmp(argv[i], "--drive")) {
  580. ++i;
  581. if (i >= argc) {
  582. fprintf(stderr,"--drive requires an argument\n");
  583. return 1;
  584. } else if (strcmp(argv[i], "-") == 0) {
  585. drive_adr[0] = 0;
  586. driveno = -1;
  587. } else if (isdigit(argv[i][0])) {
  588. drive_adr[0] = 0;
  589. driveno = atoi(argv[i]);
  590. } else {
  591. if(strlen(argv[i]) >= BURN_DRIVE_ADR_LEN) {
  592. fprintf(stderr,"--drive address too long (max. %d)\n",
  593. BURN_DRIVE_ADR_LEN-1);
  594. return 2;
  595. }
  596. strcpy(drive_adr, argv[i]);
  597. }
  598. } else if ((!strcmp(argv[i], "--format_overwrite")) ||
  599. (!strcmp(argv[i], "--format"))) {
  600. do_blank = 101;
  601. } else if (!strcmp(argv[i], "--multi")) {
  602. do_multi = 1;
  603. } else if (!strcmp(argv[i], "--stdin_size")) { /* obsoleted */
  604. i++;
  605. } else if (!strcmp(argv[i], "--try_to_simulate")) {
  606. simulate_burn = 1;
  607. } else if (!strcmp(argv[i], "--help")) {
  608. print_help = 1;
  609. } else if (!strncmp(argv[i], "--",2)) {
  610. fprintf(stderr, "Unidentified option: %s\n", argv[i]);
  611. return 7;
  612. } else {
  613. if(strlen(argv[i]) >= 4096) {
  614. fprintf(stderr, "Source address too long (max. %d)\n", 4096-1);
  615. return 5;
  616. }
  617. if(source_adr_count >= 99) {
  618. fprintf(stderr, "Too many tracks (max. 99)\n");
  619. return 6;
  620. }
  621. strcpy(source_adr[source_adr_count], argv[i]);
  622. source_adr_count++;
  623. }
  624. }
  625. insuffient_parameters = 1;
  626. if (driveno < 0)
  627. insuffient_parameters = 0;
  628. if (source_adr_count > 0)
  629. insuffient_parameters = 0;
  630. if (do_blank)
  631. insuffient_parameters = 0;
  632. if (print_help || insuffient_parameters ) {
  633. printf("Usage: %s\n", argv[0]);
  634. printf(" [--drive <address>|<driveno>|\"-\"] [--audio]\n");
  635. printf(" [--blank_fast|--blank_full|--format] [--try_to_simulate]\n");
  636. printf(" [--multi] [<one or more imagefiles>|\"-\"]\n");
  637. printf("Examples\n");
  638. printf("A bus scan (needs rw-permissions to see a drive):\n");
  639. printf(" %s --drive -\n",argv[0]);
  640. printf("Burn a file to drive chosen by number, leave appendable:\n");
  641. printf(" %s --drive 0 --multi my_image_file\n", argv[0]);
  642. printf("Burn a file to drive chosen by persistent address, close:\n");
  643. printf(" %s --drive /dev/hdc my_image_file\n", argv[0]);
  644. printf("Blank a used CD-RW (is combinable with burning in one run):\n");
  645. printf(" %s --drive /dev/hdc --blank_fast\n",argv[0]);
  646. printf("Blank a used DVD-RW (is combinable with burning in one run):\n");
  647. printf(" %s --drive /dev/hdc --blank_full\n",argv[0]);
  648. printf("Format a DVD-RW, BD-RE or BD-R:\n");
  649. printf(" %s --drive /dev/hdc --format\n", argv[0]);
  650. printf("Burn two audio tracks (to CD only):\n");
  651. printf(" lame --decode -t /path/to/track1.mp3 track1.cd\n");
  652. printf(" test/dewav /path/to/track2.wav -o track2.cd\n");
  653. printf(" %s --drive /dev/hdc --audio track1.cd track2.cd\n", argv[0]);
  654. printf("Burn a compressed afio archive on-the-fly:\n");
  655. printf(" ( cd my_directory ; find . -print | afio -oZ - ) | \\\n");
  656. printf(" %s --drive /dev/hdc -\n", argv[0]);
  657. printf("To be read from *not mounted* media via: afio -tvZ /dev/hdc\n");
  658. if (insuffient_parameters)
  659. return 6;
  660. }
  661. return 0;
  662. }
  663. int main(int argc, char **argv)
  664. {
  665. int ret;
  666. /* A warning to programmers who start their own projekt from here. */
  667. if (sizeof(off_t) != 8) {
  668. fprintf(stderr,
  669. "\nFATAL: Compile time misconfiguration. off_t is not 64 bit.\n\n");
  670. exit(39);
  671. }
  672. ret = libburner_setup(argc, argv);
  673. if (ret)
  674. exit(ret);
  675. printf("Initializing libburnia-project.org ...\n");
  676. if (burn_initialize())
  677. printf("Done\n");
  678. else {
  679. printf("FAILED\n");
  680. fprintf(stderr,"\nFATAL: Failed to initialize.\n");
  681. exit(33);
  682. }
  683. /* Print messages of severity SORRY or more directly to stderr */
  684. burn_msgs_set_severities("NEVER", "SORRY", "libburner : ");
  685. /* Activate the synchronous signal handler which eventually will try to
  686. properly shutdown drive and library on aborting events. */
  687. burn_set_signal_handling("libburner : ", NULL, 0x0);
  688. /** Note: driveno might change its value in this call */
  689. ret = libburner_aquire_drive(drive_adr, &driveno);
  690. if (ret<=0) {
  691. fprintf(stderr,"\nFATAL: Failed to acquire drive.\n");
  692. { ret = 34; goto finish_libburn; }
  693. }
  694. if (ret == 2)
  695. { ret = 0; goto release_drive; }
  696. if (do_blank) {
  697. if (do_blank > 100)
  698. ret = libburner_format(drive_list[driveno].drive);
  699. else
  700. ret = libburner_blank_disc(drive_list[driveno].drive,
  701. do_blank == 1);
  702. if (ret<=0)
  703. { ret = 36; goto release_drive; }
  704. }
  705. if (source_adr_count > 0) {
  706. ret = libburner_payload(drive_list[driveno].drive,
  707. source_adr, source_adr_count,
  708. do_multi, simulate_burn, all_tracks_type);
  709. if (ret<=0)
  710. { ret = 38; goto release_drive; }
  711. }
  712. ret = 0;
  713. release_drive:;
  714. if (drive_is_grabbed)
  715. burn_drive_release(drive_list[driveno].drive, 0);
  716. finish_libburn:;
  717. if (burn_is_aborting(0) > 0) {
  718. burn_abort(4400, burn_abort_pacifier, "libburner : ");
  719. fprintf(stderr,"\nlibburner run aborted\n");
  720. exit(1);
  721. }
  722. /* This app does not bother to know about exact scan state.
  723. Better to accept a memory leak here. We are done anyway. */
  724. /* burn_drive_info_free(drive_list); */
  725. burn_finish();
  726. exit(ret);
  727. }
  728. /* License and copyright aspects:
  729. This all is provided under GPL.
  730. Read. Try. Think. Play. Write yourself some code. Be free of my copyright.
  731. Be also invited to study the code of cdrskin/cdrskin.c et al.
  732. History:
  733. libburner is a compilation of my own contributions to test/burniso.c and
  734. fresh code which replaced the remaining parts under copyright of
  735. Derek Foreman.
  736. My respect and my thanks to Derek for providing me a start back in 2005.
  737. */