summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-04-30 01:24:16 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-04-30 01:31:32 +0200
commitfa7f378bf627ddcfd7a93a000149e4d8c3810bf5 (patch)
treee431943a22bc73395c4cb6a639b4243fc45f7deb
Initial commit
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--.gitignore21
-rw-r--r--LICENSE674
-rw-r--r--Makefile.am12
-rwxr-xr-xautogen.sh3
-rw-r--r--configure.ac57
-rw-r--r--include/fstree.h78
-rw-r--r--include/squashfs.h177
-rw-r--r--lib/Makemodule.am5
-rw-r--r--lib/fstree/fstree.c200
-rw-r--r--lib/fstree/fstree_from_file.c436
-rw-r--r--lib/fstree/fstree_sort.c76
-rw-r--r--m4/compiler.m440
-rw-r--r--mkfs/Makemodule.am5
-rw-r--r--mkfs/mksquashfs.c70
-rw-r--r--mkfs/options.c295
-rw-r--r--mkfs/options.h20
16 files changed, 2169 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d10a7fe
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,21 @@
+.deps
+.dirstamp
+.gdb_history
+Makefile
+Makefile.in
+aclocal.m4
+autom4te.cache
+compile
+config.h.in
+config.log
+config.status
+configure
+depcomp
+install-sh
+missing
+stamp-h1
+config.h
+*.o
+*.a
+*~
+mksquashfs
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..f288702
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,674 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users. We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors. You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights. Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received. You must make sure that they, too, receive
+or can get the source code. And you must show them these terms so they
+know their rights.
+
+ Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+ For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software. For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+ Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so. This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software. The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable. Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products. If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+ Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary. To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Use with the GNU Affero General Public License.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+ <program> Copyright (C) <year> <name of author>
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+<https://www.gnu.org/licenses/>.
+
+ The GNU General Public License does not permit incorporating your program
+into proprietary programs. If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License. But first, please read
+<https://www.gnu.org/licenses/why-not-lgpl.html>.
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..1b8284b
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,12 @@
+ACLOCAL_AMFLAGS = -I m4
+
+AM_CPPFLAGS = -I$(top_srcdir)/include -D_GNU_SOURCE
+AM_CFLAGS = $(WARN_CFLAGS)
+
+noinst_LIBRARIES =
+bin_PROGRAMS =
+
+EXTRA_DIST = autogen.sh LICENSE
+
+include lib/Makemodule.am
+include mkfs/Makemodule.am
diff --git a/autogen.sh b/autogen.sh
new file mode 100755
index 0000000..c08fadf
--- /dev/null
+++ b/autogen.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+autoreconf --force --install --symlink
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..18a4a81
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,57 @@
+AC_PREREQ([2.60])
+AC_INIT([squashfs-tools-ng], [0.1], [goliath@sigma-star.at], squashfs-tools-ng)
+AC_CONFIG_MACRO_DIR([m4])
+AM_INIT_AUTOMAKE([foreign dist-xz subdir-objects])
+AM_SILENT_RULES([yes])
+AC_PROG_CC
+AC_PROG_CC_C99
+AC_PROG_RANLIB
+AC_PROG_INSTALL
+
+m4_ifndef([PKG_PROG_PKG_CONFIG],
+ [m4_fatal([Could not locate the pkg-config autoconf
+ macros. These are usually located in /usr/share/aclocal/pkg.m4.
+ If your macros are in a different location, try setting the
+ environment variable AL_OPTS="-I/other/macro/dir" before running
+ ./autogen.sh or autoreconf again. Make sure pkg-config is installed.])])
+PKG_PROG_PKG_CONFIG
+
+UL_WARN_ADD([-Wall])
+UL_WARN_ADD([-Wextra])
+UL_WARN_ADD([-Wunused])
+UL_WARN_ADD([-Wmissing-prototypes])
+UL_WARN_ADD([-Wmissing-declarations])
+UL_WARN_ADD([-Wwrite-strings])
+UL_WARN_ADD([-Wjump-misses-init])
+UL_WARN_ADD([-Wuninitialized])
+UL_WARN_ADD([-Winit-self])
+UL_WARN_ADD([-Wlogical-op])
+UL_WARN_ADD([-Wunused-but-set-parameter])
+UL_WARN_ADD([-Wunused-but-set-variable])
+UL_WARN_ADD([-Wunused-parameter])
+UL_WARN_ADD([-Wunused-result])
+UL_WARN_ADD([-Wunused-variable])
+UL_WARN_ADD([-Wduplicated-cond])
+UL_WARN_ADD([-Wduplicated-branches])
+UL_WARN_ADD([-Wrestrict])
+UL_WARN_ADD([-Wnull-dereference])
+UL_WARN_ADD([-pedantic])
+
+AC_SUBST([WARN_CFLAGS])
+
+##### search for dependencies #####
+
+have_zlib="no"
+have_lzma="no"
+
+PKG_CHECK_MODULES(ZLIB, [zlib], [have_zlib="yes"], [])
+PKG_CHECK_MODULES(XZ, [liblzma >= 5.0.0], [have_lzma="yes"], [])
+
+AM_CONDITIONAL([WITH_ZLIB], [test "x$have_zlib" == "xyes"])
+AM_CONDITIONAL([WITH_LZMA], [test "x$have_lzma" == "xyes"])
+
+##### generate output #####
+
+AC_CONFIG_HEADERS([config.h])
+
+AC_OUTPUT([Makefile])
diff --git a/include/fstree.h b/include/fstree.h
new file mode 100644
index 0000000..68bbc7d
--- /dev/null
+++ b/include/fstree.h
@@ -0,0 +1,78 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#ifndef FSTREE_H
+#define FSTREE_H
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+
+typedef struct tree_node_t tree_node_t;
+typedef struct file_info_t file_info_t;
+typedef struct dir_info_t dir_info_t;
+typedef struct fstree_t fstree_t;
+
+struct file_info_t {
+ char *input_file;
+ file_info_t *frag_next;
+ uint64_t size;
+ uint64_t startblock;
+ uint32_t fragment;
+ uint32_t fragment_offset;
+ uint32_t blocksizes[];
+};
+
+struct dir_info_t {
+ tree_node_t *children;
+ uint64_t size;
+ uint64_t start_block;
+ uint32_t block_offset;
+ bool created_implicitly;
+};
+
+struct tree_node_t {
+ tree_node_t *next;
+ char *name;
+ uint32_t uid;
+ uint32_t gid;
+ uint16_t mode;
+
+ union {
+ dir_info_t *dir;
+ file_info_t *file;
+ char *slink_target;
+ uint64_t devno;
+ } data;
+
+ uint8_t payload[];
+};
+
+struct fstree_t {
+ uint32_t default_uid;
+ uint32_t default_gid;
+ uint32_t default_mode;
+ uint32_t default_mtime;
+ size_t block_size;
+
+ tree_node_t *root;
+};
+
+int fstree_init(fstree_t *fs, size_t block_size, uint32_t mtime,
+ uint16_t default_mode, uint32_t default_uid,
+ uint32_t default_gid);
+
+void fstree_cleanup(fstree_t *fs);
+
+tree_node_t *fstree_add(fstree_t *fs, const char *path, uint16_t mode,
+ uint32_t uid, uint32_t gid, size_t extra_len);
+
+tree_node_t *fstree_add_file(fstree_t *fs, const char *path, uint16_t mode,
+ uint32_t uid, uint32_t gid, uint64_t filesz,
+ const char *input);
+
+int fstree_from_file(fstree_t *fs, const char *filename);
+
+void fstree_sort(fstree_t *fs);
+
+#endif /* FSTREE_H */
diff --git a/include/squashfs.h b/include/squashfs.h
new file mode 100644
index 0000000..68f9fce
--- /dev/null
+++ b/include/squashfs.h
@@ -0,0 +1,177 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#ifndef SQUASHFS_H
+#define SQUASHFS_H
+
+#include <stdint.h>
+
+#define SQFS_MAGIC 0x73717368
+#define SQFS_VERSION_MAJOR 4
+#define SQFS_VERSION_MINOR 0
+#define SQFS_META_BLOCK_SIZE 8192
+#define SQFS_DEFAULT_BLOCK_SIZE 131072
+#define SQFS_DEVBLK_SIZE 4096
+#define SQFS_MAX_DIR_ENT 256
+
+typedef struct {
+ uint32_t magic;
+ uint32_t inode_count;
+ uint32_t modification_time;
+ uint32_t block_size;
+ uint32_t fragment_entry_count;
+ uint16_t compression_id;
+ uint16_t block_log;
+ uint16_t flags;
+ uint16_t id_count;
+ uint16_t version_major;
+ uint16_t version_minor;
+ uint64_t root_inode_ref;
+ uint64_t bytes_used;
+ uint64_t id_table_start;
+ uint64_t xattr_id_table_start;
+ uint64_t inode_table_start;
+ uint64_t directory_table_start;
+ uint64_t fragment_table_start;
+ uint64_t export_table_start;
+} __attribute__((packed)) sqfs_super_t;
+
+typedef struct {
+ uint64_t start_offset;
+ uint32_t size;
+ uint32_t pad0;
+} sqfs_fragment_t;
+
+typedef struct {
+ uint16_t type;
+ uint16_t mode;
+ uint16_t uid_idx;
+ uint16_t gid_idx;
+ uint32_t mod_time;
+ uint32_t inode_number;
+} sqfs_inode_t;
+
+typedef struct {
+ uint32_t nlink;
+ uint32_t devno;
+} sqfs_inode_dev_t;
+
+typedef struct {
+ uint32_t nlink;
+ uint32_t devno;
+ uint32_t xattr_idx;
+} sqfs_inode_dev_ext_t;
+
+typedef struct {
+ uint32_t nlink;
+ uint32_t target_size;
+ uint8_t target[];
+} sqfs_inode_slink_t;
+
+typedef struct {
+ uint32_t blocks_start;
+ uint32_t fragment_index;
+ uint32_t fragment_offset;
+ uint32_t file_size;
+ uint32_t block_sizes[];
+} sqfs_inode_file_t;
+
+typedef struct {
+ uint64_t blocks_start;
+ uint64_t file_size;
+ uint64_t sparse;
+ uint32_t nlink;
+ uint32_t fragment_idx;
+ uint32_t fragment_offset;
+ uint32_t xattr_idx;
+ uint32_t block_sizes[];
+} sqfs_inode_file_ext_t;
+
+typedef struct {
+ uint32_t start_block;
+ uint32_t nlink;
+ uint16_t size;
+ uint16_t offset;
+ uint32_t parent_inode;
+} sqfs_inode_dir_t;
+
+typedef struct {
+ uint32_t nlink;
+ uint32_t size;
+ uint32_t start_block;
+ uint32_t parent_inode;
+ uint16_t inodex_count;
+ uint16_t offset;
+ uint32_t xattr_idx;
+} sqfs_inode_dir_ext_t;
+
+typedef union {
+ sqfs_inode_t inode;
+
+ union {
+ sqfs_inode_dev_t dev;
+ sqfs_inode_dev_ext_t dev_ext;
+ sqfs_inode_slink_t slink;
+ sqfs_inode_file_t file;
+ sqfs_inode_file_ext_t file_ext;
+ sqfs_inode_dir_t dir;
+ sqfs_inode_dir_ext_t dir_ext;
+ } data;
+} sqfs_inode_generic_t;
+
+typedef struct {
+ uint32_t count;
+ uint32_t start_block;
+ uint32_t inode_number;
+} sqfs_dir_header_t;
+
+typedef struct {
+ uint16_t offset;
+ uint16_t inode_number;
+ uint16_t type;
+ uint16_t size;
+ uint8_t name[];
+} sqfs_dir_entry_t;
+
+typedef enum {
+ SQFS_COMP_GZIP = 1,
+ SQFS_COMP_LZMA = 2,
+ SQFS_COMP_LZO = 3,
+ SQFS_COMP_XZ = 4,
+ SQFS_COMP_LZ4 = 5,
+ SQFS_COMP_ZSTD = 6,
+
+ SQFS_COMP_MIN = 1,
+ SQFS_COMP_MAX = 6,
+} E_SQFS_COMPRESSOR;
+
+typedef enum {
+ SQFS_FLAG_UNCOMPRESSED_INODES = 0x0001,
+ SQFS_FLAG_UNCOMPRESSED_DATA = 0x0002,
+ SQFS_FLAG_UNCOMPRESSED_FRAGMENTS = 0x0008,
+ SQFS_FLAG_NO_FRAGMENTS = 0x0010,
+ SQFS_FLAG_ALWAYS_FRAGMENTS = 0x0020,
+ SQFS_FLAG_DUPLICATES = 0x0040,
+ SQFS_FLAG_EXPORTABLE = 0x0080,
+ SQFS_FLAG_UNCOMPRESSED_XATTRS = 0x0100,
+ SQFS_FLAG_NO_XATTRS = 0x0200,
+ SQFS_FLAG_COMPRESSOR_OPTIONS = 0x0400,
+ SQFS_FLAG_UNCOMPRESSED_IDS = 0x0800,
+} E_SQFS_SUPER_FLAGS;
+
+typedef enum {
+ SQFS_INODE_DIR = 1,
+ SQFS_INODE_FILE = 2,
+ SQFS_INODE_SLINK = 3,
+ SQFS_INODE_BDEV = 4,
+ SQFS_INODE_CDEV = 5,
+ SQFS_INODE_FIFO = 6,
+ SQFS_INODE_SOCKET = 7,
+ SQFS_INODE_EXT_DIR = 8,
+ SQFS_INODE_EXT_FILE = 9,
+ SQFS_INODE_EXT_SLINK = 10,
+ SQFS_INODE_EXT_BDEV = 11,
+ SQFS_INODE_EXT_CDEV = 12,
+ SQFS_INODE_EXT_FIFO = 13,
+ SQFS_INODE_EXT_SOCKET = 14,
+} E_SQFS_INODE_TYPE;
+
+#endif /* SQUASHFS_H */
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
new file mode 100644
index 0000000..18ec8c4
--- /dev/null
+++ b/lib/Makemodule.am
@@ -0,0 +1,5 @@
+libfstree_a_SOURCES = lib/fstree/fstree.c lib/fstree/fstree_from_file.c
+libfstree_a_SOURCES += lib/fstree/fstree_sort.c
+libfstree_a_SOURCES += include/fstree.h
+
+noinst_LIBRARIES += libfstree.a
diff --git a/lib/fstree/fstree.c b/lib/fstree/fstree.c
new file mode 100644
index 0000000..2722b4a
--- /dev/null
+++ b/lib/fstree/fstree.c
@@ -0,0 +1,200 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+
+static tree_node_t *mknode(tree_node_t *parent, const char *name,
+ size_t name_len, size_t extra_len,
+ uint16_t mode, uint32_t uid, uint32_t gid)
+{
+ size_t size = sizeof(tree_node_t) + extra_len;
+ tree_node_t *n;
+
+ switch (mode & S_IFMT) {
+ case S_IFDIR:
+ size += sizeof(*n->data.dir);
+ break;
+ case S_IFREG:
+ size += sizeof(*n->data.file);
+ break;
+ }
+
+ n = calloc(1, size + name_len + 1);
+ if (n == NULL)
+ return NULL;
+
+ if (parent != NULL) {
+ n->next = parent->data.dir->children;
+ parent->data.dir->children = n;
+ }
+
+ n->uid = uid;
+ n->gid = gid;
+ n->mode = mode;
+
+ switch (mode & S_IFMT) {
+ case S_IFDIR:
+ n->data.dir = (dir_info_t *)n->payload;
+ break;
+ case S_IFREG:
+ n->data.file = (file_info_t *)n->payload;
+ break;
+ case S_IFLNK:
+ n->data.slink_target = (char *)n->payload;
+ break;
+ }
+
+ n->name = (char *)n + size;
+ memcpy(n->name, name, name_len);
+ return n;
+}
+
+static void free_recursive(tree_node_t *n)
+{
+ tree_node_t *it;
+
+ if (S_ISDIR(n->mode)) {
+ while (n->data.dir->children != NULL) {
+ it = n->data.dir->children;
+ n->data.dir->children = it->next;
+
+ free_recursive(it);
+ }
+ }
+
+ free(n);
+}
+
+static tree_node_t *child_by_name(tree_node_t *root, const char *name,
+ size_t len)
+{
+ tree_node_t *n = root->data.dir->children;
+
+ while (n != NULL) {
+ if (strncmp(n->name, name, len) == 0 && n->name[len] == '\0')
+ break;
+
+ n = n->next;
+ }
+
+ return n;
+}
+
+static tree_node_t *get_parent_node(fstree_t *fs, tree_node_t *root,
+ const char *path)
+{
+ const char *end;
+ tree_node_t *n;
+
+ for (;;) {
+ if (!S_ISDIR(root->mode)) {
+ errno = ENOTDIR;
+ return NULL;
+ }
+
+ end = strchr(path, '/');
+ if (end == NULL)
+ break;
+
+ n = child_by_name(root, path, end - path);
+
+ if (n == NULL) {
+ n = mknode(root, path, end - path, 0,
+ S_IFDIR | fs->default_mode,
+ fs->default_uid, fs->default_gid);
+ if (n == NULL)
+ return NULL;
+
+ n->data.dir->created_implicitly = true;
+ }
+
+ root = n;
+ path = end + 1;
+ }
+
+ return root;
+}
+
+tree_node_t *fstree_add(fstree_t *fs, const char *path, uint16_t mode,
+ uint32_t uid, uint32_t gid, size_t extra_len)
+{
+ tree_node_t *child, *parent;
+ const char *name;
+
+ name = strrchr(path, '/');
+ name = (name == NULL ? path : (name + 1));
+
+ parent = get_parent_node(fs, fs->root, path);
+ if (parent == NULL)
+ return NULL;
+
+ child = child_by_name(parent, name, strlen(name));
+ if (child != NULL) {
+ if (S_ISDIR(child->mode) && S_ISDIR(mode) &&
+ child->data.dir->created_implicitly) {
+ child->data.dir->created_implicitly = false;
+ return child;
+ }
+
+ errno = EEXIST;
+ return NULL;
+ }
+
+ return mknode(parent, name, strlen(name), extra_len, mode, uid, gid);
+}
+
+tree_node_t *fstree_add_file(fstree_t *fs, const char *path, uint16_t mode,
+ uint32_t uid, uint32_t gid, uint64_t filesz,
+ const char *input)
+{
+ tree_node_t *node;
+ size_t count, extra;
+ char *ptr;
+
+ count = filesz / fs->block_size;
+ extra = sizeof(uint32_t) * count + strlen(input) + 1;
+
+ mode &= 07777;
+ node = fstree_add(fs, path, S_IFREG | mode, uid, gid, extra);
+
+ if (node != NULL) {
+ ptr = (char *)(node->data.file->blocksizes + count);
+ strcpy(ptr, input);
+
+ node->data.file->input_file = ptr;
+ node->data.file->size = filesz;
+ }
+ return node;
+}
+
+int fstree_init(fstree_t *fs, size_t block_size, uint32_t mtime,
+ uint16_t default_mode, uint32_t default_uid,
+ uint32_t default_gid)
+{
+ memset(fs, 0, sizeof(*fs));
+
+ fs->default_uid = default_uid;
+ fs->default_gid = default_gid;
+ fs->default_mode = default_mode & 07777;
+ fs->default_mtime = mtime;
+ fs->block_size = block_size;
+
+ fs->root = mknode(NULL, "", 0, 0, S_IFDIR | fs->default_mode,
+ default_uid, default_gid);
+
+ if (fs->root == NULL) {
+ perror("initializing file system tree");
+ return -1;
+ }
+
+ return 0;
+}
+
+void fstree_cleanup(fstree_t *fs)
+{
+ free_recursive(fs->root);
+ memset(fs, 0, sizeof(*fs));
+}
diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c
new file mode 100644
index 0000000..66d71a2
--- /dev/null
+++ b/lib/fstree/fstree_from_file.c
@@ -0,0 +1,436 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <sys/sysmacros.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+
+static int add_dir(fstree_t *fs, const char *filename, size_t line_num,
+ const char *path, uint16_t mode, uint32_t uid,
+ uint32_t gid, const char *extra)
+{
+ if (extra != NULL && *extra != '\0') {
+ fprintf(stderr, "%s: %zu: WARNING: ignoring extra arguments\n",
+ filename, line_num);
+ }
+
+ if (fstree_add(fs, path, S_IFDIR | mode, uid, gid, 0) == NULL) {
+ fprintf(stderr, "%s: %zu: mkdir -p %s: %s\n",
+ filename, line_num, path, strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+static int add_slink(fstree_t *fs, const char *filename, size_t line_num,
+ const char *path, uint16_t mode, uint32_t uid,
+ uint32_t gid, const char *extra)
+{
+ tree_node_t *node;
+ (void)mode;
+
+ if (extra == NULL || *extra == '\0') {
+ fprintf(stderr, "%s: %zu: missing symlink target\n",
+ filename, line_num);
+ return -1;
+ }
+
+ node = fstree_add(fs, path, S_IFLNK | 0777, uid, gid,
+ strlen(extra) + 1);
+
+ if (node == NULL) {
+ fprintf(stderr, "%s: %zu: ln -s %s %s\n",
+ filename, line_num, extra, path);
+ return -1;
+ }
+
+ strcpy(node->data.slink_target, extra);
+ return 0;
+}
+
+static int add_device(fstree_t *fs, const char *filename, size_t line_num,
+ const char *path, uint16_t mode, uint32_t uid,
+ uint32_t gid, const char *extra)
+{
+ unsigned int maj = 0, min = 0;
+ tree_node_t *node;
+
+ if (extra == NULL || *extra == '\0') {
+ fprintf(stderr, "%s: %zu: missing device type\n",
+ filename, line_num);
+ return -1;
+ }
+
+ if ((*extra == 'c' || *extra == 'C') && isspace(extra[1])) {
+ mode |= S_IFCHR;
+ } else if ((*extra == 'b' || *extra == 'B') && isspace(extra[1])) {
+ mode |= S_IFBLK;
+ } else {
+ fprintf(stderr, "%s: %zu: unsupported device type",
+ filename, line_num);
+ return -1;
+ }
+
+ ++extra;
+ while (isspace(*extra))
+ ++extra;
+
+ if (!isdigit(*extra))
+ goto fail_devno;
+
+ while (isdigit(*extra))
+ maj = maj * 10 + *(extra++) - '0';
+
+ if (*(extra++) != ':')
+ goto fail_devno;
+
+ if (!isdigit(*extra))
+ goto fail_devno;
+
+ while (isdigit(*extra))
+ min = min * 10 + *(extra++) - '0';
+
+ while (isspace(*extra))
+ ++extra;
+
+ if (*extra != '\0') {
+ fprintf(stderr, "%s: %zu: WARNING: ignoring extra arguments\n",
+ filename, line_num);
+ }
+
+ node = fstree_add(fs, path, mode, uid, gid, 0);
+ if (node == NULL) {
+ fprintf(stderr, "%s: %zu: mknod %s %c %u %u: %s\n",
+ filename, line_num, path, S_ISCHR(mode) ? 'c' : 'b',
+ maj, min, strerror(errno));
+ return -1;
+ }
+
+ node->data.devno = makedev(maj, min);
+ return 0;
+fail_devno:
+ fprintf(stderr, "%s: %zu: error in device number format\n",
+ filename, line_num);
+ return -1;
+}
+
+static int add_pipe(fstree_t *fs, const char *filename, size_t line_num,
+ const char *path, uint16_t mode, uint32_t uid,
+ uint32_t gid, const char *extra)
+{
+ if (extra != NULL && *extra != '\0') {
+ fprintf(stderr, "%s: %zu: WARNING: ignoring extra arguments\n",
+ filename, line_num);
+ }
+
+ if (fstree_add(fs, path, S_IFIFO | mode, uid, gid, 0) == NULL) {
+ fprintf(stderr, "%s: %zu: mkfifo %s: %s\n",
+ filename, line_num, path, strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+static int add_socket(fstree_t *fs, const char *filename, size_t line_num,
+ const char *path, uint16_t mode, uint32_t uid,
+ uint32_t gid, const char *extra)
+{
+ if (extra != NULL && *extra != '\0') {
+ fprintf(stderr, "%s: %zu: WARNING: ignoring extra arguments\n",
+ filename, line_num);
+ }
+
+ if (fstree_add(fs, path, S_IFSOCK | mode, uid, gid, 0) == NULL) {
+ fprintf(stderr, "%s: %zu: creating Unix socket %s: %s\n",
+ filename, line_num, path, strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+static int add_file(fstree_t *fs, const char *filename, size_t line_num,
+ const char *path, uint16_t mode, uint32_t uid,
+ uint32_t gid, const char *extra)
+{
+ char *infile = NULL;
+ tree_node_t *node;
+ const char *ptr;
+ struct stat sb;
+ int ret;
+
+ ptr = strrchr(filename, '/');
+
+ if (extra == NULL || *extra == '\0') {
+ if (ptr == NULL) {
+ extra = path;
+ } else {
+ ret = asprintf(&infile, "%.*s/%s",
+ (int)(filename - ptr), filename, path);
+ if (ret < 0)
+ goto fail_asprintf;
+ }
+ } else if (*extra != '/' && ptr != NULL) {
+ ret = asprintf(&infile, "%.*s/%s",
+ (int)(filename - ptr), filename, extra);
+ if (ret < 0)
+ goto fail_asprintf;
+ }
+
+ if (infile != NULL)
+ extra = infile;
+
+ if (stat(extra, &sb) != 0) {
+ fprintf(stderr, "%s: %zu: stat %s: %s\n", filename, line_num,
+ extra, strerror(errno));
+ goto fail;
+ }
+
+ node = fstree_add_file(fs, path, mode, uid, gid, sb.st_size, extra);
+
+ if (node == NULL) {
+ fprintf(stderr, "%s: %zu: adding %s as %s: %s\n",
+ filename, line_num, extra, path, strerror(errno));
+ goto fail;
+ }
+
+ free(infile);
+ return 0;
+fail:
+ free(infile);
+ return -1;
+fail_asprintf:
+ fprintf(stderr, "%s: %zu: pasting together file path: %s\n",
+ filename, line_num, strerror(errno));
+ return -1;
+}
+
+static const struct {
+ const char *keyword;
+ int (*callback)(fstree_t *fs, const char *filename, size_t line_num,
+ const char *path, uint16_t mode, uint32_t uid,
+ uint32_t gid, const char *extra);
+} file_list_hooks[] = {
+ { "dir", add_dir },
+ { "slink", add_slink },
+ { "nod", add_device },
+ { "pipe", add_pipe },
+ { "pipe", add_socket },
+ { "file", add_file },
+};
+
+#define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0]))
+
+static void trim_line(char *line)
+{
+ size_t i;
+
+ for (i = 0; isspace(line[i]); ++i)
+ ;
+
+ if (line[i] == '#') {
+ line[0] = '\0';
+ return;
+ }
+
+ if (i > 0)
+ memmove(line, line + i, strlen(line + i) + 1);
+
+ i = strlen(line);
+ while (i > 0 && isspace(line[i - 1]))
+ --i;
+
+ line[i] = '\0';
+}
+
+static int handle_line(fstree_t *fs, const char *filename,
+ size_t line_num, char *line)
+{
+ const char *path, *extra = NULL, *msg = NULL;
+ unsigned int mode = 0, uid = 0, gid = 0, x;
+ char keyword[16];
+ size_t i;
+
+ /* isolate keyword */
+ for (i = 0; isalpha(line[i]); ++i)
+ ;
+
+ if (i >= sizeof(keyword) || i == 0 || !isspace(line[i]))
+ goto fail_ent;
+
+ memcpy(keyword, line, i);
+ keyword[i] = '\0';
+
+ while (isspace(line[i]))
+ ++i;
+
+ /* isolate path */
+ path = line + i;
+
+ for (; line[i] != '\0'; ++i) {
+ /* TODO: escape sequences to support spaces in path */
+
+ if (isspace(line[i]))
+ break;
+ }
+
+ if (!isspace(line[i]))
+ goto fail_ent;
+
+ line[i++] = '\0';
+ while (isspace(line[i]))
+ ++i;
+
+ /* mode */
+ if (!isdigit(line[i]))
+ goto fail_mode;
+
+ for (; isdigit(line[i]); ++i) {
+ if (line[i] > '7')
+ goto fail_mode;
+
+ mode = (mode << 3) | (line[i] - '0');
+
+ if (mode > 07777)
+ goto fail_mode_bits;
+ }
+
+ if (!isspace(line[i]))
+ goto fail_ent;
+
+ while (isspace(line[i]))
+ ++i;
+
+ /* uid */
+ if (!isdigit(line[i]))
+ goto fail_uid_gid;
+
+ for (; isdigit(line[i]); ++i) {
+ x = line[i] - '0';
+
+ if (uid > (0xFFFFFFFF - x) / 10)
+ goto fail_ov;
+
+ uid = uid * 10 + x;
+ }
+
+ if (!isspace(line[i]))
+ goto fail_ent;
+
+ while (isspace(line[i]))
+ ++i;
+
+ /* gid */
+ if (!isdigit(line[i]))
+ goto fail_uid_gid;
+
+ for (; isdigit(line[i]); ++i) {
+ x = line[i] - '0';
+
+ if (gid > (0xFFFFFFFF - x) / 10)
+ goto fail_ov;
+
+ gid = gid * 10 + x;
+ }
+
+ /* extra */
+ if (isspace(line[i])) {
+ while (isspace(line[i]))
+ ++i;
+
+ if (line[i] != '\0')
+ extra = line + i;
+ }
+
+ /* forward to callback */
+ for (i = 0; i < NUM_HOOKS; ++i) {
+ if (strcmp(file_list_hooks[i].keyword, keyword) == 0) {
+ return file_list_hooks[i].callback(fs, filename,
+ line_num, path,
+ mode, uid, gid,
+ extra);
+ }
+ }
+
+ fprintf(stderr, "%s: %zu: unknown entry type '%s'.\n", filename,
+ line_num, keyword);
+ return -1;
+fail_ov:
+ msg = "numeric overflow";
+ goto fail_ent;
+fail_uid_gid:
+ msg = "uid & gid must be decimal numbers";
+ goto out_desc;
+fail_mode:
+ msg = "mode must be an octal number";
+ goto out_desc;
+fail_mode_bits:
+ msg = "you can only set the permission bits in the mode";
+ goto out_desc;
+fail_ent:
+ msg = "error in entry description";
+ goto out_desc;
+out_desc:
+ fprintf(stderr, "%s: %zu: %s.\n", filename, line_num, msg);
+ fputs("expected: <type> <path> <mode> <uid> <gid> [<extra>]\n",
+ stderr);
+ return -1;
+}
+
+int fstree_from_file(fstree_t *fs, const char *filename)
+{
+ FILE *fp = fopen(filename, "rb");
+ size_t n, line_num = 0;
+ ssize_t ret;
+ char *line;
+
+ if (fp == NULL) {
+ perror(filename);
+ return -1;
+ }
+
+ for (;;) {
+ line = NULL;
+ n = 0;
+ errno = 0;
+
+ ret = getline(&line, &n, fp);
+ ++line_num;
+
+ if (ret < 0) {
+ if (errno == 0) {
+ free(line);
+ break;
+ }
+
+ perror(filename);
+ goto fail_line;
+ }
+
+ trim_line(line);
+
+ if (line[0] == '\0') {
+ free(line);
+ continue;
+ }
+
+ if (handle_line(fs, filename, line_num, line))
+ goto fail_line;
+
+ free(line);
+ }
+
+ fclose(fp);
+ return 0;
+fail_line:
+ free(line);
+ fclose(fp);
+ return -1;
+}
diff --git a/lib/fstree/fstree_sort.c b/lib/fstree/fstree_sort.c
new file mode 100644
index 0000000..9f07c92
--- /dev/null
+++ b/lib/fstree/fstree_sort.c
@@ -0,0 +1,76 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "fstree.h"
+
+#include <string.h>
+
+static tree_node_t *sort_list(tree_node_t *head)
+{
+ tree_node_t *it, *prev, *lhs, *rhs;
+ size_t i, count = 0;
+
+ for (it = head; it != NULL; it = it->next)
+ ++count;
+
+ if (count < 2)
+ return head;
+
+ prev = NULL;
+ it = head;
+
+ for (i = 0; i < count / 2; ++i) {
+ prev = it;
+ it = it->next;
+ }
+
+ prev->next = NULL;
+
+ lhs = sort_list(head);
+ rhs = sort_list(it);
+
+ head = NULL;
+ prev = NULL;
+
+ while (lhs != NULL && rhs != NULL) {
+ if (strcmp(lhs->name, rhs->name) <= 0) {
+ it = lhs;
+ lhs = lhs->next;
+ } else {
+ it = rhs;
+ rhs = rhs->next;
+ }
+
+ it->next = NULL;
+
+ if (prev != NULL) {
+ prev->next = it;
+ prev = it;
+ } else {
+ prev = head = it;
+ }
+ }
+
+ it = (lhs != NULL ? lhs : rhs);
+
+ if (prev != NULL) {
+ prev->next = it;
+ } else {
+ head = it;
+ }
+
+ return head;
+}
+
+static void sort_directory(tree_node_t *n)
+{
+ n->data.dir->children = sort_list(n->data.dir->children);
+
+ for (n = n->data.dir->children; n != NULL; n = n->next) {
+ if (S_ISDIR(n->mode))
+ sort_directory(n);
+ }
+}
+
+void fstree_sort(fstree_t *fs)
+{
+ sort_directory(fs->root);
+}
diff --git a/m4/compiler.m4 b/m4/compiler.m4
new file mode 100644
index 0000000..058c73f
--- /dev/null
+++ b/m4/compiler.m4
@@ -0,0 +1,40 @@
+dnl Copyright (C) 2008-2011 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl From Simon Josefsson
+dnl -- derivated from coreutils m4/warnings.m4
+
+# UL_AS_VAR_APPEND(VAR, VALUE)
+# ----------------------------
+# Provide the functionality of AS_VAR_APPEND if Autoconf does not have it.
+m4_ifdef([AS_VAR_APPEND],
+[m4_copy([AS_VAR_APPEND], [UL_AS_VAR_APPEND])],
+[m4_define([UL_AS_VAR_APPEND],
+[AS_VAR_SET([$1], [AS_VAR_GET([$1])$2])])])
+
+# UL_ADD_WARN(COMPILER_OPTION [, VARNAME])
+# ------------------------
+# Adds parameter to WARN_CFLAGS (or to $VARNAME) if the compiler supports it.
+AC_DEFUN([UL_WARN_ADD], [
+ m4_define([warnvarname], m4_default([$2],WARN_CFLAGS))
+ AS_VAR_PUSHDEF([ul_Warn], [ul_cv_warn_$1])dnl
+ AC_CACHE_CHECK([whether compiler handles $1], m4_defn([ul_Warn]), [
+ # store AC_LANG_WERROR status, then turn it on
+ save_ac_[]_AC_LANG_ABBREV[]_werror_flag="${ac_[]_AC_LANG_ABBREV[]_werror_flag}"
+ AC_LANG_WERROR
+
+ ul_save_CPPFLAGS="$CPPFLAGS"
+ CPPFLAGS="-Werror ${CPPFLAGS} $1"
+ AC_PREPROC_IFELSE([AC_LANG_PROGRAM([])],
+ [AS_VAR_SET(ul_Warn, [yes])],
+ [AS_VAR_SET(ul_Warn, [no])])
+ # restore AC_LANG_WERROR
+ ac_[]_AC_LANG_ABBREV[]_werror_flag="${save_ac_[]_AC_LANG_ABBREV[]_werror_flag}"
+
+ CPPFLAGS="$ul_save_CPPFLAGS"
+ ])
+ AS_VAR_IF(ul_Warn, [yes], [UL_AS_VAR_APPEND(warnvarname, [" $1"])])
+])
+
diff --git a/mkfs/Makemodule.am b/mkfs/Makemodule.am
new file mode 100644
index 0000000..1d062b9
--- /dev/null
+++ b/mkfs/Makemodule.am
@@ -0,0 +1,5 @@
+mksquashfs_SOURCES = mkfs/mksquashfs.c mkfs/options.c mkfs/options.h
+mksquashfs_SOURCES += include/squashfs.h
+mksquashfs_LDADD = libfstree.a
+
+bin_PROGRAMS += mksquashfs
diff --git a/mkfs/mksquashfs.c b/mkfs/mksquashfs.c
new file mode 100644
index 0000000..bc2a215
--- /dev/null
+++ b/mkfs/mksquashfs.c
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "squashfs.h"
+#include "options.h"
+#include "fstree.h"
+
+#include <sys/sysmacros.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <ctype.h>
+
+static void print_tree(int level, tree_node_t *node)
+{
+ tree_node_t *n;
+ int i;
+
+ for (i = 1; i < level; ++i) {
+ fputs("| ", stdout);
+ }
+
+ if (level)
+ fputs("+- ", stdout);
+
+ if (S_ISDIR(node->mode)) {
+ fprintf(stdout, "%s/ (%u, %u, 0%o)\n", node->name,
+ node->uid, node->gid, node->mode & 07777);
+
+ for (n = node->data.dir->children; n != NULL; n = n->next) {
+ print_tree(level + 1, n);
+ }
+
+ if (node->data.dir->children != NULL) {
+ for (i = 0; i < level; ++i)
+ fputs("| ", stdout);
+
+ if (level)
+ fputc('\n', stdout);
+ }
+ } else {
+ fprintf(stdout, "%s (%u, %u, 0%o)\n", node->name,
+ node->uid, node->gid, node->mode & 07777);
+ }
+}
+
+
+int main(int argc, char **argv)
+{
+ options_t opt;
+ fstree_t fs;
+
+ process_command_line(&opt, argc, argv);
+
+ if (fstree_init(&fs, opt.blksz, opt.def_mtime, opt.def_mode,
+ opt.def_uid, opt.def_gid)) {
+ return EXIT_FAILURE;
+ }
+
+ if (fstree_from_file(&fs, opt.infile)) {
+ fstree_cleanup(&fs);
+ return EXIT_FAILURE;
+ }
+
+ fstree_sort(&fs);
+
+ print_tree(0, fs.root);
+
+ fstree_cleanup(&fs);
+ return EXIT_SUCCESS;
+}
diff --git a/mkfs/options.c b/mkfs/options.c
new file mode 100644
index 0000000..513e76a
--- /dev/null
+++ b/mkfs/options.c
@@ -0,0 +1,295 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "squashfs.h"
+#include "options.h"
+#include "config.h"
+
+#include <stdlib.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <limits.h>
+#include <string.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <stdio.h>
+
+static struct option long_opts[] = {
+ { "compressor", required_argument, NULL, 'c' },
+ { "block-size", required_argument, NULL, 'b' },
+ { "dev-block-size", required_argument, NULL, 'B' },
+ { "defaults", required_argument, NULL, 'd' },
+ { "force", no_argument, NULL, 'f' },
+ { "version", no_argument, NULL, 'V' },
+ { "help", no_argument, NULL, 'h' },
+};
+
+static const char *short_opts = "c:b:B:d:fhV";
+
+enum {
+ DEF_UID = 0,
+ DEF_GID,
+ DEF_MODE,
+ DEF_MTIME,
+};
+
+static const char *defaults[] = {
+ [DEF_UID] = "uid",
+ [DEF_GID] = "gid",
+ [DEF_MODE] = "mode",
+ [DEF_MTIME] = "mtime",
+ NULL
+};
+
+#define LICENSE_SHORT "GPLv3+"
+#define LICENSE_LONG "GNU GPL version 3 or later"
+#define LICENSE_URL "https://gnu.org/licenses/gpl.html"
+
+extern char *__progname;
+
+static const char *version_string =
+"%s (%s) %s\n"
+"Copyright (c) 2019 David Oberhollenzer\n"
+"License " LICENSE_SHORT ": " LICENSE_LONG " <" LICENSE_URL ">.\n"
+"This is free software: you are free to change and redistribute it.\n"
+"There is NO WARRANTY, to the extent permitted by law.\n"
+"\n"
+"Written by David Oberhollenzer.\n";
+
+static const char *help_string =
+"Usage: %s [OPTIONS] <file-list> <squashfs-file>\n"
+"\n"
+"<file-list> is a file containing newline separated entries that describe\n"
+"the files to be included in the squashfs image:\n"
+"\n"
+"# a comment\n"
+"file <path> <mode> <uid> <gid> [<location>]\n"
+"dir <path> <mode> <uid> <gid>\n"
+"nod <path> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
+"slink <path> <mode> <uid> <gid> <target>\n"
+"pipe <path> <mode> <uid> <gid>\n"
+"sock <path> <mode> <uid> <gid>\n"
+"\n"
+"<path> Absolute path of the entry in the image.\n"
+"<location> If given, location of the input file. Either absolute or relative\n"
+" to the description file. If omitted, the image path is used,\n"
+" relative to the description file.\n"
+"<target> Symlink target.\n"
+"<mode> Mode/permissions of the entry.\n"
+"<uid> Numeric user id.\n"
+"<gid> Numeric group id.\n"
+"<dev_type> Device type (b=block, c=character).\n"
+"<maj> Major number of a device special file.\n"
+"<min> Minor number of a device special file.\n"
+"\n"
+"Example:\n"
+"# A simple squashfs image\n"
+"dir /dev 0755 0 0\n"
+"nod /dev/console 0600 0 0 c 5 1\n"
+"dir /root 0700 0 0\n"
+"dir /sbin 0755 0 0\n"
+"\n"
+"# Add a file. Input is relative to this listing.\n"
+"file /sbin/init 0755 0 0 ../init/sbin/init\n"
+"\n"
+"# Read from ./bin/bash. /bin is created implicitly with default attributes.\n"
+"file /bin/bash 0755 0 0"
+"\n"
+"Possible options:\n"
+"\n"
+" --compressor, -c <name> Select the compressor to use.\n"
+" directories (defaults to 'xz').\n"
+" --block-size, -b <size> Block size to use for Squashfs image.\n"
+" Defaults to %u.\n"
+" --dev-block-size, -B <size> Device block size to padd the image to.\n"
+" Defaults to %u.\n"
+" --defaults, -d <options> A comma seperated list of default values for\n"
+" implicitly created directories.\n"
+"\n"
+" Possible options:\n"
+" uid=<value> 0 if not set.\n"
+" gid=<value> 0 if not set.\n"
+" mode=<value> 0755 if not set.\n"
+" mtime=<value> 0 if not set.\n"
+"\n"
+" --force, -f Overwrite the output file if it exists.\n"
+" --help, -h Print help text and exit.\n"
+" --version, -V Print version information and exit.\n"
+"\n";
+
+static const char *compressors[] = {
+ [SQFS_COMP_GZIP] = "gzip",
+ [SQFS_COMP_LZMA] = "lzma",
+ [SQFS_COMP_LZO] = "lzo",
+ [SQFS_COMP_XZ] = "xz",
+ [SQFS_COMP_LZ4] = "lz4",
+ [SQFS_COMP_ZSTD] = "zstd",
+};
+
+static long read_number(const char *name, const char *str, long min, long max)
+{
+ long base = 10, result = 0;
+ int x;
+
+ if (str[0] == '0') {
+ if (str[1] == 'x' || str[1] == 'X') {
+ base = 16;
+ str += 2;
+ } else {
+ base = 8;
+ }
+ }
+
+ if (!isxdigit(*str))
+ goto fail_num;
+
+ while (isxdigit(*str)) {
+ x = *(str++);
+
+ if (isupper(x)) {
+ x = x - 'A' + 10;
+ } else if (islower(x)) {
+ x = x - 'a' + 10;
+ } else {
+ x -= '0';
+ }
+
+ if (x >= base)
+ goto fail_num;
+
+ if (result > (LONG_MAX - x) / base)
+ goto fail_ov;
+
+ result = result * base + x;
+ }
+
+ if (result < min)
+ goto fail_uf;
+
+ if (result > max)
+ goto fail_ov;
+
+ return result;
+fail_num:
+ fprintf(stderr, "%s: expected numeric value > 0\n", name);
+ goto fail;
+fail_uf:
+ fprintf(stderr, "%s: number to small\n", name);
+ goto fail;
+fail_ov:
+ fprintf(stderr, "%s: number to large\n", name);
+ goto fail;
+fail:
+ exit(EXIT_FAILURE);
+}
+
+static void process_defaults(options_t *opt, char *subopts)
+{
+ char *value;
+ int i;
+
+ while (*subopts != '\0') {
+ i = getsubopt(&subopts, (char *const *)defaults, &value);
+
+ if (value == NULL) {
+ fprintf(stderr, "Missing value for option %s\n",
+ defaults[i]);
+ exit(EXIT_FAILURE);
+ }
+
+ switch (i) {
+ case DEF_UID:
+ opt->def_uid = read_number("Default user ID", value,
+ 0, 0xFFFFFFFF);
+ break;
+ case DEF_GID:
+ opt->def_gid = read_number("Default group ID", value,
+ 0, 0xFFFFFFFF);
+ break;
+ case DEF_MODE:
+ opt->def_mode = read_number("Default permissions",
+ value, 0, 0xFFFFFFFF);
+ break;
+ case DEF_MTIME:
+ opt->def_mtime = read_number("Default mtime", value,
+ 0, 0xFFFFFFFF);
+ break;
+ default:
+ fprintf(stderr, "Unknown option '%s'\n", value);
+ exit(EXIT_FAILURE);
+ }
+ }
+}
+
+void process_command_line(options_t *opt, int argc, char **argv)
+{
+ int i;
+
+ opt->def_uid = 0;
+ opt->def_gid = 0;
+ opt->def_mode = 0755;
+ opt->def_mtime = 0;
+ opt->outmode = O_WRONLY | O_CREAT | O_EXCL;
+ opt->compressor = SQFS_COMP_XZ;
+ opt->blksz = SQFS_DEFAULT_BLOCK_SIZE;
+ opt->devblksz = SQFS_DEVBLK_SIZE;
+ opt->infile = NULL;
+ opt->outfile = NULL;
+
+ for (;;) {
+ i = getopt_long(argc, argv, short_opts, long_opts, NULL);
+ if (i == -1)
+ break;
+
+ switch (i) {
+ case 'c':
+ for (i = SQFS_COMP_MIN; i <= SQFS_COMP_MAX; ++i) {
+ if (strcmp(compressors[i], optarg) == 0) {
+ opt->compressor = i;
+ break;
+ }
+ }
+ break;
+ case 'b':
+ opt->blksz = read_number("Block size", optarg,
+ 1024, 0xFFFFFFFF);
+ break;
+ case 'B':
+ opt->devblksz = read_number("Device block size", optarg,
+ 4096, 0xFFFFFFFF);
+ break;
+ case 'd':
+ process_defaults(opt, optarg);
+ break;
+ case 'f':
+ opt->outmode = O_WRONLY | O_CREAT | O_TRUNC;
+ break;
+ case 'h':
+ printf(help_string, __progname,
+ SQFS_DEFAULT_BLOCK_SIZE, SQFS_DEVBLK_SIZE);
+
+ fputs("Available compressors:\n", stdout);
+
+ for (i = SQFS_COMP_MIN; i <= SQFS_COMP_MAX; ++i)
+ printf("\t%s\n", compressors[i]);
+
+ exit(EXIT_SUCCESS);
+ case 'V':
+ printf(version_string, __progname,
+ PACKAGE_NAME, PACKAGE_VERSION);
+ exit(EXIT_SUCCESS);
+ default:
+ goto fail_arg;
+ }
+ }
+
+ if ((optind + 1) >= argc) {
+ fputs("Missing arguments: input and output files.\n", stderr);
+ goto fail_arg;
+ }
+
+ opt->infile = argv[optind++];
+ opt->outfile = argv[optind++];
+ return;
+fail_arg:
+ fprintf(stderr, "Try `%s --help' for more information.\n", __progname);
+ exit(EXIT_FAILURE);
+}
diff --git a/mkfs/options.h b/mkfs/options.h
new file mode 100644
index 0000000..dc11250
--- /dev/null
+++ b/mkfs/options.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#ifndef OPTIONS_H
+#define OPTIONS_H
+
+typedef struct {
+ unsigned int def_uid;
+ unsigned int def_gid;
+ unsigned int def_mode;
+ unsigned int def_mtime;
+ int outmode;
+ int compressor;
+ int blksz;
+ int devblksz;
+ const char *infile;
+ const char *outfile;
+} options_t;
+
+void process_command_line(options_t *opt, int argc, char **argv);
+
+#endif /* OPTIONS_H */