ffindex-0.9.9.3/.hg_archival.txt0000644000000000000000000000022512240136054016335 0ustar rootroot00000000000000repo: ab95744b07d43db4c017f83dcb7452da7352324f node: f929b5617e4b5589f7d623074a7186636807e633 branch: default latesttag: null latesttagdistance: 157 ffindex-0.9.9.3/.hgignore0000644000000000000000000000003712240136054015053 0ustar rootroot00000000000000.*\.o test_big bin lib include ffindex-0.9.9.3/LICENSE0000644000000000000000000005265512240136054014272 0ustar rootroot00000000000000AUTHOR FFindex was designed and implemented by Andreas Hauser . Please add your name here if you distribute modified versions. This file must be distributed with any source or binary distribution including parts of this work. SHORT SUMMARY OF THE LICENSE Ffindex is provided under the Create Commons license "Attribution-ShareAlike 3.0", which basically captures the spirit of the Gnu Public License (GPL). See: http://creativecommons.org/licenses/by-sa/3.0/ Basically you are free: * to Share — to copy, distribute and transmit the work * to Remix — to adapt the work Under the following conditions: * Attribution You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). * Share Alike If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. LICENSE THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED. BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS. 1. Definitions 1. "Adaptation" means a work based upon the Work, or upon the Work and other pre-existing works, such as a translation, adaptation, derivative work, arrangement of music or other alterations of a literary or artistic work, or phonogram or performance and includes cinematographic adaptations or any other form in which the Work may be recast, transformed, or adapted including in any form recognizably derived from the original, except that a work that constitutes a Collection will not be considered an Adaptation for the purpose of this License. For the avoidance of doubt, where the Work is a musical work, performance or phonogram, the synchronization of the Work in timed-relation with a moving image ("synching") will be considered an Adaptation for the purpose of this License. 2. "Collection" means a collection of literary or artistic works, such as encyclopedias and anthologies, or performances, phonograms or broadcasts, or other works or subject matter other than works listed in Section 1(f) below, which, by reason of the selection and arrangement of their contents, constitute intellectual creations, in which the Work is included in its entirety in unmodified form along with one or more other contributions, each constituting separate and independent works in themselves, which together are assembled into a collective whole. A work that constitutes a Collection will not be considered an Adaptation (as defined below) for the purposes of this License. 3. "Creative Commons Compatible License" means a license that is listed at http://creativecommons.org/compatiblelicenses that has been approved by Creative Commons as being essentially equivalent to this License, including, at a minimum, because that license: (i) contains terms that have the same purpose, meaning and effect as the License Elements of this License; and, (ii) explicitly permits the relicensing of adaptations of works made available under that license under this License or a Creative Commons jurisdiction license with the same License Elements as this License. 4. "Distribute" means to make available to the public the original and copies of the Work or Adaptation, as appropriate, through sale or other transfer of ownership. 5. "License Elements" means the following high-level license attributes as selected by Licensor and indicated in the title of this License: Attribution, ShareAlike. 6. "Licensor" means the individual, individuals, entity or entities that offer(s) the Work under the terms of this License. 7. "Original Author" means, in the case of a literary or artistic work, the individual, individuals, entity or entities who created the Work or if no individual or entity can be identified, the publisher; and in addition (i) in the case of a performance the actors, singers, musicians, dancers, and other persons who act, sing, deliver, declaim, play in, interpret or otherwise perform literary or artistic works or expressions of folklore; (ii) in the case of a phonogram the producer being the person or legal entity who first fixes the sounds of a performance or other sounds; and, (iii) in the case of broadcasts, the organization that transmits the broadcast. 8. "Work" means the literary and/or artistic work offered under the terms of this License including without limitation any production in the literary, scientific and artistic domain, whatever may be the mode or form of its expression including digital form, such as a book, pamphlet and other writing; a lecture, address, sermon or other work of the same nature; a dramatic or dramatico-musical work; a choreographic work or entertainment in dumb show; a musical composition with or without words; a cinematographic work to which are assimilated works expressed by a process analogous to cinematography; a work of drawing, painting, architecture, sculpture, engraving or lithography; a photographic work to which are assimilated works expressed by a process analogous to photography; a work of applied art; an illustration, map, plan, sketch or three-dimensional work relative to geography, topography, architecture or science; a performance; a broadcast; a phonogram; a compilation of data to the extent it is protected as a copyrightable work; or a work performed by a variety or circus performer to the extent it is not otherwise considered a literary or artistic work. 9. "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this License despite a previous violation. 10. "Publicly Perform" means to perform public recitations of the Work and to communicate to the public those public recitations, by any means or process, including by wire or wireless means or public digital performances; to make available to the public Works in such a way that members of the public may access these Works from a place and at a place individually chosen by them; to perform the Work to the public by any means or process and the communication to the public of the performances of the Work, including by public digital performance; to broadcast and rebroadcast the Work by any means including signs, sounds or images. 11. "Reproduce" means to make copies of the Work by any means including without limitation by sound or visual recordings and the right of fixation and reproducing fixations of the Work, including storage of a protected performance or phonogram in digital form or other electronic medium. 2. Fair Dealing Rights. Nothing in this License is intended to reduce, limit, or restrict any uses free from copyright or rights arising from limitations or exceptions that are provided for in connection with the copyright protection under copyright law or other applicable laws. 3. License Grant. Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated below: 1. to Reproduce the Work, to incorporate the Work into one or more Collections, and to Reproduce the Work as incorporated in the Collections; 2. to create and Reproduce Adaptations provided that any such Adaptation, including any translation in any medium, takes reasonable steps to clearly label, demarcate or otherwise identify that changes were made to the original Work. For example, a translation could be marked "The original work was translated from English to Spanish," or a modification could indicate "The original work has been modified."; 3. to Distribute and Publicly Perform the Work including as incorporated in Collections; and, 4. to Distribute and Publicly Perform Adaptations. 5. For the avoidance of doubt: 1. Non-waivable Compulsory License Schemes. In those jurisdictions in which the right to collect royalties through any statutory or compulsory licensing scheme cannot be waived, the Licensor reserves the exclusive right to collect such royalties for any exercise by You of the rights granted under this License; 2. Waivable Compulsory License Schemes. In those jurisdictions in which the right to collect royalties through any statutory or compulsory licensing scheme can be waived, the Licensor waives the exclusive right to collect such royalties for any exercise by You of the rights granted under this License; and, 3. Voluntary License Schemes. The Licensor waives the right to collect royalties, whether individually or, in the event that the Licensor is a member of a collecting society that administers voluntary licensing schemes, via that society, from any exercise by You of the rights granted under this License. The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights include the right to make such modifications as are technically necessary to exercise the rights in other media and formats. Subject to Section 8(f), all rights not expressly granted by Licensor are hereby reserved. 4. Restrictions. The license granted in Section 3 above is expressly made subject to and limited by the following restrictions: 1. You may Distribute or Publicly Perform the Work only under the terms of this License. You must include a copy of, or the Uniform Resource Identifier (URI) for, this License with every copy of the Work You Distribute or Publicly Perform. You may not offer or impose any terms on the Work that restrict the terms of this License or the ability of the recipient of the Work to exercise the rights granted to that recipient under the terms of the License. You may not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties with every copy of the Work You Distribute or Publicly Perform. When You Distribute or Publicly Perform the Work, You may not impose any effective technological measures on the Work that restrict the ability of a recipient of the Work from You to exercise the rights granted to that recipient under the terms of the License. This Section 4(a) applies to the Work as incorporated in a Collection, but this does not require the Collection apart from the Work itself to be made subject to the terms of this License. If You create a Collection, upon notice from any Licensor You must, to the extent practicable, remove from the Collection any credit as required by Section 4(c), as requested. If You create an Adaptation, upon notice from any Licensor You must, to the extent practicable, remove from the Adaptation any credit as required by Section 4(c), as requested. 2. You may Distribute or Publicly Perform an Adaptation only under the terms of: (i) this License; (ii) a later version of this License with the same License Elements as this License; (iii) a Creative Commons jurisdiction license (either this or a later license version) that contains the same License Elements as this License (e.g., Attribution-ShareAlike 3.0 US)); (iv) a Creative Commons Compatible License. If you license the Adaptation under one of the licenses mentioned in (iv), you must comply with the terms of that license. If you license the Adaptation under the terms of any of the licenses mentioned in (i), (ii) or (iii) (the "Applicable License"), you must comply with the terms of the Applicable License generally and the following provisions: (I) You must include a copy of, or the URI for, the Applicable License with every copy of each Adaptation You Distribute or Publicly Perform; (II) You may not offer or impose any terms on the Adaptation that restrict the terms of the Applicable License or the ability of the recipient of the Adaptation to exercise the rights granted to that recipient under the terms of the Applicable License; (III) You must keep intact all notices that refer to the Applicable License and to the disclaimer of warranties with every copy of the Work as included in the Adaptation You Distribute or Publicly Perform; (IV) when You Distribute or Publicly Perform the Adaptation, You may not impose any effective technological measures on the Adaptation that restrict the ability of a recipient of the Adaptation from You to exercise the rights granted to that recipient under the terms of the Applicable License. This Section 4(b) applies to the Adaptation as incorporated in a Collection, but this does not require the Collection apart from the Adaptation itself to be made subject to the terms of the Applicable License. 3. If You Distribute, or Publicly Perform the Work or any Adaptations or Collections, You must, unless a request has been made pursuant to Section 4(a), keep intact all copyright notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or pseudonym, if applicable) if supplied, and/or if the Original Author and/or Licensor designate another party or parties (e.g., a sponsor institute, publishing entity, journal) for attribution ("Attribution Parties") in Licensor's copyright notice, terms of service or by other reasonable means, the name of such party or parties; (ii) the title of the Work if supplied; (iii) to the extent reasonably practicable, the URI, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to the copyright notice or licensing information for the Work; and (iv) , consistent with Ssection 3(b), in the case of an Adaptation, a credit identifying the use of the Work in the Adaptation (e.g., "French translation of the Work by Original Author," or "Screenplay based on original Work by Original Author"). The credit required by this Section 4(c) may be implemented in any reasonable manner; provided, however, that in the case of a Adaptation or Collection, at a minimum such credit will appear, if a credit for all contributing authors of the Adaptation or Collection appears, then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors. For the avoidance of doubt, You may only use the credit required by this Section for the purpose of attribution in the manner set out above and, by exercising Your rights under this License, You may not implicitly or explicitly assert or imply any connection with, sponsorship or endorsement by the Original Author, Licensor and/or Attribution Parties, as appropriate, of You or Your use of the Work, without the separate, express prior written permission of the Original Author, Licensor and/or Attribution Parties. 4. Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable law, if You Reproduce, Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or Collections, You must not distort, mutilate, modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author's honor or reputation. Licensor agrees that in those jurisdictions (e.g. Japan), in which any exercise of the right granted in Section 3(b) of this License (the right to make Adaptations) would be deemed to be a distortion, mutilation, modification or other derogatory action prejudicial to the Original Author's honor and reputation, the Licensor will waive or not assert, as appropriate, this Section, to the fullest extent permitted by the applicable national law, to enable You to reasonably exercise Your right under Section 3(b) of this License (right to make Adaptations) but not otherwise. 5. Representations, Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE WORK, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU. 6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 7. Termination 1. This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License. Individuals or entities who have received Adaptations or Collections from You under this License, however, will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License. 2. Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time; provided, however that any such election will not serve to withdraw this License (or any other license that has been, or is required to be, granted under the terms of this License), and this License will continue in full force and effect unless terminated as stated above. 8. Miscellaneous 1. Each time You Distribute or Publicly Perform the Work or a Collection, the Licensor offers to the recipient a license to the Work on the same terms and conditions as the license granted to You under this License. 2. Each time You Distribute or Publicly Perform an Adaptation, Licensor offers to the recipient a license to the original Work on the same terms and conditions as the license granted to You under this License. 3. If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this License, and without further action by the parties to this agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. 4. No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or consent shall be in writing and signed by the party to be charged with such waiver or consent. 5. This License constitutes the entire agreement between the parties with respect to the Work licensed here. There are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall not be bound by any additional provisions that may appear in any communication from You. This License may not be modified without the mutual written agreement of the Licensor and You. 6. The rights granted under, and the subject matter referenced, in this License were drafted utilizing the terminology of the Berne Convention for the Protection of Literary and Artistic Works (as amended on September 28, 1979), the Rome Convention of 1961, the WIPO Copyright Treaty of 1996, the WIPO Performances and Phonograms Treaty of 1996 and the Universal Copyright Convention (as revised on July 24, 1971). These rights and subject matter take effect in the relevant jurisdiction in which the License terms are sought to be enforced according to the corresponding provisions of the implementation of those treaty provisions in the applicable national law. If the standard suite of rights granted under applicable copyright law includes additional rights not granted under this License, such additional rights are deemed to be included in the License; this License is not intended to restrict the license of any rights under applicable law. ffindex-0.9.9.3/Makefile0000644000000000000000000000033112240136054014705 0ustar rootroot00000000000000OS:= $(shell uname) ifeq ($(OS), Darwin) MFILE=Makefile.osx else MFILE=Makefile endif all: $(MAKE) -C src -f $(MFILE) $@ %: $(MAKE) -C src -f $(MFILE) $@ release: hg archive -t tgz ffindex-`cat VERSION`.tar.gz ffindex-0.9.9.3/README0000644000000000000000000000600512240136054014131 0ustar rootroot00000000000000* Copyright FFindex was written by Andreas Hauser . Please add your name here if you distribute modified versions. FFindex is provided under the Create Commons license "Attribution-ShareAlike 3.0", which basically captures the spirit of the Gnu Public License (GPL). See: http://creativecommons.org/licenses/by-sa/3.0/ * Thanks Thanks to Laszlo Kajan for creating and maintaining Debian packages and many suggestions to improve the build and user experience. * Overview FFindex is a very simple index/database for huge amounts of small files. The files are stored concatenated in one big data file, seperated by '\0'. A second file contains a plain text index, giving name, offset and length of of the small files. The lookup is currently done with a binary search on an array made from the index file. * Installation $ cd src $ make $ make test If you have MPI and want to compile ffindex_apply_mpi: $ make HAVE_MPI=1 On OS X use for the first make line: $ make -f Makefile.osx # Please use a sensible value for INSTALL_DIR, e.g. /usr/local or /opt/ffindex # or $HOME/ffindex instead of "..". $ make install INSTALL_DIR=.. and with MPI: $ make install INSTALL_DIR=.. HAVE_MPI=1 * Usage Please note that before querying or unlinking entries a ffindex must be sorted, although you can add to it without. So either specify -s with ffindex_build or sorted later with ffindex_modify -s. Also the length of the entry names is restricted. See the usage output of the ffindex_build program. Setup environment: $ export PATH="$INSTALL_DIR/bin:$PATH" $ export LD_LIBRARY_PATH="$INSTALL_DIR/lib:$LD_LIBRARY_PATH" On OS X set DYLD_LIBRARY_PATH instead of LD_LIBRARY_PATH. Build index from files in test/data and test/data2. $ ffindex_build -s /tmp/test.data /tmp/test.ffindex test/data test/data2 Retrieve three entries: $ ffindex_get /tmp/test.data /tmp/test.ffindex a b foo Unlink (Remove reference from index) an entry: $ ffindex_modify -u /tmp/test.ffindex b Retrieve three entries, "b" should now be missing: $ ffindex_get /tmp/test.data /tmp/test.ffindex a b foo Convert a Fasta file to ffindex, entry names are incerental IDs starting from 1: $ ffindex_from_fasta -s fasta.ffdata fasta.ffindex NC_007779.ffn Get first entry by name: $ ffindex_get fasta.ffdata fasta.ffindex 1 Get first and third entry by entry index, this a little faster: $ ffindex_get fasta.ffdata fasta.ffindex -n 1 3 Count the characters including header in each entry: $ ffindex_apply fasta.ffdata fasta.ffindex wc -c Count the number of characters in each sequence, without the header: $ ffindex_apply fasta.ffdata fasta.ffindex perl -ne '$x += length unless(/^>/); END{print "$x\n"}' Parallel version for counting the characters including header in each entry: $ mpirun -np 4 ffindex_apply_mpi fasta.ffdata fasta.ffindex -- wc -c Parallel version for counting the characters including header in each entry and saving the output to a new ffindex: $ mpirun -np 4 ffindex_apply_mpi fasta.ffdata fasta.ffindex -i out-wc.ffindex -o out-wc.ffdata -- wc -c ffindex-0.9.9.3/VERSION0000644000000000000000000000001012240136054014307 0ustar rootroot000000000000000.9.9.3 ffindex-0.9.9.3/rpm/ffindex.spec0000644000000000000000000000177212240136054016354 0ustar rootroot00000000000000Summary: FFindex is a very simple index/database for huge amounts of small files. Name: ffindex Version: 0.9.9.3 Release: 1 License: Create Commons license "Attribution-ShareAlike 3.0" Group: Utilities/System Source: https://www.splashground.de/~andy/programs/FFindex/ffindex-0.9.9.3.tar.gz %description FFindex is a very simple index/database for huge amounts of small files. The files are stored concatenated in one big data file, seperated by '\0'. A second file contains a plain text index, giving name, offset and length of of the small files. The lookup is currently done with a binary search on an array made from the index file. %prep %setup %build make %install make install INSTALL_DIR=%{buildroot}%{_prefix} %files %doc README LICENSE /usr/bin/ffindex_build /usr/bin/ffindex_get /usr/bin/ffindex_modify /usr/bin/ffindex_from_fasta /usr/bin/ffindex_apply /usr/bin/ffindex_unpack /usr/include/ffindex.h /usr/include/ffutil.h %{_libdir}/libffindex.a %{_libdir}/libffindex.so %{_libdir}/libffindex.so.0.1 ffindex-0.9.9.3/src/Makefile0000644000000000000000000001106312240136054015500 0ustar rootroot00000000000000INSTALL_DIR?=.. libdir=`([ -d /usr/lib64 ] && echo lib64) || echo lib` LANG=C CC=gcc -fopenmp #CC=icc -openmp CFLAGS+=-std=c99 -O2 -g -fPIC -Wall -pedantic -D_GNU_SOURCE=1 LDFLAGS=-lffindex -L. ifdef FFINDEX_STATIC LDFLAGS+=-static FFINDEX_OBJECTS=libffindex.a else FFINDEX_OBJECTS=libffindex.so libffindex.so.0.1 endif #-Wl,--version-script=libffindex.map OBJECTS=ffindex_apply ffindex_from_fasta ffindex_get ffindex_build ffindex_modify libffindex.a libffindex.so ffindex_unpack ifdef HAVE_MPI OBJECTS+=ffindex_apply_mpi endif all: $(OBJECTS) ffindex.o: ffindex.c ffindex.h ffutil.h ffindex_apply: $(FFINDEX_OBJECTS) ffindex_apply.o ffindex.h ffindex_apply_mpi: $(FFINDEX_OBJECTS) ffindex_apply_mpi.o ffindex.h mpicc $(CFLAGS) -o $@ ffindex_apply_mpi.o $(LDFLAGS) ffindex_apply_mpi.o: ffindex_apply_mpi.c ffindex.h mpicc $(CFLAGS) -c ffindex_apply_mpi.c -o $@ ffindex_from_fasta: $(FFINDEX_OBJECTS) ffindex_from_fasta.o ffindex.h $(CC) -o $@ ffindex_from_fasta.o $(LDFLAGS) -L. -lffindex ffindex_get: $(FFINDEX_OBJECTS) ffindex_get.o ffindex.h $(CC) -o $@ ffindex_get.o $(LDFLAGS) -L. -lffindex ffindex_build: $(FFINDEX_OBJECTS) ffindex_build.o ffindex.h $(CC) -o $@ ffindex_build.o $(LDFLAGS) -L. -lffindex ffindex_modify: $(FFINDEX_OBJECTS) ffindex_modify.o ffindex.h $(CC) -o $@ ffindex_modify.o $(LDFLAGS) -L. -lffindex ffindex_unpack: $(FFINDEX_OBJECTS) ffindex_unpack.o ffindex.h $(CC) -o $@ ffindex_unpack.o $(LDFLAGS) -L. -lffindex libffindex.so.0.1: ffindex.o ffutil.o $(CC) -Wl,-z,defs -shared -Wl,-soname,libffindex.so.0.1 -o libffindex.so.0.1 ffindex.o ffutil.o -lc libffindex.so: libffindex.so.0.1 ln -sf libffindex.so.0.1 libffindex.so libffindex.a: ffindex.o ffutil.o ar rcs libffindex.a ffutil.o ffindex.o test: rm -f /tmp/test.data /tmp/test.ffindex LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_build -s /tmp/test.data /tmp/test.ffindex ../test/data ../test/data2 LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_get /tmp/test.data /tmp/test.ffindex a b foo | tee /tmp/test.out LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_modify -u /tmp/test.ffindex b LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_get /tmp/test.data /tmp/test.ffindex a b foo | tee /tmp/test-unlink.out LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_build -a -s /tmp/test.data /tmp/test.ffindex ../test/testfile @echo ======== compare results ========= diff /tmp/test.out test.should diff /tmp/test-unlink.out test-unlink.should @echo ======== error handling ========== @echo LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_get /tmp/test.data /tmp/test.ffindex nonexistquery b foo || echo LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_get /nonexist/data /tmp/test.ffindex a foo || echo LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_get /tmp/test.data /nonexist/index a foo || echo time: LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" zsh -c "time ./ffindex_get ../test_big/nr20_11Jan10_a3m_db ../test_big/nr20_11Jan10_a3m_db.ffindex BABBAMIBA.a3m KARDUZEBA.a3m HARPAQIBA.a3m WOQQATEBA.a3m BEBRAQIBA.a3m GIRSAKEBA.a3m NAQTIFIBA.a3m BIWCIQABA.a3m > /dev/null" clean: rm -f ffindex.o ffutil.o ffindex_build ffindex_build.o ffindex_get ffindex_get.o libffindex.so libffindex.so.0.1 ffindex_modify ffindex_modify.o \ libffindex.a ffindex_from_fasta.o ffindex_from_fasta ffindex_apply_mpi.o ffindex_apply.o install: mkdir -p $(INSTALL_DIR)/bin mkdir -p $(INSTALL_DIR)/$(libdir) mkdir -p $(INSTALL_DIR)/include install ffindex_get $(INSTALL_DIR)/bin/ffindex_get install ffindex_build $(INSTALL_DIR)/bin/ffindex_build install ffindex_modify $(INSTALL_DIR)/bin/ffindex_modify install ffindex_from_fasta $(INSTALL_DIR)/bin/ffindex_from_fasta install ffindex_apply $(INSTALL_DIR)/bin/ffindex_apply install ffindex_unpack $(INSTALL_DIR)/bin/ffindex_unpack install libffindex.so.0.1 $(INSTALL_DIR)/$(libdir)/libffindex.so.0.1 install libffindex.a $(INSTALL_DIR)/$(libdir)/libffindex.a ln -sf libffindex.so.0.1 $(INSTALL_DIR)/$(libdir)/libffindex.so install ffindex.h $(INSTALL_DIR)/include/ffindex.h install ffutil.h $(INSTALL_DIR)/include/ffutil.h ifdef HAVE_MPI install ffindex_apply_mpi $(INSTALL_DIR)/bin/ffindex_apply_mpi endif deinstall: rm -f $(INSTALL_DIR)/bin/ffindex_get $(INSTALL_DIR)/bin/ffindex_build $(INSTALL_DIR)/bin/ffindex_modify \ $(INSTALL_DIR)/$(libdir)/libffindex.so.0.1 $(INSTALL_DIR)/$(libdir)/libffindex.a \ $(INSTALL_DIR)/$(libdir)/libffindex.so $(INSTALL_DIR)/include/ffindex.h $(INSTALL_DIR)/include/ffutil.h\ $(INSTALL_DIR)/bin/ffindex_from_fasta rmdir $(INSTALL_DIR)/bin || true rmdir $(INSTALL_DIR)/$(libdir) || true rmdir $(INSTALL_DIR)/include || true ffindex-0.9.9.3/src/Makefile.osx0000644000000000000000000000366012240136054016314 0ustar rootroot00000000000000INSTALL_DIR=.. LANG=C CFLAGS+=-std=c99 -O3 -g -fPIC -Wall -fnested-functions LDFLAGS= -L. -lffindex #LDFLAGS+=-Wl,-z,defs #-Wl,--version-script=libffindex.map all: ext/fmemopen.o libffindex.a ffindex_get ffindex_build ffindex_modify ffindex_from_fasta ext/fmemopen.o: ext/fmemopen.c cd ext && make ffindex.o: ffindex.c ffindex.h ffutil.h ffindex_get: libffindex.so libffindex.so.0.1 ffindex_get.o $(CC) $(CFLAGS) -L. -lffindex ffindex_get.o -o $@ ffindex_build: libffindex.so libffindex.so.0.1 ffindex_build.o $(CC) $(CFLAGS) -L. -lffindex ffindex_build.o -o $@ ffindex_modify: libffindex.so libffindex.so.0.1 ffindex_modify.o $(CC) $(CFLAGS) -L. -lffindex ffindex_modify.o -o $@ libffindex.so.0.1: ffindex.o ext/fmemopen.o ffutil.o $(CC) $(LDFLAGS) -shared -o libffindex.so.0.1 ext/fmemopen.o ffindex.o ffutil.o -lc libffindex.so: libffindex.so.0.1 ln -sf libffindex.so.0.1 libffindex.so libffindex.a: ffindex.o ffutil.o ar rcs libffindex.a ffutil.o ffindex.o ext/fmemopen.o test: LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_build -s test.data test.ffindex ../test/data ../test/data2 LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" ./ffindex_get test.data test.ffindex a b foo time: LD_LIBRARY_PATH=".:$(LD_LIBRARY_PATH)" zsh -c "time ./ffindex_get ../test_big/nr20_11Jan10_a3m_db ../test_big/nr20_11Jan10_a3m_db.ffindex BABBAMIBA.a3m KARDUZEBA.a3m HARPAQIBA.a3m WOQQATEBA.a3m BEBRAQIBA.a3m GIRSAKEBA.a3m NAQTIFIBA.a3m BIWCIQABA.a3m > /dev/null" clean: rm -f ffindex.o ffindex_build ffindex_build.o ffindex_get ffindex_get.o libffindex.so libffindex.so.0.1 install: mkdir -p $(INSTALL_DIR)/bin mkdir -p $(INSTALL_DIR)/lib mkdir -p $(INSTALL_DIR)/include install ffindex_get $(INSTALL_DIR)/bin/ffindex_get install ffindex_build $(INSTALL_DIR)/bin/ffindex_build install libffindex.so.0.1 $(INSTALL_DIR)/lib/libffindex.so.0.1 ln -sf libffindex.so.0.1 $(INSTALL_DIR)/lib/libffindex.so install ffindex.h $(INSTALL_DIR)/include/ffindex.h ffindex-0.9.9.3/src/ext/Makefile.osx0000644000000000000000000000023112240136054017103 0ustar rootroot00000000000000INSTALL_DIR=.. LANG=C CFLAGS+=-std=c99 -O3 -g -fPIC -Wall #LDFLAGS+=-Wl,-z,defs #-Wl,--version-script=libffindex.map fmemopen.o: fmemopen.c fmemopen.h ffindex-0.9.9.3/src/ext/fmemopen.c0000644000000000000000000000621312240136054016613 0ustar rootroot00000000000000/** * Copyright (c) 2009 Open Information Security Foundation * * \author Pablo Rincon Crespo * Based on FMem.c of Alexandre Flori (2008/10/17 AF) */ #define _GNU_SOURCE #include #include #include #include "fmemopen.h" #ifdef OS_DARWIN #define USE_FMEM_WRAPPER 1 #endif #ifdef OS_FREEBSD #define USE_FMEM_WRAPPER 1 #endif #ifdef USE_FMEM_WRAPPER /** * \brief Seek the mem file from offset and whence * \param handler pointer to the memfile * \param osffset number of bytes to move from whence * \param whence SEEK_SET, SEEK_CUR, SEEK_END * \retval pos the position by the last operation, -1 if sizes are out of bounds */ static fpos_t SeekFn(void *handler, fpos_t offset, int whence) { size_t pos = 0; fmem_t *mem = handler; switch (whence) { case SEEK_SET: if (offset > 0 && (size_t)offset <= mem->size) return mem->pos = offset; break; case SEEK_CUR: if (mem->pos + offset <= mem->size) return mem->pos = offset; break; case SEEK_END: /* must be negative */ if (mem->size + offset <= mem->size) return pos = mem->size + offset; break; } return -1; } /** * \brief Read from the buffer looking for the available memory limits * \param handler pointer to the memfile * \param buf buffer to read from the handler * \param number of bytes to read * \retval count , the number of bytes read */ static int ReadFn(void *handler, char *buf, int size) { size_t count = 0; fmem_t *mem = handler; size_t available = mem->size - mem->pos; if (size < 0) return - 1; if ((size_t)size > available) size = available; while (count < (size_t)size) buf[count++] = mem->buffer[mem->pos++]; return count; } /** * \brief Write into the buffer looking for the available memory limits * \param handler pointer to the memfile * \param buf buffer to write in the handler * \param number of bytes to write * \retval count , the number of bytes writen */ static int WriteFn(void *handler, const char *buf, int size) { size_t count = 0; fmem_t *mem = handler; size_t available = mem->size - mem->pos; if (size < 0) return - 1; if ((size_t)size > available) size = available; while (count < (size_t)size) mem->buffer[mem->pos++] = buf[count++]; return count; } /** * \brief close the mem file handler * \param handler pointer to the memfile * \retval 0 on succesful */ static int CloseFn(void *handler) { free (handler); return 0; } /** * \brief portable version of SCFmemopen for OS X / BSD built on top of funopen() * \param buffer that holds the file content * \param size of the file buffer * \param mode mode of the file to open * \retval pointer to the file; NULL if something is wrong */ FILE *fmemopen(void *buf, size_t size, const char *mode) { fmem_t *mem = (fmem_t *) malloc(sizeof(fmem_t)); memset(mem, 0, sizeof(fmem_t)); mem->size = size, mem->buffer = buf; return funopen(mem, ReadFn, WriteFn, SeekFn, CloseFn); } #endif ffindex-0.9.9.3/src/ext/fmemopen.h0000644000000000000000000000076112240136054016622 0ustar rootroot00000000000000#ifndef __FMEMOPEN_H__ #define __FMEMOPEN_H__ #include #include #include /* Include this file only for OSX / BSD compilations */ #ifdef OS_DARWIN #define USE_FMEM_WRAPPER 1 #endif #ifdef OS_FREEBSD #define USE_FMEM_WRAPPER 1 #endif #define USE_FMEM_WRAPPER 1 #ifdef USE_FMEM_WRAPPER struct fmem { size_t pos; size_t size; char *buffer; }; typedef struct fmem fmem_t; FILE *fmemopen(void *, size_t, const char *); #endif #endif /* __FMEMOPEN_H__ */ ffindex-0.9.9.3/src/ext/fmemopen.o0000644000000000000000000001452412240136054016633 0ustar rootroot00000000000000 808__text__TEXT0h__debug_frame__DWARF__debug_info__DWARF __debug_abbrev__DWARF' nW__debug_aranges__DWARF 0(__debug_macinfo__DWARF __debug_line__DWARF 0__debug_loc__DWARF `__debug_pubnames__DWARF+[__debug_pubtypes__DWARFJz__debug_inlined__DWARF __debug_str__DWARFM __debug_ranges__DWARF(X__eh_frame__TEXT(XH h P  UHtWt2tHHfH~H;wwH7fHDfDHGHH9rHHHH;GwH7UHLGHx=LH)LcL9r11E1u$HHGA0IHAHM9rDÉLcfDUHLGHx=LH)LcL9r11E1u$HHWA0 HAHIM9rDÉLcfDUHH]LeHIHHHHXL`LH HH5H$Ld$UH1x $q $Z $Z 4@]  $  GNU C 4.2.1 (Apple Inc. build 5664)fmemopen.c/Users/admin.hauser/ffindex/src/extsigned charunsigned charshort intshort unsigned intintunsigned int__int64_t.long long intlong long unsigned intlong intchar__darwin_size_tZ9long unsigned int__darwin_off_tnsize_tP"fpos_tWP__sbufb_basec#_sized#__sFILE_p#_r#_w# _flags#_file#_bf#_lbfsize#(_cookieN#0_close#8_read>#@_seek^#H_write#P_ub#X_extra#h_ur#p_ubuf#t_nbuf#w_lb#x_blksize#_offsett#  N 8 N 8  t^ N t D ~ N ~  d __sFILEX  FILEfmemposf#sizef#buffer8#fmem_tF tq NUoffset tLwhence Qpos!fmem">>NUbuf>8Tsize>Qcount?fmem@Af6U:({UNUbufU~TsizeUtcountVfmemWXf"ON__destNN__valN__lenNfv@bufvNbsizevfmodev~memw>jRjN= ]__CFConstantStringClassReferenceR%  $ > : ; I$ >   : ;  : ; I8  I ' I I &I < I!I/ .: ; ' I@ : ; I : ; I: ; I 4: ; I4: ; I4: ; I.: ; ' I  : ; I.? : ; ' I@ : ; I!4I? 4 < , /usr/include/i386/usr/include/sys/usr/include/usr/include/securefmemopen.c_types.h_types.hstdio.hfmemopen.h_string.h N yqt Xzu<. * Please add your name here if you distribute modified versions. * * Ffindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include #include #include #include "ext/fmemopen.h" /* For OS not yet implementing this new standard function */ #include "ffutil.h" #include "ffindex.h" /* XXX Use page size? */ #define FFINDEX_BUFFER_SIZE 4096 char* ffindex_copyright_text = FFINDEX_COPYRIGHT; char* ffindex_copyright() { return ffindex_copyright_text; } /* return *out_data_file, *out_index_file, out_offset. Setting to a given offset could be supported with a special mode. */ int ffindex_index_open(char *data_filename, char *index_filename, char* mode, FILE **out_data_file, FILE **out_index_file, size_t *out_offset) { /* open index and data file, seek to end if needed */ if(mode[0] == 'a') { *out_data_file = fopen(data_filename, "a"); if(*out_data_file == NULL) { perror(data_filename); return EXIT_FAILURE; } *out_index_file = fopen(index_filename, "a+"); if(*out_index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } struct stat sb; fstat(fileno(*out_index_file), &sb); fseek(*out_index_file, sb.st_size, SEEK_SET); fstat(fileno(*out_data_file), &sb); fseek(*out_data_file, sb.st_size, SEEK_SET); *out_offset = sb.st_size; } else { struct stat st; if(stat(data_filename, &st) == 0) { errno = EEXIST; perror(data_filename); return EXIT_FAILURE; } *out_data_file = fopen(data_filename, "w"); if(*out_data_file == NULL) { perror(data_filename); return EXIT_FAILURE; } if(stat(index_filename, &st) == 0) { errno = EEXIST; perror(index_filename); return EXIT_FAILURE; } *out_index_file = fopen(index_filename, "w+"); if(*out_index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } *out_offset = 0; } return EXIT_SUCCESS; } /* Insert a memory chunk (string even without \0) into ffindex */ int ffindex_insert_memory(FILE *data_file, FILE *index_file, size_t *offset, char *from_start, size_t from_length, char *name) { int myerrno = 0; size_t write_size; size_t offset_before = *offset; /* Write data */ write_size = fwrite(from_start, sizeof(char), from_length, data_file); *offset += write_size; if(from_length != write_size) { myerrno = errno; fferror_print(__FILE__, __LINE__, __func__, name); goto EXCEPTION_ffindex_insert_memory; } /* Seperate by '\0' and thus also make sure at least one byte is written */ char buffer[1] = {'\0'}; if(fwrite(buffer, sizeof(char), 1, data_file) != 1) { myerrno = errno; fferror_print(__FILE__, __LINE__, __func__, name); goto EXCEPTION_ffindex_insert_memory; } *offset += 1; /* Write index entry */ fprintf(index_file, "%s\t%zd\t%zd\n", name, offset_before, *offset - offset_before); EXCEPTION_ffindex_insert_memory: return myerrno; } /* Insert all file from a list into ffindex */ int ffindex_insert_list_file(FILE *data_file, FILE *index_file, size_t *start_offset, FILE *list_file) { size_t offset = *start_offset; char path[PATH_MAX]; while(fgets(path, PATH_MAX, list_file) != NULL) ffindex_insert_file(data_file, index_file, &offset, ffnchomp(path, strlen(path)), basename(path)); /* update return value */ *start_offset = offset; return 0; } /* Insert all files from directory into ffindex */ int ffindex_insert_dir(FILE *data_file, FILE *index_file, size_t *start_offset, char *input_dir_name) { DIR *dir = opendir(input_dir_name); if(dir == NULL) { fferror_print(__FILE__, __LINE__, __func__, input_dir_name); return -1; } size_t input_dir_name_len = strlen(input_dir_name); char path[PATH_MAX]; strncpy(path, input_dir_name, NAME_MAX); if(input_dir_name[input_dir_name_len - 1] != '/') { path[input_dir_name_len] = '/'; input_dir_name_len += 1; } size_t offset = *start_offset; struct dirent *entry; while((entry = readdir(dir)) != NULL) { if(entry->d_name[0] == '.') continue; strncpy(path + input_dir_name_len, entry->d_name, NAME_MAX); struct stat sb; if(stat(path, &sb) == -1) fferror_print(__FILE__, __LINE__, __func__, path); if(!S_ISREG(sb.st_mode)) continue; ffindex_insert_file(data_file, index_file, &offset, path, entry->d_name); } closedir(dir); /* update return value */ *start_offset = offset; return 0; } /* Insert one file by path into ffindex */ int ffindex_insert_file(FILE *data_file, FILE *index_file, size_t *offset, const char *path, char *name) { FILE *file = fopen(path, "r"); if(file == NULL) return errno; int ret = ffindex_insert_filestream(data_file, index_file, offset, file, name); fclose(file); return ret; } /* Insert one file by handle into ffindex */ int ffindex_insert_filestream(FILE *data_file, FILE *index_file, size_t *offset, FILE* file, char *name) { int myerrno = 0; /* copy and paste file to data file */ char buffer[FFINDEX_BUFFER_SIZE]; size_t offset_before = *offset; size_t read_size; while((read_size = fread(buffer, sizeof(char), sizeof(buffer), file)) > 0) { size_t write_size = fwrite(buffer, sizeof(char), read_size, data_file); *offset += write_size; if(read_size != write_size) fferror_print(__FILE__, __LINE__, __func__, name); } if(ferror(file)) warn("fread"); /* Seperate by '\0' and thus also make sure at least one byte is written */ buffer[0] = '\0'; if(fwrite(buffer, sizeof(char), 1, data_file) != 1) perror("ffindex_insert_filestream"); *offset += 1; if(ferror(data_file) != 0) goto EXCEPTION_ffindex_insert_file; /* write index entry */ fprintf(index_file, "%s\t%zd\t%zd\n", name, offset_before, *offset - offset_before); if(ferror(file) != 0) goto EXCEPTION_ffindex_insert_file; return myerrno; EXCEPTION_ffindex_insert_file: { fferror_print(__FILE__, __LINE__, __func__, ""); return myerrno; } } int ffindex_insert_ffindex(FILE* data_file, FILE* index_file, size_t* offset, char* data_to_add, ffindex_index_t* index_to_add) { int err = EXIT_SUCCESS; for(size_t entry_i = 0; entry_i < index_to_add->n_entries; entry_i++) { ffindex_entry_t *entry = ffindex_get_entry_by_index(index_to_add, entry_i); if(entry == NULL) { fferror_print(__FILE__, __LINE__, __func__, ""); return EXIT_FAILURE; } err = ffindex_insert_memory(data_file, index_file, offset, ffindex_get_data_by_entry(data_to_add, entry), entry->length - 1, entry->name); // skip \0 suffix if(err != EXIT_SUCCESS) { fferror_print(__FILE__, __LINE__, __func__, ""); return EXIT_FAILURE;} } return EXIT_SUCCESS; } /* XXX not implemented yet, the functionality is provided by ffindex_unpack.c though */ int ffindex_restore(FILE *data_file, FILE *index_file, char *output_dir_name) { return -1; } char* ffindex_mmap_data(FILE *file, size_t* size) { struct stat sb; fstat(fileno(file), &sb); *size = sb.st_size; int fd = fileno(file); if(fd < 0) { fferror_print(__FILE__, __LINE__, __func__, "mmap failed"); return MAP_FAILED; } return (char*)mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); } static int ffindex_compare_entries_by_name(const void *pentry1, const void *pentry2) { ffindex_entry_t* entry1 = (ffindex_entry_t*)pentry1; ffindex_entry_t* entry2 = (ffindex_entry_t*)pentry2; return strncmp(entry1->name, entry2->name, FFINDEX_MAX_ENTRY_NAME_LENTH); } ffindex_entry_t* ffindex_get_entry_by_name(ffindex_index_t *index, char *name) { return ffindex_bsearch_get_entry(index, name); } ffindex_entry_t* ffindex_bsearch_get_entry(ffindex_index_t *index, char *name) { ffindex_entry_t search; strncpy(search.name, name, FFINDEX_MAX_ENTRY_NAME_LENTH); return (ffindex_entry_t*)bsearch(&search, index->entries, index->n_entries, sizeof(ffindex_entry_t), ffindex_compare_entries_by_name); } ffindex_index_t* ffindex_index_parse(FILE *index_file, size_t num_max_entries) { if(num_max_entries == 0) num_max_entries = FFINDEX_MAX_INDEX_ENTRIES_DEFAULT; size_t nbytes = sizeof(ffindex_index_t) + (sizeof(ffindex_entry_t) * num_max_entries); ffindex_index_t *index = (ffindex_index_t *)malloc(nbytes); if(index == NULL) { fprintf(stderr, "Failed to allocate %ld bytes\n", nbytes); fferror_print(__FILE__, __LINE__, __func__, "malloc failed"); return NULL; } index->num_max_entries = num_max_entries; index->file = index_file; index->index_data = ffindex_mmap_data(index_file, &(index->index_data_size)); if(index->index_data_size == 0) warn("Problem with data file. Is it empty or is another process readning it?"); if(index->index_data == MAP_FAILED) return NULL; index->type = SORTED_ARRAY; /* XXX Assume a sorted file for now */ /* Faster than scanf per line */ size_t names_too_long = 0; char* d = index->index_data; int i; for(i = 0; d < (index->index_data + index->index_data_size); i++) { char* end; int p; for(p = 0; *d != '\t'; d++) if(p < FFINDEX_MAX_ENTRY_NAME_LENTH) index->entries[i].name[p++] = *d; else names_too_long++; index->entries[i].name[p] = '\0'; index->entries[i].offset = strtol(d, &end, 10); d = end; index->entries[i].length = strtol(d, &end, 10); d = end + 1; /* +1 for newline */ } index->n_entries = i; if(index->n_entries == 0) warnx("index with 0 entries"); if(names_too_long) { errno = ENAMETOOLONG; warn("Cut-off is %d characters. Are they still unique?" " Warning encountered %zd times", FFINDEX_MAX_ENTRY_NAME_LENTH, names_too_long); errno = 0; } return index; } ffindex_entry_t* ffindex_get_entry_by_index(ffindex_index_t *index, size_t entry_index) { if(entry_index < index->n_entries) return &index->entries[entry_index]; else return NULL; } /* Using a function for this looks like overhead. But a more advanced data format, * say a compressed one, can do it's magic here. */ char* ffindex_get_data_by_offset(char* data, size_t offset) { return data + offset; } char* ffindex_get_data_by_entry(char *data, ffindex_entry_t* entry) { return ffindex_get_data_by_offset(data, entry->offset); } char* ffindex_get_data_by_name(char *data, ffindex_index_t *index, char *name) { ffindex_entry_t* entry = ffindex_bsearch_get_entry(index, name); if(entry == NULL) return NULL; return ffindex_get_data_by_entry(data, entry); } char* ffindex_get_data_by_index(char *data, ffindex_index_t *index, size_t entry_index) { ffindex_entry_t* entry = ffindex_get_entry_by_index(index, entry_index); if(entry == NULL) return NULL; return ffindex_get_data_by_entry(data, entry); } FILE* ffindex_fopen_by_entry(char *data, ffindex_entry_t* entry) { char *filedata = ffindex_get_data_by_offset(data, entry->offset); return fmemopen(filedata, entry->length, "r"); } FILE* ffindex_fopen_by_name(char *data, ffindex_index_t *index, char *filename) { ffindex_entry_t* entry = ffindex_bsearch_get_entry(index, filename); if(entry == NULL) return NULL; return ffindex_fopen_by_entry(data, entry); } void ffindex_sort_index_file(ffindex_index_t *index) { qsort(index->entries, index->n_entries, sizeof(ffindex_entry_t), ffindex_compare_entries_by_name); } int ffindex_write(ffindex_index_t* index, FILE* index_file) { /* Use tree if available */ if(index->type == TREE) return ffindex_tree_write(index, index_file); for(size_t i = 0; i < index->n_entries; i++) { ffindex_entry_t ffindex_entry = index->entries[i]; if(fprintf(index_file, "%s\t%zd\t%zd\n", ffindex_entry.name, ffindex_entry.offset, ffindex_entry.length) < 0) return EXIT_FAILURE; } return EXIT_SUCCESS; } ffindex_index_t* ffindex_unlink_entries(ffindex_index_t* index, char** sorted_names_to_unlink, int n_names) { int i = index->n_entries - 1; /* walk list of names to delete */ for(int n = n_names - 1; n >= 0; n--) { char* name_to_unlink = sorted_names_to_unlink[n]; /* walk index entries */ for(; i >= 0; i--) { int cmp = strncmp(name_to_unlink, index->entries[i].name, FFINDEX_MAX_ENTRY_NAME_LENTH); if(cmp == 0) /* found entry */ { /* Move entries after the unlinked ones to close the gap */ size_t n_entries_to_move = index->n_entries - i - 1; if(n_entries_to_move > 0) /* not last element of array */ memmove(index->entries + i, index->entries + i + 1, n_entries_to_move * sizeof(ffindex_entry_t)); index->n_entries--; break; } else if(cmp > 0) /* not found */ break; } } return index; } ffindex_index_t* ffindex_unlink(ffindex_index_t* index, char* name_to_unlink) { /* Use tree if available */ if(index->type == TREE) return ffindex_tree_unlink(index, name_to_unlink); ffindex_entry_t* entry = ffindex_bsearch_get_entry(index, name_to_unlink); if(entry == NULL) { fprintf(stderr, "Warning: could not find '%s'\n", name_to_unlink); return index; } /* Move entries after the unlinked one to close the gap */ size_t n_entries_to_move = index->entries + index->n_entries - entry - 1; if(n_entries_to_move > 0) /* not last element of array */ memmove(entry, entry + 1, n_entries_to_move * sizeof(ffindex_entry_t)); index->n_entries--; return index; } /* tree version */ ffindex_entry_t *ffindex_tree_get_entry(ffindex_index_t* index, char* name) { ffindex_entry_t search; strncpy(search.name, name, FFINDEX_MAX_ENTRY_NAME_LENTH); return (ffindex_entry_t *)tfind((const void *)&search, &index->tree_root, ffindex_compare_entries_by_name); } ffindex_index_t* ffindex_tree_unlink(ffindex_index_t* index, char* name_to_unlink) { if(index->tree_root == NULL) { fferror_print(__FILE__, __LINE__, __func__, "tree is NULL"); return NULL; } ffindex_entry_t search; strncpy(search.name, name_to_unlink, FFINDEX_MAX_ENTRY_NAME_LENTH); tdelete((const void *)&search, &index->tree_root, ffindex_compare_entries_by_name); return index; } ffindex_index_t* ffindex_index_as_tree(ffindex_index_t* index) { index->tree_root = NULL; for(size_t i = 0; i < index->n_entries; i++) { ffindex_entry_t *entry = &index->entries[i]; tsearch((const void *)entry, &index->tree_root, ffindex_compare_entries_by_name); //entry = *(ffindex_entry_t **)tsearch((const void *)entry, &index->tree_root, ffindex_compare_entries_by_name); //printf("entry find: %s\n", entry->name); } index->type = TREE; return index; } int ffindex_tree_write(ffindex_index_t* index, FILE* index_file) { int ret = EXIT_SUCCESS; void action(const void *node, const VISIT which, const int depth) { ffindex_entry_t *entry; switch (which) { case preorder: break; case endorder: break; case postorder: case leaf: entry = *(ffindex_entry_t **) node; if(fprintf(index_file, "%s\t%zd\t%zd\n", entry->name, entry->offset, entry->length) < 0) ret = EXIT_FAILURE; break; } } twalk(index->tree_root, action); return ret; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffindex.h0000644000000000000000000000714212240136054015637 0ustar rootroot00000000000000/* * Ffindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * Ffindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ * * Ffindex is a very simple database for small files. The files are stored * concatenated in one big data file, seperated by '\0'. A second file * contains a plain text index, giving name, offset and length of of the small * files. */ #ifndef _FFINDEX_H #define _FFINDEX_H 1 #include #define FFINDEX_VERSION 0.993 #define FFINDEX_COPYRIGHT "\nDesigned and implemented by Andreas Hauser .\n" #define FFINDEX_MAX_INDEX_ENTRIES_DEFAULT 80000000 #define FFINDEX_MAX_ENTRY_NAME_LENTH 63 enum ffindex_type { PLAIN_FILE, SORTED_FILE, SORTED_ARRAY, TREE }; typedef struct ffindex_entry { size_t offset; size_t length; char name[FFINDEX_MAX_ENTRY_NAME_LENTH + 1]; } ffindex_entry_t; typedef struct ffindex_index { enum ffindex_type type; char* filename; FILE* file; char* index_data; size_t index_data_size; void* tree_root; size_t num_max_entries; size_t n_entries; ffindex_entry_t entries[]; /* This array is as big as the excess memory allocated for this struct. */ } ffindex_index_t; /* return *out_data_file, *out_index_file, out_offset. */ int ffindex_index_open(char *data_filename, char *index_filename, char* mode, FILE **out_data_file, FILE **out_index_file, size_t *out_offset); int ffindex_insert_memory(FILE *data_file, FILE *index_file, size_t *offset, char *from_start, size_t from_length, char *name); int ffindex_insert_file(FILE *data_file, FILE *index_file, size_t *offset, const char *path, char *name); int ffindex_insert_filestream(FILE *data_file, FILE *index_file, size_t *offset, FILE* file, char *name); int ffindex_insert_list_file(FILE *data_file, FILE *index_file, size_t *start_offset, FILE *list_file); int ffindex_insert_dir(FILE *data_file, FILE *index_file, size_t *offset, char *input_dir_name); int ffindex_insert_ffindex(FILE* data_file, FILE* index_file, size_t* offset, char* data_to_add, ffindex_index_t* index_to_add); FILE* ffindex_fopen_by_entry(char *data, ffindex_entry_t* entry); FILE* ffindex_fopen_by_name(char *data, ffindex_index_t *index, char *name); char* ffindex_mmap_data(FILE *file, size_t* size); char* ffindex_get_data_by_offset(char* data, size_t offset); char* ffindex_get_data_by_entry(char *data, ffindex_entry_t* entry); char* ffindex_get_data_by_name(char *data, ffindex_index_t *index, char *name); char* ffindex_get_data_by_index(char *data, ffindex_index_t *index, size_t entry_index); ffindex_entry_t* ffindex_get_entry_by_index(ffindex_index_t *index, size_t entry_index); ffindex_entry_t* ffindex_get_entry_by_name(ffindex_index_t *index, char *name); ffindex_index_t* ffindex_index_parse(FILE *index_file, size_t num_max_entries); ffindex_entry_t* ffindex_bsearch_get_entry(ffindex_index_t *index, char *name); void ffindex_sort_index_file(ffindex_index_t *index); int ffindex_write(ffindex_index_t* index, FILE* index_file); ffindex_index_t* ffindex_unlink(ffindex_index_t* index, char *entry_name); ffindex_index_t* ffindex_index_as_tree(ffindex_index_t* index); ffindex_index_t* ffindex_tree_unlink(ffindex_index_t* index, char* name_to_unlink); ffindex_index_t* ffindex_unlink_entries(ffindex_index_t* index, char** sorted_names_to_unlink, int n_names); int ffindex_tree_write(ffindex_index_t* index, FILE* index_file); char* ffindex_copyright(); #endif ffindex-0.9.9.3/src/ffindex_apply.c0000644000000000000000000000665012240136054017042 0ustar rootroot00000000000000/* * FFindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * FFindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ * * ffindex_apply * apply a program to each FFindex entry */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include "ffindex.h" #include "ffutil.h" int main(int argn, char **argv) { if(argn < 4) { fprintf(stderr, "USAGE: %s DATA_FILENAME INDEX_FILENAME PROGRAM [PROGRAM_ARGS]*\n" FFINDEX_COPYRIGHT, argv[0]); return -1; } char *data_filename = argv[1]; char *index_filename = argv[2]; char *program_name = argv[3]; char **program_argv = argv + 3; FILE *data_file = fopen(data_filename, "r"); FILE *index_file = fopen(index_filename, "r"); if( data_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename); exit(EXIT_FAILURE); } if(index_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename); exit(EXIT_FAILURE); } size_t data_size; char *data = ffindex_mmap_data(data_file, &data_size); ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_index_parse", index_filename); exit(EXIT_FAILURE); } // Ignore SIGPIPE struct sigaction handler; handler.sa_handler = SIG_IGN; sigemptyset(&handler.sa_mask); handler.sa_flags = 0; sigaction(SIGPIPE, &handler, NULL); size_t range_start = 0; size_t range_end = index->n_entries; // Foreach entry //#pragma omp parallel for for(size_t entry_index = range_start; entry_index < range_end; entry_index++) { //fprintf(stderr, "index %ld\n", entry_index); int ret = 0; ffindex_entry_t* entry = ffindex_get_entry_by_index(index, entry_index); if(entry == NULL) { perror(entry->name); continue; } int pipefd[2]; ret = pipe(pipefd); if(ret != 0) { perror(entry->name); continue; } pid_t child_pid = fork(); if(child_pid == 0) { fclose(data_file); fclose(index_file); close(pipefd[1]); // Make pipe from parent our new stdin int newfd = dup2(pipefd[0], fileno(stdin)); if(newfd < 0) { fprintf(stdout, "%d %d\n", pipefd[0], newfd); perror(entry->name); } close(pipefd[0]); // exec program with the pipe as stdin execvp(program_name, program_argv); // never reached } else if(child_pid > 0) { // Read end is for child only close(pipefd[0]); // Write file data to child's stdin. char *filedata = ffindex_get_data_by_entry(data, entry); ssize_t written = 0; while(written < entry->length) { int w = write(pipefd[1], filedata + written, entry->length - written); if(w < 0 && errno != EPIPE) { perror(entry->name); break; } else if(w == 0 && errno != 0) { perror(entry->name); break; } else written += w; } close(pipefd[1]); // child gets EOF waitpid(child_pid, NULL, 0); } else { perror(entry->name); exit(errno); } } return 0; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffindex_apply_mpi.c0000644000000000000000000002531412240136054017705 0ustar rootroot00000000000000/* * FFindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * FFindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ * * ffindex_apply * apply a program to each FFindex entry */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include "ffindex.h" #include "ffutil.h" char* read_buffer; int ffindex_apply_by_entry(char *data, ffindex_index_t* index, ffindex_entry_t* entry, char* program_name, char** program_argv, FILE* data_file_out, FILE* index_file_out, size_t *offset, FILE* log_file) { int ret = 0; int capture_stdout = (data_file_out != NULL); int pipefd_stdin[2]; int pipefd_stdout[2]; ret = pipe(pipefd_stdin); if(ret != 0) { fprintf(stderr, "ERROR in pipe stdin!\n"); perror(entry->name); return errno; } if(capture_stdout) { ret = pipe(pipefd_stdout); if(ret != 0) { fprintf(stderr, "ERROR in pipe stdout!\n"); perror(entry->name); return errno; } } // Flush so child doesn't copy and also flushes, leading to duplicate output fflush(data_file_out); fflush(index_file_out); pid_t child_pid = fork(); if(child_pid == 0) { close(pipefd_stdin[1]); if(capture_stdout) { fclose(data_file_out); fclose(index_file_out); close(pipefd_stdout[0]); } // Make pipe from parent our new stdin int newfd_in = dup2(pipefd_stdin[0], fileno(stdin)); if(newfd_in < 0) { fprintf(stderr, "ERROR in dup2 in %d %d\n", pipefd_stdin[0], newfd_in); perror(entry->name); } close(pipefd_stdin[0]); if(capture_stdout) { int newfd_out = dup2(pipefd_stdout[1], fileno(stdout)); if(newfd_out < 0) { fprintf(stderr, "ERROR in dup2 out %d %d\n", pipefd_stdout[1], newfd_out); perror(entry->name); } close(pipefd_stdout[1]); } setenv("FFINDEX_ENTRY_NAME", entry->name, 1); // exec program with the pipe as stdin execvp(program_name, program_argv); // never reached } else if(child_pid > 0) { // parent writes to and possible reads from child int flags = 0; // Read end is for child only close(pipefd_stdin[0]); if(capture_stdout) close(pipefd_stdout[1]); char *filedata = ffindex_get_data_by_entry(data, entry); if(capture_stdout) { flags = fcntl(pipefd_stdout[0], F_GETFL, 0); fcntl(pipefd_stdout[0], F_SETFL, flags | O_NONBLOCK); } // Write file data to child's stdin. ssize_t written = 0; size_t to_write = entry->length - 1; // Don't write ffindex trailing '\0' char* b = read_buffer; while(written < to_write) { size_t rest = to_write - written; int batch_size = PIPE_BUF; if(rest < PIPE_BUF) batch_size = rest; ssize_t w = write(pipefd_stdin[1], filedata + written, batch_size); if(w < 0 && errno != EPIPE) { fprintf(stderr, "ERROR in child!\n"); perror(entry->name); break; } else written += w; if(capture_stdout) { // To avoid blocking try to read some data ssize_t r = read(pipefd_stdout[0], b, PIPE_BUF); if(r > 0) b += r; } } close(pipefd_stdin[1]); // child gets EOF if(capture_stdout) { // Read rest fcntl(pipefd_stdout[0], F_SETFL, flags); // Remove O_NONBLOCK ssize_t r; while((r = read(pipefd_stdout[0], b, PIPE_BUF)) > 0) b += r; close(pipefd_stdout[0]); ffindex_insert_memory(data_file_out, index_file_out, offset, read_buffer, b - read_buffer, entry->name); } int status; waitpid(child_pid, &status, 0); if(log_file != NULL) fprintf(log_file, "%s\t%ld\t%ld\t%d\n", entry->name, entry->offset, entry->length, WEXITSTATUS(status)); } else { fprintf(stderr, "ERROR in fork()\n"); perror(entry->name); return errno; } return EXIT_SUCCESS; } int main(int argn, char **argv) { int mpi_error, mpi_rank, mpi_num_procs; mpi_error = MPI_Init(&argn, &argv); if(mpi_error) goto EXCEPTION; mpi_error = MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); if(mpi_error) goto EXCEPTION; mpi_error = MPI_Comm_size(MPI_COMM_WORLD, &mpi_num_procs); if(mpi_error) goto EXCEPTION; int opt, merge = 1; char *data_filename_out = NULL, *index_filename_out = NULL, *log_filename = NULL; while ((opt = getopt(argn, argv, "d:i:mql:")) != -1) { switch (opt) { case 'l': log_filename = optarg; break; case 'm': merge = 0; break; case 'd': data_filename_out = optarg; break; case 'i': index_filename_out = optarg; break; } } if(argn - optind < 3) { fprintf(stderr, "ERROR Not enough arguments.\n\n"); fprintf(stderr, "USAGE: %s [-m] [-q] [-l LOG_FILE] -d DATA_FILENAME_OUT -i INDEX_FILENAME_OUT DATA_FILENAME INDEX_FILENAME -- PROGRAM [PROGRAM_ARGS]*\n" "\t-l\tLOG_FILE like an FFindex file but with the return value of apply added.\n" "\t\tThis file can be processed and used as FFindex file.\n" "\t\tE.g. grep \"\t1$\" foo.log | cut -f 1-3 > foo-special.ffindex.\n" "\t\twhere maybe the ffindex_apply_mpi ran with -- perl -e 'return 1 if(/^SPECIAL/)'\n" "\t-m\tDo not merge the FFindex parts generated by the different MPI processes\n" "\t\tThis is useful for large MPI Jobs where merge time might be accounted.\n" "\t-d\tFFDATA output file, stdout of program will be captured\n" "\t-i\tFFINDEX output file, entries get the same name as input entry\n" FFINDEX_COPYRIGHT, argv[0]); errno = EINVAL; goto EXCEPTION; } read_buffer = malloc(400 * 1024 * 1024); char *data_filename = argv[optind++]; char *index_filename = argv[optind++]; char *program_name = argv[optind]; char **program_argv = argv + optind; FILE *data_file = fopen(data_filename, "r"); FILE *index_file = fopen(index_filename, "r"); if( data_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename); exit(EXIT_FAILURE); } if(index_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename); exit(EXIT_FAILURE); } // Setup one output FFindex for each MPI process FILE *data_file_out = NULL, *index_file_out = NULL; if(data_filename_out != NULL && index_filename_out != NULL) { char* data_filename_out_rank = malloc(FILENAME_MAX); char* index_filename_out_rank = malloc(FILENAME_MAX); snprintf( data_filename_out_rank, FILENAME_MAX, "%s.%d", data_filename_out, mpi_rank); snprintf(index_filename_out_rank, FILENAME_MAX, "%s.%d", index_filename_out, mpi_rank); data_file_out = fopen(data_filename_out_rank, "w+"); index_file_out = fopen(index_filename_out_rank, "w+"); if( data_file_out == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename_out); exit(EXIT_FAILURE); } if(index_file_out == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename_out); exit(EXIT_FAILURE); } } FILE *log_file = NULL; // Setup log file if(log_filename) { char* log_filename_rank = malloc(FILENAME_MAX); snprintf(log_filename_rank, FILENAME_MAX, "%s.%d", index_filename_out, mpi_rank); log_file = fopen(log_filename_rank, "w+"); if( log_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], log_filename_rank); exit(EXIT_FAILURE); } } int capture_stdout = (data_file_out != NULL); size_t data_size; char *data = ffindex_mmap_data(data_file, &data_size); ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_index_parse", index_filename); MPI_Finalize(); exit(EXIT_FAILURE); } // Ignore SIGPIPE struct sigaction handler; handler.sa_handler = SIG_IGN; sigemptyset(&handler.sa_mask); handler.sa_flags = 0; sigaction(SIGPIPE, &handler, NULL); size_t batch_size, range_start, range_end; if(index->n_entries >= mpi_num_procs) batch_size = index->n_entries / mpi_num_procs; else batch_size = 0; range_start = mpi_rank * batch_size; range_end = range_start + batch_size; size_t offset = 0; // Foreach entry if(batch_size > 0) for(size_t entry_index = range_start; entry_index < range_end; entry_index++) { ffindex_entry_t* entry = ffindex_get_entry_by_index(index, entry_index); if(entry == NULL) { perror(entry->name); goto EXCEPTION; } int error = ffindex_apply_by_entry(data, index, entry, program_name, program_argv, data_file_out, index_file_out, &offset, log_file); if(error != 0) { perror(entry->name); break; } } ssize_t left_over = index->n_entries - (batch_size * mpi_num_procs); if(mpi_rank < left_over) { size_t left_over_entry_index = (batch_size * mpi_num_procs) + mpi_rank; ffindex_entry_t* entry = ffindex_get_entry_by_index(index, left_over_entry_index); if(entry == NULL) { perror(entry->name); return errno; } //fprintf(stderr, "handling left over: %ld\n", left_over_entry_index); int error = ffindex_apply_by_entry(data, index, entry, program_name, program_argv, data_file_out, index_file_out, &offset, log_file); if(error != 0) perror(entry->name); } if(capture_stdout) fclose(data_file_out); if(index_file_out != NULL) fclose(index_file_out); MPI_Barrier(MPI_COMM_WORLD); // merge FFindexes in master if(mpi_rank == 0 && merge && data_filename_out != NULL) { char* merge_command = malloc(FILENAME_MAX * 5); for(int i = 0; i < mpi_num_procs; i++) { snprintf( merge_command, FILENAME_MAX, "ffindex_build -as %s %s -d %s.%d -i %s.%d", data_filename_out, index_filename_out, data_filename_out, i, index_filename_out, i); //puts(merge_command); if(system(merge_command) == 0) { snprintf(merge_command, FILENAME_MAX, "%s.%d", data_filename_out, i); unlink(merge_command); snprintf(merge_command, FILENAME_MAX, "%s.%d", index_filename_out, i); unlink(merge_command); } else { fprintf(stderr, "merge command failed: %s\n", merge_command); goto EXCEPTION; } } } MPI_Finalize(); return EXIT_SUCCESS; EXCEPTION: perror(argv[0]); fprintf(stderr, "\nEXCEPTION ffindex_apply_mpi in rank: %d\n", mpi_rank); MPI_Finalize(); return EXIT_FAILURE; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffindex_build.c0000644000000000000000000001514712240136054017015 0ustar rootroot00000000000000/* * FFindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * Ffindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include "ffindex.h" #include "ffutil.h" #define MAX_FILENAME_LIST_FILES 4096 void usage(char *program_name) { fprintf(stderr, "USAGE: %s [-a|-v] [-s] [-f file]* OUT_DATA_FILE OUT_INDEX_FILE [-d 2ND_DATA_FILE -i 2ND_INDEX_FILE] [DIR_TO_INDEX|FILE]*\n" "\t-a\t\tappend files/indexes, also needed for sorting an already existing ffindex\n" "\t-d FFDATA_FILE\ta second ffindex data file for inserting/appending\n" "\t-i FFINDEX_FILE\ta second ffindex index file for inserting/appending\n" "\t-f FILE\t\tfile containing a list of file names, one per line\n" "\t\t\t-f can be specified up to %d times\n" "\t-s\t\tsort index file, so that the index can queried.\n" "\t\t\tAnother append operations can be done without sorting.\n" "\t-v\t\tprint version and other info then exit\n" "\nEXAMPLES:\n" "\tCreate a new ffindex containing all files from the \"bar/\" directory containing\n" "\tsay myfile1.txt, myfile2.txt and sort (-s) it so that e.g. ffindex_get can use it.\n" "\t\t$ ffindex_build -s foo.ffdata foo.ffindex bar/\n" "\n\tAdd (-a) more files: myfile3.txt, myfile4.txt.\n" "\t\t$ ffindex_build -a foo.ffdata foo.ffindex myfile3.txt myfile4.txt\n" "\n\tOops, forgot to sort it (-s) so do it afterwards:\n" "\t\t$ ffindex_build -as foo.ffdata foo.ffindex\n" "\nNOTE:\n" "\tMaximum key/filename length is %d and maximum entries are by default %d\n" "\tThis can be changed in the sources.\n" FFINDEX_COPYRIGHT, program_name, MAX_FILENAME_LIST_FILES, FFINDEX_MAX_ENTRY_NAME_LENTH, FFINDEX_MAX_INDEX_ENTRIES_DEFAULT); } int main(int argn, char **argv) { int append = 0, sort = 0, unlink = 0, version = 0; int opt, err = EXIT_SUCCESS; char* list_filenames[MAX_FILENAME_LIST_FILES]; char* list_ffindex_data[MAX_FILENAME_LIST_FILES]; char* list_ffindex_index[MAX_FILENAME_LIST_FILES]; size_t list_ffindex_data_index = 0; size_t list_ffindex_index_index = 0; size_t list_filenames_index = 0; while ((opt = getopt(argn, argv, "asuvd:f:i:")) != -1) { switch (opt) { case 'a': append = 1; break; case 'd': list_ffindex_data[list_ffindex_data_index++] = optarg; break; case 'i': list_ffindex_index[list_ffindex_index_index++] = optarg; break; case 'f': list_filenames[list_filenames_index++] = optarg; break; case 's': sort = 1; break; case 'v': version = 1; break; default: usage(argv[0]); return EXIT_FAILURE; } } if(version == 1) { /* Don't you dare running it on a platform where byte != 8 bits */ printf("%s version: %.3f (off_t = %zd bits)\n", argv[0], FFINDEX_VERSION, sizeof(off_t) * 8); return EXIT_SUCCESS; } if(argn - optind < 2) { usage(argv[0]); return EXIT_FAILURE; } if(append && unlink) { fprintf(stderr, "ERROR: append (-a) and unlink (-u) are mutually exclusive\n"); return EXIT_FAILURE; } if(list_ffindex_data_index != list_ffindex_index_index) { fprintf(stderr, "ERROR: -d and -i must be specified pairwise\n"); return EXIT_FAILURE; } char *data_filename = argv[optind++]; char *index_filename = argv[optind++]; FILE *data_file = NULL, *index_file = NULL; size_t offset = 0; /* open index and data file, seek to end if needed */ char *mode = "w"; if(append) mode = "a"; err = ffindex_index_open(data_filename, index_filename, mode, &data_file, &index_file, &offset); if(err != EXIT_SUCCESS) return EXIT_FAILURE; /* For each list_file insert */ if(list_filenames_index > 0) for(int i = 0; i < list_filenames_index; i++) { FILE *list_file = fopen(list_filenames[i], "r"); if( list_file == NULL) { perror(list_filenames[i]); return EXIT_FAILURE; } if(ffindex_insert_list_file(data_file, index_file, &offset, list_file) < 0) { perror(list_filenames[i]); err = -1; } } /* Append other ffindexes */ if(list_ffindex_data_index > 0) { for(int i = 0; i < list_ffindex_data_index; i++) { FILE* data_file_to_add = fopen(list_ffindex_data[i], "r"); if( data_file_to_add == NULL) { perror(list_ffindex_data[i]); return EXIT_FAILURE; } FILE* index_file_to_add = fopen(list_ffindex_index[i], "r"); if( index_file_to_add == NULL) { perror(list_ffindex_index[i]); return EXIT_FAILURE; } size_t data_size; char *data_to_add = ffindex_mmap_data(data_file_to_add, &data_size); ffindex_index_t* index_to_add = ffindex_index_parse(index_file_to_add, 0); ffindex_insert_ffindex(data_file, index_file, &offset, data_to_add, index_to_add); } } /* Insert files and directories into the index */ for(int i = optind; i < argn; i++) { char *path = argv[i]; struct stat sb; if(stat(path, &sb) == -1) { fferror_print(__FILE__, __LINE__, __func__, path); continue; } if(S_ISDIR(sb.st_mode)) { err = ffindex_insert_dir(data_file, index_file, &offset, path); if(err < 0)fferror_print(__FILE__, __LINE__, __func__, path); } else if(S_ISREG(sb.st_mode)) { ffindex_insert_file(data_file, index_file, &offset, path, path); } } fclose(data_file); /* Sort the index entries and write back */ if(sort) { rewind(index_file); ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { fferror_print(__FILE__, __LINE__, __func__, index_filename); exit(EXIT_FAILURE); } fclose(index_file); ffindex_sort_index_file(index); index_file = fopen(index_filename, "w"); if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } err += ffindex_write(index, index_file); } return err; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffindex_from_fasta.c0000644000000000000000000000620112240136054020026 0ustar rootroot00000000000000/* * FFindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * FFindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include "ffindex.h" #include "ffutil.h" #define MAX_FILENAME_LIST_FILES 4096 void usage(char *program_name) { fprintf(stderr, "USAGE: %s -v | [-s] data_filename index_filename fasta_filename\n" "\t-s\tsort index file\n" FFINDEX_COPYRIGHT, program_name); } int main(int argn, char **argv) { int sort = 0, version = 0; int opt, err = EXIT_SUCCESS; while ((opt = getopt(argn, argv, "sv")) != -1) { switch (opt) { case 's': sort = 1; break; case 'v': version = 1; break; default: usage(argv[0]); return EXIT_FAILURE; } } if(version == 1) { /* Don't you dare running it on a platform where byte != 8 bits */ printf("%s version %.2f, off_t = %zd bits\n", argv[0], FFINDEX_VERSION, sizeof(off_t) * 8); return EXIT_SUCCESS; } if(argn - optind < 3) { usage(argv[0]); return EXIT_FAILURE; } char *data_filename = argv[optind++]; char *index_filename = argv[optind++]; char *fasta_filename = argv[optind++]; FILE *data_file, *index_file, *fasta_file; size_t offset = 0; /* open ffindex */ err = ffindex_index_open(data_filename, index_filename, "w", &data_file, &index_file, &offset); if(err != EXIT_SUCCESS) return err; fasta_file = fopen(fasta_filename, "r"); if(fasta_file == NULL) { perror(fasta_filename); return EXIT_FAILURE; } size_t fasta_size; char *fasta_data = ffindex_mmap_data(fasta_file, &fasta_size); size_t from_length = 0; char name[FFINDEX_MAX_ENTRY_NAME_LENTH]; int seq_id = 1; for(size_t fasta_offset = 1; fasta_offset < fasta_size; fasta_offset++) // position after first ">" { from_length = 1; while(fasta_offset < fasta_size && !(*(fasta_data + fasta_offset) == '>' && *(fasta_data + fasta_offset - 1) == '\n')) { fasta_offset++; from_length++; } sprintf(name, "%d", seq_id++); ffindex_insert_memory(data_file, index_file, &offset, fasta_data + (fasta_offset - from_length), from_length, name); } fclose(data_file); /* Sort the index entries and write back */ if(sort) { rewind(index_file); ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { perror("ffindex_index_parse failed"); exit(EXIT_FAILURE); } fclose(index_file); ffindex_sort_index_file(index); index_file = fopen(index_filename, "w"); if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } err += ffindex_write(index, index_file); } return err; } /* vim: ts=2 sw=2 et: */ ffindex-0.9.9.3/src/ffindex_get.c0000644000000000000000000000722612240136054016474 0ustar rootroot00000000000000/* * Ffindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * Ffindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include "ffindex.h" #include "ffutil.h" void usage(char* program_name) { fprintf(stderr, "USAGE: %s data_filename index_filename entry name(s)\n" "-n\tuse index of entry instead of entry name\n" FFINDEX_COPYRIGHT, program_name); } int main(int argn, char **argv) { int by_index = 0; int opt; while ((opt = getopt(argn, argv, "n")) != -1) { switch (opt) { case 'n': by_index = 1; break; default: usage(argv[0]); return EXIT_FAILURE; } } if(argn < 3) { usage(argv[0]); return EXIT_FAILURE; } char *data_filename = argv[optind++]; char *index_filename = argv[optind++]; FILE *data_file = fopen(data_filename, "r"); FILE *index_file = fopen(index_filename, "r"); if( data_file == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_get", data_filename); exit(EXIT_FAILURE); } if(index_file == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_get", index_filename); exit(EXIT_FAILURE); } size_t data_size; char *data = ffindex_mmap_data(data_file, &data_size); ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_index_parse", index_filename); exit(EXIT_FAILURE); } if(by_index) { for(int i = optind; i < argn; i++) { size_t index_n = atol(argv[i]) - 1; // offset from 0 but specify from 1 ffindex_entry_t* entry = ffindex_get_entry_by_index(index, index_n); if(entry == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_get entry index out of range", argv[i]); } else { char *filedata = ffindex_get_data_by_entry(data, entry); if(filedata == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_get entry index out of range", argv[i]); } else fwrite(filedata, entry->length - 1, 1, stdout); } } } else // by name { for(int i = optind; i < argn; i++) { char *filename = argv[i]; ffindex_entry_t* entry = ffindex_get_entry_by_name(index, filename); if(entry == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_get key not found in index", filename); } else { char *filedata = ffindex_get_data_by_entry(data, entry); if(filedata == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_get key not found in index", filename); } else fwrite(filedata, entry->length - 1, 1, stdout); } } /* Alternative code using (slower) ffindex_fopen */ /* FILE *file = ffindex_fopen(data, index, filename); if(file == NULL) { errno = ENOENT; fferror_print(__FILE__, __LINE__, "ffindex_fopen file not found in index", filename); } else { char line[LINE_MAX]; while(fgets(line, LINE_MAX, file) != NULL) printf("%s", line); } */ } return 0; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffindex_modify.c0000644000000000000000000001274512240136054017206 0ustar rootroot00000000000000/* * Ffindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * Ffindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include "ffindex.h" #include "ffutil.h" #define MAX_FILENAME_LIST_FILES 4096 void usage(char *program_name) { fprintf(stderr, "USAGE: %s -s|-u [-v] [-f file]* index_filename [filename]*\n" "\t-f file\tfile each line containing a filename\n" "\t\t-f can be specified up to %d times\n" "\t-s\tsort index file\n" "\t-u\tunlink entry (remove from index only)\n" "\t-v\tprint version and other info then exit\n" "EXAMPLE:\n" "\t%s -us foo.ffindex entry1\n" "\tTo clean up the data file after unlinking insert it to an new one:\n" "\tffindex_build -as cleaned.ffdata cleaned.ffindex -d foo.ffdata -i foo.ffindex\n" FFINDEX_COPYRIGHT, program_name, MAX_FILENAME_LIST_FILES, program_name); } int main(int argn, char **argv) { int sort = 0, unlink = 0, version = 0, use_tree = 1; int opt, err = EXIT_SUCCESS; char* list_filenames[MAX_FILENAME_LIST_FILES]; size_t list_filenames_index = 0; while ((opt = getopt(argn, argv, "stuvf:")) != -1) { switch (opt) { case 'f': list_filenames[list_filenames_index++] = optarg; break; case 's': sort = 1; break; case 't': use_tree = 1; break; case 'u': unlink = 1; break; case 'v': version = 1; break; default: fprintf(stderr, "Option %c not recognized\n", opt); usage(argv[0]); return EXIT_FAILURE; } } if(version == 1) { /* Don't you dare running it on a platform where byte != 8 bits */ printf("%s version %.2f, off_t = %zd bits\n", argv[0], FFINDEX_VERSION, sizeof(off_t) * 8); return EXIT_SUCCESS; } if(optind >= argn) { usage(argv[0]); return EXIT_FAILURE; } char *index_filename = argv[optind++]; FILE *index_file; index_file = fopen(index_filename, "r+"); if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { perror("ffindex_index_parse failed"); return (EXIT_FAILURE); } fclose(index_file); /* Unlink entries */ if(unlink) { if(use_tree) { /* Build tree */ index = ffindex_index_as_tree(index); /* For each list_file unlink all entries */ if(list_filenames_index > 0) for(int i = 0; i < list_filenames_index; i++) { printf("Unlinking entries from '%s'\n", list_filenames[i]); FILE *list_file = fopen(list_filenames[i], "r"); if( list_file == NULL) { perror(list_filenames[i]); return EXIT_FAILURE; } /* unlink entries in file, one per line */ char path[PATH_MAX]; while(fgets(path, PATH_MAX, list_file) != NULL) index = ffindex_unlink(index, ffnchomp(path, strlen(path))); } /* unlink entries specified by args */ for(int i = optind; i < argn; i++) index = ffindex_unlink(index, argv[i]); } else // unused { char** sorted_names_to_unlink = malloc(FFINDEX_MAX_INDEX_ENTRIES_DEFAULT * sizeof(char *)); if(sorted_names_to_unlink == NULL) fferror_print(__FILE__, __LINE__, __func__, "malloc failed"); /* For each list_file unlink all entries */ if(list_filenames_index > 0) for(int i = 0; i < list_filenames_index; i++) { printf("Unlinking entries from '%s'\n", list_filenames[i]); FILE *list_file = fopen(list_filenames[i], "r"); if( list_file == NULL) { perror(list_filenames[i]); return EXIT_FAILURE; } /* unlink entries in file, one per line */ char path[PATH_MAX]; while(fgets(path, PATH_MAX, list_file) != NULL) sorted_names_to_unlink[i++] = ffnchomp(strdup(path), strlen(path)); ffindex_unlink_entries(index, sorted_names_to_unlink, i); } /* unlink entries specified by args */ int y = 0; for(int i = optind; i < argn; i++, y++) sorted_names_to_unlink[y] = argv[i]; ffindex_unlink_entries(index, sorted_names_to_unlink, y); /* Sort the index entries and write back */ if(sort) { ffindex_sort_index_file(index); index_file = fopen(index_filename, "w"); if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } err += ffindex_write(index, index_file); } } } else if(sort) { ffindex_sort_index_file(index); index_file = fopen(index_filename, "w"); if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } err += ffindex_write(index, index_file); } /* Write index back */ index_file = fopen(index_filename, "w"); if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } err += ffindex_write(index, index_file); return err; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffindex_optimize.c0000644000000000000000000001162012240136054017546 0ustar rootroot00000000000000/* * Ffindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * Ffindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include "ffindex.h" #include "ffutil.h" #define MAX_FILENAME_LIST_FILES 4096 void usage(char *program_name) { fprintf(stderr, "USAGE: %s OUT_DATA_FILE OUT_INDEX_FILE -d IN_DATA_FILE -i IN_INDEX_FILE\n" "\t-d FFDATA_FILE\tinput ffindex data file to be optimized\n" "\t-i FFINDEX_FILE\tinput ffindex index file to be optimized\n" "\t-v\t\tprint version and other info then exit\n" FFINDEX_COPYRIGHT, program_name, MAX_FILENAME_LIST_FILES, FFINDEX_MAX_ENTRY_NAME_LENTH, FFINDEX_MAX_INDEX_ENTRIES_DEFAULT); } int main(int argn, char **argv) { int version = 0; int opt, err = EXIT_SUCCESS; char* list_ffindex_data[MAX_FILENAME_LIST_FILES]; char* list_ffindex_index[MAX_FILENAME_LIST_FILES]; size_t list_ffindex_data_index = 0; size_t list_ffindex_index_index = 0; size_t list_filenames_index = 0; while ((opt = getopt(argn, argv, "vd:i:")) != -1) { switch (opt) { case 'd': list_ffindex_data[list_ffindex_data_index++] = optarg; break; case 'i': list_ffindex_index[list_ffindex_index_index++] = optarg; break; case 'v': version = 1; break; default: usage(argv[0]); return EXIT_FAILURE; } } if(version == 1) { /* Don't you dare running it on a platform where byte != 8 bits */ printf("%s version %.2f, off_t = %zd bits\n", argv[0], FFINDEX_VERSION, sizeof(off_t) * 8); return EXIT_SUCCESS; } if(argn - optind < 2) { usage(argv[0]); return EXIT_FAILURE; } if(list_ffindex_data_index != list_ffindex_index_index) { fprintf(stderr, "ERROR: -d and -i must be specified pairwise\n"); return EXIT_FAILURE; } char *data_filename = argv[optind++]; char *index_filename = argv[optind++]; FILE *data_file = NULL, *index_file = NULL; size_t offset = 0; /* open index and data file, seek to end if needed */ char *mode = "w"; if(append) mode = "a"; err = ffindex_index_open(data_filename, index_filename, mode, &data_file, &index_file, &offset); if(err != EXIT_SUCCESS) return EXIT_FAILURE; /* For each list_file insert */ if(list_filenames_index > 0) for(int i = 0; i < list_filenames_index; i++) { FILE *list_file = fopen(list_filenames[i], "r"); if( list_file == NULL) { perror(list_filenames[i]); return EXIT_FAILURE; } if(ffindex_insert_list_file(data_file, index_file, &offset, list_file) < 0) { perror(list_filenames[i]); err = -1; } } /* Append other ffindexes */ if(list_ffindex_data_index > 0) { for(int i = 0; i < list_ffindex_data_index; i++) { FILE* data_file_to_add = fopen(list_ffindex_data[i], "r"); if( data_file_to_add == NULL) { perror(list_ffindex_data[i]); return EXIT_FAILURE; } FILE* index_file_to_add = fopen(list_ffindex_index[i], "r"); if( index_file_to_add == NULL) { perror(list_ffindex_index[i]); return EXIT_FAILURE; } size_t data_size; char *data_to_add = ffindex_mmap_data(data_file_to_add, &data_size); ffindex_index_t* index_to_add = ffindex_index_parse(index_file_to_add, 0); ffindex_insert_ffindex(data_file, index_file, &offset, data_to_add, index_to_add); } } /* Insert files and directories into the index */ for(int i = optind; i < argn; i++) { char *path = argv[i]; struct stat sb; if(stat(path, &sb) == -1) { fferror_print(__FILE__, __LINE__, __func__, path); continue; } if(S_ISDIR(sb.st_mode)) { err = ffindex_insert_dir(data_file, index_file, &offset, path); if(err < 0)fferror_print(__FILE__, __LINE__, __func__, path); } else if(S_ISREG(sb.st_mode)) { ffindex_insert_file(data_file, index_file, &offset, path, path); } } fclose(data_file); /* Sort the index entries and write back */ if(sort) { rewind(index_file); ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { fferror_print(__FILE__, __LINE__, __func__, index_filename); exit(EXIT_FAILURE); } fclose(index_file); ffindex_sort_index_file(index); index_file = fopen(index_filename, "w"); if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; } err += ffindex_write(index, index_file); } return err; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffindex_unpack.c0000644000000000000000000000455312240136054017176 0ustar rootroot00000000000000/* * FFindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * FFindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ * * ffindex_apply * apply a program to each FFindex entry */ #define _GNU_SOURCE 1 #define _LARGEFILE64_SOURCE 1 #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include "ffindex.h" #include "ffutil.h" int main(int argn, char **argv) { if(argn < 4) { fprintf(stderr, "USAGE: %s DATA_FILENAME INDEX_FILENAME OUT_DIR\n" FFINDEX_COPYRIGHT, argv[0]); return -1; } char *data_filename = argv[1]; char *index_filename = argv[2]; char *out_dir = argv[3]; FILE *data_file = fopen(data_filename, "r"); FILE *index_file = fopen(index_filename, "r"); if( data_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename); exit(EXIT_FAILURE); } if(index_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename); exit(EXIT_FAILURE); } size_t data_size; char *data = ffindex_mmap_data(data_file, &data_size); ffindex_index_t* index = ffindex_index_parse(index_file, 0); if(index == NULL) { fferror_print(__FILE__, __LINE__, "ffindex_index_parse", index_filename); exit(EXIT_FAILURE); } if(chdir(out_dir) < 0){ fferror_print(__FILE__, __LINE__, argv[0], out_dir); exit(EXIT_FAILURE); } size_t range_start = 0; size_t range_end = index->n_entries; // Foreach entry //#pragma omp parallel for for(size_t entry_index = range_start; entry_index < range_end; entry_index++) { //fprintf(stderr, "index %ld\n", entry_index); ffindex_entry_t* entry = ffindex_get_entry_by_index(index, entry_index); if(entry == NULL) { perror(entry->name); continue; } FILE *output_file = fopen(entry->name, "w"); // Write file data to child's stdin. char *filedata = ffindex_get_data_by_entry(data, entry); size_t written = fwrite(filedata, entry->length - 1, 1, output_file); if(written < 1) { perror(entry->name); break; } fclose(output_file); } return 0; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffutil.c0000644000000000000000000000213212240136054015472 0ustar rootroot00000000000000/* * Ffindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * Ffindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ * * Ffindex is a very simple database for small files. The files are stored * concatenated in one big data file, seperated by '\0'. A second file * contains a plain text index, giving name, offset and length of of the small * files. */ #include #include "ffutil.h" int fferror_print(char *sourcecode_filename, int line, const char *function_name, const char *message) { int myerrno = errno; char* errstr = strerror(myerrno); fprintf(stderr, "%s:%d %s: %s: %s\n", sourcecode_filename , line, function_name, message, errstr); return myerrno; } /* remove \n, assumes UNIX line endings! */ char* ffnchomp(char *s, size_t len) { len -= 1; if(len >= 0 && s[len] == '\n') s[len] = '\0'; return s; } /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/ffutil.h0000644000000000000000000000147312240136054015506 0ustar rootroot00000000000000/* * Ffindex * written by Andreas Hauser . * Please add your name here if you distribute modified versions. * * Ffindex is provided under the Create Commons license "Attribution-ShareAlike * 3.0", which basically captures the spirit of the Gnu Public License (GPL). * * See: * http://creativecommons.org/licenses/by-sa/3.0/ * * Ffindex is a very simple database for small files. The files are stored * concatenated in one big data file, seperated by '\0'. A second file * contains a plain text index, giving name, offset and length of of the small * files. */ #include #include #include int fferror_print(char *sourcecode_filename, int line, const char *function_name, const char *message); char* ffnchomp(char *s, size_t len); /* vim: ts=2 sw=2 et */ ffindex-0.9.9.3/src/test-unlink.should0000644000000000000000000000001412240136054017527 0ustar rootroot00000000000000a fooo fooo ffindex-0.9.9.3/src/test.should0000644000000000000000000000001712240136054016234 0ustar rootroot00000000000000a bb fooo fooo ffindex-0.9.9.3/test/data/a0000644000000000000000000000000212240136054015273 0ustar rootroot00000000000000a ffindex-0.9.9.3/test/data/b0000644000000000000000000000000312240136054015275 0ustar rootroot00000000000000bb ffindex-0.9.9.3/test/data/c0000644000000000000000000000000312240136054015276 0ustar rootroot00000000000000cc ffindex-0.9.9.3/test/data2/foo0000644000000000000000000000001212240136054015721 0ustar rootroot00000000000000fooo fooo ffindex-0.9.9.3/test/testfile0000644000000000000000000000001112240136054015761 0ustar rootroot00000000000000testfile