"Fossies" - the Fresh Open Source Software Archive

Member "xorriso-1.5.4/libburn/sg-dummy.c" (30 Jan 2021, 9712 Bytes) of package /linux/misc/xorriso-1.5.4.pl02.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sg-dummy.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.5.2_vs_1.5.4.

    1 /* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
    2 
    3 /* 
    4    Copyright (c) 2009 - 2011 Thomas Schmitt <scdbackup@gmx.net>
    5    Provided under GPL version 2 or later.
    6 */
    7 
    8 #ifdef HAVE_CONFIG_H
    9 #include "../config.h"
   10 #endif
   11 
   12 
   13 /*
   14 
   15 This is the main operating system dependent SCSI part of libburn. It implements
   16 the transport level aspects of SCSI control and command i/o.
   17 
   18 Present implementation: default dummy which enables libburn only to work
   19                         with stdio: pseudo drive addresses.
   20                         For real implementations see sg-linux.c, sg-freebsd.c,
   21                         sg-libcdio.c
   22 */
   23 
   24 
   25 #include <unistd.h>
   26 #include <stdio.h>
   27 #include <sys/types.h>
   28 #include <errno.h>
   29 #include <fcntl.h>
   30 #include <sys/stat.h>
   31 #include <string.h>
   32 #include <stdlib.h>
   33 
   34 #ifdef Libburn_os_has_statvfS
   35 #include <sys/statvfs.h>
   36 #endif /* Libburn_os_has_stavtfS */
   37 
   38 /* ts B41126 : O_BINARY is needed for Cygwin but undefined elsewhere */
   39 #ifndef O_BINARY
   40 #define O_BINARY 0
   41 #endif
   42 
   43 #include "transport.h"
   44 #include "drive.h"
   45 #include "sg.h"
   46 #include "spc.h"
   47 #include "mmc.h"
   48 #include "sbc.h"
   49 #include "debug.h"
   50 #include "toc.h"
   51 #include "util.h"
   52 #include "init.h"
   53 
   54 #include "libdax_msgs.h"
   55 extern struct libdax_msgs *libdax_messenger;
   56 
   57 
   58 /** Returns the id string  of the SCSI transport adapter and eventually
   59     needed operating system facilities.
   60     This call is usable even if sg_initialize() was not called yet. In that
   61     case a preliminary constant message might be issued if detailed info is
   62     not available yet.
   63     @param msg   returns id string
   64     @param flag  unused yet, submit 0
   65     @return      1 = success, <=0 = failure
   66 */
   67 int sg_id_string(char msg[1024], int flag)
   68 {
   69     strcpy(msg, "internal X/Open adapter sg-dummy");
   70     return 1;
   71 }
   72 
   73 
   74 /** Performs global initialization of the SCSI transport adapter and eventually
   75     needed operating system facilities. Checks for compatibility supporting
   76     software components.
   77     @param msg   returns ids and/or error messages of eventual helpers
   78     @param flag  unused yet, submit 0
   79     @return      1 = success, <=0 = failure
   80 */
   81 int sg_initialize(char msg[1024], int flag)
   82 {
   83     return sg_id_string(msg, 0);
   84 }
   85 
   86 
   87 /** Performs global finalization of the SCSI transport adapter and eventually
   88     needed operating system facilities. Releases globally acquired resources.
   89     @param flag  unused yet, submit 0
   90     @return      1 = success, <=0 = failure
   91 */  
   92 int sg_shutdown(int flag)
   93 {
   94     return 1;
   95 }
   96 
   97 
   98 /** Finalizes BURN_OS_TRANSPORT_DRIVE_ELEMENTS, the components of
   99     struct burn_drive which are defined in os-*.h.
  100     This will be called when a burn_drive gets disposed.
  101     @param d     the drive to be finalized
  102     @param flag  unused yet, submit 0
  103     @return      1 = success, <=0 = failure
  104 */
  105 int sg_dispose_drive(struct burn_drive *d, int flag)
  106 {
  107         return 1;
  108 }
  109 
  110 
  111 /** Returns the next index number and the next enumerated drive address.
  112     The enumeration has to cover all available and accessible drives. It is
  113     allowed to return addresses of drives which are not available but under
  114     some (even exotic) circumstances could be available. It is on the other
  115     hand allowed, only to hand out addresses which can really be used right
  116     in the moment of this call. (This implementation chooses the former.)
  117     @param idx An opaque handle. Make no own theories about it.
  118     @param adr Takes the reply
  119     @param adr_size Gives maximum size of reply including final 0
  120     @param initialize  1 = start new,
  121                        0 = continue, use no other values for now
  122                       -1 = finish
  123     @return 1 = reply is a valid address , 0 = no further address available
  124            -1 = severe error (e.g. adr_size too small)
  125 */
  126 int sg_give_next_adr(burn_drive_enumerator_t *idx,
  127              char adr[], int adr_size, int initialize)
  128 {
  129     return 0;
  130 }
  131 
  132 
  133 /** Brings all available, not-whitelist-banned, and accessible drives into
  134     libburn's list of drives.
  135 */
  136 /* ts A61115: replacing call to sg-implementation internals from drive.c */
  137 int scsi_enumerate_drives(void)
  138 {
  139     libdax_msgs_submit(libdax_messenger, -1, 0x0002016b,
  140         LIBDAX_MSGS_SEV_WARNING, LIBDAX_MSGS_PRIO_HIGH,
  141         "No MMC transport adapter is present. Running on sg-dummy.c.",
  142         0, 0);
  143     return 1;
  144 }
  145 
  146 
  147 /** Tells whether libburn has the given drive in use or exclusively reserved.
  148     If it is "open" then libburn will eventually call sg_release() on it when
  149     it is time to give up usage and reservation.
  150 */
  151 /** Published as burn_drive.drive_is_open() */
  152 int sg_drive_is_open(struct burn_drive * d)
  153 {
  154     return 0;
  155 }
  156 
  157 
  158 /** Opens the drive for SCSI commands and - if burn activities are prone
  159     to external interference on your system - obtains an exclusive access lock
  160     on the drive. (Note: this is not physical tray locking.)
  161     A drive that has been opened with sg_grab() will eventually be handed
  162     over to sg_release() for closing and unreserving.
  163 */
  164 int sg_grab(struct burn_drive *d)
  165 {
  166     libdax_msgs_submit(libdax_messenger, d->global_index, 0x0002016a,
  167         LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  168         "No MMC transport adapter is present. Running on sg-dummy.c.",
  169         0, 0);
  170     return 0;
  171 }
  172 
  173 
  174 /** Gives up the drive for SCSI commands and releases eventual access locks.
  175     (Note: this is not physical tray locking.)
  176 */
  177 int sg_release(struct burn_drive *d)
  178 {
  179     return 0;
  180 }
  181 
  182 
  183 /** Sends a SCSI command to the drive, receives reply and evaluates whether
  184     the command succeeded or shall be retried or finally failed.
  185     Returned SCSI errors shall not lead to a return value indicating failure.
  186     The callers get notified by c->error. An SCSI failure which leads not to
  187     a retry shall be notified via scsi_notify_error().
  188     The Libburn_log_sg_commandS facility might be of help when problems with
  189     a drive have to be examined. It shall stay disabled for normal use.
  190     @return: 1 success , <=0 failure
  191 */
  192 int sg_issue_command(struct burn_drive *d, struct command *c)
  193 {
  194     libdax_msgs_submit(libdax_messenger, d->global_index, 0x0002016a,
  195         LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  196         "No MMC transport adapter is present. Running on sg-dummy.c.",
  197         0, 0);
  198     return -1;
  199 }
  200 
  201 
  202 /** Tries to obtain SCSI address parameters.
  203     @return  1 is success , 0 is failure
  204 */
  205 int sg_obtain_scsi_adr(char *path, int *bus_no, int *host_no, int *channel_no,
  206                        int *target_no, int *lun_no)
  207 {
  208         libdax_msgs_submit(libdax_messenger, -1, 0x0002016c,
  209                 LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  210                 "No MMC transport adapter is present. Running on sg-dummy.c.",
  211                 0, 0);
  212     return 0;
  213 }
  214 
  215 
  216 /** Tells whether a text is a persistent address as listed by the enumeration
  217     functions.
  218 */
  219 int sg_is_enumerable_adr(char *adr)
  220 {
  221     return(0);
  222 }
  223 
  224 
  225 /* Return 1 if the given path leads to a regular file or a device that can be
  226    fseeked, read, and possibly written with 2 kB granularity. 
  227 */
  228 int burn_os_is_2k_seekrw(char *path, int flag)
  229 {
  230     struct stat stbuf;
  231 
  232     if (stat(path, &stbuf) == -1)
  233         return 0;
  234     if (S_ISREG(stbuf.st_mode))
  235         return 1;
  236     if (S_ISBLK(stbuf.st_mode))
  237         return 1;
  238     return 0;
  239 }
  240 
  241 
  242 /** Estimate the potential payload capacity of a file address.
  243     @param path  The address of the file to be examined. If it does not
  244                  exist yet, then the directory will be inquired.
  245     @param bytes The pointed value gets modified, but only if an estimation is
  246                  possible.
  247     @return      -2 = cannot perform necessary operations on file object
  248                  -1 = neither path nor dirname of path exist
  249                   0 = could not estimate size capacity of file object
  250                   1 = estimation has been made, bytes was set
  251 */
  252 int burn_os_stdio_capacity(char *path, off_t write_start, off_t *bytes)
  253 {
  254     struct stat stbuf;
  255 
  256 #ifdef Libburn_os_has_statvfS
  257     struct statvfs vfsbuf;
  258 #endif
  259 
  260     char *testpath = NULL, *cpt;
  261     off_t add_size = 0;
  262     int ret;
  263 
  264     BURN_ALLOC_MEM(testpath, char, 4096);
  265 
  266     testpath[0] = 0;
  267     if (stat(path, &stbuf) == -1) {
  268         strcpy(testpath, path);
  269         cpt = strrchr(testpath, '/');
  270         if(cpt == NULL)
  271             strcpy(testpath, ".");
  272         else if(cpt == testpath)
  273             testpath[1] = 0;
  274         else
  275             *cpt = 0;
  276         if (stat(testpath, &stbuf) == -1)
  277             {ret = -1; goto ex;}
  278 
  279 #ifdef Libburn_if_this_was_linuX
  280 
  281     } else if(S_ISBLK(stbuf.st_mode)) {
  282         long blocks;
  283 
  284         blocks = *bytes / 512;
  285         fd = open(path, open_mode | O_BINARY);
  286         if (fd == -1)
  287             {ret = -2; goto ex;}
  288         ret = ioctl(fd, BLKGETSIZE, &blocks);
  289         close(fd);
  290         if (ret == -1)
  291             {ret = -2; goto ex;}
  292         *bytes = ((off_t) blocks) * (off_t) 512;
  293 
  294 #endif /* Libburn_if_this_was_linuX */
  295 
  296     } else if(S_ISREG(stbuf.st_mode)) {
  297         add_size = burn_sparse_file_addsize(write_start, &stbuf);
  298         strcpy(testpath, path);
  299     } else
  300         {ret = 0; goto ex;}
  301 
  302     if (testpath[0]) {  
  303 
  304 #ifdef Libburn_os_has_statvfS
  305 
  306         if (statvfs(testpath, &vfsbuf) == -1)
  307             {ret = -2; goto ex;}
  308         *bytes = add_size + ((off_t) vfsbuf.f_frsize) *
  309                         (off_t) vfsbuf.f_bavail;
  310 
  311 #else /* Libburn_os_has_statvfS */
  312 
  313         {ret = 0; goto ex;}
  314 
  315 #endif /* ! Libburn_os_has_stavtfS */
  316 
  317     }
  318     ret = 1;
  319 ex:;
  320     BURN_FREE_MEM(testpath);
  321     return ret;
  322 }
  323 
  324 
  325 /* ts A91122 : an interface to open(O_DIRECT) or similar OS tricks. */
  326 
  327 #ifdef Libburn_read_o_direcT
  328 
  329     /* No special O_DIRECT-like precautions are implemented here */
  330 
  331 #endif /* Libburn_read_o_direcT */
  332 
  333 
  334 int burn_os_open_track_src(char *path, int open_flags, int flag)
  335 {
  336     int fd;
  337 
  338     fd = open(path, open_flags | O_BINARY);
  339     return fd;
  340 }
  341 
  342 
  343 void *burn_os_alloc_buffer(size_t amount, int flag)
  344 {
  345     void *buf = NULL;
  346 
  347     buf = calloc(1, amount);
  348     return buf;
  349 }
  350 
  351 
  352 int burn_os_free_buffer(void *buffer, size_t amount, int flag)
  353 {
  354     if (buffer == NULL)
  355         return 0;
  356     free(buffer);
  357     return 1;
  358 }
  359