void * /* list of char* -- caller must free it */ iso9660_fs_readdir (const CdIo *cdio, const char pathname[], bool is_mode2) { iso9660_stat_t stat; cdio_assert (cdio != NULL); cdio_assert (pathname != NULL); if (iso9660_fs_stat (cdio, pathname, &stat, is_mode2)) return NULL; if (stat.type != _STAT_DIR) return NULL; { unsigned offset = 0; uint8_t *_dirbuf = NULL; CdioList *retval = _cdio_list_new (); if (stat.size != ISO_BLOCKSIZE * stat.secsize) { cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!", (unsigned) stat.size, (unsigned long int) ISO_BLOCKSIZE * stat.secsize); } _dirbuf = _cdio_malloc (stat.secsize * ISO_BLOCKSIZE); if (is_mode2) { if (cdio_read_mode2_sectors (cdio, _dirbuf, stat.lsn, false, stat.secsize)) cdio_assert_not_reached (); } else { if (cdio_read_mode1_sectors (cdio, _dirbuf, stat.lsn, false, stat.secsize)) cdio_assert_not_reached (); } while (offset < (stat.secsize * ISO_BLOCKSIZE)) { const iso9660_dir_t *idr = (void *) &_dirbuf[offset]; if (!iso9660_get_dir_len(idr)) { offset++; continue; } _cdio_list_append (retval, _idr2name (idr)); offset += iso9660_get_dir_len(idr); } cdio_assert (offset == (stat.secsize * ISO_BLOCKSIZE)); free (_dirbuf); return retval; } }
static bool find_fs_lsn_recurse (const CdIo *cdio, const char pathname[], /*out*/ iso9660_stat_t *statbuf, lsn_t lsn) { CdioList *entlist = iso9660_fs_readdir (cdio, pathname, true); CdioList *dirlist = _cdio_list_new (); CdioListNode *entnode; cdio_assert (entlist != NULL); /* iterate over each entry in the directory */ _CDIO_LIST_FOREACH (entnode, entlist) { char *name = _cdio_list_node_data (entnode); char _fullname[4096] = { 0, }; snprintf (_fullname, sizeof (_fullname), "%s%s", pathname, name); if (iso9660_fs_stat (cdio, _fullname, statbuf, true)) cdio_assert_not_reached (); strncat (_fullname, "/", sizeof (_fullname)); if (statbuf->type == _STAT_DIR && strcmp (name, ".") && strcmp (name, "..")) _cdio_list_append (dirlist, strdup (_fullname)); if (statbuf->lsn == lsn) { _cdio_list_free (entlist, true); _cdio_list_free (dirlist, true); return true; } }
int main(int argc, const char *argv[]) { iso9660_stat_t *p_statbuf; FILE *p_outfd; int i; char const *psz_image; char const *psz_fname; char translated_name[256]; char untranslated_name[256] = ISO9660_PATH; CdIo_t *p_cdio; unsigned int i_fname=sizeof(ISO9660_FILENAME); if (argc > 3) { printf("usage %s [CD-ROM-or-image [filename]]\n", argv[0]); printf("Extracts filename from CD-ROM-or-image.\n"); return 1; } if (argc > 1) psz_image = argv[1]; else psz_image = ISO9660_IMAGE; if (argc > 2) { psz_fname = argv[2]; i_fname = strlen(psz_fname)+1; } else psz_fname = ISO9660_FILENAME; strncat(untranslated_name, psz_fname, i_fname); p_cdio = cdio_open (psz_image, DRIVER_UNKNOWN); if (!p_cdio) { fprintf(stderr, "Sorry, couldn't open %s\n", psz_image); return 1; } p_statbuf = iso9660_fs_stat (p_cdio, untranslated_name); if (NULL == p_statbuf) { fprintf(stderr, "Could not get ISO-9660 file information for file %s\n", untranslated_name); cdio_destroy(p_cdio); return 2; } iso9660_name_translate(psz_fname, translated_name); if (!(p_outfd = fopen (translated_name, "wb"))) { perror ("fopen()"); cdio_destroy(p_cdio); free(p_statbuf); return 3; } /* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */ { const unsigned int i_blocks = CEILING(p_statbuf->size, ISO_BLOCKSIZE); for (i = 0; i < i_blocks; i ++) { char buf[ISO_BLOCKSIZE]; const lsn_t lsn = p_statbuf->lsn + i; memset (buf, 0, ISO_BLOCKSIZE); if ( 0 != cdio_read_data_sectors (p_cdio, buf, lsn, ISO_BLOCKSIZE, 1) ) { fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n", (long unsigned int) p_statbuf->lsn); my_exit(4); } fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd); if (ferror (p_outfd)) { perror ("fwrite()"); my_exit(5); } } } fflush (p_outfd); /* Make sure the file size has the exact same byte size. Without the truncate below, the file will a multiple of ISO_BLOCKSIZE. */ if (ftruncate (fileno (p_outfd), p_statbuf->size)) perror ("ftruncate()"); printf("Extraction of file '%s' from '%s' successful.\n", translated_name, untranslated_name); my_exit(0); }
/*! Read psz_path (a directory) and return a list of iso9660_stat_t of the files inside that. The caller must free the returned result. b_mode2 is historical. It is not used. */ CdioList_t * iso9660_fs_readdir (CdIo_t *p_cdio, const char psz_path[], bool b_mode2) { generic_img_private_t *p_env; iso9660_stat_t *p_stat; if (!p_cdio) return NULL; if (!psz_path) return NULL; p_env = (generic_img_private_t *) p_cdio->env; p_stat = iso9660_fs_stat (p_cdio, psz_path); if (!p_stat) return NULL; if (p_stat->type != _STAT_DIR) { free(p_stat->rr.psz_symlink); free(p_stat); return NULL; } { unsigned offset = 0; uint8_t *_dirbuf = NULL; CdioList_t *retval = _cdio_list_new (); if (p_stat->size != ISO_BLOCKSIZE * p_stat->secsize) { cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!", (unsigned) p_stat->size, (unsigned long int) ISO_BLOCKSIZE * p_stat->secsize); } _dirbuf = calloc(1, p_stat->secsize * ISO_BLOCKSIZE); if (!_dirbuf) { cdio_warn("Couldn't calloc(1, %d)", p_stat->secsize * ISO_BLOCKSIZE); return NULL; } if (cdio_read_data_sectors (p_cdio, _dirbuf, p_stat->lsn, ISO_BLOCKSIZE, p_stat->secsize)) return NULL; while (offset < (p_stat->secsize * ISO_BLOCKSIZE)) { iso9660_dir_t *p_iso9660_dir = (void *) &_dirbuf[offset]; iso9660_stat_t *p_iso9660_stat; if (!iso9660_get_dir_len(p_iso9660_dir)) { offset++; continue; } p_iso9660_stat = _iso9660_dir_to_statbuf(p_iso9660_dir, dunno, p_env->i_joliet_level); _cdio_list_append (retval, p_iso9660_stat); offset += iso9660_get_dir_len(p_iso9660_dir); } cdio_assert (offset == (p_stat->secsize * ISO_BLOCKSIZE)); free (_dirbuf); free (p_stat); return retval; } }
/*! Read pathname (a directory) and return a list of iso9660_stat_t of the files inside that. The caller must free the returned result. */ CdioList_t * iso9660_fs_readdir (CdIo_t *p_cdio, const char pathname[], bool b_mode2) { iso9660_stat_t *p_stat; generic_img_private_t *p_env = (generic_img_private_t *) p_cdio->env; if (!p_cdio) return NULL; if (!pathname) return NULL; p_stat = iso9660_fs_stat (p_cdio, pathname); if (!p_stat) return NULL; if (p_stat->type != _STAT_DIR) { free(p_stat); return NULL; } { unsigned offset = 0; uint8_t *_dirbuf = NULL; CdioList_t *retval = _cdio_list_new (); if (p_stat->size != ISO_BLOCKSIZE * p_stat->secsize) { cdio_warn ("bad size for ISO9660 directory (%ud) should be (%lu)!", (unsigned) p_stat->size, (unsigned long int) ISO_BLOCKSIZE * p_stat->secsize); } _dirbuf = _cdio_malloc (p_stat->secsize * ISO_BLOCKSIZE); if (b_mode2) { if (cdio_read_mode2_sectors (p_cdio, _dirbuf, p_stat->lsn, false, p_stat->secsize)) cdio_assert_not_reached (); } else { if (cdio_read_mode1_sectors (p_cdio, _dirbuf, p_stat->lsn, false, p_stat->secsize)) cdio_assert_not_reached (); } while (offset < (p_stat->secsize * ISO_BLOCKSIZE)) { iso9660_dir_t *p_iso9660_dir = (void *) &_dirbuf[offset]; iso9660_stat_t *p_iso9660_stat; if (!iso9660_get_dir_len(p_iso9660_dir)) { offset++; continue; } p_iso9660_stat = _iso9660_dir_to_statbuf(p_iso9660_dir, b_mode2, p_env->i_joliet_level); _cdio_list_append (retval, p_iso9660_stat); offset += iso9660_get_dir_len(p_iso9660_dir); } cdio_assert (offset == (p_stat->secsize * ISO_BLOCKSIZE)); free (_dirbuf); free (p_stat); return retval; } }