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.
 
 
 
 
 
 

891 lines
24 KiB

  1. /* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
  2. /*
  3. Copyright (c) 2010 Thomas Schmitt <scdbackup@gmx.net>
  4. Provided under GPL version 2 or later.
  5. */
  6. /*
  7. This is the main operating system dependent SCSI part of libburn. It implements
  8. the transport level aspects of SCSI control and command i/o.
  9. Present implementation: Solaris uscsi, e.g. for SunOS 5.11
  10. PORTING:
  11. Porting libburn typically will consist of adding a new operating system case
  12. to the following switcher files:
  13. os.h Operating system specific libburn definitions and declarations.
  14. sg.c Operating system dependent transport level modules.
  15. and of deriving the following system specific files from existing examples:
  16. os-*.h Included by os.h. You will need some general system knowledge
  17. about signals and knowledge about the storage object needs of your
  18. transport level module sg-*.c.
  19. sg-*.c This source module. You will need special system knowledge about
  20. how to detect all potentially available drives, how to open them,
  21. eventually how to exclusively reserve them, how to perform
  22. SCSI transactions, how to inquire the (pseudo-)SCSI driver.
  23. You will not need to care about CD burning, MMC or other high-level
  24. SCSI aspects.
  25. Said sg-*.c operations are defined by a public function interface, which has
  26. to be implemented in a way that provides libburn with the desired services:
  27. sg_id_string() returns an id string of the SCSI transport adapter.
  28. It may be called before initialization but then may
  29. return only a preliminary id.
  30. sg_initialize() performs global initialization of the SCSI transport
  31. adapter and eventually needed operating system
  32. facilities. Checks for compatibility of supporting
  33. software components.
  34. sg_shutdown() performs global finalizations and releases golbally
  35. aquired resources.
  36. sg_give_next_adr() iterates over the set of potentially useful drive
  37. address strings.
  38. scsi_enumerate_drives() brings all available, not-whitelist-banned, and
  39. accessible drives into libburn's list of drives.
  40. sg_dispose_drive() finalizes adapter specifics of struct burn_drive
  41. on destruction. Releases resources which were aquired
  42. underneath scsi_enumerate_drives().
  43. sg_drive_is_open() tells wether libburn has the given drive in use.
  44. sg_grab() opens the drive for SCSI commands and ensures
  45. undisturbed access.
  46. sg_release() closes a drive opened by sg_grab()
  47. sg_issue_command() sends a SCSI command to the drive, receives reply,
  48. and evaluates wether the command succeeded or shall
  49. be retried or finally failed.
  50. sg_obtain_scsi_adr() tries to obtain SCSI address parameters.
  51. burn_os_is_2k_seekrw() tells whether the given path leads to a file object
  52. that can be used in 2 kB granularity by lseek(2),
  53. read(2), and possibly write(2) if not read-only..
  54. E.g. a USB stick or a hard disk.
  55. burn_os_stdio_capacity() estimates the emulated media space of stdio-drives.
  56. burn_os_open_track_src() opens a disk file in a way that allows best
  57. throughput with file reading and/or SCSI write command
  58. transmission.
  59. burn_os_alloc_buffer() allocates a memory area that is suitable for file
  60. descriptors issued by burn_os_open_track_src().
  61. The buffer size may be rounded up for alignment
  62. reasons.
  63. burn_os_free_buffer() delete a buffer obtained by burn_os_alloc_buffer().
  64. Porting hints are marked by the text "PORTING:".
  65. Send feedback to libburn-hackers@pykix.org .
  66. */
  67. #ifdef HAVE_CONFIG_H
  68. #include "../config.h"
  69. #endif
  70. /** PORTING : ------- OS dependent headers and definitions ------ */
  71. #include <unistd.h>
  72. #include <stropts.h>
  73. #include <stdio.h>
  74. #include <sys/types.h>
  75. #include <errno.h>
  76. #include <fcntl.h>
  77. #include <sys/stat.h>
  78. #include <string.h>
  79. #include <stdlib.h>
  80. #include <dirent.h>
  81. #ifdef Libburn_os_has_statvfS
  82. #include <sys/statvfs.h>
  83. #endif /* Libburn_os_has_stavtfS */
  84. #include <sys/dkio.h>
  85. #include <sys/vtoc.h>
  86. #include <sys/scsi/impl/uscsi.h>
  87. /* The waiting time before eventually retrying a failed SCSI command.
  88. Before each retry wait Libburn_sg_linux_retry_incR longer than with
  89. the previous one.
  90. */
  91. #define Libburn_sg_solaris_retry_usleeP 100000
  92. #define Libburn_sg_solaris_retry_incR 100000
  93. /** PORTING : ------ libburn portable headers and definitions ----- */
  94. #include "transport.h"
  95. #include "drive.h"
  96. #include "sg.h"
  97. #include "spc.h"
  98. #include "sbc.h"
  99. #include "debug.h"
  100. #include "toc.h"
  101. #include "util.h"
  102. #include "libdax_msgs.h"
  103. extern struct libdax_msgs *libdax_messenger;
  104. /* is in portable part of libburn */
  105. int burn_drive_is_banned(char *device_address);
  106. int burn_drive_resolve_link(char *path, char adr[],
  107. int *recursion_count, int flag); /* drive.c */
  108. /* Whether to log SCSI commands:
  109. bit0= log in /tmp/libburn_sg_command_log
  110. bit1= log to stderr
  111. bit2= flush every line
  112. */
  113. extern int burn_sg_log_scsi;
  114. /* ------------------------------------------------------------------------ */
  115. /* PORTING: Private definitions. Port only if needed by public functions. */
  116. /* (Public functions are listed below) */
  117. /* ------------------------------------------------------------------------ */
  118. /* Storage object is in libburn/init.c
  119. whether to strive for exclusive access to the drive
  120. */
  121. extern int burn_sg_open_o_excl;
  122. /* ------------------------------------------------------------------------ */
  123. /* PORTING: Private functions. Port only if needed by public functions */
  124. /* (Public functions are listed below) */
  125. /* ------------------------------------------------------------------------ */
  126. static int sg_close_drive(struct burn_drive * d)
  127. {
  128. if (d->fd != -1) {
  129. close(d->fd);
  130. d->fd = -1;
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. static int decode_btl_number(char **cpt, int stopper, int *no)
  136. {
  137. *no = 0;
  138. for ((*cpt)++; **cpt != stopper; (*cpt)++) {
  139. if (**cpt < '0' || **cpt > '9')
  140. return 0;
  141. *no = *no * 10 + **cpt - '0';
  142. }
  143. return 1;
  144. }
  145. /* Read bus, target, lun from name "cXtYdZs2".
  146. Return 0 if name is not of the desired form.
  147. */
  148. static int decode_btl_solaris(char *name, int *busno, int *tgtno, int *lunno,
  149. int flag)
  150. {
  151. char *cpt;
  152. int ret;
  153. *busno = *tgtno = *lunno = -1;
  154. cpt = name;
  155. if (*cpt != 'c')
  156. return 0;
  157. ret = decode_btl_number(&cpt, 't', busno);
  158. if (ret <= 0)
  159. return ret;
  160. ret = decode_btl_number(&cpt, 'd', tgtno);
  161. if (ret <= 0)
  162. return ret;
  163. ret = decode_btl_number(&cpt, 's', lunno);
  164. if (ret <= 0)
  165. return ret;
  166. cpt++;
  167. if (*cpt != '2' || *(cpt + 1) != 0)
  168. return 0;
  169. return 1;
  170. }
  171. static int start_enum_cXtYdZs2(burn_drive_enumerator_t *idx, int flag)
  172. {
  173. DIR *dir;
  174. idx->dir = NULL;
  175. dir = opendir("/dev/rdsk");
  176. if (dir == NULL) {
  177. libdax_msgs_submit(libdax_messenger, -1,
  178. 0x0002000c, LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  179. "Cannot start device file enumeration. opendir(\"/dev/rdsk\") failed.",
  180. errno, 0);
  181. return 0;
  182. }
  183. idx->dir = dir;
  184. return 1;
  185. }
  186. static int next_enum_cXtYdZs2(burn_drive_enumerator_t *idx,
  187. char adr[], int adr_size, int flag)
  188. {
  189. int busno, tgtno, lunno, ret, fd = -1;
  190. char volpath[160];
  191. struct dirent *entry;
  192. struct dk_cinfo cinfo;
  193. DIR *dir;
  194. dir = idx->dir;
  195. while (1) {
  196. errno = 0;
  197. entry = readdir(dir);
  198. if (entry == NULL) {
  199. if (errno) {
  200. libdax_msgs_submit(libdax_messenger,
  201. -1, 0x0002000d,
  202. LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  203. "Cannot enumerate next device. readdir() from \"/dev/rdsk\" failed.",
  204. errno, 0);
  205. return -1;
  206. }
  207. break;
  208. }
  209. if (strlen(entry->d_name) > sizeof(volpath) - 11)
  210. continue;
  211. ret = decode_btl_solaris(entry->d_name,
  212. &busno, &tgtno, &lunno, 0);
  213. if (ret <= 0)
  214. continue; /* not cXtYdZs2 */
  215. sprintf(volpath, "/dev/rdsk/%s", entry->d_name);
  216. if (burn_drive_is_banned(volpath))
  217. continue;
  218. fd = open(volpath, O_RDONLY | O_NDELAY);
  219. if (fd < 0)
  220. continue;
  221. /* See man dkio */
  222. ret = ioctl(fd, DKIOCINFO, &cinfo);
  223. close(fd);
  224. if (ret < 0)
  225. continue;
  226. if (cinfo.dki_ctype != DKC_CDROM)
  227. continue;
  228. if (adr_size <= strlen(volpath))
  229. return -1;
  230. strcpy(adr, volpath);
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. static int end_enum_cXtYdZs2(burn_drive_enumerator_t *idx, int flag)
  236. {
  237. DIR *dir;
  238. dir = idx->dir;
  239. if(dir != NULL)
  240. closedir(dir);
  241. idx->dir = NULL;
  242. return 1;
  243. }
  244. /* ----------------------------------------------------------------------- */
  245. /* PORTING: Private functions which contain publicly needed functionality. */
  246. /* Their portable part must be performed. So it is probably best */
  247. /* to replace the non-portable part and to call these functions */
  248. /* in your port, too. */
  249. /* ----------------------------------------------------------------------- */
  250. /** Wraps a detected drive into libburn structures and hands it over to
  251. libburn drive list.
  252. */
  253. static void enumerate_common(char *fname,
  254. int bus_no, int host_no,
  255. int channel_no, int target_no, int lun_no)
  256. {
  257. int ret;
  258. struct burn_drive out;
  259. /* General libburn drive setup */
  260. burn_setup_drive(&out, fname);
  261. /* This transport adapter uses SCSI-family commands and models
  262. (seems the adapter would know better than its boss, if ever) */
  263. ret = burn_scsi_setup_drive(&out, bus_no, host_no, channel_no,
  264. target_no, lun_no, 0);
  265. if (ret <= 0)
  266. return;
  267. /* PORTING: ------------------- non portable part --------------- */
  268. /* Transport adapter is Solaris uscsi */
  269. /* Adapter specific handles and data */
  270. out.fd = -1;
  271. /* PORTING: ---------------- end of non portable part ------------ */
  272. /* Adapter specific functions with standardized names */
  273. out.grab = sg_grab;
  274. out.release = sg_release;
  275. out.drive_is_open = sg_drive_is_open;
  276. out.issue_command = sg_issue_command;
  277. /* Finally register drive and inquire drive information */
  278. burn_drive_finish_enum(&out);
  279. }
  280. /* ------------------------------------------------------------------------ */
  281. /* PORTING: Public functions. These MUST be ported. */
  282. /* ------------------------------------------------------------------------ */
  283. /** Returns the id string of the SCSI transport adapter and eventually
  284. needed operating system facilities.
  285. This call is usable even if sg_initialize() was not called yet. In that
  286. case a preliminary constant message might be issued if detailed info is
  287. not available yet.
  288. @param msg returns id string
  289. @param flag unused yet, submit 0
  290. @return 1 = success, <=0 = failure
  291. */
  292. int sg_id_string(char msg[1024], int flag)
  293. {
  294. sprintf(msg, "internal Solaris uscsi adapter sg-solaris");
  295. return 1;
  296. }
  297. /** Performs global initialization of the SCSI transport adapter and eventually
  298. needed operating system facilities. Checks for compatibility of supporting
  299. software components.
  300. @param msg returns ids and/or error messages of eventual helpers
  301. @param flag unused yet, submit 0
  302. @return 1 = success, <=0 = failure
  303. */
  304. int sg_initialize(char msg[1024], int flag)
  305. {
  306. return sg_id_string(msg, 0);
  307. }
  308. /** Performs global finalization of the SCSI transport adapter and eventually
  309. needed operating system facilities. Releases globally aquired resources.
  310. @param flag unused yet, submit 0
  311. @return 1 = success, <=0 = failure
  312. */
  313. int sg_shutdown(int flag)
  314. {
  315. return 1;
  316. }
  317. /** Finalizes BURN_OS_TRANSPORT_DRIVE_ELEMENTS, the components of
  318. struct burn_drive which are defined in os-*.h.
  319. The eventual initialization of those components was made underneath
  320. scsi_enumerate_drives().
  321. This will be called when a burn_drive gets disposed.
  322. @param d the drive to be finalized
  323. @param flag unused yet, submit 0
  324. @return 1 = success, <=0 = failure
  325. */
  326. int sg_dispose_drive(struct burn_drive *d, int flag)
  327. {
  328. return 1;
  329. }
  330. /** Returns the next index number and the next enumerated drive address.
  331. The enumeration has to cover all available and accessible drives. It is
  332. allowed to return addresses of drives which are not available but under
  333. some (even exotic) circumstances could be available. It is on the other
  334. hand allowed, only to hand out addresses which can really be used right
  335. in the moment of this call. (This implementation chooses the latter.)
  336. @param idx An opaque handle. Make no own theories about it.
  337. @param adr Takes the reply
  338. @param adr_size Gives maximum size of reply including final 0
  339. @param initialize 1 = start new,
  340. 0 = continue, use no other values for now
  341. -1 = finish
  342. @return 1 = reply is a valid address , 0 = no further address available
  343. -1 = severe error (e.g. adr_size too small)
  344. */
  345. int sg_give_next_adr(burn_drive_enumerator_t *idx,
  346. char adr[], int adr_size, int initialize)
  347. {
  348. int ret;
  349. if (initialize == 1) {
  350. ret = start_enum_cXtYdZs2(idx, 0);
  351. if (ret <= 0)
  352. return ret;
  353. } else if (initialize == -1) {
  354. ret = end_enum_cXtYdZs2(idx, 0);
  355. return 0;
  356. }
  357. ret = next_enum_cXtYdZs2(idx, adr, adr_size, 0);
  358. return ret;
  359. }
  360. /** Brings all available, not-whitelist-banned, and accessible drives into
  361. libburn's list of drives.
  362. */
  363. int scsi_enumerate_drives(void)
  364. {
  365. burn_drive_enumerator_t idx;
  366. int initialize = 1, ret, i_bus_no = -1;
  367. int i_host_no = -1, i_channel_no = -1, i_target_no = -1, i_lun_no = -1;
  368. char buf[4096];
  369. while(1) {
  370. ret = sg_give_next_adr(&idx, buf, sizeof(buf), initialize);
  371. initialize = 0;
  372. if (ret <= 0)
  373. break;
  374. if (burn_drive_is_banned(buf))
  375. continue;
  376. sg_obtain_scsi_adr(buf, &i_bus_no, &i_host_no,
  377. &i_channel_no, &i_target_no, &i_lun_no);
  378. enumerate_common(buf,
  379. i_bus_no, i_host_no, i_channel_no,
  380. i_target_no, i_lun_no);
  381. }
  382. sg_give_next_adr(&idx, buf, sizeof(buf), -1);
  383. return 1;
  384. }
  385. /** Tells whether libburn has the given drive in use or exclusively reserved.
  386. If it is "open" then libburn will eventually call sg_release() on it when
  387. it is time to give up usage resp. reservation.
  388. */
  389. /** Published as burn_drive.drive_is_open() */
  390. int sg_drive_is_open(struct burn_drive * d)
  391. {
  392. return (d->fd != -1);
  393. }
  394. /** Opens the drive for SCSI commands and - if burn activities are prone
  395. to external interference on your system - obtains an exclusive access lock
  396. on the drive. (Note: this is not physical tray locking.)
  397. A drive that has been opened with sg_grab() will eventually be handed
  398. over to sg_release() for closing and unreserving.
  399. */
  400. int sg_grab(struct burn_drive *d)
  401. {
  402. char msg[4096];
  403. int os_errno, ret;
  404. struct dk_cinfo cinfo;
  405. if (d->fd != -1) {
  406. d->released = 0;
  407. return 1;
  408. }
  409. d->fd = open(d->devname, O_RDONLY | O_NDELAY);
  410. if (d->fd == -1) {
  411. os_errno = errno;
  412. sprintf(msg, "Could not grab drive '%s'", d->devname);
  413. libdax_msgs_submit(libdax_messenger, d->global_index,
  414. 0x00020003,
  415. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  416. msg, os_errno, 0);
  417. return 0;
  418. }
  419. ret = ioctl(d->fd, DKIOCINFO, &cinfo);
  420. if (ret < 0)
  421. goto revoke;
  422. if (cinfo.dki_ctype != DKC_CDROM)
  423. goto revoke;
  424. /* >>> obtain eventual locks */;
  425. d->released = 0;
  426. return 1;
  427. revoke:;
  428. sprintf(msg, "Could not grab drive '%s'. Not a CDROM device.",
  429. d->devname);
  430. libdax_msgs_submit(libdax_messenger, d->global_index,
  431. 0x00020003,
  432. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  433. msg, 0, 0);
  434. return 0;
  435. }
  436. /** PORTING: Is mainly about the call to sg_close_drive() and whether it
  437. implements the demanded functionality.
  438. */
  439. /** Gives up the drive for SCSI commands and releases eventual access locks.
  440. (Note: this is not physical tray locking.)
  441. */
  442. int sg_release(struct burn_drive *d)
  443. {
  444. if (d->fd < 0) {
  445. burn_print(1, "release an ungrabbed drive. die\n");
  446. return 0;
  447. }
  448. sg_close_drive(d);
  449. return 0;
  450. }
  451. /** Sends a SCSI command to the drive, receives reply and evaluates wether
  452. the command succeeded or shall be retried or finally failed.
  453. Returned SCSI errors shall not lead to a return value indicating failure.
  454. The callers get notified by c->error. An SCSI failure which leads not to
  455. a retry shall be notified via scsi_notify_error().
  456. The Libburn_log_sg_commandS facility might be of help when problems with
  457. a drive have to be examined. It shall stay disabled for normal use.
  458. @return: 1 success , <=0 failure
  459. */
  460. int sg_issue_command(struct burn_drive *d, struct command *c)
  461. {
  462. int i, usleep_time, timeout_ms, no_retry = 0, ret, key, asc, ascq;
  463. time_t start_time;
  464. struct uscsi_cmd cgc;
  465. char msg[80];
  466. static FILE *fp = NULL;
  467. c->error = 0;
  468. memset(c->sense, 0, sizeof(c->sense));
  469. if (d->fd == -1)
  470. return 0;
  471. if (burn_sg_log_scsi & 1) {
  472. if (fp == NULL) {
  473. fp= fopen("/tmp/libburn_sg_command_log", "a");
  474. fprintf(fp,
  475. "\n-----------------------------------------\n");
  476. }
  477. }
  478. if (burn_sg_log_scsi & 3)
  479. scsi_log_cmd(c,fp,0);
  480. memset (&cgc, 0, sizeof (struct uscsi_cmd));
  481. /* No error messages, no retries,
  482. do not execute with other commands, request sense data
  483. */
  484. cgc.uscsi_flags = USCSI_SILENT | USCSI_DIAGNOSE | USCSI_ISOLATE
  485. | USCSI_RQENABLE;
  486. cgc.uscsi_timeout = 200;
  487. cgc.uscsi_cdb = (caddr_t) c->opcode;
  488. cgc.uscsi_bufaddr = (caddr_t) c->page->data;
  489. if (c->dir == TO_DRIVE) {
  490. cgc.uscsi_flags |= USCSI_WRITE;
  491. cgc.uscsi_buflen = c->page->bytes;
  492. } else if (c->dir == FROM_DRIVE) {
  493. cgc.uscsi_flags |= USCSI_READ;
  494. if (c->dxfer_len >= 0)
  495. cgc.uscsi_buflen = c->dxfer_len;
  496. else
  497. cgc.uscsi_buflen = BUFFER_SIZE;
  498. /* touch page so we can use valgrind */
  499. memset(c->page->data, 0, BUFFER_SIZE);
  500. } else {
  501. cgc.uscsi_buflen = 0;
  502. }
  503. cgc.uscsi_cdblen = c->oplen;
  504. cgc.uscsi_rqlen = sizeof(c->sense);
  505. cgc.uscsi_rqbuf = (caddr_t) c->sense;
  506. /* retry-loop */
  507. start_time = time(NULL);
  508. timeout_ms = 200000;
  509. for(i = 0; ; i++) {
  510. ret = ioctl(d->fd, USCSICMD, &cgc);
  511. /* For cgc.uscsi_status see SAM-3 5.3.1, Table 22
  512. 0 = GOOD , 2 = CHECK CONDITION : Sense Data are delivered
  513. 8 = BUSY
  514. */
  515. if (ret != 0 && cgc.uscsi_status != 2) {
  516. sprintf(msg,
  517. "Failed to transfer command to drive. (uscsi_status = 0x%X)",
  518. (unsigned int) cgc.uscsi_status),
  519. libdax_msgs_submit(libdax_messenger,
  520. d->global_index, 0x0002010c,
  521. LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
  522. msg, errno, 0);
  523. sg_close_drive(d);
  524. d->released = 1;
  525. d->busy = BURN_DRIVE_IDLE;
  526. c->error = 1;
  527. return -1;
  528. }
  529. /* >>> valid sense: cgc.uscsi_rqlen - cgc.uscsi_rqresid */;
  530. spc_decode_sense(c->sense, 0, &key, &asc, &ascq);
  531. if (key || asc || ascq) {
  532. if (no_retry || !c->retry) {
  533. c->error = 1;
  534. goto ex;
  535. }
  536. switch (scsi_error(d, c->sense, 18)) {
  537. case RETRY:
  538. if (burn_sg_log_scsi & 3) {
  539. /* >>> Need own duration time
  540. measurement. Then remove bit1 */
  541. scsi_log_err(c, fp, c->sense, 18, 0,
  542. 1 | 2);
  543. scsi_log_cmd(c,fp,0);
  544. }
  545. break;
  546. case FAIL:
  547. c->error = 1;
  548. goto ex;
  549. }
  550. /*
  551. Calming down retries and breaking up endless cycle
  552. */
  553. usleep_time = Libburn_sg_solaris_retry_usleeP +
  554. i * Libburn_sg_solaris_retry_incR;
  555. if (time(NULL) + usleep_time / 1000000 - start_time >
  556. timeout_ms / 1000 + 1) {
  557. c->error = 1;
  558. goto ex;
  559. }
  560. usleep(usleep_time);
  561. } else
  562. break; /* retry-loop */
  563. } /* end of retry-loop */
  564. ex:;
  565. if (c->error)
  566. scsi_notify_error(d, c, c->sense, 18, 0);
  567. if (burn_sg_log_scsi & 3)
  568. /* >>> Need own duration time measurement. Then remove bit1 */
  569. scsi_log_err(c, fp, c->sense, 18, 0, (c->error != 0) | 2);
  570. return 1;
  571. }
  572. /** Tries to obtain SCSI address parameters.
  573. @return 1 is success , 0 is failure
  574. */
  575. int sg_obtain_scsi_adr(char *path, int *bus_no, int *host_no, int *channel_no,
  576. int *target_no, int *lun_no)
  577. {
  578. int ret;
  579. /* Try to guess from path */
  580. if (strncmp("/dev/rdsk/", path, 10) == 0) {
  581. ret = decode_btl_solaris(path + 10,
  582. bus_no, target_no, lun_no, 0);
  583. if (ret > 0) {
  584. *host_no = *bus_no;
  585. *channel_no = 0;
  586. return 1;
  587. }
  588. }
  589. *bus_no = *host_no = *channel_no = *target_no = *lun_no = -1;
  590. /* >>> Could need a ioctl which gives SCSI numbers */;
  591. return (0);
  592. }
  593. /** Tells wether a text is a persistent address as listed by the enumeration
  594. functions.
  595. */
  596. #ifndef NIX
  597. int sg_is_enumerable_adr(char* path)
  598. {
  599. int ret;
  600. int bus_no, target_no, lun_no;
  601. struct stat stbuf;
  602. if (strncmp("/dev/rdsk/", path, 10) != 0)
  603. return 0;
  604. ret = decode_btl_solaris(path + 10, &bus_no, &target_no, &lun_no, 0);
  605. if (ret <= 0)
  606. return 0;
  607. if (stat(path, &stbuf) == -1)
  608. return 0;
  609. return 1;
  610. }
  611. #else /* ! NIX */
  612. int sg_is_enumerable_adr(char* adr)
  613. {
  614. burn_drive_enumerator_t idx;
  615. int initialize = 1, ret;
  616. char buf[64];
  617. while(1) {
  618. ret = sg_give_next_adr(&idx, buf, sizeof(buf), initialize);
  619. initialize = 0;
  620. if (ret <= 0)
  621. break;
  622. if (strcmp(adr, buf) == 0) {
  623. sg_give_next_adr(&idx, buf, sizeof(buf), -1);
  624. return 1;
  625. }
  626. }
  627. sg_give_next_adr(&idx, buf, sizeof(buf), -1);
  628. return (0);
  629. }
  630. #endif /* NIX */
  631. /* Return 1 if the given path leads to a regular file or a device that can be
  632. seeked, read, and possibly written with 2 kB granularity.
  633. */
  634. int burn_os_is_2k_seekrw(char *path, int flag)
  635. {
  636. struct stat stbuf;
  637. if (stat(path, &stbuf) == -1)
  638. return 0;
  639. if (S_ISREG(stbuf.st_mode))
  640. return 1;
  641. if (S_ISBLK(stbuf.st_mode))
  642. return 1;
  643. return 0;
  644. }
  645. /** Estimate the potential payload capacity of a file address.
  646. @param path The address of the file to be examined. If it does not
  647. exist yet, then the directory will be inquired.
  648. @param bytes The pointed value gets modified, but only if an estimation is
  649. possible.
  650. @return -2 = cannot perform necessary operations on file object
  651. -1 = neither path nor dirname of path exist
  652. 0 = could not estimate size capacity of file object
  653. 1 = estimation has been made, bytes was set
  654. */
  655. int burn_os_stdio_capacity(char *path, off_t *bytes)
  656. {
  657. struct stat stbuf;
  658. #ifdef Libburn_os_has_statvfS
  659. struct statvfs vfsbuf;
  660. #endif
  661. char testpath[4096], *cpt;
  662. long blocks;
  663. off_t add_size = 0;
  664. testpath[0] = 0;
  665. blocks = *bytes / 512;
  666. if (stat(path, &stbuf) == -1) {
  667. strcpy(testpath, path);
  668. cpt = strrchr(testpath, '/');
  669. if(cpt == NULL)
  670. strcpy(testpath, ".");
  671. else if(cpt == testpath)
  672. testpath[1] = 0;
  673. else
  674. *cpt = 0;
  675. if (stat(testpath, &stbuf) == -1)
  676. return -1;
  677. } else if(S_ISBLK(stbuf.st_mode)) {
  678. int open_mode = O_RDONLY, fd;
  679. fd = open(path, open_mode);
  680. if (fd == -1)
  681. return -2;
  682. *bytes = lseek(fd, 0, SEEK_END);
  683. close(fd);
  684. if (*bytes == -1) {
  685. *bytes = 0;
  686. return 0;
  687. }
  688. } else if(S_ISREG(stbuf.st_mode)) {
  689. add_size = stbuf.st_blocks * (off_t) 512;
  690. strcpy(testpath, path);
  691. } else
  692. return 0;
  693. if (testpath[0]) {
  694. #ifdef Libburn_os_has_statvfS
  695. if (statvfs(testpath, &vfsbuf) == -1)
  696. return -2;
  697. *bytes = add_size + ((off_t) vfsbuf.f_frsize) *
  698. (off_t) vfsbuf.f_bavail;
  699. #else /* Libburn_os_has_statvfS */
  700. return 0;
  701. #endif /* ! Libburn_os_has_stavtfS */
  702. }
  703. return 1;
  704. }
  705. /* ts A91122 : an interface to open(O_DIRECT) or similar OS tricks. */
  706. #ifdef Libburn_read_o_direcT
  707. /* No special O_DIRECT-like precautions are implemented here */
  708. #endif /* Libburn_read_o_direcT */
  709. int burn_os_open_track_src(char *path, int open_flags, int flag)
  710. {
  711. int fd;
  712. fd = open(path, open_flags);
  713. return fd;
  714. }
  715. void *burn_os_alloc_buffer(size_t amount, int flag)
  716. {
  717. void *buf = NULL;
  718. buf = calloc(1, amount);
  719. return buf;
  720. }
  721. int burn_os_free_buffer(void *buffer, size_t amount, int flag)
  722. {
  723. if (buffer == NULL)
  724. return 0;
  725. free(buffer);
  726. return 1;
  727. }