pax_global_header00006660000000000000000000000064125217405630014517gustar00rootroot0000000000000052 comment=a7581651f3cd0fef3d06b1954a036de4c5f2ffe0 jnr-posix-3.0.12/000077500000000000000000000000001252174056300135335ustar00rootroot00000000000000jnr-posix-3.0.12/.gitignore000066400000000000000000000002031252174056300155160ustar00rootroot00000000000000.idea *.iml *.orig$ *.rej$ *.class$ *~ nbproject/private build dist target lib/nblibraries-private.properties /nbproject/ /.redcar/jnr-posix-3.0.12/.travis.yml000066400000000000000000000004331252174056300156440ustar00rootroot00000000000000language: java jdk: - oraclejdk7 - openjdk6 os: - linux - osx notifications: irc: channels: - "irc.freenode.org#jnr" on_success: change on_failure: always template: - "%{repository} (%{branch}:%{commit} by %{author}): %{message} (%{build_url})" jnr-posix-3.0.12/LICENSE.txt000066400000000000000000001502551252174056300153660ustar00rootroot00000000000000jnr-posix is released under a tri CPL/GPL/LGPL license. You can use it, redistribute it and/or modify it under the terms of the license of your choice. === Common Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 1. DEFINITIONS "Contribution" means: a) in the case of the initial Contributor, the initial code and documentation distributed under this Agreement, and b) in the case of each subsequent Contributor: i) changes to the Program, and ii) additions to the Program; where such changes and/or additions to the Program originate from and are distributed by that particular Contributor. A Contribution 'originates' from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include additions to the Program which: (i) are separate modules of software distributed in conjunction with the Program under their own license agreement, and (ii) are not derivative works of the Program. "Contributor" means any person or entity that distributes the Program. "Licensed Patents " mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. "Program" means the Contributions distributed in accordance with this Agreement. "Recipient" means anyone who receives the Program under this Agreement, including all Contributors. 2. GRANT OF RIGHTS a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, distribute and sublicense the Contribution of such Contributor, if any, and such derivative works, in source code and object code form. b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in source code and object code form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program. d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. 3. REQUIREMENTS A Contributor may choose to distribute the Program in object code form under its own license agreement, provided that: a) it complies with the terms and conditions of this Agreement; and b) its license agreement: i) effectively disclaims on behalf of all Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; ii) effectively excludes on behalf of all Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; iii) states that any provisions which differ from this Agreement are offered by that Contributor alone and not by any other party; and iv) states that source code for the Program is available from such Contributor, and informs licensees how to obtain it in a reasonable manner on or through a medium customarily used for software exchange. When the Program is made available in source code form: a) it must be made available under this Agreement; and b) a copy of this Agreement must be included with each copy of the Program. Contributors may not remove or alter any copyright notices contained within the Program. Each Contributor must identify itself as the originator of its Contribution, if any, in a manner that reasonably allows subsequent Recipients to identify the originator of the Contribution. 4. COMMERCIAL DISTRIBUTION Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. 5. NO WARRANTY EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. 6. DISCLAIMER OF LIABILITY EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 7. GENERAL If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. If Recipient institutes patent litigation against a Contributor with respect to a patent applicable to software (including a cross-claim or counterclaim in a lawsuit), then any patent licenses granted by that Contributor to such Recipient under this Agreement shall terminate as of the date such litigation is filed. In addition, if Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed. All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. IBM is the initial Agreement Steward. IBM may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation. === GPL 2.0 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) 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 this service 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 make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. 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. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute 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 and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), 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 distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the 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 a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, 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. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE 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. END OF TERMS AND CONDITIONS == GNU LGPL 2.1 GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, 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 this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), 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 distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser 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 Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "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 LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY 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 LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice That's all there is to it! jnr-posix-3.0.12/MANIFEST.MF000066400000000000000000000000741252174056300151660ustar00rootroot00000000000000Implementation-Title: JNA-POSIX Implementation-Version: 0.5 jnr-posix-3.0.12/README.txt000066400000000000000000000002211252174056300152240ustar00rootroot00000000000000jnr-posix is a lightweight cross-platform POSIX emulation layer for Java, written in Java and is part of the JNR project (http://github.com/jnr) jnr-posix-3.0.12/pom.xml000066400000000000000000000073521252174056300150570ustar00rootroot00000000000000 4.0.0 org.sonatype.oss oss-parent 7 com.github.jnr jnr-posix jar 3.0.12 jnr-posix Common cross-project/cross-platform POSIX APIs JIRA http://jira.codehaus.org/browse/JRUBY scm:git:git@github.com:jnr/jnr-posix.git scm:git:git@github.com:jnr/jnr-posix.git git@github.com:jnr/jnr-posix.git Common Public License - v 1.0 http://www-128.ibm.com/developerworks/library/os-cpl.html repo GNU General Public License Version 2 http://www.gnu.org/copyleft/gpl.html repo GNU Lesser General Public License Version 2.1 http://www.gnu.org/licenses/lgpl.html repo enebo Thomas E Enebo tom.enebo@gmail.com wmeissner Wayne Meissner wmeissner@gmail.com UTF-8 1.5 1.5 junit junit 4.11 test com.github.jnr jnr-ffi 2.0.3 com.github.jnr jnr-constants 0.8.7 org.apache.maven.wagon wagon-webdav-jackrabbit 1.0-beta-7 maven-jar-plugin 2.3.1 MANIFEST.MF org.apache.maven.plugins maven-surefire-plugin 2.16 2 false -Xmx1024m -XX:MaxPermSize=256m org.apache.maven.plugins maven-source-plugin 2.2.1 attach-sources jar jnr-posix-3.0.12/src/000077500000000000000000000000001252174056300143225ustar00rootroot00000000000000jnr-posix-3.0.12/src/main/000077500000000000000000000000001252174056300152465ustar00rootroot00000000000000jnr-posix-3.0.12/src/main/java/000077500000000000000000000000001252174056300161675ustar00rootroot00000000000000jnr-posix-3.0.12/src/main/java/jnr/000077500000000000000000000000001252174056300167605ustar00rootroot00000000000000jnr-posix-3.0.12/src/main/java/jnr/posix/000077500000000000000000000000001252174056300201225ustar00rootroot00000000000000jnr-posix-3.0.12/src/main/java/jnr/posix/AixFileStat.java000066400000000000000000000106651252174056300231520ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.NativeType; import jnr.ffi.StructLayout; /** * This corresponds with struct stat64x on AIX */ public final class AixFileStat extends BaseFileStat { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final Unsigned64 st_dev = new Unsigned64(); public final Signed64 st_ino = new Signed64(); public final Unsigned32 st_mode = new Unsigned32(); public final Signed16 st_nlink = new Signed16(); public final Unsigned16 st_flag = new Unsigned16(); public final Unsigned32 st_uid = new Unsigned32(); public final Unsigned32 st_gid = new Unsigned32(); public final Unsigned64 st_rdev = new Unsigned64(); public final Signed64 st_size = new Signed64(); public final Signed64 st_atime = new Signed64(); public final Signed32 st_atime_n = new Signed32(); public final Signed32 st_pad1 = new Signed32(); public final Signed64 st_mtime = new Signed64(); public final Signed32 st_mtime_n = new Signed32(); public final Signed32 st_pad2 = new Signed32(); public final Signed64 st_ctime = new Signed64(); public final Signed32 st_ctime_n = new Signed32(); public final Signed32 st_pad3 = new Signed32(); public final Unsigned64 st_blksize = new Unsigned64(); public final Unsigned64 st_blocks = new Unsigned64(); public final Signed32 st_vfstype = new Signed32(); public final Unsigned32 st_vfs = new Unsigned32(); public final Unsigned32 st_type = new Unsigned32(); public final Unsigned32 st_gen = new Unsigned32(); public final Padding st_reserved = new Padding(NativeType.UINT, 11); /* total size should be 176 bytes */ } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public AixFileStat(NativePOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atime.get(memory); } public long blocks() { return layout.st_blocks.get(memory); } public long blockSize() { return layout.st_blksize.get(memory); } public long ctime() { return layout.st_ctime.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return (int) layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return (int) layout.st_mode.get(memory) & 0xffff; } public long mtime() { return layout.st_mtime.get(memory); } public int nlink() { return (int) layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout.st_size.get(memory); } public int uid() { return (int) layout.st_uid.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/AixLibC.java000066400000000000000000000032411252174056300222400ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; public interface AixLibC extends UnixLibC { public int stat64x(CharSequence path, AixFileStat stat); public int fstat64x(int fd, AixFileStat stat); public int lstat64x(CharSequence path, AixFileStat stat); } jnr-posix-3.0.12/src/main/java/jnr/posix/AixPOSIX.java000066400000000000000000000047531252174056300223420ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.constants.platform.Sysconf; import jnr.ffi.Pointer; import jnr.ffi.mapper.FromNativeContext; import jnr.posix.util.MethodName; import java.io.FileDescriptor; final class AixPOSIX extends BaseNativePOSIX { AixPOSIX(LibCProvider libc, POSIXHandler handler) { super(libc, handler); } public FileStat allocateStat() { return new AixFileStat(this); } public MsgHdr allocateMsgHdr() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public SocketMacros socketMacros() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public long sysconf(Sysconf name) { return libc().sysconf(name); } public Times times() { return NativeTimes.times(this); } public static final PointerConverter PASSWD = new PointerConverter() { public Object fromNative(Object arg, FromNativeContext ctx) { return arg != null ? new AixPasswd((Pointer) arg) : null; } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/AixPasswd.java000066400000000000000000000062221252174056300226720ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.StructLayout; public class AixPasswd extends NativePasswd implements Passwd { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final UTF8StringRef pw_name = new UTF8StringRef(); // user name public final UTF8StringRef pw_passwd = new UTF8StringRef(); // password (encrypted) public final uid_t pw_uid = new uid_t(); // user id public final gid_t pw_gid = new gid_t(); // user id public final UTF8StringRef pw_gecos = new UTF8StringRef(); // login info public final UTF8StringRef pw_dir = new UTF8StringRef(); // home directory public final UTF8StringRef pw_shell = new UTF8StringRef(); // default shell } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); AixPasswd(jnr.ffi.Pointer memory) { super(memory); } public String getAccessClass() { return "unknown"; } public String getGECOS() { return layout.pw_gecos.get(memory); } public long getGID() { return layout.pw_gid.get(memory); } public String getHome() { return layout.pw_dir.get(memory); } public String getLoginName() { return layout.pw_name.get(memory); } public int getPasswdChangeTime() { return 0; } public String getPassword() { return layout.pw_passwd.get(memory); } public String getShell() { return layout.pw_shell.get(memory); } public long getUID() { return layout.pw_uid.get(memory); } public int getExpire() { return Integer.MAX_VALUE; } } jnr-posix-3.0.12/src/main/java/jnr/posix/BaseCmsgHdr.java000066400000000000000000000023221252174056300231060ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Memory; import jnr.ffi.Pointer; import java.nio.ByteBuffer; /** * @author Bob McWhirter */ abstract class BaseCmsgHdr implements CmsgHdr { protected final NativePOSIX posix; final Pointer memory; protected BaseCmsgHdr(NativePOSIX posix, Pointer memory) { this.posix = posix; this.memory = memory; } protected BaseCmsgHdr(NativePOSIX posix, Pointer memory, int totalLen) { this.posix = posix; this.memory = memory; setLen( totalLen ); } public void setData(ByteBuffer data) { byte[] bytes = new byte[data.capacity() - data.position()]; data.get(bytes); posix.socketMacros().CMSG_DATA(this.memory).put(0, bytes, 0, bytes.length); } public ByteBuffer getData() { int dataLen = getLen() - posix.socketMacros().CMSG_LEN(0); if ( dataLen == 0 ) { return null; } byte[] bytes = new byte[dataLen]; posix.socketMacros().CMSG_DATA(this.memory).get(0, bytes, 0, bytes.length); ByteBuffer buf = ByteBuffer.allocate(bytes.length); buf.put(bytes); buf.flip(); return buf; } abstract void setLen(int len); } jnr-posix-3.0.12/src/main/java/jnr/posix/BaseFileStat.java000066400000000000000000000131551252174056300233000ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.Memory; import jnr.ffi.Pointer; import jnr.ffi.StructLayout; /** * */ public abstract class BaseFileStat implements FileStat { protected final POSIX posix; protected final Pointer memory; protected BaseFileStat(NativePOSIX posix, StructLayout layout) { this.posix = posix; this.memory = Memory.allocate(posix.getRuntime(), layout.size()); } public java.lang.String ftype() { if (isFile()) { return "file"; } else if (isDirectory()) { return "directory"; } else if (isCharDev()) { return "characterSpecial"; } else if (isBlockDev()) { return "blockSpecial"; } else if (isFifo()) { return "fifo"; } else if (isSymlink()) { return "link"; } else if (isSocket()) { return "socket"; } return "unknown"; } public boolean groupMember(int gid) { if (posix.getgid() == gid || posix.getegid() == gid) { return true; } // FIXME: Though not Posix, windows has different mechanism for this. return false; } public boolean isBlockDev() { return (mode() & S_IFMT) == S_IFBLK; } public boolean isCharDev() { return (mode() & S_IFMT) == S_IFCHR; } public boolean isDirectory() { return (mode() & S_IFMT) == S_IFDIR; } public boolean isEmpty() { return st_size() == 0; } public boolean isExecutable() { if (posix.geteuid() == 0) return (mode() & S_IXUGO) != 0; if (isOwned()) return (mode() & S_IXUSR) != 0; if (isGroupOwned()) return (mode() & S_IXGRP) != 0; return (mode() & S_IXOTH) != 0; } public boolean isExecutableReal() { if (posix.getuid() == 0) return (mode() & S_IXUGO) != 0; if (isROwned()) return (mode() & S_IXUSR) != 0; if (groupMember(gid())) return (mode() & S_IXGRP) != 0; return (mode() & S_IXOTH) != 0; } public boolean isFile() { return (mode() & S_IFMT) == S_IFREG; } public boolean isFifo() { return (mode() & S_IFMT) == S_IFIFO; } public boolean isGroupOwned() { return groupMember(gid()); } public boolean isIdentical(FileStat other) { return dev() == other.dev() && ino() == other.ino(); } public boolean isNamedPipe() { return (mode() & S_IFIFO) != 0; } public boolean isOwned() { return posix.geteuid() == uid(); } public boolean isROwned() { return posix.getuid() == uid(); } public boolean isReadable() { if (posix.geteuid() == 0) return true; if (isOwned()) return (mode() & S_IRUSR) != 0; if (isGroupOwned()) return (mode() & S_IRGRP) != 0; return (mode() & S_IROTH) != 0; } public boolean isReadableReal() { if (posix.getuid() == 0) return true; if (isROwned()) return (mode() & S_IRUSR) != 0; if (groupMember(gid())) return (mode() & S_IRGRP) != 0; return (mode() & S_IROTH) != 0; } public boolean isSetgid() { return (mode() & S_ISGID) != 0; } public boolean isSetuid() { return (mode() & S_ISUID) != 0; } public boolean isSocket() { return (mode() & S_IFMT) == S_IFSOCK; } public boolean isSticky() { return (mode() & S_ISVTX) != 0; } public boolean isSymlink() { return (mode() & S_IFMT) == S_IFLNK; } public boolean isWritable() { if (posix.geteuid() == 0) return true; if (isOwned()) return (mode() & S_IWUSR) != 0; if (isGroupOwned()) return (mode() & S_IWGRP) != 0; return (mode() & S_IWOTH) != 0; } public boolean isWritableReal() { if (posix.getuid() == 0) return true; if (isROwned()) return (mode() & S_IWUSR) != 0; if (groupMember(gid())) return (mode() & S_IWGRP) != 0; return (mode() & S_IWOTH) != 0; } public int major(long dev) { return (int) (dev >> 24) & 0xff; } public int minor(long dev) { return (int) (dev & 0xffffff); } } jnr-posix-3.0.12/src/main/java/jnr/posix/BaseIovec.java000066400000000000000000000036111252174056300226260ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Runtime; import jnr.ffi.Memory; import jnr.ffi.Pointer; import jnr.ffi.StructLayout; import java.nio.ByteBuffer; /** * @author Bob McWhirter */ public class BaseIovec implements Iovec { public static class Layout extends StructLayout { protected Layout(Runtime runtime) { super(runtime); } public final Pointer iov_base = new Pointer(); public final size_t iov_len = new size_t(); } public static final Layout layout = new Layout(Runtime.getSystemRuntime()); private final NativePOSIX posix; protected final Pointer memory; public String toString(String indent) { StringBuffer buf = new StringBuffer(); buf.append( indent ).append( "iovec {\n" ); buf.append(indent).append( " iov_base=" ).append(layout.iov_base.get(this.memory)).append( ",\n" ); buf.append( indent ).append( " iov_len=" ).append( layout.iov_len.get( this.memory ) ).append(",\n"); buf.append(indent).append( "}" ); return buf.toString(); } protected BaseIovec(NativePOSIX posix) { this.posix = posix; this.memory = Memory.allocate(posix.getRuntime(), layout.size()); } BaseIovec(NativePOSIX posix, Pointer memory) { this.posix = posix; this.memory = memory; } public ByteBuffer get() { int len = getLen(); byte[] bytes = new byte[len]; layout.iov_base.get( this.memory ).get(0, bytes, 0, len ); return ByteBuffer.wrap( bytes ); } public void set(ByteBuffer buf) { int len = buf.remaining(); layout.iov_base.set( this.memory, Pointer.wrap( posix.getRuntime(), buf ) ); setLen(len); } protected void setLen(int len) { layout.iov_len.set( this.memory, len ); } protected int getLen() { return (int) layout.iov_len.get( this.memory ); } } jnr-posix-3.0.12/src/main/java/jnr/posix/BaseMsgHdr.java000066400000000000000000000101531252174056300227440ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; import jnr.ffi.Runtime; import jnr.ffi.StructLayout; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; /** * @author Bob McWhirter */ public abstract class BaseMsgHdr implements MsgHdr { protected final NativePOSIX posix; protected final Pointer memory; protected BaseMsgHdr(NativePOSIX posix, StructLayout layout) { this.posix = posix; this.memory = posix.getRuntime().getMemoryManager().allocateTemporary(layout.size(), true); } public void setName(String name) { if (name == null) { setNamePointer(null); setNameLen(0); return; } byte[] nameBytes = name.getBytes(Charset.forName("US-ASCII")); Pointer p = Runtime.getSystemRuntime().getMemoryManager().allocateTemporary(nameBytes.length, true); p.put(0, nameBytes, 0, nameBytes.length); setNamePointer(p); setNameLen(nameBytes.length); } public String getName() { Pointer ptr = getNamePointer(); if (ptr == null) { return null; } return ptr.getString(0, getNameLen(), Charset.forName( "US-ASCII" )); } public CmsgHdr allocateControl(int dataLength) { CmsgHdr[] controls = allocateControls(new int[]{dataLength}); return controls[0]; } public CmsgHdr[] allocateControls(int[] dataLengths) { CmsgHdr[] cmsgs = new CmsgHdr[dataLengths.length]; int totalSize = 0; for (int i = 0; i < dataLengths.length; ++i) { totalSize += posix.socketMacros().CMSG_SPACE(dataLengths[i]); } Pointer ptr = posix.getRuntime().getMemoryManager().allocateDirect(totalSize); int offset = 0; for (int i = 0; i < dataLengths.length; ++i) { int eachLen = posix.socketMacros().CMSG_SPACE(dataLengths[i]); CmsgHdr each = allocateCmsgHdrInternal(posix, ptr.slice(offset, eachLen), eachLen); cmsgs[i] = each; offset += eachLen; } setControlPointer(ptr); setControlLen(totalSize); return cmsgs; } public CmsgHdr[] getControls() { int len = getControlLen(); if (len == 0) { return new CmsgHdr[0]; } List control = new ArrayList(); int offset = 0; Pointer controlPtr = getControlPointer(); while (offset < len) { CmsgHdr each = allocateCmsgHdrInternal(posix, controlPtr.slice(offset), -1); offset += each.getLen(); control.add(each); } return control.toArray(new CmsgHdr[control.size()]); } public void setIov(ByteBuffer[] buffers) { Pointer iov = Runtime.getSystemRuntime().getMemoryManager().allocateDirect(BaseIovec.layout.size() * buffers.length); for (int i = 0; i < buffers.length; ++i) { Pointer eachIovecPtr = iov.slice(BaseIovec.layout.size() * i); BaseIovec eachIovec = new BaseIovec(posix, eachIovecPtr); eachIovec.set(buffers[i]); } setIovPointer(iov); setIovLen(buffers.length); } public ByteBuffer[] getIov() { int len = getIovLen(); ByteBuffer[] buffers = new ByteBuffer[len]; Pointer iov = getIovPointer(); for (int i = 0; i < len; ++i) { Pointer eachPtr = iov.slice(BaseIovec.layout.size() * i); BaseIovec eachIov = new BaseIovec(posix, eachPtr); buffers[i] = eachIov.get(); } return buffers; } abstract void setNamePointer(Pointer name); abstract Pointer getNamePointer(); abstract void setNameLen(int len); abstract int getNameLen(); abstract void setIovPointer(Pointer iov); abstract Pointer getIovPointer(); abstract int getIovLen(); abstract void setIovLen(int len); abstract CmsgHdr allocateCmsgHdrInternal(NativePOSIX posix, Pointer pointer, int len); abstract void setControlPointer(Pointer control); abstract Pointer getControlPointer(); abstract void setControlLen(int len); } jnr-posix-3.0.12/src/main/java/jnr/posix/BaseNativePOSIX.java000066400000000000000000000471441252174056300236430ustar00rootroot00000000000000package jnr.posix; import jnr.constants.Constant; import jnr.constants.platform.Errno; import jnr.constants.platform.Fcntl; import jnr.constants.platform.Sysconf; import jnr.ffi.*; import jnr.ffi.byref.NumberByReference; import jnr.ffi.mapper.FromNativeContext; import jnr.ffi.mapper.FromNativeConverter; import jnr.ffi.mapper.ToNativeContext; import jnr.ffi.mapper.ToNativeConverter; import jnr.posix.util.Java5ProcessMaker; import jnr.posix.util.MethodName; import jnr.posix.util.ProcessMaker; import java.io.FileDescriptor; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.Collection; import java.util.HashMap; import java.util.Map; import jnr.constants.platform.Signal; abstract class BaseNativePOSIX extends NativePOSIX implements POSIX { private final LibC libc; protected final POSIXHandler handler; protected final JavaLibCHelper helper; protected final Map signalHandlers = new HashMap(); BaseNativePOSIX(LibCProvider libcProvider, POSIXHandler handler) { this.handler = handler; this.libc = libcProvider.getLibC(); this.helper = new JavaLibCHelper(handler); } public ProcessMaker newProcessMaker(String... command) { return new Java5ProcessMaker(handler, command); } public ProcessMaker newProcessMaker() { return new Java5ProcessMaker(handler); } public final LibC libc() { return libc; } POSIXHandler handler() { return handler; } protected T unimplementedNull() { handler().unimplementedError(MethodName.getCallerMethodName()); return null; } protected int unimplementedInt() { handler().unimplementedError(MethodName.getCallerMethodName()); return -1; } public int chmod(String filename, int mode) { return libc().chmod(filename, mode); } public int fchmod(int fd, int mode) { return libc().fchmod(fd, mode); } public int chown(String filename, int user, int group) { return libc().chown(filename, user, group); } public int fchown(int fd, int user, int group) { return libc().fchown(fd, user, group); } public CharSequence crypt(CharSequence key, CharSequence salt) { return libc().crypt(key, salt); } public byte[] crypt(byte[] key, byte[] salt) { Pointer ptr = libc().crypt(key, salt); if (ptr == null) return null; int end = ptr.indexOf(0, (byte)0); byte[] bytes = new byte[end + 1]; ptr.get(0, bytes, 0, end); return bytes; } public int exec(String path, String... args) { handler.unimplementedError("exec unimplemented"); return -1; } public int exec(String path, String[] args, String[] envp) { handler.unimplementedError("exec unimplemented"); return -1; } public int execv(String path, String[] args) { return libc().execv(path, args); } public int execve(String path, String[] args, String[] env) { return libc().execve(path, args, env); } public FileStat fstat(FileDescriptor fileDescriptor) { FileStat stat = allocateStat(); if (fstat(fileDescriptor, stat) < 0) handler.error(Errno.valueOf(errno()), "fstat", "" + helper.getfd(fileDescriptor)); return stat; } public FileStat fstat(int fd) { FileStat stat = allocateStat(); if (fstat(fd, stat) < 0) handler.error(Errno.valueOf(errno()), "fstat", "" + fd); return stat; } public int fstat(FileDescriptor fileDescriptor, FileStat stat) { int fd = helper.getfd(fileDescriptor); return libc().fstat(fd, stat); } public int fstat(int fd, FileStat stat) { return libc().fstat(fd, stat); } public String getenv(String envName) { return libc().getenv(envName); } public int getegid() { return libc().getegid(); } public int geteuid() { return libc().geteuid(); } public int getgid() { return libc().getgid(); } public int getdtablesize() { return libc().getdtablesize(); } public String getlogin() { return libc().getlogin(); } public int getpgid() { return libc().getpgid(); } public int getpgrp() { return libc().getpgrp(); } public int getpid() { return libc().getpid(); } public int getppid() { return libc().getppid(); } public Passwd getpwent() { return libc().getpwent(); } public Passwd getpwuid(int which) { return libc().getpwuid(which); } public Passwd getpwnam(String which) { return libc().getpwnam(which); } public Group getgrent() { return libc().getgrent(); } public Group getgrgid(int which) { return libc().getgrgid(which); } public Group getgrnam(String which) { return libc().getgrnam(which); } public int setpwent() { return libc().setpwent(); } public int endpwent() { return libc().endpwent(); } public int setgrent() { return libc().setgrent(); } public int endgrent() { return libc().endgrent(); } public int getuid() { return libc().getuid(); } public int setegid(int egid) { return libc().setegid(egid); } public int seteuid(int euid) { return libc().seteuid(euid); } public int setgid(int gid) { return libc().setgid(gid); } public int getfd(FileDescriptor descriptor) { return helper.getfd(descriptor); } public int getpgid(int pid) { return libc().getpgid(pid); } public int setpgid(int pid, int pgid) { return libc().setpgid(pid, pgid); } public int setpgrp(int pid, int pgrp) { return libc().setpgrp(pid, pgrp); } public int setsid() { return libc().setsid(); } public int setuid(int uid) { return libc().setuid(uid); } public int kill(int pid, int signal) { return libc().kill(pid, signal); } public SignalHandler signal(Signal sig, final SignalHandler handler) { synchronized (signalHandlers) { SignalHandler old = signalHandlers.get(sig); long result = libc().signal(sig.intValue(), new LibC.LibCSignalHandler() { public void signal(int sig) { handler.handle(sig); } }); if (result != -1) { signalHandlers.put(sig, handler); } return old; } } public int lchmod(String filename, int mode) { try { return libc().lchmod(filename, mode); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int lchown(String filename, int user, int group) { try { return libc().lchown(filename, user, group); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int link(String oldpath, String newpath) { return libc().link(oldpath, newpath); } public FileStat lstat(String path) { FileStat stat = allocateStat(); if (lstat(path, stat) < 0) handler.error(Errno.valueOf(errno()), "lstat", path); return stat; } public int lstat(String path, FileStat stat) { return libc().lstat(path, stat); } public int mkdir(String path, int mode) { int res = libc().mkdir(path, mode); if (res < 0) { int errno = errno(); handler.error(Errno.valueOf(errno), "mkdir", path); } return res; } public int rmdir(String path) { int res = libc().rmdir(path); if (res < 0) handler.error(Errno.valueOf(errno()), "rmdir", path); return res; } public int setenv(String envName, String envValue, int overwrite) { return libc().setenv(envName, envValue, overwrite); } public FileStat stat(String path) { FileStat stat = allocateStat(); if (stat(path, stat) < 0) handler.error(Errno.valueOf(errno()), "stat", path); return stat; } public int stat(String path, FileStat stat) { return libc().stat(path, stat); } public int symlink(String oldpath, String newpath) { return libc().symlink(oldpath, newpath); } public String readlink(String oldpath) throws IOException { // TODO: this should not be hardcoded to 256 bytes ByteBuffer buffer = ByteBuffer.allocate(256); int result = libc().readlink(oldpath, buffer, buffer.capacity()); if (result == -1) return null; buffer.position(0); buffer.limit(result); return Charset.forName("ASCII").decode(buffer).toString(); } public int unsetenv(String envName) { return libc().unsetenv(envName); } public int umask(int mask) { return libc().umask(mask); } public int utimes(String path, long[] atimeval, long[] mtimeval) { Timeval[] times = null; if (atimeval != null && mtimeval != null) { times = Struct.arrayOf(getRuntime(), DefaultNativeTimeval.class, 2); times[0].setTime(atimeval); times[1].setTime(mtimeval); } return libc().utimes(path, times); } public int futimes(int fd, long[] atimeval, long[] mtimeval) { Timeval[] times = null; if (atimeval != null && mtimeval != null) { times = Struct.arrayOf(getRuntime(), DefaultNativeTimeval.class, 2); times[0].setTime(atimeval); times[1].setTime(mtimeval); } return libc().futimes(fd, times); } public int fork() { return libc().fork(); } public int waitpid(int pid, int[] status, int flags) { return waitpid((long)pid, status, flags); } public int waitpid(long pid, int[] status, int flags) { return libc().waitpid(pid, status, flags); } public int wait(int[] status) { return libc().wait(status); } public int getpriority(int which, int who) { return libc().getpriority(which, who); } public int setpriority(int which, int who, int prio) { return libc().setpriority(which, who, prio); } public boolean isatty(FileDescriptor fd) { return libc().isatty(helper.getfd(fd)) != 0; } public int errno() { return LastError.getLastError(getRuntime()); } public void errno(int value) { LastError.setLastError(getRuntime(), value); } public int chdir(String path) { return libc().chdir(path); } public boolean isNative() { return true; } public long posix_spawnp(String path, Collection fileActions, CharSequence[] argv, CharSequence[] envp) { return posix_spawnp(path, fileActions, null, argv, envp); } public long posix_spawnp(String path, Collection fileActions, Collection argv, Collection envp) { return posix_spawnp(path, fileActions, null, argv, envp); } public long posix_spawnp(String path, Collection fileActions, Collection spawnAttributes, Collection argv, Collection envp) { CharSequence[] nativeArgv = new CharSequence[argv.size()]; argv.toArray(nativeArgv); CharSequence[] nativeEnv = new CharSequence[envp.size()]; envp.toArray(nativeEnv); return posix_spawnp(path, fileActions, spawnAttributes, nativeArgv, nativeEnv); } public long posix_spawnp(String path, Collection fileActions, Collection spawnAttributes, CharSequence[] argv, CharSequence[] envp) { // AbstractNumberReference pid = getRuntime().findType(TypeAlias.pid_t).size() == 4 // ? new IntByReference(-1) : new LongLongByReference(-1); NumberByReference pid = new NumberByReference(TypeAlias.pid_t); Pointer nativeFileActions = fileActions != null && !fileActions.isEmpty() ? nativeFileActions(fileActions) : null; Pointer nativeSpawnAttributes = spawnAttributes != null && !spawnAttributes.isEmpty() ? nativeSpawnAttributes(spawnAttributes) : null; long result; try { result = ((UnixLibC) libc()).posix_spawnp(pid, path, nativeFileActions, nativeSpawnAttributes, argv, envp); } finally { if (nativeFileActions != null) ((UnixLibC) libc()).posix_spawn_file_actions_destroy(nativeFileActions); if (nativeSpawnAttributes != null) ((UnixLibC) libc()).posix_spawnattr_destroy(nativeSpawnAttributes); } if (result != 0) return -1; // result will be errno, but we can't indicate error because we return pid return pid.longValue(); } public int flock(int fd, int mode) { return libc().flock(fd, mode); } public int dup(int fd) { return libc().dup(fd); } public int dup2(int oldFd, int newFd) { return libc().dup2(oldFd, newFd); } public int fcntlInt(int fd, Fcntl fcntl, int arg) { return libc().fcntl(fd, fcntl.intValue(), arg); } public int fcntl(int fd, Fcntl fcntl) { return libc().fcntl(fd, fcntl.intValue()); } public int fcntl(int fd, Fcntl fcntl, int... arg) { return libc().fcntl(fd, fcntl.intValue()); } public int close(int fd) { return libc().close(fd); } private Pointer nativeFileActions(Collection fileActions) { Pointer nativeFileActions = Memory.allocateDirect(getRuntime(), 128); ((UnixLibC) libc()).posix_spawn_file_actions_init(nativeFileActions); for (SpawnFileAction action : fileActions) { action.act(this, nativeFileActions); } return nativeFileActions; } private Pointer nativeSpawnAttributes(Collection spawnAttributes) { Pointer nativeSpawnAttributes = Memory.allocateDirect(getRuntime(), 128); ((UnixLibC) libc()).posix_spawnattr_init(nativeSpawnAttributes); for (SpawnAttribute action : spawnAttributes) { action.set(this, nativeSpawnAttributes); } return nativeSpawnAttributes; } public abstract FileStat allocateStat(); public long sysconf(Sysconf name) { switch (name) { case _SC_CLK_TCK: return JavaTimes.HZ; default: errno(Errno.EOPNOTSUPP.intValue()); return -1; } } public Times times() { return new JavaTimes(); } public int unlink(CharSequence path) { return libc().unlink(path); } public int open(CharSequence path, int flags, int perm) { return libc().open(path, flags, perm); } public int read(int fd, byte[] buf, int n) { return libc().read(fd, buf, n); } public int write(int fd, byte[] buf, int n) { return libc().write(fd, buf, n); } public int read(int fd, ByteBuffer buf, int n) { return libc().read(fd, buf, n); } public int write(int fd, ByteBuffer buf, int n) { return libc().write(fd, buf, n); } public int pread(int fd, byte[] buf, int n, int offset) { return libc().pread(fd, buf, n, offset); } public int pwrite(int fd, byte[] buf, int n, int offset) { return libc().pwrite(fd, buf, n, offset); } public int pread(int fd, ByteBuffer buf, int n, int offset) { return libc().pread(fd, buf, n, offset); } public int pwrite(int fd, ByteBuffer buf, int n, int offset) { return libc().pwrite(fd, buf, n, offset); } public int lseek(int fd, long offset, int whence) { return libc().lseek(fd, offset, whence); } public int pipe(int[] fds) { return libc().pipe(fds); } public int socketpair(int domain, int type, int protocol, int[] fds) { return libc().socketpair(domain, type, protocol, fds); } public int sendmsg(int socket, MsgHdr message, int flags) { return libc().sendmsg( socket, message, flags ); } public int recvmsg(int socket, MsgHdr message, int flags) { return libc().recvmsg(socket, message, flags); } public int ftruncate(int fd, long offset) { return libc().ftruncate(fd, offset); } public String getcwd() { byte[] cwd = new byte[1024]; long result = libc().getcwd(cwd, 1024); if (result == -1) return null; int len = 0; for (; len < 1024; len++) if (cwd[len] == 0) break; return new String(cwd, 0, len); } public int fsync(int fd) { return libc().fsync(fd); } public int fdatasync(int fd) { return libc().fdatasync(fd); } public static abstract class PointerConverter implements FromNativeConverter { public Class nativeType() { return Pointer.class; } } public static final PointerConverter GROUP = new PointerConverter() { public Object fromNative(Object arg, FromNativeContext ctx) { return arg != null ? new DefaultNativeGroup((Pointer) arg) : null; } }; public static final ToNativeConverter FileStatConverter = new ToNativeConverter() { public Pointer toNative(FileStat value, ToNativeContext context) { if (value instanceof BaseFileStat) { return ((BaseFileStat) value).memory; } else if (value instanceof Struct) { return Struct.getMemory((Struct) value); } else if (value == null) { return null; } throw new IllegalArgumentException("instance of " + value.getClass() + " is not a struct"); } public Class nativeType() { return Pointer.class; } }; public static final ToNativeConverter TimesConverter = new ToNativeConverter() { public Pointer toNative(NativeTimes value, ToNativeContext context) { return value.memory; } public Class nativeType() { return Pointer.class; } }; public static final ToNativeConverter ConstantConverter = new ToNativeConverter() { public Integer toNative(Constant value, ToNativeContext context) { return value.intValue(); } public Class nativeType() { return Integer.class; } }; public static final ToNativeConverter MsgHdrConverter = new ToNativeConverter() { public Pointer toNative(MsgHdr value, ToNativeContext context) { if ( value instanceof BaseMsgHdr ) { return ((BaseMsgHdr) value).memory; } else if ( value instanceof Struct ) { return Struct.getMemory((Struct) value); } else if ( value == null ) { return null; } throw new IllegalArgumentException("instance of " + value.getClass() + " is not a struct"); } public Class nativeType() { return Pointer.class; } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/CheckedPOSIX.java000066400000000000000000000443071252174056300231460ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Fcntl; import jnr.constants.platform.Signal; import jnr.constants.platform.Sysconf; import jnr.posix.util.MethodName; import jnr.posix.util.ProcessMaker; import java.io.FileDescriptor; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Collection; final class CheckedPOSIX implements POSIX { private final POSIX posix; private final POSIXHandler handler; CheckedPOSIX(POSIX posix, POSIXHandler handler) { this.posix = posix; this.handler = handler; } private T unimplementedNull() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } private int unimplementedInt() { handler.unimplementedError(MethodName.getCallerMethodName()); return -1; } private boolean unimplementedBool() { handler.unimplementedError(MethodName.getCallerMethodName()); return false; } private String unimplementedString() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public ProcessMaker newProcessMaker(String... command) { try { return posix.newProcessMaker(command); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public ProcessMaker newProcessMaker() { try { return posix.newProcessMaker(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public FileStat allocateStat() { try { return posix.allocateStat(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public MsgHdr allocateMsgHdr() { try { return posix.allocateMsgHdr(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int chdir(String path) { try { return posix.chdir(path); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int chmod(String filename, int mode) { try { return posix.chmod(filename, mode); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int fchmod(int fd, int mode) { try { return posix.fchmod(fd, mode); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int chown(String filename, int user, int group) { try { return posix.chown(filename, user, group); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public CharSequence crypt(CharSequence key, CharSequence salt) { try { return posix.crypt(key, salt); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public byte[] crypt(byte[] key, byte[] salt) { try { return posix.crypt(key, salt); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int fchown(int fd, int user, int group) { try { return posix.fchown(fd, user, group); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int endgrent() { try { return posix.endgrent(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int endpwent() { try { return posix.endpwent(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int errno() { return posix.errno(); } public void errno(int value) { posix.errno(value); } public int exec(String path, String... args) { try { return posix.exec(path, args); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int exec(String path, String[] args, String[] envp) { try { return posix.exec(path, args, envp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int execv(String path, String[] argv) { try { return posix.execv(path, argv); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int execve(String path, String[] argv, String[] envp) { try { return posix.execve(path, argv, envp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int fork() { try { return posix.fork(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public FileStat fstat(int fd) { try { return posix.fstat(fd); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int fstat(int fd, FileStat stat) { try { return posix.fstat(fd, stat); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public FileStat fstat(FileDescriptor descriptor) { try { return posix.fstat(descriptor); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int fstat(FileDescriptor descriptor, FileStat stat) { try { return posix.fstat(descriptor, stat); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int getegid() { try { return posix.getegid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int geteuid() { try { return posix.geteuid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int getgid() { try { return posix.getgid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int getdtablesize() { try { return posix.getdtablesize(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public Group getgrent() { try { return posix.getgrent(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public Group getgrgid(int which) { try { return posix.getgrgid(which); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public Group getgrnam(String which) { try { return posix.getgrnam(which); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public String getlogin() { try { return posix.getlogin(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int getpgid() { try { return posix.getpgid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int getpgid(int pid) { try { return posix.getpgid(pid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int getpgrp() { try { return posix.getpgrp(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int getpid() { try { return posix.getpid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int getppid() { try { return posix.getppid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int getpriority(int which, int who) { try { return posix.getpriority(which, who); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public Passwd getpwent() { try { return posix.getpwent(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public Passwd getpwnam(String which) { try { return posix.getpwnam(which); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public Passwd getpwuid(int which) { try { return posix.getpwuid(which); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int getuid() { try { return posix.getuid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public boolean isatty(FileDescriptor descriptor) { try { return posix.isatty(descriptor); } catch (UnsatisfiedLinkError ule) { return unimplementedBool(); } } public int kill(int pid, int signal) { try { return posix.kill(pid, signal); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public SignalHandler signal(Signal sig, SignalHandler handler) { try { return posix.signal(sig, handler); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int lchmod(String filename, int mode) { try { return posix.lchmod(filename, mode); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int lchown(String filename, int user, int group) { try { return posix.lchown(filename, user, group); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int link(String oldpath, String newpath) { try { return posix.link(oldpath, newpath); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public FileStat lstat(String path) { try { return posix.lstat(path); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int lstat(String path, FileStat stat) { try { return posix.lstat(path, stat); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int mkdir(String path, int mode) { try { return posix.mkdir(path, mode); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public String readlink(String path) throws IOException { try { return posix.readlink(path); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int rmdir(String path) { try { return posix.rmdir(path); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setegid(int egid) { try { return posix.setegid(egid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int seteuid(int euid) { try { return posix.seteuid(euid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setgid(int gid) { try { return posix.setgid(gid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setgrent() { try { return posix.setgrent(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setpgid(int pid, int pgid) { try { return posix.setpgid(pid, pgid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setpgrp(int pid, int pgrp) { try { return posix.setpgrp(pid, pgrp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setpriority(int which, int who, int prio) { try { return posix.setpriority(which, who, prio); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setpwent() { try { return posix.setpwent(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setsid() { try { return posix.setsid(); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int setuid(int uid) { try { return posix.setuid(uid); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public FileStat stat(String path) { try { return posix.stat(path); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int stat(String path, FileStat stat) { try { return posix.stat(path, stat); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int symlink(String oldpath, String newpath) { try { return posix.symlink(oldpath, newpath); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int umask(int mask) { try { return posix.umask(mask); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int utimes(String path, long[] atimeval, long[] mtimeval) { try { return posix.utimes(path, atimeval, mtimeval); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int futimes(int fd, long[] atimeval, long[] mtimeval) { try { return posix.futimes(fd, atimeval, mtimeval); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int wait(int[] status) { try { return posix.wait(status); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int waitpid(int pid, int[] status, int flags) { return waitpid((long)pid, status, flags); } public int waitpid(long pid, int[] status, int flags) { try { return posix.waitpid(pid, status, flags); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public boolean isNative() { return posix.isNative(); } public LibC libc() { return posix.libc(); } public String getenv(String envName) { try { return posix.getenv(envName); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int setenv(String envName, String envValue, int overwrite) { try { return posix.setenv(envName, envValue, overwrite); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int unsetenv(String envName) { try { return posix.unsetenv(envName); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public long posix_spawnp(String path, Collection fileActions, Collection argv, Collection envp) { try { return posix.posix_spawnp(path, fileActions, argv, envp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public long posix_spawnp(String path, Collection fileActions, Collection spawnAttributes, Collection argv, Collection envp) { try { return posix.posix_spawnp(path, fileActions, spawnAttributes, argv, envp); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public long sysconf(Sysconf name) { try { return posix.sysconf(name); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public Times times() { try { return posix.times(); } catch (UnsatisfiedLinkError ule) { return unimplementedNull(); } } public int flock(int fd, int mode) { return posix.flock(fd, mode); } public int dup(int fd) { try { return posix.dup(fd); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int dup2(int oldFd, int newFd) { try { return posix.dup2(oldFd, newFd); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int fcntlInt(int fd, Fcntl fcntlConst, int arg) { try { return posix.fcntlInt(fd, fcntlConst, arg); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int fcntl(int fd, Fcntl fcntlConst) { try { return posix.fcntl(fd, fcntlConst); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int fcntl(int fd, Fcntl fcntlConst, int... arg) { try { return posix.fcntl(fd, fcntlConst); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int close(int fd) { try { return posix.close(fd); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int unlink(CharSequence path) { try { return posix.unlink(path); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int open(CharSequence path, int flags, int perm) { try { return posix.open(path, flags, perm); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int read(int fd, byte[] buf, int n) { try { return posix.read(fd, buf, n); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int write(int fd, byte[] buf, int n) { try { return posix.write(fd, buf, n); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int read(int fd, ByteBuffer buf, int n) { try { return posix.read(fd, buf, n); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int write(int fd, ByteBuffer buf, int n) { try { return posix.write(fd, buf, n); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int pread(int fd, byte[] buf, int n, int offset) { try { return posix.pread(fd, buf, n, offset); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int pwrite(int fd, byte[] buf, int n, int offset) { try { return posix.pwrite(fd, buf, n, offset); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int pread(int fd, ByteBuffer buf, int n, int offset) { try { return posix.pread(fd, buf, n, offset); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int pwrite(int fd, ByteBuffer buf, int n, int offset) { try { return posix.pwrite(fd, buf, n, offset); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int lseek(int fd, long offset, int whence) { try { return posix.lseek(fd, offset, whence); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int pipe(int[] fds) { try {return posix.pipe(fds); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int socketpair(int domain, int type, int protocol, int[] fds) { try {return posix.socketpair(domain, type, protocol, fds); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int sendmsg(int socket, MsgHdr message, int flags) { try {return posix.sendmsg(socket, message, flags); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int recvmsg(int socket, MsgHdr message, int flags) { try {return posix.recvmsg(socket, message, flags); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int ftruncate(int fd, long offset) { try {return posix.ftruncate(fd, offset); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public String getcwd() { try {return posix.getcwd(); } catch (UnsatisfiedLinkError ule) { return unimplementedString(); } } public int fsync(int fd) { try {return posix.fsync(fd); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } public int fdatasync(int fd) { try {return posix.fsync(fd); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); } } } jnr-posix-3.0.12/src/main/java/jnr/posix/CmsgHdr.java000066400000000000000000000004471252174056300223210ustar00rootroot00000000000000package jnr.posix; import java.nio.ByteBuffer; /** * @author Bob McWhirter */ public interface CmsgHdr { void setLevel(int level); int getLevel(); void setType(int type); int getType(); void setData(ByteBuffer data); ByteBuffer getData(); int getLen(); } jnr-posix-3.0.12/src/main/java/jnr/posix/DefaultNativeGroup.java000066400000000000000000000060721252174056300245420ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ package jnr.posix; import jnr.ffi.*; import java.util.ArrayList; import java.util.List; /** * The default native group layout. * *

* This implementation should work on Solaris, Linux and MacOS. *

*/ public final class DefaultNativeGroup extends NativeGroup implements Group { static final class Layout extends StructLayout { public Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final UTF8StringRef gr_name = new UTF8StringRef(); // name public final UTF8StringRef gr_passwd = new UTF8StringRef(); // group password (encrypted) public final Signed32 gr_gid = new Signed32(); // group id public final Pointer gr_mem = new Pointer(); } static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); private final Pointer memory; DefaultNativeGroup(jnr.ffi.Pointer memory) { super(memory.getRuntime(), layout); this.memory = memory; } public java.lang.String getName() { return layout.gr_name.get(memory); } public java.lang.String getPassword() { return layout.gr_passwd.get(memory); } public long getGID() { return layout.gr_gid.get(memory); } public java.lang.String[] getMembers() { List lst = new ArrayList(); jnr.ffi.Pointer ptr = layout.gr_mem.get(memory); Pointer member; int ptrSize = runtime.addressSize(); for (int i = 0; (member = ptr.getPointer(i)) != null; i += ptrSize) { lst.add(member.getString(0)); } return lst.toArray(new java.lang.String[lst.size()]); } } jnr-posix-3.0.12/src/main/java/jnr/posix/DefaultNativeTimeval.java000066400000000000000000000006571252174056300250520ustar00rootroot00000000000000package jnr.posix; public final class DefaultNativeTimeval extends Timeval { public final SignedLong tv_sec = new SignedLong(); public final SignedLong tv_usec = new SignedLong(); public DefaultNativeTimeval(jnr.ffi.Runtime runtime) { super(runtime); } public void setTime(long[] timeval) { assert timeval.length == 2; tv_sec.set(timeval[0]); tv_usec.set(timeval[1]); } } jnr-posix-3.0.12/src/main/java/jnr/posix/FileStat.java000066400000000000000000000056511252174056300225070ustar00rootroot00000000000000package jnr.posix; public interface FileStat { public static final int S_IFIFO = 0010000; // named pipe (fifo) public static final int S_IFCHR = 0020000; // character special public static final int S_IFDIR = 0040000; // directory public static final int S_IFBLK = 0060000; // block special public static final int S_IFREG = 0100000; // regular public static final int S_IFLNK = 0120000; // symbolic link public static final int S_IFSOCK = 0140000; // socket public static final int S_IFMT = 0170000; // file mask for type checks public static final int S_ISUID = 0004000; // set user id on execution public static final int S_ISGID = 0002000; // set group id on execution public static final int S_ISVTX = 0001000; // save swapped text even after use public static final int S_IRUSR = 0000400; // read permission, owner public static final int S_IWUSR = 0000200; // write permission, owner public static final int S_IXUSR = 0000100; // execute/search permission, owner public static final int S_IRGRP = 0000040; // read permission, group public static final int S_IWGRP = 0000020; // write permission, group public static final int S_IXGRP = 0000010; // execute/search permission, group public static final int S_IROTH = 0000004; // read permission, other public static final int S_IWOTH = 0000002; // write permission, other public static final int S_IXOTH = 0000001; // execute permission, other public static final int ALL_READ = S_IRUSR | S_IRGRP | S_IROTH; public static final int ALL_WRITE = S_IWUSR | S_IWGRP | S_IWOTH; public static final int S_IXUGO = S_IXUSR | S_IXGRP | S_IXOTH; public long atime(); public long blocks(); public long blockSize(); public long ctime(); public long dev(); public String ftype(); public int gid(); public boolean groupMember(int gid); public long ino(); public boolean isBlockDev(); public boolean isCharDev(); public boolean isDirectory(); public boolean isEmpty(); public boolean isExecutable(); public boolean isExecutableReal(); public boolean isFifo(); public boolean isFile(); public boolean isGroupOwned(); public boolean isIdentical(FileStat other); public boolean isNamedPipe(); public boolean isOwned(); public boolean isROwned(); public boolean isReadable(); public boolean isReadableReal(); public boolean isWritable(); public boolean isWritableReal(); public boolean isSetgid(); public boolean isSetuid(); public boolean isSocket(); public boolean isSticky(); public boolean isSymlink(); public int major(long dev); public int minor(long dev); public int mode(); public long mtime(); public int nlink(); public long rdev(); /** * Note: Name 'st_size' since Structure has a 'size' method already */ public long st_size(); public int uid(); } jnr-posix-3.0.12/src/main/java/jnr/posix/FileTime.java000066400000000000000000000004351252174056300224650ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Struct; public class FileTime extends Struct { public final Unsigned32 dwLowDateTime = new Unsigned32(); public final Unsigned32 dwHighDateTime = new Unsigned32(); FileTime(jnr.ffi.Runtime runtime) { super(runtime); } } jnr-posix-3.0.12/src/main/java/jnr/posix/FreeBSDFileStat.java000066400000000000000000000103551252174056300236370ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.StructLayout; public final class FreeBSDFileStat extends BaseFileStat { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final class time_t extends SignedLong {} public final class dev_t extends Signed32 {} public final dev_t st_dev = new dev_t(); public final Signed32 st_ino = new Signed32(); public final Signed16 st_mode = new Signed16(); public final Signed16 st_nlink = new Signed16(); public final Signed32 st_uid = new Signed32(); public final Signed32 st_gid = new Signed32(); public final dev_t st_rdev = new dev_t(); public final time_t st_atime = new time_t(); public final SignedLong st_atimensec = new SignedLong(); public final time_t st_mtime = new time_t(); public final SignedLong st_mtimensec = new SignedLong(); public final time_t st_ctime = new time_t(); public final SignedLong st_ctimensec = new SignedLong(); public final Signed64 st_size = new Signed64(); public final Signed64 st_blocks = new Signed64(); public final Signed32 st_blksize = new Signed32(); public final Signed32 st_flags = new Signed32(); public final Signed32 st_gen = new Signed32(); public final Signed32 st_lspare = new Signed32(); public final time_t st_birthtime = new time_t(); public final SignedLong st_birthtimensec = new SignedLong(); /* FIXME: This padding isn't quite correct */ public final Signed64 st_qspare0 = new Signed64(); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public FreeBSDFileStat(NativePOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atime.get(memory); } public long blocks() { return layout.st_blocks.get(memory); } public long blockSize() { return layout.st_blksize.get(memory); } public long ctime() { return layout.st_ctime.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return layout.st_mode.get(memory) & 0xffff; } public long mtime() { return layout.st_mtime.get(memory); } public int nlink() { return layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout.st_size.get(memory); } public int uid() { return layout.st_uid.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/FreeBSDPOSIX.java000066400000000000000000000047351252174056300230330ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.constants.platform.Sysconf; import jnr.ffi.mapper.FromNativeContext; import jnr.ffi.Pointer; import jnr.posix.util.MethodName; final class FreeBSDPOSIX extends BaseNativePOSIX { FreeBSDPOSIX(LibCProvider libc, POSIXHandler handler) { super(libc, handler); } public FileStat allocateStat() { return new FreeBSDFileStat(this); } public MsgHdr allocateMsgHdr() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public SocketMacros socketMacros() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public long sysconf(Sysconf name) { return libc().sysconf(name); } public Times times() { return NativeTimes.times(this); } public static final PointerConverter PASSWD = new PointerConverter() { public Object fromNative(Object arg, FromNativeContext ctx) { return arg != null ? new FreeBSDPasswd((Pointer) arg) : null; } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/FreeBSDPasswd.java000066400000000000000000000072041252174056300233640ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.StructLayout; public class FreeBSDPasswd extends NativePasswd implements Passwd { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final UTF8StringRef pw_name = new UTF8StringRef(); // user name public final UTF8StringRef pw_passwd = new UTF8StringRef(); // password (encrypted) public final Signed32 pw_uid = new Signed32(); // user id public final Signed32 pw_gid = new Signed32(); // user id public final SignedLong pw_change = new SignedLong();// password change time public final UTF8StringRef pw_class = new UTF8StringRef(); // user access class public final UTF8StringRef pw_gecos = new UTF8StringRef(); // login info public final UTF8StringRef pw_dir = new UTF8StringRef(); // home directory public final UTF8StringRef pw_shell = new UTF8StringRef(); // default shell public final SignedLong pw_expire = new SignedLong(); // account expiration public final Signed32 pw_fields = new Signed32(); // internal: fields filled in } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); FreeBSDPasswd(jnr.ffi.Pointer memory) { super(memory); } public java.lang.String getAccessClass() { return layout.pw_class.get(memory); } public java.lang.String getGECOS() { return layout.pw_gecos.get(memory); } public long getGID() { return layout.pw_gid.get(memory); } public java.lang.String getHome() { return layout.pw_dir.get(memory); } public java.lang.String getLoginName() { return layout.pw_name.get(memory); } public int getPasswdChangeTime() { return layout.pw_change.intValue(memory); } public java.lang.String getPassword() { return layout.pw_passwd.get(memory); } public java.lang.String getShell() { return layout.pw_shell.get(memory); } public long getUID() { return layout.pw_uid.get(memory); } public int getExpire() { return layout.pw_expire.intValue(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/Group.java000066400000000000000000000002511252174056300220570ustar00rootroot00000000000000package jnr.posix; public interface Group { public String getName(); public String getPassword(); public long getGID(); public String[] getMembers(); } jnr-posix-3.0.12/src/main/java/jnr/posix/HANDLE.java000066400000000000000000000024761252174056300217310ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; import jnr.ffi.mapper.DataConverter; import jnr.ffi.mapper.FromNativeContext; import jnr.ffi.mapper.ToNativeContext; public final class HANDLE { public static final long INVALID_HANDLE_VALUE = -1L; private final Pointer pointer; public HANDLE(Pointer pointer) { this.pointer = pointer; } public final Pointer toPointer() { return pointer; } public final boolean isValid() { return pointer.address() != (INVALID_HANDLE_VALUE & jnr.ffi.Runtime.getSystemRuntime().addressMask()); } public static HANDLE valueOf(Pointer value) { return new HANDLE(value); } public static HANDLE valueOf(long value) { return new HANDLE(jnr.ffi.Runtime.getSystemRuntime().getMemoryManager().newPointer(value)); } public static final DataConverter Converter = new DataConverter() { public Pointer toNative(HANDLE value, ToNativeContext context) { return value != null ? value.pointer : null; } public HANDLE fromNative(Pointer nativeValue, FromNativeContext context) { return nativeValue != null ? new HANDLE(nativeValue) : null; } public Class nativeType() { return Pointer.class; } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/Iovec.java000066400000000000000000000002421252174056300220300ustar00rootroot00000000000000package jnr.posix; import java.nio.ByteBuffer; /** * @author Bob McWhirter */ public interface Iovec { ByteBuffer get(); void set(ByteBuffer buf); } jnr-posix-3.0.12/src/main/java/jnr/posix/JavaFileStat.java000066400000000000000000000202421252174056300233020ustar00rootroot00000000000000package jnr.posix; import java.io.File; import java.io.IOException; import jnr.posix.POSIXHandler.WARNING_ID; public final class JavaFileStat implements FileStat { private final POSIXHandler handler; private final POSIX posix; short st_mode; int st_blksize; long st_size; int st_ctime; int st_mtime; public JavaFileStat(POSIX posix, POSIXHandler handler) { this.handler = handler; this.posix = posix; } public void setup(String path) { // ENEBO: This was originally JRubyFile, but I believe we use JRuby file for normalization // of paths. So we should be safe. File file = new JavaSecuredFile(path); // Limitation: We cannot determine, so always return 4096 (better than blowing up) st_blksize = 4096; st_mode = calculateMode(file, st_mode); st_size = file.length(); // Parent file last modified will only represent when something was added or removed. // This is not correct, but it is better than nothing and does work in one common use // case. st_mtime = (int) (file.lastModified() / 1000); if (file.getParentFile() != null) { st_ctime = (int) (file.getParentFile().lastModified() / 1000); } else { st_ctime = st_mtime; } } private short calculateMode(File file, short st_mode) { // implementation to lowest common denominator... // Windows has no file mode, but C ruby returns either 0100444 or 0100644 if (file.canRead()) { st_mode |= ALL_READ; } if (file.canWrite()) { st_mode |= ALL_WRITE; st_mode &= ~(S_IWGRP | S_IWOTH); } if (file.isDirectory()) { st_mode |= S_IFDIR; } else if (file.isFile()) { st_mode |= S_IFREG; } try { st_mode = calculateSymlink(file, st_mode); } catch (IOException e) { // Not sure we can do much in this case... } return st_mode; } private short calculateSymlink(File file, short st_mode) throws IOException { if (file.getAbsoluteFile().getParentFile() == null) { return st_mode; } File absoluteParent = file.getAbsoluteFile().getParentFile(); File canonicalParent = absoluteParent.getCanonicalFile(); if (canonicalParent.getAbsolutePath().equals(absoluteParent.getAbsolutePath())) { // parent doesn't change when canonicalized, compare absolute and canonical file directly if (!file.getAbsolutePath().equalsIgnoreCase(file.getCanonicalPath())) { st_mode |= S_IFLNK; return st_mode; } } // directory itself has symlinks (canonical != absolute), so build new path with canonical parent and compare file = new JavaSecuredFile(canonicalParent.getAbsolutePath() + "/" + file.getName()); if (!file.getAbsolutePath().equalsIgnoreCase(file.getCanonicalPath())) { st_mode |= S_IFLNK; } return st_mode; } /** * Limitation: Java has no access time support, so we return mtime as the next best thing. */ public long atime() { return st_mtime; } public long blocks() { handler.unimplementedError("stat.st_blocks"); return -1; } public long blockSize() { return st_blksize; } public long ctime() { return st_ctime; } public long dev() { handler.unimplementedError("stat.st_dev"); return -1; } public String ftype() { if (isFile()) { return "file"; } else if (isDirectory()) { return "directory"; } return "unknown"; } public int gid() { handler.unimplementedError("stat.st_gid"); return -1; } public boolean groupMember(int gid) { return posix.getgid() == gid || posix.getegid() == gid; } /** * Limitation: We have no pure-java way of getting inode. webrick needs this defined to work. */ public long ino() { return 0; } public boolean isBlockDev() { handler.unimplementedError("block device detection"); return false; } /** * Limitation: [see JRUBY-1516] We just pick more likely value. This is a little scary. */ public boolean isCharDev() { return false; } public boolean isDirectory() { return (mode() & S_IFDIR) != 0; } public boolean isEmpty() { return st_size() == 0; } // At least one major library depends on this method existing. public boolean isExecutable() { handler.warn(WARNING_ID.DUMMY_VALUE_USED, "executable? does not in this environment and will return a dummy value", "executable"); return true; } // At least one major library depends on this method existing. public boolean isExecutableReal() { handler.warn(WARNING_ID.DUMMY_VALUE_USED, "executable_real? does not work in this environmnt and will return a dummy value", "executable_real"); return true; } public boolean isFifo() { handler.unimplementedError("fifo file detection"); return false; } public boolean isFile() { return (mode() & S_IFREG) != 0; } public boolean isGroupOwned() { return groupMember(gid()); } public boolean isIdentical(FileStat other) { handler.unimplementedError("identical file detection"); return false; } public boolean isNamedPipe() { handler.unimplementedError("piped file detection"); return false; } public boolean isOwned() { return posix.geteuid() == uid(); } public boolean isROwned() { return posix.getuid() == uid(); } public boolean isReadable() { int mode = mode(); if ((mode & S_IRUSR) != 0) return true; if ((mode & S_IRGRP) != 0) return true; if ((mode & S_IROTH) != 0) return true; return false; } // We do both readable and readable_real through the same method because public boolean isReadableReal() { return isReadable(); } public boolean isSymlink() { return (mode() & S_IFLNK) == S_IFLNK; } public boolean isWritable() { int mode = mode(); if ((mode & S_IWUSR) != 0) return true; if ((mode & S_IWGRP) != 0) return true; if ((mode & S_IWOTH) != 0) return true; return false; } // We do both readable and readable_real through the same method because // in our java process effective and real userid will always be the same. public boolean isWritableReal() { return isWritable(); } public boolean isSetgid() { handler.unimplementedError("setgid detection"); return false; } public boolean isSetuid() { handler.unimplementedError("setuid detection"); return false; } public boolean isSocket() { handler.unimplementedError("socket file type detection"); return false; } public boolean isSticky() { handler.unimplementedError("sticky bit detection"); return false; } public int major(long dev) { handler.unimplementedError("major device"); return -1; } public int minor(long dev) { handler.unimplementedError("minor device"); return -1; } public int mode() { return st_mode & 0xffff; } public long mtime() { return st_mtime; } public int nlink() { handler.unimplementedError("stat.nlink"); return -1; } public long rdev() { handler.unimplementedError("stat.rdev"); return -1; } public long st_size() { return st_size; } // Limitation: We have no pure-java way of getting uid. RubyZip needs this defined to work. public int uid() { return -1; } } jnr-posix-3.0.12/src/main/java/jnr/posix/JavaLibCHelper.java000066400000000000000000000354301252174056300235450ustar00rootroot00000000000000 /* **** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * Copyright (C) 2007 * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ package jnr.posix; import static jnr.constants.platform.Errno.*; import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.channels.Channel; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Pattern; import jnr.constants.platform.Errno; import jnr.posix.util.Chmod; import jnr.posix.util.ExecIt; import jnr.posix.util.FieldAccess; import jnr.posix.util.JavaCrypt; import jnr.posix.util.Platform; /** * This libc implementation is created one per runtime instance versus the others which * are expected to be one static instance for whole JVM. Because of this it is no big * deal to make reference to a POSIXHandler directly. */ // FIXME: we ignore all exceptions with shell launcher...should we do something better public class JavaLibCHelper { public static final int STDIN = 0; public static final int STDOUT = 1; public static final int STDERR = 2; private static final ThreadLocal errno = new ThreadLocal(); private final POSIXHandler handler; private final Field fdField, handleField; private final Map env; private static final Class SEL_CH_IMPL; private static final Method SEL_CH_IMPL_GET_FD; private static final Class FILE_CHANNEL_IMPL; private static final Field FILE_CHANNEL_IMPL_FD; private static final Field FILE_DESCRIPTOR_FD; public JavaLibCHelper(POSIXHandler handler) { this.env = new HashMap(); this.handler = handler; if (Platform.IS_WINDOWS) { // Exception generated if we are not on Windows. this.handleField = FieldAccess.getProtectedField(FileDescriptor.class, "handle"); } else { this.handleField = null; } fdField = FILE_DESCRIPTOR_FD; } static { Method getFD; Class selChImpl; try { selChImpl = Class.forName("sun.nio.ch.SelChImpl"); try { getFD = selChImpl.getMethod("getFD"); getFD.setAccessible(true); } catch (Exception e) { getFD = null; } } catch (Exception e) { selChImpl = null; getFD = null; } SEL_CH_IMPL = selChImpl; SEL_CH_IMPL_GET_FD = getFD; Field fd; Class fileChannelImpl; try { fileChannelImpl = Class.forName("sun.nio.ch.FileChannelImpl"); try { fd = fileChannelImpl.getDeclaredField("fd"); fd.setAccessible(true); } catch (Exception e) { fd = null; } } catch (Exception e) { fileChannelImpl = null; fd = null; } FILE_CHANNEL_IMPL = fileChannelImpl; FILE_CHANNEL_IMPL_FD = fd; Field ffd; try { ffd = FileDescriptor.class.getDeclaredField("fd"); ffd.setAccessible(true); } catch (Exception e) { ffd = null; } FILE_DESCRIPTOR_FD = ffd; } public static FileDescriptor getDescriptorFromChannel(Channel channel) { if (SEL_CH_IMPL_GET_FD != null && SEL_CH_IMPL.isInstance(channel)) { // Pipe Source and Sink, Sockets, and other several other selectable channels try { return (FileDescriptor)SEL_CH_IMPL_GET_FD.invoke(channel); } catch (Exception e) { // return bogus below } } else if (FILE_CHANNEL_IMPL_FD != null && FILE_CHANNEL_IMPL.isInstance(channel)) { // FileChannels try { return (FileDescriptor)FILE_CHANNEL_IMPL_FD.get(channel); } catch (Exception e) { // return bogus below } } else if (FILE_DESCRIPTOR_FD != null) { // anything else that implements a getFD method that returns an int FileDescriptor unixFD = new FileDescriptor(); try { Method getFD = channel.getClass().getMethod("getFD"); FILE_DESCRIPTOR_FD.set(unixFD, (Integer)getFD.invoke(channel)); return unixFD; } catch (Exception e) { // return bogus below } } return new FileDescriptor(); } static int errno() { Integer errno = JavaLibCHelper.errno.get(); return errno != null ? errno : 0; } static void errno(int errno) { JavaLibCHelper.errno.set(errno); } static void errno(Errno errno) { JavaLibCHelper.errno.set(errno.intValue()); } public int chmod(String filename, int mode) { return Chmod.chmod(new JavaSecuredFile(filename), Integer.toOctalString(mode)); } public int chown(String filename, int user, int group) { PosixExec launcher = new PosixExec(handler); int chownResult = -1; int chgrpResult = -1; try { if (user != -1) chownResult = launcher.runAndWait("chown", "" + user, filename); if (group != -1) chgrpResult = launcher.runAndWait("chgrp ", "" + user, filename); } catch (Exception e) {} return chownResult != -1 && chgrpResult != -1 ? 0 : 1; } public static CharSequence crypt(CharSequence original, CharSequence salt) { return JavaCrypt.crypt(original, salt); } // FIXME: This version has no idea what charset you want, so it just uses default. public static byte[] crypt(byte[] original, byte[] salt) { return JavaCrypt.crypt(new String(original), new String(salt)).toString().getBytes(); } public int getfd(FileDescriptor descriptor) { return getfdFromDescriptor(descriptor); } public static int getfdFromDescriptor(FileDescriptor descriptor) { if (descriptor == null || FILE_DESCRIPTOR_FD == null) return -1; try { return FILE_DESCRIPTOR_FD.getInt(descriptor); } catch (SecurityException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } return -1; } public HANDLE gethandle(FileDescriptor descriptor) { if (descriptor == null || handleField == null) return HANDLE.valueOf(-1); try { return HANDLE.valueOf(handleField.getLong(descriptor)); } catch (SecurityException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } return HANDLE.valueOf(-1); } public String getlogin() { return System.getProperty("user.name"); } public int getpid() { return handler.getPID(); } ThreadLocal pwIndex = new ThreadLocal() { @Override protected Integer initialValue() { return 0; } }; public Passwd getpwent() { Passwd retVal = pwIndex.get().intValue() == 0 ? new JavaPasswd(handler) : null; pwIndex.set(pwIndex.get() + 1); return retVal; } public int setpwent() { return 0; } public int endpwent() { pwIndex.set(0); return 0; } public Passwd getpwuid(int which) { return which == JavaPOSIX.LoginInfo.UID ? new JavaPasswd(handler) : null; } public int isatty(int fd) { return (fd == STDOUT || fd == STDIN || fd == STDERR) ? 1 : 0; } public int link(String oldpath, String newpath) { try { return new PosixExec(handler).runAndWait("ln", oldpath, newpath); } catch (Exception e) { errno(EINVAL); return -1; // We tried and failed for some reason. Indicate error. } } public int lstat(String path, FileStat stat) { File file = new JavaSecuredFile(path); if (!file.exists()) { errno(ENOENT); return -1; } // FIXME: Bulletproof this or no? JavaFileStat jstat = (JavaFileStat) stat; jstat.setup(path); // TODO: Add error reporting for cases we can calculate: ENOTDIR, ENAMETOOLONG, ENOENT // EACCES, ELOOP, EFAULT, EIO return 0; } public int mkdir(String path, int mode) { File dir = new JavaSecuredFile(path); if (!dir.mkdir()) return -1; chmod(path, mode); return 0; } public int rmdir(String path) { return new JavaSecuredFile(path).delete() ? 0 : -1; } public static int chdir(String path) { System.setProperty("user.dir", path); return 0; } public int stat(String path, FileStat stat) { // FIXME: Bulletproof this or no? JavaFileStat jstat = (JavaFileStat) stat; try { File file = new JavaSecuredFile(path); if (!file.exists()) return -1; jstat.setup(file.getCanonicalPath()); } catch (IOException e) { // TODO: Throw error when we have problems stat'ing canonicalizing } // TODO: Add error reporting for cases we can calculate: ENOTDIR, ENAMETOOLONG, ENOENT // EACCES, ELOOP, EFAULT, EIO return 0; } public int symlink(String oldpath, String newpath) { try { return new PosixExec(handler).runAndWait("ln", "-s", oldpath, newpath); } catch (Exception e) { errno(EEXIST); return -1; // We tried and failed for some reason. Indicate error. } } public int readlink(String oldpath, ByteBuffer buffer, int length) throws IOException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); new PosixExec(handler).runAndWait(baos, "readlink", oldpath); byte[] bytes = baos.toByteArray(); if (bytes.length > length || bytes.length == 0) return -1; buffer.put(bytes, 0, bytes.length - 1); // trim off \n return buffer.position(); } catch (InterruptedException e) { } errno(ENOENT); return -1; // We tried and failed for some reason. Indicate error. } public Map getEnv() { return env; } public static FileDescriptor toFileDescriptor(int fileDescriptor) { FileDescriptor descriptor = new FileDescriptor(); try { FILE_DESCRIPTOR_FD.set(descriptor, fileDescriptor); } catch (IllegalAccessException e) { throw new RuntimeException(e); } return descriptor; } private static class PosixExec extends ExecIt { private final AtomicReference errno = new AtomicReference(Errno.EINVAL); private final ErrnoParsingOutputStream errorStream = new ErrnoParsingOutputStream(errno); public PosixExec(POSIXHandler handler) { super(handler); } private int parseResult(int result) { if (result == 0) { return result; } errno(errno.get()); return -1; } public int runAndWait(String... args) throws IOException, InterruptedException { return runAndWait(handler.getOutputStream(), errorStream, args); } public int runAndWait(OutputStream output, String... args) throws IOException, InterruptedException { return runAndWait(output, errorStream, args); } public int runAndWait(OutputStream output, OutputStream error, String... args) throws IOException, InterruptedException { return parseResult(super.runAndWait(output, error, args)); } } private static final class ErrnoParsingOutputStream extends OutputStream { private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); private final AtomicReference errno; private ErrnoParsingOutputStream(AtomicReference errno) { this.errno = errno; } @Override public void write(int b) throws IOException { if (b != '\r' && b != '\n' && b != -1) { baos.write(b); } else if (baos.size() > 0) { String errorString = baos.toString(); baos.reset(); parseError(errorString); } } static Map errorPatterns = new HashMap(); static { errorPatterns.put(Pattern.compile("File exists"), Errno.EEXIST); errorPatterns.put(Pattern.compile("Operation not permitted"), Errno.EPERM); errorPatterns.put(Pattern.compile("No such file or directory"), Errno.ENOENT); errorPatterns.put(Pattern.compile("Input/output error"), Errno.EIO); errorPatterns.put(Pattern.compile("Not a directory"), Errno.ENOTDIR); errorPatterns.put(Pattern.compile("No space left on device"), Errno.ENOSPC); errorPatterns.put(Pattern.compile("Read-only file system"), Errno.EROFS); errorPatterns.put(Pattern.compile("Too many links"), Errno.EMLINK); } void parseError(String errorString) { for (Map.Entry entry : errorPatterns.entrySet()) { if (entry.getKey().matcher(errorString).find()) { errno.set(entry.getValue()); } } } } } jnr-posix-3.0.12/src/main/java/jnr/posix/JavaPOSIX.java000066400000000000000000000425501252174056300224770ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Errno; import jnr.constants.platform.Fcntl; import jnr.constants.platform.Sysconf; import jnr.posix.util.Java5ProcessMaker; import jnr.posix.util.MethodName; import jnr.posix.util.Platform; import jnr.posix.util.ProcessMaker; import sun.misc.Signal; import java.io.*; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.Collection; import java.util.Map; import static jnr.constants.platform.Errno.EINVAL; import static jnr.constants.platform.Errno.ENOENT; final class JavaPOSIX implements POSIX { private final POSIXHandler handler; private final JavaLibCHelper helper; JavaPOSIX(POSIXHandler handler) { this.handler = handler; this.helper = new JavaLibCHelper(handler); } public ProcessMaker newProcessMaker(String... command) { return new Java5ProcessMaker(handler, command); } public ProcessMaker newProcessMaker() { return new Java5ProcessMaker(handler); } public FileStat allocateStat() { return new JavaFileStat(this, handler); } public MsgHdr allocateMsgHdr() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public SocketMacros socketMacros() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public int chmod(String filename, int mode) { return helper.chmod(filename, mode); } public int fchmod(int fd, int mode) { handler.unimplementedError("No fchmod in Java (yet)"); return -1; } public int chown(String filename, int user, int group) { return helper.chown(filename, user, group); } public int fchown(int fd, int user, int group) { handler.unimplementedError("No fchown in Java (yet)"); return -1; } public CharSequence crypt(CharSequence key, CharSequence salt) { return helper.crypt(key, salt); } public byte[] crypt(byte[] key, byte[] salt) { return helper.crypt(key, salt); } public int exec(String path, String... argv) { handler.unimplementedError("No exec in Java (yet)"); return -1; } public int exec(String path, String[] argv, String[] envp) { handler.unimplementedError("No exec in Java (yet)"); return -1; } public int execv(String path, String[] argv) { handler.unimplementedError("No execv in Java (yet)"); return -1; } public int execve(String path, String[] argv, String[] envp) { handler.unimplementedError("No execve in Java (yet)"); return -1; } public FileStat fstat(FileDescriptor descriptor) { handler.unimplementedError("fstat unimplemented"); return null; } public FileStat fstat(int descriptor) { handler.unimplementedError("fstat unimplemented"); return null; } public int fstat(int fd, FileStat stat) { handler.unimplementedError("fstat unimplemented"); return -1; } public int fstat(FileDescriptor descriptor, FileStat stat) { handler.unimplementedError("fstat unimplemented"); return -1; } public int getegid() { return LoginInfo.GID; } public int geteuid() { return LoginInfo.UID; } public int getgid() { return LoginInfo.GID; } public int getdtablesize() { handler.unimplementedError("getdtablesize unimplemented"); return -1; } public String getlogin() { return helper.getlogin(); } public int getpgid() { return unimplementedInt("getpgid"); } public int getpgrp() { return unimplementedInt("getpgrp"); } public int getpid() { return helper.getpid(); } public int getppid() { return unimplementedInt("getppid"); } public Passwd getpwent() { return helper.getpwent(); } public Passwd getpwuid(int which) { return helper.getpwuid(which); } public Group getgrgid(int which) { handler.unimplementedError("getgrgid unimplemented"); return null; } public Passwd getpwnam(String which) { handler.unimplementedError("getpwnam unimplemented"); return null; } public Group getgrnam(String which) { handler.unimplementedError("getgrnam unimplemented"); return null; } public Group getgrent() { handler.unimplementedError("getgrent unimplemented"); return null; } public int setpwent() { return helper.setpwent(); } public int endpwent() { return helper.endpwent(); } public int setgrent() { return unimplementedInt("setgrent"); } public int endgrent() { return unimplementedInt("endgrent"); } // @see setenv for more on the environment methods public String getenv(String envName) { return helper.getEnv().get(envName); } public int getuid() { return LoginInfo.UID; } public int fork() { return -1; } public boolean isatty(FileDescriptor fd) { return (fd == FileDescriptor.in || fd == FileDescriptor.out || fd == FileDescriptor.err); } public int kill(int pid, int signal) { return unimplementedInt("kill"); // FIXME: Can be implemented } private static class SunMiscSignalHandler implements sun.misc.SignalHandler { final SignalHandler handler; public SunMiscSignalHandler(SignalHandler handler) { this.handler = handler; } public void handle(Signal signal) { handler.handle(signal.getNumber()); } } public SignalHandler signal(jnr.constants.platform.Signal sig, final SignalHandler handler) { Signal s = new Signal(sig.name().substring("SIG".length())); sun.misc.SignalHandler oldHandler = Signal.handle(s, new SunMiscSignalHandler(handler)); if (oldHandler instanceof SunMiscSignalHandler) { return ((SunMiscSignalHandler)oldHandler).handler; } else { return null; } } public int lchmod(String filename, int mode) { return unimplementedInt("lchmod"); // FIXME: Can be implemented } public int lchown(String filename, int user, int group) { return unimplementedInt("lchown"); // FIXME: Can be implemented } public int link(String oldpath, String newpath) { return helper.link(oldpath, newpath); } public FileStat lstat(String path) { FileStat stat = allocateStat(); if (lstat(path, stat) < 0) handler.error(ENOENT, "lstat", path); return stat; } public int lstat(String path, FileStat stat) { return helper.lstat(path, stat); } public int mkdir(String path, int mode) { return helper.mkdir(path, mode); } public int rmdir(String path) { return helper.rmdir(path); } public String readlink(String path) throws IOException { // TODO: this should not be hardcoded to 256 bytes ByteBuffer buffer = ByteBuffer.allocateDirect(256); int result = helper.readlink(path, buffer, buffer.capacity()); if (result == -1) return null; buffer.position(0); buffer.limit(result); return Charset.forName("ASCII").decode(buffer).toString(); } // At this point the environment is not being used by any methods here. // getenv/setenv/unsetenv do behave properly via POSIX definitions, but // it is only a storage facility at the moment. In a future release, this // map will be hooked up to the methods which depend on env. public int setenv(String envName, String envValue, int overwrite) { Map env = helper.getEnv(); if (envName.contains("=")) { handler.error(EINVAL, "setenv", envName); return -1; } // POSIX specified. Existence is success if overwrite is 0. if (overwrite == 0 && env.containsKey(envName)) return 0; env.put(envName, envValue); return 0; } public FileStat stat(String path) { FileStat stat = allocateStat(); if (helper.stat(path, stat) < 0) handler.error(ENOENT, "stat", path); return stat; } public int stat(String path, FileStat stat) { return helper.stat(path, stat); } public int symlink(String oldpath, String newpath) { return helper.symlink(oldpath, newpath); } public int setegid(int egid) { return unimplementedInt("setegid"); } public int seteuid(int euid) { return unimplementedInt("seteuid"); } public int setgid(int gid) { return unimplementedInt("setgid"); } public int getpgid(int pid) { return unimplementedInt("getpgid"); } public int setpgid(int pid, int pgid) { return unimplementedInt("setpgid"); } public int setpgrp(int pid, int pgrp) { return unimplementedInt("setpgrp"); } public int setsid() { return unimplementedInt("setsid"); } public int setuid(int uid) { return unimplementedInt("setuid"); } public int umask(int mask) { // TODO: We can possibly maintain an internal mask and try and apply it to individual // libc methods. return 0; } public int unsetenv(String envName) { if (helper.getEnv().remove(envName) == null) { handler.error(EINVAL, "unsetenv", envName); return -1; } return 0; } public int utimes(String path, long[] atimeval, long[] mtimeval) { long mtimeMillis; if (mtimeval != null) { assert mtimeval.length == 2; mtimeMillis = (mtimeval[0] * 1000) + (mtimeval[1] / 1000); } else { mtimeMillis = System.currentTimeMillis(); } new File(path).setLastModified(mtimeMillis); return 0; } public int futimes(int fd, long[] atimeval, long[] mtimeval) { handler.unimplementedError("futimes"); return unimplementedInt("futimes"); } public int wait(int[] status) { return unimplementedInt("wait"); } public int waitpid(int pid, int[] status, int flags) { return unimplementedInt("waitpid"); } public int waitpid(long pid, int[] status, int flags) { return unimplementedInt("waitpid"); } public int getpriority(int which, int who) { return unimplementedInt("getpriority"); } public int setpriority(int which, int who, int prio) { return unimplementedInt("setpriority"); } public long posix_spawnp(String path, Collection fileActions, Collection argv, Collection envp) { return unimplementedInt("posix_spawnp"); } public long posix_spawnp(String path, Collection fileActions, Collection spawnAttributes, Collection argv, Collection envp) { return unimplementedInt("posix_spawnp"); } public int errno() { return JavaLibCHelper.errno(); } public void errno(int value) { JavaLibCHelper.errno(value); } public int chdir(String path) { return JavaLibCHelper.chdir(path); } public boolean isNative() { return false; } public LibC libc() { return null; } private int unimplementedInt(String message) { handler.unimplementedError(message); return -1; } public long sysconf(Sysconf name) { switch (name) { case _SC_CLK_TCK: return JavaTimes.HZ; default: errno(Errno.EOPNOTSUPP.intValue()); return -1; } } public Times times() { return new JavaTimes(); } public int flock(int fd, int mode) { return unimplementedInt("waitpid"); } public int dup(int fd) { return unimplementedInt("dup"); } public int dup2(int oldFd, int newFd) { return unimplementedInt("dup2"); } public int fcntlInt(int fd, Fcntl fcntlConst, int arg) { return unimplementedInt("fcntl"); } public int fcntl(int fd, Fcntl fcntlConst) { return unimplementedInt("fcntl"); } public int fcntl(int fd, Fcntl fcntlConst, int... arg) { return unimplementedInt("fcntl"); } public int close(int fd) { return unimplementedInt("close"); } public int unlink(CharSequence path) { handler.unimplementedError("unlink"); return -1; } public int open(CharSequence path, int flags, int perm) { handler.unimplementedError("open"); return -1; } public int read(int fd, byte[] buf, int n) { handler.unimplementedError("read"); return -1; } public int write(int fd, byte[] buf, int n) { handler.unimplementedError("write"); return -1; } public int read(int fd, ByteBuffer buf, int n) { handler.unimplementedError("read"); return -1; } public int write(int fd, ByteBuffer buf, int n) { handler.unimplementedError("write"); return -1; } public int pread(int fd, byte[] buf, int n, int offset) { handler.unimplementedError("pread"); return -1; } public int pwrite(int fd, byte[] buf, int n, int offset) { handler.unimplementedError("pwrite"); return -1; } public int pread(int fd, ByteBuffer buf, int n, int offset) { handler.unimplementedError("pread"); return -1; } public int pwrite(int fd, ByteBuffer buf, int n, int offset) { handler.unimplementedError("pwrite"); return -1; } public int lseek(int fd, long offset, int whence) { handler.unimplementedError("lseek"); return -1; } public int pipe(int[] fds) { handler.unimplementedError("pipe"); return -1; } public int socketpair(int domain, int type, int protocol, int[] fds) { handler.unimplementedError("socketpair"); return -1; } public int sendmsg(int socket, MsgHdr message, int flags) { handler.unimplementedError("sendmsg"); return -1; } public int recvmsg(int socket, MsgHdr message, int flags) { handler.unimplementedError("recvmsg"); return -1; } public int ftruncate(int fd, long offset) { handler.unimplementedError("ftruncate"); return -1; } public String getcwd() { return System.getProperty("user.dir"); } public int fsync(int fd) { handler.unimplementedError("fsync"); return unimplementedInt("fsync not available for Java"); } public int fdatasync(int fd) { handler.unimplementedError("fdatasync"); return unimplementedInt("fdatasync not available for Java"); } static final class LoginInfo { public static final int UID = IDHelper.getInt("-u"); public static final int GID = IDHelper.getInt("-g"); public static final String USERNAME = IDHelper.getString("-un"); } private static final class IDHelper { private static final String ID_CMD = Platform.IS_SOLARIS ? "/usr/xpg4/bin/id" : "/usr/bin/id"; private static final int NOBODY = Platform.IS_WINDOWS ? 0 : Short.MAX_VALUE; public static int getInt(String option) { try { Process p = Runtime.getRuntime().exec(new String[] { ID_CMD, option }); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); return Integer.parseInt(r.readLine()); } catch (IOException ex) { return NOBODY; } catch (NumberFormatException ex) { return NOBODY; } catch (SecurityException ex) { return NOBODY; } } public static String getString(String option) { try { Process p = Runtime.getRuntime().exec(new String[] { ID_CMD, option }); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); return r.readLine(); } catch (IOException ex) { return null; } } } private static final class FakePasswd implements Passwd { public String getLoginName() { return LoginInfo.USERNAME; } public String getPassword() { return ""; } public long getUID() { return LoginInfo.UID; } public long getGID() { return LoginInfo.GID; } public int getPasswdChangeTime() { return 0; } public String getAccessClass() { return ""; } public String getGECOS() { return getLoginName(); } public String getHome() { return "/"; } public String getShell() { return "/bin/sh"; } public int getExpire() { return ~0; } } } jnr-posix-3.0.12/src/main/java/jnr/posix/JavaPasswd.java000066400000000000000000000023701252174056300230320ustar00rootroot00000000000000package jnr.posix; final class JavaPasswd implements Passwd { private final POSIXHandler handler; public JavaPasswd(POSIXHandler handler) { this.handler = handler; } public String getAccessClass() { handler.unimplementedError("passwd.pw_access unimplemented"); return null; } public String getGECOS() { return getLoginName(); } public long getGID() { return JavaPOSIX.LoginInfo.GID; } public String getHome() { return System.getProperty("user.home"); } public String getLoginName() { return System.getProperty("user.name"); } public int getPasswdChangeTime() { handler.unimplementedError("passwd.pw_change unimplemented"); return 0; } public String getPassword() { handler.unimplementedError("passwd.pw_passwd unimplemented"); return null; } public String getShell() { handler.unimplementedError("passwd.pw_env unimplemented"); return null; } public long getUID() { return JavaPOSIX.LoginInfo.UID; } public int getExpire() { handler.unimplementedError("passwd.expire unimplemented"); return ~0; } } jnr-posix-3.0.12/src/main/java/jnr/posix/JavaSecuredFile.java000066400000000000000000000141611252174056300237640ustar00rootroot00000000000000/** * ** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 *

* The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html *

* Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *

*

* Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. * **** END LICENSE BLOCK **** */ package jnr.posix; import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.io.IOException; import java.net.URI; @SuppressWarnings("serial") public class JavaSecuredFile extends File { public JavaSecuredFile(String pathname) { super(pathname); } public JavaSecuredFile(String parent, String child) { super(parent, child); } public JavaSecuredFile(File parent, String child) { super(parent, child); } public JavaSecuredFile(URI uri) { super(uri); } @Override public File getParentFile() { String path = getParent(); return path == null ? null : new JavaSecuredFile(path); } @Override public File getAbsoluteFile() { String path = getAbsolutePath(); return path == null ? null : new JavaSecuredFile(path); } @Override public File getCanonicalFile() throws IOException { String path = getCanonicalPath(); return path == null ? null : new JavaSecuredFile(path); } @Override public boolean canRead() { try { return super.canRead(); } catch (SecurityException e) { return false; } } @Override public boolean canWrite() { try { return super.canWrite(); } catch (SecurityException e) { return false; } } @Override public boolean exists() { try { return super.exists(); } catch (SecurityException e) { return false; } } @Override public boolean isDirectory() { try { return super.isDirectory(); } catch (SecurityException e) { return false; } } @Override public boolean isFile() { try { return super.isFile(); } catch (SecurityException e) { return false; } } @Override public boolean isHidden() { try { return super.isHidden(); } catch (SecurityException e) { return false; } } @Override public boolean delete() { try { return super.delete(); } catch (SecurityException e) { return false; } } @Override public boolean mkdir() { try { return super.mkdir(); } catch (SecurityException e) { return false; } } @Override public boolean mkdirs() { try { return super.mkdirs(); } catch (SecurityException e) { return false; } } @Override public boolean renameTo(File dest) { try { return super.renameTo(dest); } catch (SecurityException e) { return false; } } @Override public boolean setLastModified(long time) { try { return super.setLastModified(time); } catch (SecurityException e) { return false; } } @Override public boolean setReadOnly() { try { return super.setReadOnly(); } catch (SecurityException e) { return false; } } @Override public String getCanonicalPath() throws IOException { try { return super.getCanonicalPath(); } catch (SecurityException e) { throw new IOException(e); } } @Override public boolean createNewFile() throws IOException { try { return super.createNewFile(); } catch (SecurityException e) { throw new IOException(e); } } @Override public String[] list() { try { return super.list(); } catch (SecurityException e) { return null; } } @Override public String[] list(FilenameFilter filter) { try { return super.list(filter); } catch (SecurityException e) { return null; } } @Override public File[] listFiles() { try { return super.listFiles(); } catch (SecurityException e) { return null; } } @Override public File[] listFiles(FileFilter filter) { try { return super.listFiles(filter); } catch (SecurityException e) { return null; } } @Override public long lastModified() { try { return super.lastModified(); } catch (SecurityException e) { return 0L; } } @Override public long length() { try { return super.length(); } catch (SecurityException e) { return 0L; } } }jnr-posix-3.0.12/src/main/java/jnr/posix/JavaTimes.java000066400000000000000000000006461252174056300226560ustar00rootroot00000000000000package jnr.posix; final class JavaTimes implements Times { private static final long startTime = System.currentTimeMillis(); static final long HZ = 1000; public long utime() { return Math.max(System.currentTimeMillis() - startTime, 1); } public long stime() { return 0; } public long cutime() { return 0; } public long cstime() { return 0; } } jnr-posix-3.0.12/src/main/java/jnr/posix/LazyPOSIX.java000066400000000000000000000266121252174056300225360ustar00rootroot00000000000000 package jnr.posix; import jnr.constants.platform.Fcntl; import jnr.constants.platform.Sysconf; import jnr.posix.util.ProcessMaker; import java.io.FileDescriptor; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Collection; import jnr.constants.platform.Signal; final class LazyPOSIX implements POSIX { private final POSIXHandler handler; private final boolean useNativePosix; // NOTE: because all implementations of POSIX that are loaded via loadPOSIX() // are immutable, there is no need for 'posix' to be a volatile field. // See http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html // (but since volatile reads on x86 and x86_64 are cheap, do it anyway) private volatile POSIX posix; LazyPOSIX(POSIXHandler handler, boolean useNativePosix) { this.handler = handler; this.useNativePosix = useNativePosix; } private final POSIX posix() { return posix != null ? posix : loadPOSIX(); } private final synchronized POSIX loadPOSIX() { return posix != null ? posix : (posix = POSIXFactory.loadPOSIX(handler, useNativePosix)); } public ProcessMaker newProcessMaker(String... command) { return posix().newProcessMaker(command); } public ProcessMaker newProcessMaker() { return posix().newProcessMaker(); } public FileStat allocateStat() { return posix().allocateStat(); } public MsgHdr allocateMsgHdr() { return posix().allocateMsgHdr(); } public int chdir(String path) { return posix().chdir(path); } public int chmod(String filename, int mode) { return posix().chmod(filename, mode); } public int fchmod(int fd, int mode) { return posix().fchmod(fd, mode); } public int chown(String filename, int user, int group) { return posix().chown(filename, user, group); } public CharSequence crypt(CharSequence key, CharSequence salt) { return posix().crypt(key, salt); } public byte[] crypt(byte[] key, byte[] salt) { return posix().crypt(key, salt); } public int fchown(int fd, int user, int group) { return posix().fchown(fd, user, group); } public int endgrent() { return posix().endgrent(); } public int endpwent() { return posix().endpwent(); } public int errno() { return posix().errno(); } public void errno(int value) { posix().errno(value); } public int exec(String path, String... args) { return posix().exec(path, args); } public int exec(String path, String[] args, String[] envp) { return posix().exec(path, args, envp); } public int execv(String path, String[] argv) { return posix().execv(path, argv); } public int execve(String path, String[] argv, String[] envp) { return posix().execve(path, argv, envp); } public int fork() { return posix().fork(); } public FileStat fstat(int fd) { return posix().fstat(fd); } public int fstat(int fd, FileStat stat) { return posix().fstat(fd, stat); } public FileStat fstat(FileDescriptor descriptor) { return posix().fstat(descriptor); } public int fstat(FileDescriptor descriptor, FileStat stat) { return posix().fstat(descriptor, stat); } public int getegid() { return posix().getegid(); } public int geteuid() { return posix().geteuid(); } public int getgid() { return posix().getgid(); } public int getdtablesize() { return posix().getdtablesize(); } public Group getgrent() { return posix().getgrent(); } public Group getgrgid(int which) { return posix().getgrgid(which); } public Group getgrnam(String which) { return posix().getgrnam(which); } public String getlogin() { return posix().getlogin(); } public int getpgid() { return posix().getpgid(); } public int getpgid(int pid) { return posix().getpgid(pid); } public int getpgrp() { return posix().getpgrp(); } public int getpid() { return posix().getpid(); } public int getppid() { return posix().getppid(); } public int getpriority(int which, int who) { return posix().getpriority(which, who); } public Passwd getpwent() { return posix().getpwent(); } public Passwd getpwnam(String which) { return posix().getpwnam(which); } public Passwd getpwuid(int which) { return posix().getpwuid(which); } public int getuid() { return posix().getuid(); } public boolean isatty(FileDescriptor descriptor) { return posix().isatty(descriptor); } public int kill(int pid, int signal) { return posix().kill(pid, signal); } public SignalHandler signal(Signal sig, SignalHandler handler) { return posix().signal(sig, handler); } public int lchmod(String filename, int mode) { return posix().lchmod(filename, mode); } public int lchown(String filename, int user, int group) { return posix().lchown(filename, user, group); } public int link(String oldpath, String newpath) { return posix().link(oldpath, newpath); } public FileStat lstat(String path) { return posix().lstat(path); } public int lstat(String path, FileStat stat) { return posix().lstat(path, stat); } public int mkdir(String path, int mode) { return posix().mkdir(path, mode); } public String readlink(String path) throws IOException { return posix().readlink(path); } public int rmdir(String path) { return posix().rmdir(path); } public int setegid(int egid) { return posix().setegid(egid); } public int seteuid(int euid) { return posix().seteuid(euid); } public int setgid(int gid) { return posix().setgid(gid); } public int setgrent() { return posix().setgrent(); } public int setpgid(int pid, int pgid) { return posix().setpgid(pid, pgid); } public int setpgrp(int pid, int pgrp) { return posix().setpgrp(pid, pgrp); } public int setpriority(int which, int who, int prio) { return posix().setpriority(which, who, prio); } public int setpwent() { return posix().setpwent(); } public int setsid() { return posix().setsid(); } public int setuid(int uid) { return posix().setuid(uid); } public FileStat stat(String path) { return posix().stat(path); } public int stat(String path, FileStat stat) { return posix().stat(path, stat); } public int symlink(String oldpath, String newpath) { return posix().symlink(oldpath, newpath); } public int umask(int mask) { return posix().umask(mask); } public int utimes(String path, long[] atimeval, long[] mtimeval) { return posix().utimes(path, atimeval, mtimeval); } public int futimes(int fd, long[] atimeval, long[] mtimeval) { return posix().futimes(fd, atimeval, mtimeval); } public int wait(int[] status) { return posix().wait(status); } public int waitpid(int pid, int[] status, int flags) { return waitpid((long) pid, status, flags); } public int waitpid(long pid, int[] status, int flags) { return posix().waitpid(pid, status, flags); } public boolean isNative() { return posix().isNative(); } public LibC libc() { return posix().libc(); } public String getenv(String envName) { return posix().getenv(envName); } public int setenv(String envName, String envValue, int overwrite) { return posix().setenv(envName, envValue, overwrite); } public int unsetenv(String envName) { return posix().unsetenv(envName); } public long posix_spawnp(String path, Collection fileActions, Collection argv, Collection envp) { return posix().posix_spawnp(path, fileActions, argv, envp); } public long posix_spawnp(String path, Collection fileActions, Collection spawnAttributes, Collection argv, Collection envp) { return posix().posix_spawnp(path, fileActions, spawnAttributes, argv, envp); } public long sysconf(Sysconf name) { return posix().sysconf(name); } public Times times() { return posix().times(); } public int flock(int fd, int mode) { return posix().flock(fd, mode); } public int dup(int fd) { return posix().dup(fd); } public int dup2(int oldFd, int newFd) { return posix().dup2(oldFd, newFd); } public int fcntlInt(int fd, Fcntl fcntlConst, int arg) { return posix().fcntlInt(fd, fcntlConst, arg); } public int fcntl(int fd, Fcntl fcntlConst) { return posix().fcntl(fd, fcntlConst); } public int fcntl(int fd, Fcntl fcntlConst, int... arg) { return posix().fcntl(fd, fcntlConst); } public int close(int fd) { return posix().close(fd); } public int unlink(CharSequence path) { return posix().unlink(path); } public int open(CharSequence path, int flags, int perm) { return posix().open(path, flags, perm); } public int write(int fd, byte[] buf, int n) { return posix().write(fd, buf, n); } public int read(int fd, byte[] buf, int n) { return posix().read(fd, buf, n); } public int read(int fd, ByteBuffer buf, int n) { return posix().read(fd, buf, n); } public int write(int fd, ByteBuffer buf, int n) { return posix().write(fd, buf, n); } public int pread(int fd, byte[] buf, int n, int offset) { return posix().pread(fd, buf, n, offset); } public int pwrite(int fd, byte[] buf, int n, int offset) { return posix().pwrite(fd, buf, n, offset); } public int pread(int fd, ByteBuffer buf, int n, int offset) { return posix().pread(fd, buf, n, offset); } public int pwrite(int fd, ByteBuffer buf, int n, int offset) { return posix().pwrite(fd, buf, n, offset); } public int lseek(int fd, long offset, int whence) { return posix().lseek(fd, offset, whence); } public int pipe(int[] fds) { return posix().pipe(fds); } public int socketpair(int domain, int type, int protocol, int[] fds) { return posix().socketpair(domain, type, protocol, fds); } public int sendmsg(int socket, MsgHdr message, int flags) { return posix().sendmsg( socket, message, flags ); } public int recvmsg(int socket, MsgHdr message, int flags) { return posix().recvmsg( socket, message, flags ); } public int ftruncate(int fd, long offset) { return posix().ftruncate(fd, offset); } public String getcwd() { return posix().getcwd(); } public int fsync(int fd) { return posix().fsync(fd); } public int fdatasync(int fd) { return posix().fdatasync(fd); } } jnr-posix-3.0.12/src/main/java/jnr/posix/LibC.java000066400000000000000000000133111252174056300215750ustar00rootroot00000000000000/* **** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * Copyright (C) 2007 Thomas E Enebo * Copyright (C) 2007 Charles O Nutter * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ package jnr.posix; import jnr.constants.platform.Sysconf; import jnr.ffi.Pointer; import jnr.ffi.annotations.*; import jnr.ffi.types.clock_t; import java.nio.ByteBuffer; import jnr.ffi.types.intptr_t; public interface LibC { CharSequence crypt(CharSequence key, CharSequence salt); Pointer crypt(byte[] key, byte[] salt); int chmod(CharSequence filename, int mode); int fchmod(int fd, int mode); int chown(CharSequence filename, int user, int group); int fchown(int fd, int user, int group); int fstat(int fd, @Out @Transient FileStat stat); int fstat64(int fd, @Out @Transient FileStat stat); String getenv(CharSequence envName); @IgnoreError int getegid(); int setegid(int egid); @IgnoreError int geteuid(); int seteuid(int euid); @IgnoreError int getgid(); String getlogin(); int setgid(int gid); int getpgid(); int getpgid(int pid); int setpgid(int pid, int pgid); int getpgrp(); int setpgrp(int pid, int pgrp); @IgnoreError int getppid(); @IgnoreError int getpid(); NativePasswd getpwent(); NativePasswd getpwuid(int which); NativePasswd getpwnam(CharSequence which); NativeGroup getgrent(); NativeGroup getgrgid(int which); NativeGroup getgrnam(CharSequence which); int setpwent(); int endpwent(); int setgrent(); int endgrent(); @IgnoreError int getuid(); int setsid(); int setuid(int uid); int kill(int pid, int signal); int dup(int fd); int dup2(int oldFd, int newFd); int fcntl(int fd, int fnctl, int arg); int fcntl(int fd, int fnctl); @Deprecated int fcntl(int fd, int fnctl, int... arg); int getdtablesize(); public interface LibCSignalHandler { @Delegate void signal(int sig); } @intptr_t long signal(int sig, LibCSignalHandler handler); int lchmod(CharSequence filename, int mode); int lchown(CharSequence filename, int user, int group); int link(CharSequence oldpath, CharSequence newpath); int lstat(CharSequence path, @Out @Transient FileStat stat); int lstat64(CharSequence path, @Out @Transient FileStat stat); int mkdir(CharSequence path, int mode); int rmdir(CharSequence path); int stat(CharSequence path, @Out @Transient FileStat stat); int stat64(CharSequence path, @Out @Transient FileStat stat); int symlink(CharSequence oldpath, CharSequence newpath); int readlink(CharSequence oldpath, @Out ByteBuffer buffer, int len); int setenv(CharSequence envName, CharSequence envValue, int overwrite); @IgnoreError int umask(int mask); int unsetenv(CharSequence envName); int utimes(CharSequence path, @In Timeval[] times); int futimes(int fd, @In Timeval[] times); int fork(); int waitpid(long pid, @Out int[] status, int options); int wait(@Out int[] status); int getpriority(int which, int who); int setpriority(int which, int who, int prio); @IgnoreError int isatty(int fd); int read(int fd, @Out ByteBuffer dst, int len); int write(int fd, @In ByteBuffer src, int len); int read(int fd, @Out byte[] dst, int len); int write(int fd, @In byte[] src, int len); int pread(int fd, @Out ByteBuffer src, int len, int offset); int pread(int fd, @Out byte[] src, int len, int offset); int pwrite(int fd, @In ByteBuffer src, int len, int offset); int pwrite(int fd, @In byte[] src, int len, int offset); int lseek(int fd, long offset, int whence); int close(int fd); int execv(CharSequence path, @In CharSequence... argv); int execve(CharSequence path, @In CharSequence[] argv, @In CharSequence[] envp); int chdir(CharSequence path); public long sysconf(Sysconf name); public @clock_t long times(@Out @Transient NativeTimes tms); int flock(int fd, int mode); int unlink(CharSequence path); int open(CharSequence path, int flags, int perm); int pipe(@Out int[] fds); int ftruncate(int fd, long offset); long getcwd(byte[] cwd, int len); int fsync(int fd); int fdatasync(int fd); int socketpair(int domain, int type, int protocol, @Out int[] fds); int sendmsg(int socket, @In MsgHdr message, int flags); int recvmsg(int socket, @Direct MsgHdr message, int flags); } jnr-posix-3.0.12/src/main/java/jnr/posix/LibCProvider.java000066400000000000000000000001221252174056300233040ustar00rootroot00000000000000 package jnr.posix; public interface LibCProvider { public LibC getLibC(); } jnr-posix-3.0.12/src/main/java/jnr/posix/LinuxCmsgHdr.java000066400000000000000000000036571252174056300233470ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; import jnr.ffi.Runtime; import jnr.ffi.StructLayout; /** * @author Bob McWhirter */ class LinuxCmsgHdr extends BaseCmsgHdr { public static class Layout extends StructLayout { protected Layout(Runtime runtime) { super(runtime); } // contrary to man page public final size_t cmsg_len = new size_t(); public final Signed32 cmsg_level = new Signed32(); public final Signed32 cmsg_type = new Signed32(); } public static final Layout layout = new Layout(Runtime.getSystemRuntime()); public LinuxCmsgHdr(NativePOSIX posix, Pointer memory) { super(posix, memory); } public LinuxCmsgHdr(NativePOSIX posix, Pointer memory, int totalLen) { super(posix, memory, totalLen); } public void setLevel(int level) { layout.cmsg_level.set(this.memory, level); } public int getLevel() { return layout.cmsg_level.get(this.memory); } public void setType(int type) { layout.cmsg_type.set(this.memory, type); } public int getType() { return layout.cmsg_type.get(this.memory); } public int getLen() { return (int) layout.cmsg_len.get(this.memory); } void setLen(int len) { layout.cmsg_len.set(this.memory, len); } public String toString(String indent) { StringBuffer buf = new StringBuffer(); buf.append(indent).append("cmsg {\n"); buf.append(indent).append(" cmsg_len=").append(layout.cmsg_len.get(this.memory)).append("\n"); buf.append(indent).append(" cmsg_level=").append(layout.cmsg_level.get(this.memory)).append("\n"); buf.append(indent).append(" cmsg_type=").append(layout.cmsg_type.get(this.memory)).append("\n"); buf.append(indent).append(" cmsg_data=").append(getData()).append("\n"); buf.append(indent).append("}"); return buf.toString(); } } jnr-posix-3.0.12/src/main/java/jnr/posix/LinuxFileStat32.java000066400000000000000000000110201252174056300236570ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.*; public final class LinuxFileStat32 extends BaseFileStat { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final Signed64 st_dev = new Signed64(); public final Signed16 __pad1 = new Signed16(); public final Signed32 st_ino = new Signed32(); public final Signed32 st_mode = new Signed32(); public final Signed32 st_nlink = new Signed32(); public final Signed32 st_uid = new Signed32(); public final Signed32 st_gid = new Signed32(); public final Signed64 st_rdev = new Signed64(); public final Signed16 __pad2 = new Signed16(); public final Signed64 st_size = new Signed64(); public final Signed32 st_blksize = new Signed32(); public final Signed32 st_blocks = new Signed32(); public final Signed32 __unused4 = new Signed32(); public final Signed32 st_atim_sec = new Signed32(); // Time of last access (time_t) public final Signed32 st_atim_nsec = new Signed32(); // Time of last access (nanoseconds) public final Signed32 st_mtim_sec = new Signed32(); // Last data modification time (time_t) public final Signed32 st_mtim_nsec = new Signed32(); // Last data modification time (nanoseconds) public final Signed32 st_ctim_sec = new Signed32(); // Time of last status change (time_t) public final Signed32 st_ctim_nsec = new Signed32(); // Time of last status change (nanoseconds) public final Signed64 __unused5 = new Signed64(); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public LinuxFileStat32() { this(null); } public LinuxFileStat32(BaseNativePOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atim_sec.get(memory); } public long aTimeNanoSecs() { return layout.st_atim_nsec.get(memory); } public long blocks() { return layout.st_blocks.get(memory); } public long blockSize() { return layout.st_blksize.get(memory); } public long ctime() { return layout.st_ctim_sec.get(memory); } public long cTimeNanoSecs() { return layout.st_ctim_nsec.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return layout.st_mode.get(memory) & 0xffff; } public long mtime() { return layout.st_mtim_sec.get(memory); } public long mTimeNanoSecs() { return layout.st_mtim_nsec.get(memory); } public int nlink() { return layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout.st_size.get(memory); } public int uid() { return layout.st_uid.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/LinuxFileStat64.java000066400000000000000000000057361252174056300237050ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.StructLayout; public final class LinuxFileStat64 extends BaseFileStat { public static final class Layout extends StructLayout { public Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final Signed64 st_dev = new Signed64(); public final Signed64 st_ino = new Signed64(); public final Signed64 st_nlink = new Signed64(); public final Signed32 st_mode = new Signed32(); public final Signed32 st_uid = new Signed32(); public final Signed32 st_gid = new Signed32(); public final Signed64 st_rdev = new Signed64(); public final Signed64 st_size = new Signed64(); public final Signed64 st_blksize = new Signed64(); public final Signed64 st_blocks = new Signed64(); public final Signed64 st_atime = new Signed64(); // Time of last access (time_t) public final Signed64 st_atimensec = new Signed64(); // Time of last access (nanoseconds) public final Signed64 st_mtime = new Signed64(); // Last data modification time (time_t) public final Signed64 st_mtimensec = new Signed64(); // Last data modification time (nanoseconds) public final Signed64 st_ctime = new Signed64(); // Time of last status change (time_t) public final Signed64 st_ctimensec = new Signed64(); // Time of last status change (nanoseconds) public final Signed64 __unused4 = new Signed64(); public final Signed64 __unused5 = new Signed64(); public final Signed64 __unused6 = new Signed64(); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public LinuxFileStat64(LinuxPOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atime.get(memory); } public long aTimeNanoSecs() { return layout.st_atimensec.get(memory); } public long blockSize() { return layout.st_blksize.get(memory); } public long blocks() { return layout.st_blocks.get(memory); } public long ctime() { return layout.st_ctime.get(memory); } public long cTimeNanoSecs() { return layout.st_ctimensec.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return layout.st_mode.get(memory); } public long mtime() { return layout.st_mtime.get(memory); } public long mTimeNanoSecs() { return layout.st_mtimensec.get(memory); } public int nlink() { return (int) layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout. st_size.get(memory); } public int uid() { return layout.st_uid.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/LinuxLibC.java000066400000000000000000000022171252174056300226200ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.annotations.In; import jnr.ffi.annotations.NulTerminate; import jnr.ffi.annotations.Out; import jnr.ffi.annotations.Transient; import java.nio.ByteBuffer; public interface LinuxLibC extends UnixLibC { public int __fxstat(int version, int fd, @Out @Transient FileStat stat); public int __lxstat(int version, CharSequence path, @Out @Transient FileStat stat); public int __lxstat(int version, @NulTerminate @In ByteBuffer path, @Out @Transient FileStat stat); public int __xstat(int version, CharSequence path, @Out @Transient FileStat stat); public int __xstat(int version, @NulTerminate @In ByteBuffer path, @Out @Transient FileStat stat); public int __fxstat64(int version, int fd, @Out @Transient FileStat stat); public int __lxstat64(int version, CharSequence path, @Out @Transient FileStat stat); public int __lxstat64(int version, @NulTerminate @In ByteBuffer path, @Out @Transient FileStat stat); public int __xstat64(int version, CharSequence path, @Out @Transient FileStat stat); public int __xstat64(int version, @NulTerminate @In ByteBuffer path, @Out @Transient FileStat stat); } jnr-posix-3.0.12/src/main/java/jnr/posix/LinuxMsgHdr.java000066400000000000000000000077141252174056300232020ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; import jnr.ffi.Runtime; import jnr.ffi.StructLayout; /** * @author Bob McWhirter */ class LinuxMsgHdr extends BaseMsgHdr { public static class Layout extends StructLayout { protected Layout(Runtime runtime) { super(runtime); } public final Pointer msg_name = new Pointer(); public final socklen_t msg_namelen = new socklen_t(); public final Pointer msg_iov = new Pointer(); public final size_t msg_iovlen = new size_t(); public final Pointer msg_control = new Pointer(); public final size_t msg_controllen = new size_t(); public final Signed32 msg_flags = new Signed32(); } private static final Layout layout = new Layout(Runtime.getSystemRuntime()); protected LinuxMsgHdr(NativePOSIX posix) { super(posix, layout); setName(null); } CmsgHdr allocateCmsgHdrInternal(NativePOSIX posix, Pointer pointer, int len) { if (len > 0) { return new LinuxCmsgHdr(posix, pointer, len); } else { return new LinuxCmsgHdr(posix, pointer); } } @Override void setControlPointer(Pointer control) { layout.msg_control.set(this.memory, control); } @Override void setControlLen(int len) { layout.msg_controllen.set(this.memory, len); } public String toString() { StringBuffer buf = new StringBuffer(); buf.append("msghdr {\n"); buf.append(" msg_name=").append(getName()).append(",\n"); buf.append(" msg_namelen=").append(getNameLen()).append(",\n"); buf.append(" msg_iov=[\n"); Pointer iovp = layout.msg_iov.get(this.memory); int numIov = getIovLen(); for (int i = 0; i < numIov; ++i) { Pointer eachp = iovp.slice(i * BaseIovec.layout.size()); buf.append(new BaseIovec(posix, eachp).toString(" ")); if (i < (numIov - 1)) { buf.append(",\n"); } else { buf.append("\n"); } } buf.append(" ],\n"); buf.append(" msg_control=[\n"); CmsgHdr[] controls = getControls(); for (int i = 0; i < controls.length; ++i) { buf.append(((MacOSCmsgHdr) controls[i]).toString(" ")); if (i < controls.length - 1) { buf.append(",\n"); } else { buf.append("\n"); } } buf.append(" ],\n"); buf.append(" msg_controllen=").append(layout.msg_controllen.get(this.memory)).append("\n"); buf.append(" msg_iovlen=").append(getIovLen()).append(",\n"); buf.append(" msg_flags=").append(getFlags()).append(",\n"); buf.append("}"); return buf.toString(); } @Override void setNamePointer(Pointer name) { layout.msg_name.set( this.memory, name ); } @Override Pointer getNamePointer() { return layout.msg_name.get( this.memory ); } @Override void setNameLen(int len) { layout.msg_namelen.set(this.memory, len); } @Override int getNameLen() { return (int) layout.msg_namelen.get(this.memory); } @Override void setIovPointer(Pointer iov) { layout.msg_iov.set(this.memory, iov); } @Override Pointer getIovPointer() { return layout.msg_iov.get( this.memory ); } @Override void setIovLen(int len) { layout.msg_iovlen.set(this.memory, len); } @Override int getIovLen() { return (int) layout.msg_iovlen.get(this.memory); } @Override Pointer getControlPointer() { return layout.msg_control.get(this.memory); } public int getControlLen() { return (int) layout.msg_controllen.get(this.memory); } public void setFlags(int flags) { layout.msg_flags.set(this.memory, flags); } public int getFlags() { return layout.msg_flags.get(this.memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/LinuxPOSIX.java000066400000000000000000000113361252174056300227130ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Errno; import jnr.constants.platform.Sysconf; import jnr.ffi.Pointer; import jnr.ffi.mapper.FromNativeContext; import jnr.posix.util.Platform; import java.io.FileDescriptor; final class LinuxPOSIX extends BaseNativePOSIX { private volatile boolean use_fxstat64 = true; private volatile boolean use_lxstat64 = true; private volatile boolean use_xstat64 = true; private final int statVersion; LinuxPOSIX(LibCProvider libcProvider, POSIXHandler handler) { super(libcProvider, handler); if (Platform.IS_32_BIT) { statVersion = 3; } else { FileStat stat = allocateStat(); if (((LinuxLibC) libc()).__xstat64(0, "/dev/null", stat) < 0) { statVersion = 1; } else { statVersion = 0; } } } @Override public FileStat allocateStat() { if (Platform.IS_32_BIT) { return new LinuxFileStat32(this); } else { return new LinuxFileStat64(this); } } public MsgHdr allocateMsgHdr() { return new LinuxMsgHdr(this); } public SocketMacros socketMacros() { return LinuxSocketMacros.INSTANCE; } private int old_fstat(int fd, FileStat stat) { try { return super.fstat(fd, stat); } catch (UnsatisfiedLinkError ex2) { handler.unimplementedError("fstat"); return -1; } } @Override public int fstat(int fd, FileStat stat) { if (use_fxstat64) { int ret; try { if ((ret = ((LinuxLibC) libc()).__fxstat64(statVersion, fd, stat)) < 0) { handler.error(Errno.valueOf(errno()), "fstat", Integer.toString(fd)); } return ret; } catch (UnsatisfiedLinkError ex) { use_fxstat64 = false; return old_fstat(fd, stat); } } else { return old_fstat(fd, stat); } } @Override public FileStat fstat(int fd) { FileStat stat = allocateStat(); int ret = fstat(fd, stat); if (ret < 0) handler.error(Errno.valueOf(errno()), "fstat", Integer.toString(fd)); return stat; } @Override public FileStat fstat(FileDescriptor fileDescriptor) { FileStat stat = allocateStat(); int fd = helper.getfd(fileDescriptor); int ret = fstat(fd, stat); if (ret < 0) handler.error(Errno.valueOf(errno()), "fstat", Integer.toString(fd)); return stat; } private final int old_lstat(String path, FileStat stat) { try { return super.lstat(path, stat); } catch (UnsatisfiedLinkError ex) { handler.unimplementedError("lstat"); return -1; } } @Override public int lstat(String path, FileStat stat) { if (use_lxstat64) { try { return ((LinuxLibC) libc()).__lxstat64(statVersion, path, stat); } catch (UnsatisfiedLinkError ex) { use_lxstat64 = false; return old_lstat(path, stat); } } else { return old_lstat(path, stat); } } @Override public FileStat lstat(String path) { FileStat stat = allocateStat(); int ret = lstat(path, stat); if (ret < 0) handler.error(Errno.valueOf(errno()), "lstat", path); return stat; } private final int old_stat(String path, FileStat stat) { try { return super.stat(path, stat); } catch (UnsatisfiedLinkError ex) { handler.unimplementedError("stat"); return -1; } } @Override public int stat(String path, FileStat stat) { if (use_xstat64) { try { return ((LinuxLibC) libc()).__xstat64(statVersion, path, stat); } catch (UnsatisfiedLinkError ex) { use_xstat64 = false; return old_stat(path, stat); } } else { return old_stat(path, stat); } } @Override public FileStat stat(String path) { FileStat stat = allocateStat(); int ret = stat(path, stat); if (ret < 0) handler.error(Errno.valueOf(errno()), "stat", path); return stat; } public long sysconf(Sysconf name) { return libc().sysconf(name); } public Times times() { return NativeTimes.times(this); } public static final PointerConverter PASSWD = new PointerConverter() { public Object fromNative(Object arg, FromNativeContext ctx) { return arg != null ? new LinuxPasswd((Pointer) arg) : null; } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/LinuxPasswd.java000066400000000000000000000034471252174056300232560ustar00rootroot00000000000000 package jnr.posix; import jnr.ffi.StructLayout; public final class LinuxPasswd extends NativePasswd implements Passwd { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final UTF8StringRef pw_name = new UTF8StringRef(); // user name public final UTF8StringRef pw_passwd = new UTF8StringRef(); // password (encrypted) public final Signed32 pw_uid = new Signed32(); // user id public final Signed32 pw_gid = new Signed32(); // user id public final UTF8StringRef pw_gecos = new UTF8StringRef(); // login info public final UTF8StringRef pw_dir = new UTF8StringRef(); // home directory public final UTF8StringRef pw_shell = new UTF8StringRef(); // default shell } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); LinuxPasswd(jnr.ffi.Pointer memory) { super(memory); } public java.lang.String getAccessClass() { return ""; } public java.lang.String getGECOS() { return layout.pw_gecos.get(memory); } public long getGID() { return layout.pw_gid.get(memory); } public java.lang.String getHome() { return layout.pw_dir.get(memory); } public java.lang.String getLoginName() { return layout.pw_name.get(memory); } public java.lang.String getPassword() { return layout.pw_passwd.get(memory); } public java.lang.String getShell() { return layout.pw_shell.get(memory); } public long getUID() { return layout.pw_uid.get(memory); } public int getPasswdChangeTime() { return 0; } public int getExpire() { return Integer.MAX_VALUE; } } jnr-posix-3.0.12/src/main/java/jnr/posix/LinuxSocketMacros.java000066400000000000000000000013761252174056300244110ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.*; import jnr.ffi.Runtime; /** * @author Bob McWhirter */ public class LinuxSocketMacros implements SocketMacros { public static final LinuxSocketMacros INSTANCE = new LinuxSocketMacros(); public int CMSG_ALIGN(int len) { int sizeof_size_t = Runtime.getSystemRuntime().findType(TypeAlias.size_t).size(); return (len + sizeof_size_t - 1) & ~(sizeof_size_t - 1); } public int CMSG_SPACE(int l) { return CMSG_ALIGN(l) + CMSG_ALIGN(LinuxCmsgHdr.layout.size()); } public int CMSG_LEN(int l) { return CMSG_ALIGN( LinuxCmsgHdr.layout.size() + l ); } public Pointer CMSG_DATA(Pointer cmsg) { return cmsg.slice(CMSG_ALIGN(LinuxCmsgHdr.layout.size())); } } jnr-posix-3.0.12/src/main/java/jnr/posix/MacOSCmsgHdr.java000066400000000000000000000036301252174056300232010ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; import jnr.ffi.Runtime; import jnr.ffi.StructLayout; /** * @author Bob McWhirter */ class MacOSCmsgHdr extends BaseCmsgHdr { public static class Layout extends StructLayout { protected Layout(Runtime runtime) { super(runtime); } public final Unsigned32 cmsg_len = new Unsigned32(); public final Signed32 cmsg_level = new Signed32(); public final Signed32 cmsg_type = new Signed32(); } public static final Layout layout = new Layout(Runtime.getSystemRuntime()); public MacOSCmsgHdr(NativePOSIX posix, Pointer memory) { super(posix, memory); } public MacOSCmsgHdr(NativePOSIX posix, Pointer memory, int totalLen) { super(posix, memory, totalLen ); } public void setLevel(int level) { layout.cmsg_level.set(this.memory, level); } public int getLevel() { return layout.cmsg_level.get(this.memory); } public void setType(int type) { layout.cmsg_type.set(this.memory, type); } public int getType() { return layout.cmsg_type.get(this.memory); } public int getLen() { return (int) layout.cmsg_len.get(this.memory); } void setLen(int len) { layout.cmsg_len.set(this.memory, len); } public String toString(String indent) { StringBuffer buf = new StringBuffer(); buf.append(indent).append("cmsg {\n"); buf.append(indent).append(" cmsg_len=").append(layout.cmsg_len.get(this.memory)).append("\n"); buf.append(indent).append(" cmsg_level=").append(layout.cmsg_level.get(this.memory)).append("\n"); buf.append(indent).append(" cmsg_type=").append(layout.cmsg_type.get(this.memory)).append("\n"); buf.append(indent).append(" cmsg_data=").append(getData()).append("\n"); buf.append(indent).append("}"); return buf.toString(); } } jnr-posix-3.0.12/src/main/java/jnr/posix/MacOSFileStat.java000066400000000000000000000100701252174056300233610ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.StructLayout; public final class MacOSFileStat extends BaseFileStat { public static class Layout extends StructLayout { public Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final class time_t extends SignedLong { } public final Signed32 st_dev = new Signed32(); public final Signed32 st_ino = new Signed32(); public final Signed16 st_mode = new Signed16(); public final Signed16 st_nlink = new Signed16(); public final Signed32 st_uid = new Signed32(); public final Signed32 st_gid = new Signed32(); public final Signed32 st_rdev = new Signed32(); public final time_t st_atime = new time_t(); public final SignedLong st_atimensec = new SignedLong(); public final time_t st_mtime = new time_t(); public final SignedLong st_mtimensec = new SignedLong(); public final time_t st_ctime = new time_t(); public final SignedLong st_ctimensec = new SignedLong(); public final Signed64 st_size = new Signed64(); public final Signed64 st_blocks = new Signed64(); public final Signed32 st_blksize = new Signed32(); public final Signed32 st_flags = new Signed32(); public final Signed32 st_gen = new Signed32(); public final Signed32 st_lspare = new Signed32(); public final Signed64 st_qspare0 = new Signed64(); public final Signed64 st_qspare1 = new Signed64(); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public MacOSFileStat(MacOSPOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atime.get(memory); } public long blocks() { return layout.st_blocks.get(memory); } public long blockSize() { return layout.st_blksize.get(memory); } public long ctime() { return layout.st_ctime.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return layout.st_mode.get(memory) & 0xffff; } public long mtime() { return layout.st_mtime.get(memory); } public int nlink() { return layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout.st_size.get(memory); } public int uid() { return layout.st_uid.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/MacOSMsgHdr.java000066400000000000000000000077651252174056300230530ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; import jnr.ffi.Runtime; import jnr.ffi.StructLayout; import java.nio.ByteBuffer; /** * @author Bob McWhirter */ class MacOSMsgHdr extends BaseMsgHdr { public static class Layout extends StructLayout { protected Layout(Runtime runtime) { super(runtime); } public final Pointer msg_name = new Pointer(); public final socklen_t msg_namelen = new socklen_t(); public final Pointer msg_iov = new Pointer(); public final Signed32 msg_iovlen = new Signed32(); public final Pointer msg_control = new Pointer(); public final socklen_t msg_controllen = new socklen_t(); public final Signed32 msg_flags = new Signed32(); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); protected MacOSMsgHdr(NativePOSIX posix) { super(posix, layout); setName(null); } CmsgHdr allocateCmsgHdrInternal(NativePOSIX posix, Pointer pointer, int len) { if (len > 0) { return new MacOSCmsgHdr(posix, pointer, len); } else { return new MacOSCmsgHdr(posix, pointer); } } @Override void setControlPointer(Pointer control) { layout.msg_control.set(this.memory, control); } @Override void setControlLen(int len) { layout.msg_controllen.set(this.memory, len); } public String toString() { StringBuffer buf = new StringBuffer(); buf.append("msghdr {\n"); buf.append(" msg_name=").append(getName()).append(",\n"); buf.append(" msg_namelen=").append(getNameLen()).append(",\n"); buf.append(" msg_iov=[\n"); Pointer iovp = layout.msg_iov.get(this.memory); int numIov = getIovLen(); for (int i = 0; i < numIov; ++i) { Pointer eachp = iovp.slice(i * BaseIovec.layout.size()); buf.append(new BaseIovec(posix, eachp).toString(" ")); if (i < (numIov - 1)) { buf.append(",\n"); } else { buf.append("\n"); } } buf.append(" ],\n"); buf.append(" msg_control=[\n"); CmsgHdr[] controls = getControls(); for (int i = 0; i < controls.length; ++i) { buf.append(((MacOSCmsgHdr) controls[i]).toString(" ")); if (i < controls.length - 1) { buf.append(",\n"); } else { buf.append("\n"); } } buf.append(" ],\n"); buf.append(" msg_controllen=").append(layout.msg_controllen.get(this.memory)).append("\n"); buf.append(" msg_iovlen=").append(getIovLen()).append(",\n"); buf.append(" msg_flags=").append(getFlags()).append(",\n"); buf.append("}"); return buf.toString(); } @Override void setNamePointer(Pointer name) { layout.msg_name.set( this.memory, name ); } @Override Pointer getNamePointer() { return layout.msg_name.get( this.memory ); } @Override void setNameLen(int len) { layout.msg_namelen.set(this.memory, len); } @Override int getNameLen() { return (int) layout.msg_namelen.get(this.memory); } @Override void setIovPointer(Pointer iov) { layout.msg_iov.set(this.memory, iov); } @Override Pointer getIovPointer() { return layout.msg_iov.get( this.memory ); } @Override void setIovLen(int len) { layout.msg_iovlen.set(this.memory, len); } @Override int getIovLen() { return layout.msg_iovlen.get(this.memory); } @Override Pointer getControlPointer() { return layout.msg_control.get(this.memory); } public int getControlLen() { return (int) layout.msg_controllen.get(this.memory); } public void setFlags(int flags) { layout.msg_flags.set(this.memory, flags); } public int getFlags() { return layout.msg_flags.get(this.memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/MacOSPOSIX.java000066400000000000000000000016501252174056300225540ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Sysconf; import jnr.ffi.*; import jnr.ffi.mapper.FromNativeContext; final class MacOSPOSIX extends BaseNativePOSIX { MacOSPOSIX(LibCProvider libcProvider, POSIXHandler handler) { super(libcProvider, handler); } public FileStat allocateStat() { return new MacOSFileStat(this); } public MsgHdr allocateMsgHdr() { return new MacOSMsgHdr(this); } public SocketMacros socketMacros() { return MacOSSocketMacros.INSTANCE; } public long sysconf(Sysconf name) { return libc().sysconf(name); } public Times times() { return NativeTimes.times(this); } public static final PointerConverter PASSWD = new PointerConverter() { public Object fromNative(Object arg, FromNativeContext ctx) { return arg != null ? new MacOSPasswd((Pointer) arg) : null; } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/MacOSPasswd.java000066400000000000000000000042671252174056300231220ustar00rootroot00000000000000 package jnr.posix; import jnr.ffi.StructLayout; /** * */ public final class MacOSPasswd extends NativePasswd implements Passwd { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final UTF8StringRef pw_name = new UTF8StringRef(); // user name public final UTF8StringRef pw_passwd = new UTF8StringRef(); // password (encrypted) public final Signed32 pw_uid = new Signed32(); // user id public final Signed32 pw_gid = new Signed32(); // user id public final SignedLong pw_change = new SignedLong(); // password change time public final UTF8StringRef pw_class = new UTF8StringRef(); // user access class public final UTF8StringRef pw_gecos = new UTF8StringRef(); // login info public final UTF8StringRef pw_dir = new UTF8StringRef(); // home directory public final UTF8StringRef pw_shell = new UTF8StringRef(); // default shell public final SignedLong pw_expire = new SignedLong(); // account expiration } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); MacOSPasswd(jnr.ffi.Pointer memory) { super(memory); } public java.lang.String getAccessClass() { return layout.pw_class.get(memory); } public java.lang.String getGECOS() { return layout.pw_gecos.get(memory); } public long getGID() { return layout.pw_gid.get(memory); } public java.lang.String getHome() { return layout.pw_dir.get(memory); } public java.lang.String getLoginName() { return layout.pw_name.get(memory); } public int getPasswdChangeTime() { return layout.pw_change.intValue(memory); } public java.lang.String getPassword() { return layout.pw_passwd.get(memory); } public java.lang.String getShell() { return layout.pw_shell.get(memory); } public long getUID() { return layout.pw_uid.get(memory); } public int getExpire() { return layout.pw_expire.intValue(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/MacOSSocketMacros.java000066400000000000000000000012111252174056300242400ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; /** * @author Bob McWhirter */ public class MacOSSocketMacros implements SocketMacros { public static final SocketMacros INSTANCE = new MacOSSocketMacros(); public int __DARWIN_ALIGN32(int x) { return ((x + 3) & ~3); } public int CMSG_SPACE(int l) { return __DARWIN_ALIGN32(MacOSCmsgHdr.layout.size()) + __DARWIN_ALIGN32(l); } public int CMSG_LEN(int l) { return (__DARWIN_ALIGN32(MacOSCmsgHdr.layout.size())) + (l); } public Pointer CMSG_DATA(Pointer cmsg) { return cmsg.slice(__DARWIN_ALIGN32(MacOSCmsgHdr.layout.size())); } } jnr-posix-3.0.12/src/main/java/jnr/posix/MsgHdr.java000066400000000000000000000006631252174056300221560ustar00rootroot00000000000000package jnr.posix; import java.nio.ByteBuffer; /** * @author Bob McWhirter */ public interface MsgHdr { void setName(String name); String getName(); void setIov(ByteBuffer[] buffers); ByteBuffer[] getIov(); void setFlags(int flags); int getFlags(); CmsgHdr allocateControl(int dataLength); CmsgHdr[] allocateControls(int[] dataLengths); CmsgHdr[] getControls(); int getControlLen(); } jnr-posix-3.0.12/src/main/java/jnr/posix/NativeGroup.java000066400000000000000000000005331252174056300232310ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.StructLayout; public abstract class NativeGroup implements Group { protected final jnr.ffi.Runtime runtime; protected final StructLayout structLayout; NativeGroup(jnr.ffi.Runtime runtime, StructLayout structLayout) { this.runtime = runtime; this.structLayout = structLayout; } } jnr-posix-3.0.12/src/main/java/jnr/posix/NativePOSIX.java000066400000000000000000000003311252174056300230330ustar00rootroot00000000000000package jnr.posix; /** * */ abstract class NativePOSIX implements POSIX { jnr.ffi.Runtime getRuntime() { return jnr.ffi.Runtime.getRuntime(libc()); } abstract SocketMacros socketMacros(); } jnr-posix-3.0.12/src/main/java/jnr/posix/NativePasswd.java000066400000000000000000000003351252174056300233760ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; public abstract class NativePasswd implements Passwd { protected final Pointer memory; NativePasswd(jnr.ffi.Pointer pointer) { this.memory = pointer; } } jnr-posix-3.0.12/src/main/java/jnr/posix/NativeTimes.java000066400000000000000000000024261252174056300232210ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Errno; import jnr.ffi.LastError; import jnr.ffi.Memory; import jnr.ffi.Pointer; import jnr.ffi.StructLayout; /** * */ final class NativeTimes implements Times { static final class Layout extends StructLayout { public final clock_t tms_utime = new clock_t(); public final clock_t tms_stime = new clock_t(); public final clock_t tms_cutime = new clock_t(); public final clock_t tms_cstime = new clock_t(); Layout(jnr.ffi.Runtime runtime) { super(runtime); } } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); final Pointer memory; static NativeTimes times(BaseNativePOSIX posix) { NativeTimes tms = new NativeTimes(posix); return posix.libc().times(tms) == -1 ? null : tms; } NativeTimes(NativePOSIX posix) { this.memory = Memory.allocate(posix.getRuntime(), layout.size()); } public long utime() { return layout.tms_utime.get(memory); } public long stime() { return layout.tms_stime.get(memory); } public long cutime() { return layout.tms_cutime.get(memory); } public long cstime() { return layout.tms_cstime.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/OpenBSDFileStat.java000066400000000000000000000101751252174056300236570ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.StructLayout; public final class OpenBSDFileStat extends BaseFileStat { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final class time_t extends Signed64 {} public final class dev_t extends Signed32 {} public final Unsigned32 st_mode = new Unsigned32(); public final dev_t st_dev = new dev_t(); public final Unsigned64 st_ino = new Unsigned64(); public final Unsigned32 st_nlink = new Unsigned32(); public final Unsigned32 st_uid = new Unsigned32(); public final Unsigned32 st_gid = new Unsigned32(); public final dev_t st_rdev = new dev_t(); public final time_t st_atime = new time_t(); public final SignedLong st_atimensec = new SignedLong(); public final time_t st_mtime = new time_t(); public final SignedLong st_mtimensec = new SignedLong(); public final time_t st_ctime = new time_t(); public final SignedLong st_ctimensec = new SignedLong(); public final Signed64 st_size = new Signed64(); public final Signed64 st_blocks = new Signed64(); public final Unsigned32 st_blksize = new Unsigned32(); public final Unsigned32 st_flags = new Unsigned32(); public final Unsigned32 st_gen = new Unsigned32(); public final time_t st_birthtime = new time_t(); public final SignedLong st_birthtimensec = new SignedLong(); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public OpenBSDFileStat(NativePOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atime.get(memory); } public long blocks() { return layout.st_blocks.get(memory); } public long blockSize() { return layout.st_blksize.get(memory); } public long ctime() { return layout.st_ctime.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return (int) layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return (int)(layout.st_mode.get(memory) & 0xffff); } public long mtime() { return layout.st_mtime.get(memory); } public int nlink() { return (int)layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout.st_size.get(memory); } public int uid() { return (int)layout.st_uid.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/OpenBSDPOSIX.java000066400000000000000000000055671252174056300230570ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.constants.platform.Sysconf; import jnr.ffi.Struct; import jnr.ffi.mapper.FromNativeContext; import jnr.ffi.Pointer; import jnr.posix.util.MethodName; final class OpenBSDPOSIX extends BaseNativePOSIX { OpenBSDPOSIX(LibCProvider libc, POSIXHandler handler) { super(libc, handler); } public FileStat allocateStat() { return new OpenBSDFileStat(this); } public MsgHdr allocateMsgHdr() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public SocketMacros socketMacros() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public long sysconf(Sysconf name) { return libc().sysconf(name); } public Times times() { return NativeTimes.times(this); } public static final PointerConverter PASSWD = new PointerConverter() { public Object fromNative(Object arg, FromNativeContext ctx) { return arg != null ? new OpenBSDPasswd((Pointer) arg) : null; } }; @Override public int utimes(String path, long[] atimeval, long[] mtimeval) { Timeval[] times = null; if (atimeval != null && mtimeval != null) { times = Struct.arrayOf(getRuntime(), OpenBSDTimeval.class, 2); times[0].setTime(atimeval); times[1].setTime(mtimeval); } return libc().utimes(path, times); } } jnr-posix-3.0.12/src/main/java/jnr/posix/OpenBSDPasswd.java000066400000000000000000000070551252174056300234100ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import jnr.ffi.StructLayout; public class OpenBSDPasswd extends NativePasswd implements Passwd { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final UTF8StringRef pw_name = new UTF8StringRef(); // user name public final UTF8StringRef pw_passwd = new UTF8StringRef(); // password (encrypted) public final Unsigned32 pw_uid = new Unsigned32(); // user id public final Unsigned32 pw_gid = new Unsigned32(); // user id public final Signed64 pw_change = new Signed64(); // password change time public final UTF8StringRef pw_class = new UTF8StringRef(); // user access class public final UTF8StringRef pw_gecos = new UTF8StringRef(); // login info public final UTF8StringRef pw_dir = new UTF8StringRef(); // home directory public final UTF8StringRef pw_shell = new UTF8StringRef(); // default shell public final Signed64 pw_expire = new Signed64(); // account expiration } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); OpenBSDPasswd(jnr.ffi.Pointer memory) { super(memory); } public java.lang.String getAccessClass() { return layout.pw_class.get(memory); } public java.lang.String getGECOS() { return layout.pw_gecos.get(memory); } public long getGID() { return layout.pw_gid.get(memory); } public java.lang.String getHome() { return layout.pw_dir.get(memory); } public java.lang.String getLoginName() { return layout.pw_name.get(memory); } public int getPasswdChangeTime() { return layout.pw_change.intValue(memory); } public java.lang.String getPassword() { return layout.pw_passwd.get(memory); } public java.lang.String getShell() { return layout.pw_shell.get(memory); } public long getUID() { return layout.pw_uid.get(memory); } public int getExpire() { return layout.pw_expire.intValue(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/OpenBSDTimeval.java000066400000000000000000000006371252174056300235470ustar00rootroot00000000000000package jnr.posix; public final class OpenBSDTimeval extends Timeval { public final Signed64 tv_sec = new Signed64(); public final SignedLong tv_usec = new SignedLong(); public OpenBSDTimeval(jnr.ffi.Runtime runtime) { super(runtime); } public void setTime(long[] timeval) { assert timeval.length == 2; tv_sec.set(timeval[0]); tv_usec.set(timeval[1]); } } jnr-posix-3.0.12/src/main/java/jnr/posix/POSIX.java000066400000000000000000000127571252174056300217030ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Fcntl; import jnr.constants.platform.Sysconf; import jnr.posix.util.ProcessMaker; import java.io.FileDescriptor; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Collection; import jnr.constants.platform.Signal; public interface POSIX { CharSequence crypt(CharSequence key, CharSequence salt); /** * Call the crypt function with the given key and salt as raw null-terminated byte (C char) strings. * * @param key null-terminated key bytes * @param salt null-terminated salt bytes * @return null-terminated crypted bytes, or null if there was an error */ byte[] crypt(byte[] key, byte[] salt); FileStat allocateStat(); int chmod(String filename, int mode); int fchmod(int fd, int mode); int chown(String filename, int user, int group); int fchown(int fd, int user, int group); /** * Shell expanding and escaping version of exec which handles all the * preparation of a command line or command list. */ int exec(String path, String... argv); /** * Shell expanding and escaping version of exec which handles all the * preparation of a command line or command list. */ int exec(String path, String[] argv, String[] envp); int execv(String path, String[] argv); int execve(String path, String[] argv, String[] envp); int fork(); FileStat fstat(FileDescriptor descriptor); FileStat fstat(int descriptor); int fstat(FileDescriptor descriptor, FileStat stat); int fstat(int fd, FileStat stat); String getenv(String envName); int getegid(); int geteuid(); int seteuid(int euid); int getgid(); int getdtablesize(); String getlogin(); int getpgid(); int getpgid(int pid); int getpgrp(); int getpid(); int getppid(); int getpriority(int which, int who); Passwd getpwent(); Passwd getpwuid(int which); Passwd getpwnam(String which); Group getgrgid(int which); Group getgrnam(String which); Group getgrent(); int endgrent(); int setgrent(); int endpwent(); int setpwent(); int getuid(); boolean isatty(FileDescriptor descriptor); int kill(int pid, int signal); SignalHandler signal(Signal sig, SignalHandler handler); int lchmod(String filename, int mode); int lchown(String filename, int user, int group); int link(String oldpath,String newpath); FileStat lstat(String path); int lstat(String path, FileStat stat); int mkdir(String path, int mode); String readlink(String path) throws IOException; int rmdir(String path); int setenv(String envName, String envValue, int overwrite); // 0 no !0 yes int setsid(); int setgid(int gid); int setegid(int egid); int setpgid(int pid, int pgid); int setpgrp(int pid, int pgrp); int setpriority(int which, int who, int prio); int setuid(int uid); FileStat stat(String path); int stat(String path, FileStat stat); int symlink(String oldpath,String newpath); int umask(int mask); int unsetenv(String envName); int utimes(String path, long[] atimeval, long[] mtimeval); int futimes(int fd, long[] atimeval, long[] mtimeval); int waitpid(int pid, int[] status, int flags); int waitpid(long pid, int[] status, int flags); int wait(int[] status); int errno(); void errno(int value); int chdir(String path); boolean isNative(); /** Returns null if isNative returns false. */ LibC libc(); ProcessMaker newProcessMaker(String... command); ProcessMaker newProcessMaker(); public long sysconf(Sysconf name); public Times times(); public long posix_spawnp(String path, Collection fileActions, Collection argv, Collection envp); public long posix_spawnp(String path, Collection fileActions, Collection spawnAttributes, Collection argv, Collection envp); public int flock(int fd, int operation); int dup(int fd); int dup2(int oldFd, int newFd); int fcntlInt(int fd, Fcntl fcntlConst, int arg); int fcntl(int fd, Fcntl fcntlConst); int close(int fd); int unlink(CharSequence path); int open(CharSequence path, int flags, int perm); int write(int fd, byte[] buf, int n); int read(int fd, byte[] buf, int n); int pwrite(int fd, byte[] buf, int n, int offset); int pread(int fd, byte[] buf, int n, int offset); int write(int fd, ByteBuffer buf, int n); int read(int fd, ByteBuffer buf, int n); int pwrite(int fd, ByteBuffer buf, int n, int offset); int pread(int fd, ByteBuffer buf, int n, int offset); int lseek(int fd, long offset, int whence); int pipe(int[] fds); int ftruncate(int fd, long offset); String getcwd(); int socketpair(int domain, int type, int protocol, int[] fds); int sendmsg(int socket, MsgHdr message, int flags); int recvmsg(int socket, MsgHdr message, int flags); MsgHdr allocateMsgHdr(); /** * fcntl(2) * * @deprecated This version does not pass args because jnr-ffi does not support variadic invocation. * @see jnr.posix.POSIX#fcntlInt(int, jnr.constants.platform.Fcntl, int) */ int fcntl(int fd, Fcntl fcntlConst, int... arg); int fsync(int fd); int fdatasync(int fd); } jnr-posix-3.0.12/src/main/java/jnr/posix/POSIXFactory.java000066400000000000000000000227121252174056300232230ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Library; import jnr.ffi.LibraryOption; import jnr.ffi.Struct; import java.util.Collections; import java.util.HashMap; import jnr.ffi.mapper.FunctionMapper; import jnr.posix.util.DefaultPOSIXHandler; import jnr.posix.util.Platform; import java.util.Map; public class POSIXFactory { // Weird inner-class resolution problem work-around FIXME: JRUBY-5889. Someone fix JAFFL! private static final Class BOGUS_HACK = Struct.class; /** * Get a POSIX instance. If useNativePosix is true, this works just like * POSIXFactory#getPOSIX(). If useNativePosix is false, this works like * POSIXFactory#getJavaPOSIX() * * @param handler a POSIXHandler implementation * @param useNativePOSIX whether to attempt to use native code for better functionality * @return a POSIX implementation, attempting to use native code if useNativePosix is true */ public static POSIX getPOSIX(POSIXHandler handler, boolean useNativePOSIX) { return new LazyPOSIX(handler, useNativePOSIX); } /** * This will use {@link DefaultPOSIXHandler} and the native POSIX implementation, * falling back on the pure-Java implementation if native support is not available. * * @return a POSIX implementation, native if possible and pure-Java otherwise. */ public static POSIX getPOSIX() { return getPOSIX(new DefaultPOSIXHandler(), true); } /** * Get a pure-Java POSIX instance. Functionality will be limited to that which can * be provided by pure-Java/JDK features or shelling out to external commands. * * @param handler a POSIXHandler implementation * @return a pure-Java POSIX implementation */ public static POSIX getJavaPOSIX(POSIXHandler handler) { return new JavaPOSIX(handler); } /** * Get a pure-Java POSIX instance. Functionality will be limited to that which can * be provided by pure-Java/JDK features or shelling out to external commands. * * @return a pure-Java POSIX implementation */ public static POSIX getJavaPOSIX() { return getJavaPOSIX(new DefaultPOSIXHandler()); } /** * Get a POSIX instance. If a true native implementation can't be loaded, allow that * error to propagate rather than falling back on the pure-Java version. * * @param handler a POSIXHandler implementation * @return a native POSIX implementation, raising errors if the native version can't load */ public static POSIX getNativePOSIX(POSIXHandler handler) { return loadNativePOSIX(handler); } /** * Get a POSIX instance. If a true native implementation can't be loaded, allow that * error to propagate rather than falling back on the pure-Java version. * * @return a native POSIX implementation, raising errors if the native version can't load */ public static POSIX getNativePOSIX() { return getNativePOSIX(new DefaultPOSIXHandler()); } static POSIX loadPOSIX(POSIXHandler handler, boolean useNativePOSIX) { POSIX posix = null; if (useNativePOSIX) { try { posix = loadNativePOSIX(handler); posix = posix != null ? new CheckedPOSIX(posix, handler) : null; // ENEBO: Should printing be done through a handler+log method? if (handler.isVerbose()) { if (posix != null) { System.err.println("Successfully loaded native POSIX impl."); } else { System.err.println("Failed to load native POSIX impl; falling back on Java impl. Unsupported OS."); } } } catch (Throwable t) { if (handler.isVerbose()) { System.err.println("Failed to load native POSIX impl; falling back on Java impl. Stacktrace follows."); t.printStackTrace(); } } } if (posix == null) { posix = getJavaPOSIX(handler); } return posix; } private static POSIX loadNativePOSIX(POSIXHandler handler) { switch (jnr.ffi.Platform.getNativePlatform().getOS()) { case DARWIN: return loadMacOSPOSIX(handler); case LINUX: return loadLinuxPOSIX(handler); case FREEBSD: return loadFreeBSDPOSIX(handler); case OPENBSD: return loadOpenBSDPOSIX(handler); case SOLARIS: return loadSolarisPOSIX(handler); case AIX: return loadAixPOSIX(handler); case WINDOWS: return loadWindowsPOSIX(handler); } return null; } public static POSIX loadLinuxPOSIX(POSIXHandler handler) { return new LinuxPOSIX(DefaultLibCProvider.INSTANCE, handler); } public static POSIX loadMacOSPOSIX(POSIXHandler handler) { return new MacOSPOSIX(DefaultLibCProvider.INSTANCE, handler); } public static POSIX loadSolarisPOSIX(POSIXHandler handler) { return new SolarisPOSIX(DefaultLibCProvider.INSTANCE, handler); } public static POSIX loadFreeBSDPOSIX(POSIXHandler handler) { return new FreeBSDPOSIX(DefaultLibCProvider.INSTANCE, handler); } public static POSIX loadOpenBSDPOSIX(POSIXHandler handler) { return new OpenBSDPOSIX(DefaultLibCProvider.INSTANCE, handler); } public static POSIX loadWindowsPOSIX(POSIXHandler handler) { return new WindowsPOSIX(DefaultLibCProvider.INSTANCE, handler); } public static POSIX loadAixPOSIX(POSIXHandler handler) { return new AixPOSIX(DefaultLibCProvider.INSTANCE, handler); } private static String[] libraries() { switch (jnr.ffi.Platform.getNativePlatform().getOS()) { case LINUX: return new String[] { "libc.so.6", "crypt" }; case SOLARIS: return new String[] { "socket", "nsl", "c" }; case FREEBSD: case NETBSD: return new String[] { "c", "crypt" }; case AIX: return jnr.ffi.Runtime.getSystemRuntime().addressSize() == 4 ? new String[] { "libc.a(shr.o)" } : new String[] { "libc.a(shr_64.o)" }; case WINDOWS: return new String[] { "msvcrt", "kernel32" }; default: return new String[] { "c" }; } } private static Class libraryInterface() { switch (jnr.ffi.Platform.getNativePlatform().getOS()) { case LINUX: return LinuxLibC.class; case AIX: return AixLibC.class; case SOLARIS: return SolarisLibC.class; case WINDOWS: return WindowsLibC.class; default: return UnixLibC.class; } } private static FunctionMapper functionMapper() { switch (jnr.ffi.Platform.getNativePlatform().getOS()) { case AIX: return new SimpleFunctionMapper.Builder() .map("stat", "stat64x") .map("fstat", "fstat64x") .map("lstat", "lstat64x") .map("stat64", "stat64x") .map("fstat64", "fstat64x") .map("lstat64", "lstat64x") .build(); case WINDOWS: return new SimpleFunctionMapper.Builder() .map("getpid", "_getpid") .map("chmod", "_chmod") .map("fstat", "_fstat64") .map("stat", "_stat64") .map("umask", "_umask") .map("isatty", "_isatty") .map("read", "_read") .map("write", "_write") .map("close", "_close") .build(); case SOLARIS: return Platform.IS_32_BIT ? new SimpleFunctionMapper.Builder() .map("stat", "stat64") .map("fstat", "fstat64") .map("lstat", "lstat64") .build() : null; default: return null; } } private static Map options() { Map options = new HashMap(); FunctionMapper functionMapper = functionMapper(); if (functionMapper != null) { options.put(LibraryOption.FunctionMapper, functionMapper); } options.put(LibraryOption.TypeMapper, POSIXTypeMapper.INSTANCE); options.put(LibraryOption.LoadNow, Boolean.TRUE); return Collections.unmodifiableMap(options); } private static final class DefaultLibCProvider implements LibCProvider { public static final LibCProvider INSTANCE = new DefaultLibCProvider(); private static final class SingletonHolder { public static LibC libc = Library.loadLibrary(libraryInterface(), options(), libraries()); } public final LibC getLibC() { return SingletonHolder.libc; } } } jnr-posix-3.0.12/src/main/java/jnr/posix/POSIXFunctionMapper.java000066400000000000000000000011371252174056300245440ustar00rootroot00000000000000 package jnr.posix; import jnr.ffi.mapper.FunctionMapper; final class POSIXFunctionMapper implements FunctionMapper { public static final FunctionMapper INSTANCE = new POSIXFunctionMapper(); private POSIXFunctionMapper() {} public String mapFunctionName(String name, Context ctx) { if (ctx.getLibrary().getName().equals("msvcrt")) { // FIXME: We should either always _ name for msvcrt or get good list of _ methods if (name.equals("getpid") || name.equals("chmod")) { name = "_" + name; } } return name; } } jnr-posix-3.0.12/src/main/java/jnr/posix/POSIXHandler.java000066400000000000000000000042011252174056300231620ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Errno; import java.io.File; import java.io.InputStream; import java.io.PrintStream; /** * The POSIXHandler class allows you do implement the runtime-specific behavior you need in * such a way that it is insulated from the implementation of the POSIX library. Implementing * each of the methods in this interface should give you are working POSIX implementation. * */ public interface POSIXHandler { public enum WARNING_ID { DUMMY_VALUE_USED("DUMMY_VALUE_USED"); private String messageID; WARNING_ID(String messageID) { this.messageID = messageID; } } public void error(Errno error, String extraData); public void error(Errno error, String methodName, String extraData); /** * Specify that posix method is unimplemented. In JRuby we generate an * exception with this. */ public void unimplementedError(String methodName); public void warn(WARNING_ID id, String message, Object... data); /** * Should we provide verbose output about POSIX activities */ public boolean isVerbose(); /** * Get current working directory of your runtime. */ public File getCurrentWorkingDirectory(); /** * Get current set of environment variables of your runtime. */ public String[] getEnv(); /** * Get your runtime's current InputStream */ public InputStream getInputStream(); /** * Get your runtime's current OutputStream */ public PrintStream getOutputStream(); /** * Get your runtimes process ID. This is only intended for non-native POSIX support (e.g. * environments where JNA cannot load or security restricted environments). In JRuby we * found a number of packages which would rather have some identity for the runtime than * nothing. * * Note: If you do not want this to work you impl can just call unimplementedError(String). */ public int getPID(); /** * Get your runtime's current ErrorStream */ public PrintStream getErrorStream(); } jnr-posix-3.0.12/src/main/java/jnr/posix/POSIXTypeMapper.java000066400000000000000000000045421252174056300237030ustar00rootroot00000000000000package jnr.posix; import jnr.constants.Constant; import jnr.ffi.mapper.*; import jnr.posix.util.Platform; final class POSIXTypeMapper implements TypeMapper { public static final TypeMapper INSTANCE = new POSIXTypeMapper(); private POSIXTypeMapper() {} public FromNativeConverter getFromNativeConverter(Class klazz) { if (Passwd.class.isAssignableFrom(klazz)) { if (Platform.IS_MAC) { return MacOSPOSIX.PASSWD; } else if (Platform.IS_LINUX) { return LinuxPOSIX.PASSWD; } else if (Platform.IS_SOLARIS) { return SolarisPOSIX.PASSWD; } else if (Platform.IS_FREEBSD) { return FreeBSDPOSIX.PASSWD; } else if (Platform.IS_OPENBSD) { return OpenBSDPOSIX.PASSWD; } else if (Platform.IS_WINDOWS) { return WindowsPOSIX.PASSWD; } else if (jnr.ffi.Platform.getNativePlatform().getOS().equals(jnr.ffi.Platform.OS.AIX)) { return AixPOSIX.PASSWD; } return null; } else if (Group.class.isAssignableFrom(klazz)) { return BaseNativePOSIX.GROUP; } else if (HANDLE.class.isAssignableFrom(klazz)) { return HANDLE.Converter; } return null; } public ToNativeConverter getToNativeConverter(Class klazz) { if (FileStat.class.isAssignableFrom(klazz)) { return BaseNativePOSIX.FileStatConverter; } else if (NativeTimes.class.isAssignableFrom(klazz)) { return BaseNativePOSIX.TimesConverter; } else if (Constant.class.isAssignableFrom(klazz)) { return BaseNativePOSIX.ConstantConverter; } else if (WString.class.isAssignableFrom(klazz)) { return WString.Converter; } else if (HANDLE.class.isAssignableFrom(klazz)) { return HANDLE.Converter; } else if (MsgHdr.class.isAssignableFrom(klazz)) { return BaseNativePOSIX.MsgHdrConverter; } return null; } public final ToNativeConverter getToNativeConverter(Class klazz, ToNativeContext context) { return getToNativeConverter(klazz); } public final FromNativeConverter getFromNativeConverter(Class klazz, FromNativeContext context) { return getFromNativeConverter(klazz); } } jnr-posix-3.0.12/src/main/java/jnr/posix/Passwd.java000066400000000000000000000005461252174056300222330ustar00rootroot00000000000000package jnr.posix; public interface Passwd { public String getLoginName(); public String getPassword(); public long getUID(); public long getGID(); public int getPasswdChangeTime(); public String getAccessClass(); public String getGECOS(); public String getHome(); public String getShell(); public int getExpire(); } jnr-posix-3.0.12/src/main/java/jnr/posix/SignalHandler.java000066400000000000000000000001331252174056300234750ustar00rootroot00000000000000package jnr.posix; public interface SignalHandler { public void handle(int signal); } jnr-posix-3.0.12/src/main/java/jnr/posix/SimpleFunctionMapper.java000066400000000000000000000020731252174056300250730ustar00rootroot00000000000000package jnr.posix; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class SimpleFunctionMapper implements jnr.ffi.mapper.FunctionMapper { private final Map functionNameMap; private SimpleFunctionMapper(Map map) { functionNameMap = Collections.unmodifiableMap(new HashMap(map)); } public String mapFunctionName(String functionName, Context context) { String nativeFunction = functionNameMap.get(functionName); return nativeFunction != null ? nativeFunction : functionName; } public static class Builder { private final Map functionNameMap = Collections.synchronizedMap(new HashMap()); public Builder map(String posixName, String nativeFunction) { functionNameMap.put(posixName, nativeFunction); return this; } public SimpleFunctionMapper build() { return new SimpleFunctionMapper(functionNameMap); } } } jnr-posix-3.0.12/src/main/java/jnr/posix/SocketMacros.java000066400000000000000000000003361252174056300233640ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; /** * @author Bob McWhirter */ public interface SocketMacros { public int CMSG_SPACE(int l); public int CMSG_LEN(int l); public Pointer CMSG_DATA(Pointer cmsg); } jnr-posix-3.0.12/src/main/java/jnr/posix/SolarisFileStat32.java000066400000000000000000000052611252174056300242060ustar00rootroot00000000000000 package jnr.posix; import jnr.ffi.StructLayout; public class SolarisFileStat32 extends BaseFileStat { static final class Layout extends StructLayout { Layout(jnr.ffi.Runtime runtime) { super(runtime); } public static final int _ST_FSTYPSZ = 16; /* array size for file system type name */ public final Signed32 st_dev = new Signed32(); public final SignedLong[] st_pad1 = array(new SignedLong[3]); public final Signed64 st_ino = new Signed64(); public final Signed32 st_mode = new Signed32(); public final Signed32 st_nlink = new Signed32(); public final Signed32 st_uid = new Signed32(); public final Signed32 st_gid = new Signed32(); public final Signed32 st_rdev = new Signed32(); public final SignedLong[] st_pad2 = array(new SignedLong[2]); public final Signed64 st_size = new Signed64(); public final Signed32 st_atim_sec = new Signed32(); public final Signed32 st_atim_nsec = new Signed32(); public final Signed32 st_mtim_sec = new Signed32(); public final Signed32 st_mtim_nsec = new Signed32(); public final Signed32 st_ctim_sec = new Signed32(); public final Signed32 st_ctim_nsec = new Signed32(); public final Signed32 st_blksize = new Signed32(); public final Signed64 st_blocks = new Signed64(); public final Signed8[] st_fstype = array(new Signed8[_ST_FSTYPSZ]); public final SignedLong[] st_pad4 = array(new SignedLong[8]); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public SolarisFileStat32(NativePOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atim_sec.get(memory); } public long blocks() { return layout.st_blocks.get(memory); } public long blockSize() { return layout.st_blksize.get(memory); } public long ctime() { return layout.st_ctim_sec.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return layout.st_mode.get(memory) & 0xffff; } public long mtime() { return layout.st_mtim_sec.get(memory); } public int nlink() { return layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout.st_size.get(memory); } public int uid() { return layout.st_uid.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/SolarisFileStat64.java000066400000000000000000000050641252174056300242140ustar00rootroot00000000000000 package jnr.posix; import jnr.ffi.*; public class SolarisFileStat64 extends BaseFileStat { static final class Layout extends StructLayout { Layout(jnr.ffi.Runtime runtime) { super(runtime); } public static final int _ST_FSTYPSZ = 16; /* array size for file system type name */ public final UnsignedLong st_dev = new UnsignedLong(); public final Signed64 st_ino = new Signed64(); public final Signed32 st_mode = new Signed32(); public final Signed32 st_nlink = new Signed32(); public final Signed32 st_uid = new Signed32(); public final Signed32 st_gid = new Signed32(); public final UnsignedLong st_rdev = new UnsignedLong(); public final Signed64 st_size = new Signed64(); public final SignedLong st_atim_sec = new SignedLong(); public final SignedLong st_atim_nsec = new SignedLong(); public final SignedLong st_mtim_sec = new SignedLong(); public final SignedLong st_mtim_nsec = new SignedLong(); public final SignedLong st_ctim_sec = new SignedLong(); public final SignedLong st_ctim_nsec = new SignedLong(); public final Signed32 st_blksize = new Signed32(); public final Signed64 st_blocks = new Signed64(); public final Signed8[] st_fstype = array(new Signed8[_ST_FSTYPSZ]); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public SolarisFileStat64() { this(null); } public SolarisFileStat64(NativePOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atim_sec.get(memory); } public long blocks() { return layout.st_blocks.get(memory); } public long blockSize() { return layout.st_blksize.get(memory); } public long ctime() { return layout.st_ctim_sec.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return layout.st_mode.get(memory) & 0xffff; } public long mtime() { return layout.st_mtim_sec.get(memory); } public int nlink() { return layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout.st_size.get(memory); } public int uid() { return layout.st_uid.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/SolarisLibC.java000066400000000000000000000001061252174056300231300ustar00rootroot00000000000000package jnr.posix; public interface SolarisLibC extends UnixLibC { } jnr-posix-3.0.12/src/main/java/jnr/posix/SolarisPOSIX.java000066400000000000000000000023251252174056300232260ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Sysconf; import jnr.ffi.Pointer; import jnr.ffi.mapper.FromNativeContext; import jnr.posix.util.MethodName; import jnr.posix.util.Platform; import java.io.FileDescriptor; import static jnr.constants.platform.Errno.ENOENT; final class SolarisPOSIX extends BaseNativePOSIX { SolarisPOSIX(LibCProvider libc, POSIXHandler handler) { super(libc, handler); } public FileStat allocateStat() { return Platform.IS_32_BIT ? new SolarisFileStat32(this) : new SolarisFileStat64(this); } public MsgHdr allocateMsgHdr() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public SocketMacros socketMacros() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public long sysconf(Sysconf name) { return libc().sysconf(name); } public Times times() { return NativeTimes.times(this); } public static final PointerConverter PASSWD = new PointerConverter() { public Object fromNative(Object arg, FromNativeContext ctx) { return arg != null ? new SolarisPasswd((Pointer) arg) : null; } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/SolarisPasswd.java000066400000000000000000000036561252174056300235750ustar00rootroot00000000000000 package jnr.posix; import jnr.ffi.StructLayout; public class SolarisPasswd extends NativePasswd implements Passwd { static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final UTF8StringRef pw_name = new UTF8StringRef(); // user name public final UTF8StringRef pw_passwd = new UTF8StringRef(); // password (encrypted) public final Signed32 pw_uid = new Signed32(); // user id public final Signed32 pw_gid = new Signed32(); // user id public final Pointer pw_age = new Pointer(); // unused public final Pointer pw_comment = new Pointer();// unused public final UTF8StringRef pw_gecos = new UTF8StringRef(); // login info public final UTF8StringRef pw_dir = new UTF8StringRef(); // home directory public final UTF8StringRef pw_shell = new UTF8StringRef(); // default shell } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public SolarisPasswd(jnr.ffi.Pointer memory) { super(memory); } public java.lang.String getAccessClass() { return "unknown"; } public java.lang.String getGECOS() { return layout.pw_gecos.get(memory); } public long getGID() { return layout.pw_gid.get(memory); } public java.lang.String getHome() { return layout.pw_dir.get(memory); } public java.lang.String getLoginName() { return layout.pw_name.get(memory); } public int getPasswdChangeTime() { return 0; } public java.lang.String getPassword() { return layout.pw_passwd.get(memory); } public java.lang.String getShell() { return layout.pw_shell.get(memory); } public long getUID() { return layout.pw_uid.get(memory); } public int getExpire() { return Integer.MAX_VALUE; } } jnr-posix-3.0.12/src/main/java/jnr/posix/SpawnAttribute.java000066400000000000000000000061171252174056300237460ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.*; import jnr.ffi.byref.NumberByReference; public abstract class SpawnAttribute { public static final int RESETIDS = 0x0001; /* [SPN] R[UG]ID not E[UG]ID */ public static final int SETPGROUP = 0x0002; /* [SPN] set non-parent PGID */ public static final int SETSIGDEF = 0x0004; /* [SPN] reset sigset default */ public static final int SETSIGMASK = 0x0008; /* [SPN] set signal mask */ abstract boolean set(POSIX posix, Pointer nativeFileActions); public static SpawnAttribute pgroup(long pgroup) { return new PGroup(pgroup); } public static SpawnAttribute flags(short flags) { return new SetFlags(flags); } public static SpawnAttribute sigdef(long sigdef) { throw new RuntimeException("sigdefault not yet supported"); // return new Sigdef(sigdef); } public static SpawnAttribute sigmask(long sigmask) { throw new RuntimeException("sigmask not yet supported"); // return new Sigmask(sigmask); } private static final class PGroup extends SpawnAttribute { final long pgroup; public PGroup(long pgroup) { this.pgroup = pgroup; } final boolean set(POSIX posix, Pointer nativeSpawnAttr) { return ((UnixLibC) posix.libc()).posix_spawnattr_setpgroup(nativeSpawnAttr, pgroup) == 0; } public String toString() { return "SpawnAttribute::PGroup(pgroup = " + pgroup + ")"; } } private static final class SetFlags extends SpawnAttribute { final short flags; public SetFlags(short flags) { this.flags = flags; } final boolean set(POSIX posix, Pointer nativeSpawnAttr) { return ((UnixLibC) posix.libc()).posix_spawnattr_setflags(nativeSpawnAttr, flags) == 0; } public String toString() { return "SpawnAttribute::SetFlags(flags = " + Integer.toHexString(flags) + ")"; } } private static final class Sigmask extends SpawnAttribute { final long sigmask; public Sigmask(long sigmask) { this.sigmask = sigmask; } final boolean set(POSIX posix, Pointer nativeSpawnAttr) { throw new RuntimeException("sigmask not yet supported"); // return ((UnixLibC) posix.libc()).posix_spawnattr_setsigmask(nativeSpawnAttr, mask) == 0; } public String toString() { return "SpawnAttribute::Sigmask(mask = " + Long.toHexString(sigmask) + ")"; } } private static final class Sigdef extends SpawnAttribute { final long sigdef; public Sigdef(long sigdef) { this.sigdef = sigdef; } final boolean set(POSIX posix, Pointer nativeSpawnAttr) { throw new RuntimeException("sigdefault not yet supported"); // return ((UnixLibC) posix.libc()).posix_spawnattr_setsigdefault(nativeSpawnAttr, sigdef) == 0; } public String toString() { return "SpawnAttribute::Sigdef(def = " + Long.toHexString(sigdef) + ")"; } } } jnr-posix-3.0.12/src/main/java/jnr/posix/SpawnFileAction.java000066400000000000000000000042601252174056300240150ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; public abstract class SpawnFileAction { abstract boolean act(POSIX posix, Pointer nativeFileActions); public static SpawnFileAction dup(int fd, int newfd) { return new Dup(fd, newfd); } public static SpawnFileAction open(String path, int fd, int flags, int mode) { return new Open(path, fd, flags, mode); } public static SpawnFileAction close(int fd) { return new Close(fd); } private static final class Dup extends SpawnFileAction { final int fd, newfd; public Dup(int fd, int newfd) { this.fd = fd; this.newfd = newfd; } final boolean act(POSIX posix, Pointer nativeFileActions) { return ((UnixLibC) posix.libc()).posix_spawn_file_actions_adddup2(nativeFileActions, fd, newfd) == 0; } public String toString() { return "SpawnFileAction::Dup(old = " + fd + ", new = " + newfd + ")"; } } private static final class Open extends SpawnFileAction { final String path; final int fd; final int flags, mode; public Open(String path, int fd, int flags, int mode) { this.path = path; this.fd = fd; this.flags = flags; this.mode = mode; } final boolean act(POSIX posix, Pointer nativeFileActions) { return ((UnixLibC) posix.libc()).posix_spawn_file_actions_addopen(nativeFileActions, fd, path, flags, mode) == 0; } public String toString() { return "SpawnFileAction::Open(path = '" + path + "', fd = " + fd + ", flags = " + Integer.toHexString(flags) + ", mode = " + Integer.toHexString(mode) + ")"; } } private static final class Close extends SpawnFileAction { final int fd; public Close(int fd) { this.fd = fd; } final boolean act(POSIX posix, Pointer nativeFileActions) { return ((UnixLibC) posix.libc()).posix_spawn_file_actions_addclose(nativeFileActions, fd) == 0; } public String toString() { return "SpawnFileAction::Close(fd = " + fd + ")"; } } } jnr-posix-3.0.12/src/main/java/jnr/posix/Times.java000066400000000000000000000002721252174056300220470ustar00rootroot00000000000000package jnr.posix; public interface Times { public abstract long utime(); public abstract long stime(); public abstract long cutime(); public abstract long cstime(); } jnr-posix-3.0.12/src/main/java/jnr/posix/Timeval.java000066400000000000000000000003331252174056300223650ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Struct; abstract public class Timeval extends Struct { public Timeval(jnr.ffi.Runtime runtime) { super(runtime); } abstract public void setTime(long[] timeval); } jnr-posix-3.0.12/src/main/java/jnr/posix/UTimBuf64.java000066400000000000000000000005611252174056300224540ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Struct; public final class UTimBuf64 extends Struct { public final Signed64 actime = new Signed64(); public final Signed64 modtime = new Signed64(); public UTimBuf64(jnr.ffi.Runtime runtime, long actime, long modtime) { super(runtime); this.actime.set(actime); this.modtime.set(modtime); } } jnr-posix-3.0.12/src/main/java/jnr/posix/UnixLibC.java000066400000000000000000000037101252174056300224430ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; import jnr.ffi.annotations.In; import jnr.ffi.annotations.Out; import jnr.ffi.byref.ByReference; import jnr.ffi.byref.IntByReference; import jnr.ffi.byref.NumberByReference; import jnr.ffi.byref.ShortByReference; import jnr.ffi.types.pid_t; public interface UnixLibC extends LibC { public int posix_spawn(@Out ByReference pid, @In CharSequence path, @In Pointer fileActions, @In Pointer attr, @In CharSequence[] argv, @In CharSequence[] envp); public int posix_spawnp(@Out ByReference pid, @In CharSequence path, @In Pointer fileActions, @In Pointer attr, @In CharSequence[] argv, @In CharSequence[] envp); public int posix_spawn_file_actions_init(Pointer fileActions); public int posix_spawn_file_actions_destroy(Pointer fileActions); public int posix_spawn_file_actions_addclose(Pointer fileActions, int filedes); public int posix_spawn_file_actions_addopen(Pointer fileActions, int filedes, CharSequence path, int oflag, int mode); public int posix_spawn_file_actions_adddup2(Pointer fileActions, int filedes, int newfiledes); public int posix_spawnattr_init(Pointer attr); public int posix_spawnattr_destroy(Pointer attr); public int posix_spawnattr_setflags(Pointer attr, short flags); public int posix_spawnattr_getflags(Pointer attr, ShortByReference flags); public int posix_spawnattr_setpgroup(Pointer attr, @pid_t long pgroup); public int posix_spawnattr_getpgroup(Pointer attr, NumberByReference pgroup); public int posix_spawnattr_setsigmask(Pointer attr, Pointer sigmask); public int posix_spawnattr_getsigmask(Pointer attr, Pointer sigmask); public int posix_spawnattr_setsigdefault(Pointer attr, Pointer sigdefault); public int posix_spawnattr_getsigdefault(Pointer attr, Pointer sigdefault); public int sigprocmask(int how, Pointer set, Pointer get); } jnr-posix-3.0.12/src/main/java/jnr/posix/WString.java000066400000000000000000000022021252174056300223560ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Memory; import jnr.ffi.Pointer; import jnr.ffi.mapper.ToNativeContext; import jnr.ffi.mapper.ToNativeConverter; import jnr.posix.util.WindowsHelpers; public final class WString { static final jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getSystemRuntime(); private final byte[] bytes; WString(String string) { bytes = WindowsHelpers.toWString(string); } private WString(byte[] bytes) { this.bytes = bytes; } public static WString path(String path) { return new WString(WindowsHelpers.toWPath(path)); } public static final ToNativeConverter Converter = new ToNativeConverter() { public Pointer toNative(WString value, ToNativeContext context) { if (value == null) { return null; } Pointer memory = Memory.allocateDirect(runtime, value.bytes.length + 1, true); memory.put(0, value.bytes, 0, value.bytes.length); return memory; } public Class nativeType() { return Pointer.class; } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/WindowsChildRecord.java000066400000000000000000000007531252174056300245270ustar00rootroot00000000000000/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jnr.posix; /** * * @author enebo */ public class WindowsChildRecord { private final HANDLE process; private final int pid; public WindowsChildRecord(HANDLE process, int pid) { this.process = process; this.pid = pid; } public HANDLE getProcess() { return process; } public int getPid() { return pid; } } jnr-posix-3.0.12/src/main/java/jnr/posix/WindowsFileStat.java000066400000000000000000000112731252174056300240570ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.StructLayout; // http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx // This layout is meant to be used with stat64() family so _USE_32BIT_TIME_T is not in play. public class WindowsFileStat extends BaseFileStat { private static final class Layout extends StructLayout { private Layout(jnr.ffi.Runtime runtime) { super(runtime); } public final Signed32 st_dev = new Signed32(); public final Signed16 st_ino = new Signed16(); public final Signed16 st_mode = new Signed16(); public final Signed16 st_nlink = new Signed16(); public final Signed16 st_uid = new Signed16(); public final Signed16 st_gid = new Signed16(); public final Signed32 st_rdev = new Signed32(); public final Signed64 st_size = new Signed64(); public final Signed64 st_atime = new Signed64(); public final Signed64 st_mtime = new Signed64(); public final Signed64 st_ctime = new Signed64(); } private static final Layout layout = new Layout(jnr.ffi.Runtime.getSystemRuntime()); public WindowsFileStat(NativePOSIX posix) { super(posix, layout); } public long atime() { return layout.st_atime.get(memory); } public long blockSize() { return 512; } public long blocks() { return (layout.st_size.get(memory) + 512 - 1) / 512; } public long ctime() { return layout.st_ctime.get(memory); } public long dev() { return layout.st_dev.get(memory); } public int gid() { return layout.st_gid.get(memory); } public long ino() { return layout.st_ino.get(memory); } public int mode() { return layout.st_mode.get(memory) & ~(S_IWGRP | S_IWOTH) & 0xffff; } public long mtime() { return layout.st_mtime.get(memory); } public int nlink() { return layout.st_nlink.get(memory); } public long rdev() { return layout.st_rdev.get(memory); } public long st_size() { return layout.st_size.get(memory); } public int uid() { return layout.st_uid.get(memory); } // FIXME: Implement @Override public boolean groupMember(int gid) { return true; } @Override public boolean isExecutable() { if (isOwned()) return (mode() & S_IXUSR) != 0; if (isGroupOwned()) return (mode() & S_IXGRP) != 0; if ((mode() & S_IXOTH) != 0) return false; return true; } @Override public boolean isExecutableReal() { if (isROwned()) return (mode() & S_IXUSR) != 0; if (groupMember(gid())) return (mode() & S_IXGRP) != 0; if ((mode() & S_IXOTH) != 0) return false; return true; } // FIXME: Implement @Override public boolean isOwned() { return true; } // FIXME: Implement @Override public boolean isROwned() { return true; } @Override public boolean isReadable() { if (isOwned()) return (mode() & S_IRUSR) != 0; if (isGroupOwned()) return (mode() & S_IRGRP) != 0; if ((mode() & S_IROTH) != 0) return false; return true; } @Override public boolean isReadableReal() { if (isROwned()) return (mode() & S_IRUSR) != 0; if (groupMember(gid())) return (mode() & S_IRGRP) != 0; if ((mode() & S_IROTH) != 0) return false; return true; } @Override public boolean isWritable() { if (isOwned()) return (mode() & S_IWUSR) != 0; if (isGroupOwned()) return (mode() & S_IWGRP) != 0; if ((mode() & S_IWOTH) != 0) return false; return true; } @Override public boolean isWritableReal() { if (isROwned()) return (mode() & S_IWUSR) != 0; if (groupMember(gid())) return (mode() & S_IWGRP) != 0; if ((mode() & S_IWOTH) != 0) return false; return true; } @Override public java.lang.String toString() { return "st_dev: " + layout.st_dev.get(memory) + ", st_mode: " + Integer.toOctalString(mode()) + ", layout.st_nlink: " + layout.st_nlink.get(memory) + ", layout.st_rdev: " + layout.st_rdev.get(memory) + ", layout.st_size: " + layout.st_size.get(memory) + ", layout.st_uid: " + layout.st_uid.get(memory) + ", layout.st_gid: " + layout.st_gid.get(memory) + ", layout.st_atime: " + layout.st_atime.get(memory) + ", layout.st_ctime: " + layout.st_ctime.get(memory) + ", layout.st_mtime: " + layout.st_mtime.get(memory) + ", layout.st_ino: " + layout.st_ino.get(memory); } } jnr-posix-3.0.12/src/main/java/jnr/posix/WindowsLibC.java000066400000000000000000000065211252174056300231550ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.Pointer; import jnr.ffi.annotations.In; import jnr.ffi.annotations.Out; import jnr.ffi.annotations.StdCall; import jnr.ffi.annotations.Transient; import jnr.ffi.byref.IntByReference; import java.nio.ByteBuffer; public interface WindowsLibC extends LibC { public static final int STD_INPUT_HANDLE = -10; public static final int STD_OUTPUT_HANDLE = -11; public static final int STD_ERROR_HANDLE = -12; public static final int NORMAL_PRIORITY_CLASS = 0x00000020; public static final int CREATE_UNICODE_ENVIRONMENT = 0x00000400; public static final int INFINITE = -1; public static final int FILE_TYPE_DISK = 0x0001; public static final int FILE_TYPE_CHAR = 0x0002; public static final int FILE_TYPE_PIPE = 0x0003; public static final int FILE_TYPE_REMOTE = 0x8000; public static final int FILE_TYPE_UNKNOWN = 0x0000; public int _open_osfhandle(HANDLE handle, int flags); public int _close(int fd); public int _getpid(); int _stat64(CharSequence path, @Out @Transient FileStat stat); int _umask(int mask); public int _wmkdir(@In WString path); public boolean RemoveDirectoryW(@In WString path); public int _wchmod(@In WString path, int pmode); public int _wchdir(@In WString path); public int _wstat64(@In WString path, @Out @Transient FileStat stat); @StdCall public boolean CreateProcessW(byte[] applicationName, @In @Out ByteBuffer buffer, WindowsSecurityAttributes processAttributes, WindowsSecurityAttributes threadAttributes, int inheritHandles, int creationFlags, @In Pointer envp, @In byte[] currentDirectory, WindowsStartupInfo startupInfo, WindowsProcessInformation processInformation); public int GetFileAttributesW(@In WString path); public int SetFileAttributesW(@In WString path, int flags); @StdCall public boolean GetExitCodeProcess(HANDLE handle, @Out Pointer exitCode); @StdCall public boolean GetExitCodeProcess(HANDLE handle, @Out IntByReference exitCode); @StdCall public int GetFileType(HANDLE handle); @StdCall public int GetFileSize(HANDLE handle, @Out IntByReference outSizeHigh); @StdCall public HANDLE GetStdHandle(int stdHandle); @StdCall public boolean CreateHardLinkW(@In WString oldname, @In WString newName, @In WString reserved); @StdCall HANDLE CreateFileW( byte[] lpFileName, int dwDesiredAccess, int dwShareMode, Pointer lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile ); @StdCall boolean SetEnvironmentVariableW( @In WString envName, @In WString envValue); @StdCall boolean SetFileTime( HANDLE hFile, FileTime lpCreationTime, FileTime lpLastAccessTime, FileTime lpLastWriteTime ); @StdCall boolean CloseHandle(HANDLE handle); @StdCall int WaitForSingleObject(HANDLE handle, int milliseconds); } jnr-posix-3.0.12/src/main/java/jnr/posix/WindowsPOSIX.java000066400000000000000000000560601252174056300232510ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Errno; import static jnr.constants.platform.Errno.*; import static jnr.constants.platform.windows.LastError.*; import jnr.ffi.Pointer; import jnr.ffi.byref.IntByReference; import jnr.ffi.mapper.FromNativeContext; import java.io.FileDescriptor; import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Map; import jnr.posix.util.MethodName; import jnr.posix.util.WindowsHelpers; final class WindowsPOSIX extends BaseNativePOSIX { private final static int FILE_TYPE_CHAR = 0x0002; private final static Map errorToErrnoMapper = new HashMap(); static { errorToErrnoMapper.put(ERROR_INVALID_FUNCTION.value(), EINVAL); errorToErrnoMapper.put(ERROR_FILE_NOT_FOUND.value(), ENOENT); errorToErrnoMapper.put(ERROR_PATH_NOT_FOUND.value(), ENOENT); errorToErrnoMapper.put(ERROR_TOO_MANY_OPEN_FILES.value(), EMFILE); errorToErrnoMapper.put(ERROR_ACCESS_DENIED.value(), EACCES); errorToErrnoMapper.put(ERROR_INVALID_HANDLE.value(), EBADF ); errorToErrnoMapper.put(ERROR_ARENA_TRASHED.value(), ENOMEM); errorToErrnoMapper.put(ERROR_NOT_ENOUGH_MEMORY.value(), ENOMEM); errorToErrnoMapper.put(ERROR_INVALID_BLOCK.value(), ENOMEM); errorToErrnoMapper.put(ERROR_BAD_ENVIRONMENT.value(), E2BIG ); errorToErrnoMapper.put(ERROR_BAD_FORMAT.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_INVALID_ACCESS.value(), EINVAL); errorToErrnoMapper.put(ERROR_INVALID_DATA.value(), EINVAL); errorToErrnoMapper.put(ERROR_INVALID_DRIVE.value(), ENOENT); errorToErrnoMapper.put(ERROR_CURRENT_DIRECTORY.value(), EACCES); errorToErrnoMapper.put(ERROR_NOT_SAME_DEVICE.value(), EXDEV ); errorToErrnoMapper.put(ERROR_NO_MORE_FILES.value(), ENOENT); errorToErrnoMapper.put(ERROR_WRITE_PROTECT.value(), EROFS ); errorToErrnoMapper.put(ERROR_BAD_UNIT.value(), ENODEV); errorToErrnoMapper.put(ERROR_NOT_READY.value(), ENXIO ); errorToErrnoMapper.put(ERROR_BAD_COMMAND.value(), EACCES); errorToErrnoMapper.put(ERROR_CRC.value(), EACCES); errorToErrnoMapper.put(ERROR_BAD_LENGTH.value(), EACCES); errorToErrnoMapper.put(ERROR_SEEK.value(), EIO); errorToErrnoMapper.put(ERROR_NOT_DOS_DISK.value(), EACCES); errorToErrnoMapper.put(ERROR_SECTOR_NOT_FOUND.value(), EACCES); errorToErrnoMapper.put(ERROR_OUT_OF_PAPER.value(), EACCES); errorToErrnoMapper.put(ERROR_WRITE_FAULT.value(), EIO); errorToErrnoMapper.put(ERROR_READ_FAULT.value(), EIO); errorToErrnoMapper.put(ERROR_GEN_FAILURE.value(), EACCES); errorToErrnoMapper.put(ERROR_LOCK_VIOLATION.value(), EACCES); errorToErrnoMapper.put(ERROR_SHARING_VIOLATION.value(), EACCES); errorToErrnoMapper.put(ERROR_WRONG_DISK.value(), EACCES); errorToErrnoMapper.put(ERROR_SHARING_BUFFER_EXCEEDED.value(), EACCES); errorToErrnoMapper.put(ERROR_BAD_NETPATH.value(), ENOENT); errorToErrnoMapper.put(ERROR_NETWORK_ACCESS_DENIED.value(), EACCES); errorToErrnoMapper.put(ERROR_BAD_NET_NAME.value(), ENOENT); errorToErrnoMapper.put(ERROR_FILE_EXISTS.value(), EEXIST); errorToErrnoMapper.put(ERROR_CANNOT_MAKE.value(), EACCES); errorToErrnoMapper.put(ERROR_FAIL_I24.value(), EACCES); errorToErrnoMapper.put(ERROR_INVALID_PARAMETER.value(), EINVAL); errorToErrnoMapper.put(ERROR_NO_PROC_SLOTS.value(), EAGAIN); errorToErrnoMapper.put(ERROR_DRIVE_LOCKED.value(), EACCES); errorToErrnoMapper.put(ERROR_BROKEN_PIPE.value(), EPIPE); errorToErrnoMapper.put(ERROR_DISK_FULL.value(), ENOSPC); errorToErrnoMapper.put(ERROR_INVALID_TARGET_HANDLE.value(), EBADF); errorToErrnoMapper.put(ERROR_INVALID_HANDLE.value(), EINVAL); errorToErrnoMapper.put(ERROR_WAIT_NO_CHILDREN.value(), ECHILD); errorToErrnoMapper.put(ERROR_CHILD_NOT_COMPLETE.value(), ECHILD); errorToErrnoMapper.put(ERROR_DIRECT_ACCESS_HANDLE.value(), EBADF); errorToErrnoMapper.put(ERROR_NEGATIVE_SEEK.value(), EINVAL); errorToErrnoMapper.put(ERROR_SEEK_ON_DEVICE.value(), EACCES); errorToErrnoMapper.put(ERROR_DIR_NOT_EMPTY.value(), ENOTEMPTY); errorToErrnoMapper.put(ERROR_DIRECTORY.value(), ENOTDIR); errorToErrnoMapper.put(ERROR_NOT_LOCKED.value(), EACCES); errorToErrnoMapper.put(ERROR_BAD_PATHNAME.value(), ENOENT); errorToErrnoMapper.put(ERROR_MAX_THRDS_REACHED.value(), EAGAIN); errorToErrnoMapper.put(ERROR_LOCK_FAILED.value(), EACCES); errorToErrnoMapper.put(ERROR_ALREADY_EXISTS.value(), EEXIST); errorToErrnoMapper.put(ERROR_INVALID_STARTING_CODESEG.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_INVALID_STACKSEG.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_INVALID_MODULETYPE.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_INVALID_EXE_SIGNATURE.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_EXE_MARKED_INVALID.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_BAD_EXE_FORMAT.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_ITERATED_DATA_EXCEEDS_64k.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_INVALID_MINALLOCSIZE.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_DYNLINK_FROM_INVALID_RING.value(),ENOEXEC); errorToErrnoMapper.put(ERROR_IOPL_NOT_ENABLED.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_INVALID_SEGDPL.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_AUTODATASEG_EXCEEDS_64k.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_RING2SEG_MUST_BE_MOVABLE.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_RELOC_CHAIN_XEEDS_SEGLIM.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_INFLOOP_IN_RELOC_CHAIN.value(), ENOEXEC); errorToErrnoMapper.put(ERROR_FILENAME_EXCED_RANGE.value(), ENOENT); errorToErrnoMapper.put(ERROR_NESTING_NOT_ALLOWED.value(), EAGAIN); // ERROR_PIPE_LOCAL (in MRI) errorToErrnoMapper.put(229, EPIPE); errorToErrnoMapper.put(ERROR_BAD_PIPE.value(), EPIPE); errorToErrnoMapper.put(ERROR_PIPE_BUSY.value(), EAGAIN); errorToErrnoMapper.put(ERROR_NO_DATA.value(), EPIPE); errorToErrnoMapper.put(ERROR_PIPE_NOT_CONNECTED.value(), EPIPE); errorToErrnoMapper.put(ERROR_OPERATION_ABORTED.value(), EINTR); errorToErrnoMapper.put(ERROR_NOT_ENOUGH_QUOTA.value(), ENOMEM); errorToErrnoMapper.put(ERROR_MOD_NOT_FOUND.value(), ENOENT); errorToErrnoMapper.put(WSAENAMETOOLONG.value(), ENAMETOOLONG); errorToErrnoMapper.put(WSAENOTEMPTY.value(), ENOTEMPTY); errorToErrnoMapper.put(WSAEINTR.value(), EINTR); errorToErrnoMapper.put(WSAEBADF.value(), EBADF); errorToErrnoMapper.put(WSAEACCES.value(), EACCES); errorToErrnoMapper.put(WSAEFAULT.value(), EFAULT); errorToErrnoMapper.put(WSAEINVAL.value(), EINVAL); errorToErrnoMapper.put(WSAEMFILE.value(), EMFILE); } WindowsPOSIX(LibCProvider libc, POSIXHandler handler) { super(libc, handler); } @Override public FileStat allocateStat() { return new WindowsFileStat(this); } public MsgHdr allocateMsgHdr() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } public SocketMacros socketMacros() { handler.unimplementedError(MethodName.getCallerMethodName()); return null; } @Override public int kill(int pid, int signal) { handler.unimplementedError("kill"); return -1; } @Override public int chmod(String filename, int mode) { return wlibc()._wchmod(WString.path(filename), mode); } @Override public int chdir(String path) { return wlibc()._wchdir(WString.path(path)); } @Override public int chown(String filename, int user, int group) { return 0; } @Override public int exec(String path, String[] argv) { if (argv.length == 1) return spawn(true, argv[0], null, path, null); return aspawn(true, null, argv, path, null); } @Override public CharSequence crypt(CharSequence key, CharSequence salt) { return JavaLibCHelper.crypt(key, salt); } @Override public byte[] crypt(byte[] key, byte[] salt) { return JavaLibCHelper.crypt(key, salt); } @Override public int exec(String path, String[] argv, String[] envp) { if (argv.length == 1) return spawn(true, argv[0], null, path, envp); return aspawn(true, null, argv, path, envp); } @Override public int execv(String path, String[] argv) { handler.unimplementedError("egid"); return -1; } @Override public int getegid() { handler.unimplementedError("egid"); return -1; } @Override public int setegid(int egid) { handler.unimplementedError("setegid"); return -1; } @Override public int geteuid() { return 0; } @Override public String getenv(String envName) { handler.unimplementedError("getenv"); return null; } @Override public int seteuid(int euid) { handler.unimplementedError("seteuid"); return -1; } @Override public int getuid() { return 0; } @Override public int setuid(int uid) { handler.unimplementedError("setuid"); return -1; } @Override public int getgid() { return 0; } @Override public int setgid(int gid) { handler.unimplementedError("setgid"); return -1; } @Override public int getpgid(int pid) { handler.unimplementedError("getpgid"); return -1; } @Override public int getpgid() { handler.unimplementedError("getpgid"); return -1; } @Override public int setpgid(int pid, int pgid) { handler.unimplementedError("setpgid"); return -1; } @Override public int getpriority(int which, int who) { handler.unimplementedError("getpriority"); return -1; } @Override public int setpriority(int which, int who, int prio) { handler.unimplementedError("setpriority"); return -1; } @Override public int getpid(){ return wlibc()._getpid(); } @Override public int getppid() { return 0; } @Override public int lchmod(String filename, int mode) { handler.unimplementedError("lchmod"); return -1; } @Override public int lchown(String filename, int user, int group) { handler.unimplementedError("lchown"); return -1; } @Override public int fstat(FileDescriptor fileDescriptor, FileStat stat) { int fd = ((WindowsLibC) libc())._open_osfhandle(helper.gethandle(fileDescriptor), 0); try { return libc().fstat(fd, stat); } finally { ((WindowsLibC) libc())._close(fd); } } @Override public FileStat lstat(String path) { return stat(path); } @Override public int lstat(String path, FileStat stat) { return stat(path, stat); // windows stat honors windows equiv of softlinks and dangling ones. } @Override public int stat(String path, FileStat stat) { return wlibc()._wstat64(new WString(path), stat); } @Override public String readlink(String oldpath) { handler.unimplementedError("readlink"); return null; } @Override public int setenv(String envName, String envValue, int overwrite) { if (envName.contains("=")) { handler.error(EINVAL, "setenv", envName); return -1; } // FIXME: We do not have getenv implemented yet. So we are ignoring for now // POSIX specified. Existence is success if overwrite is 0. // if (overwrite == 0 && getenv(envName) != null) return 0; if (!wlibc().SetEnvironmentVariableW(new WString(envName), new WString(envValue))) { handler.error(EINVAL, "setenv", envName); return -1; } return 0; } @Override public int umask(int mask) { return wlibc()._umask(mask); } @Override public int unsetenv(String envName) { if (!wlibc().SetEnvironmentVariableW(new WString(envName), null)) { handler.error(EINVAL, "unsetenv", envName); return -1; } return 0; } private static final int GENERIC_ALL = 0x10000000; private static final int GENERIC_READ = 0x80000000; private static final int GENERIC_WRITE = 0x40000000; private static final int GENERIC_EXECUTE = 0x2000000; private static final int FILE_SHARE_DELETE = 0x00000004; private static final int FILE_SHARE_READ = 0x00000001; private static final int FILE_SHARE_WRITE = 0x00000002; private static final int CREATE_ALWAYS = 2; private static final int CREATE_NEW = 1; private static final int OPEN_ALWAYS = 4; private static final int OPEN_EXISTING = 3; private static final int TRUNCATE_EXISTING = 5; public static final int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000; @Override public int utimes(String path, long[] atimeval, long[] mtimeval) { byte[] wpath = WindowsHelpers.toWPath(path); FileTime aTime = atimeval == null ? null : unixTimeToFileTime(atimeval[0]); FileTime mTime = mtimeval == null ? null : unixTimeToFileTime(mtimeval[0]); if (aTime == null || mTime == null) { FileTime nowFile = unixTimeToFileTime(System.currentTimeMillis() / 1000L); if (aTime == null) aTime = nowFile; if (mTime == null) mTime = nowFile; } HANDLE handle = wlibc().CreateFileW(wpath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); if (!handle.isValid()) { return -1; // TODO proper error handling } boolean timeSet = wlibc().SetFileTime(handle, null, aTime, mTime); wlibc().CloseHandle(handle); return timeSet ? 0 : -1; } private FileTime unixTimeToFileTime(long unixTime) { // FILETIME is a 64-bit unsigned integer representing // the number of 100-nanosecond intervals since January 1, 1601 // UNIX timestamp is number of seconds since January 1, 1970 // 116444736000000000 = 10000000 * 60 * 60 * 24 * 365 * 369 + 89 leap days long ft = (unixTime + 11644473600L) * 10000000L; FileTime fileTime = new FileTime(getRuntime()); fileTime.dwLowDateTime.set(ft & 0xFFFFFFFFL); fileTime.dwHighDateTime.set((ft >> 32) & 0xFFFFFFFFL); return fileTime; } @Override public int wait(int[] status) { handler.unimplementedError("wait"); return -1; } @Override public int waitpid(int pid, int[] status, int flags) { handler.unimplementedError("waitpid"); return -1; } @Override public int waitpid(long pid, int[] status, int flags) { handler.unimplementedError("waitpid"); return -1; } @Override public String getlogin() { return helper.getlogin(); } @Override public int endgrent() { return 0; } @Override public int endpwent() { return helper.endpwent(); } @Override public Group getgrent() { return null; } @Override public Passwd getpwent() { return null; } @Override public Group getgrgid(int which) { return null; } @Override public Passwd getpwnam(String which) { return null; } @Override public Group getgrnam(String which) { return null; } @Override public int setgrent() { return 0; } @Override public int setpwent() { return helper.setpwent(); } @Override public Passwd getpwuid(int which) { return null; } @Override public boolean isatty(FileDescriptor fd) { HANDLE handle = helper.gethandle(fd); int type = wlibc().GetFileType(handle); return type == FILE_TYPE_CHAR; } @Override public int mkdir(String path, int mode) { WString widePath = WString.path(path); int res = -1; if (wlibc()._wmkdir(widePath) == 0) { res = wlibc()._wchmod(widePath, mode); } if (res < 0) { int errno = errno(); handler.error(Errno.valueOf(errno), "mkdir", path); } return res; } // FIXME: Should this and other fields be part of constantine/jnr-constants? static final int FILE_ATTRIBUTE_READONLY = 1; static final int INVALID_FILE_ATTRIBUTES = -1; /** * The logic here is a bit strange and this copies MRI (Ruby) which may not be language * agnostic, but windows (win7 and others) automatically mark folders as read-only when * it contains other files and folders within it. This document explains more: * http://support.microsoft.com/kb/326549 * I think the logic is based around idea that if you removed all other files it would * be empty but will stay marked as read-only. */ @Override public int rmdir(String path) { WString pathW = WString.path(path); int attr = wlibc().GetFileAttributesW(pathW); boolean isReadOnly = attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_READONLY) != 0; if (isReadOnly) wlibc().SetFileAttributesW(pathW, attr & ~FILE_ATTRIBUTE_READONLY); if (!wlibc().RemoveDirectoryW(pathW)) { int errno = errno(); if (isReadOnly) wlibc().SetFileAttributesW(pathW, attr & FILE_ATTRIBUTE_READONLY); handler.error(mapErrorToErrno(errno), "rmdir", path); return -1; } return 0; } @Override public int link(String oldpath, String newpath) { boolean linkCreated = wlibc().CreateHardLinkW(WString.path(newpath), WString.path(oldpath), null); if (!linkCreated) { int error = errno(); handler.error(mapErrorToErrno(error), "link", oldpath + " or " + newpath); return error; } else { return 0; } } /** * @param overlay is P_OVERLAY if true and P_NOWAIT if false * @param program to be invoked * @param argv is all args including argv0 being what is executed * @param path is path to be searched when needed (delimited by ; on windows) * @return the pid */ public int aspawn(boolean overlay, String program, String[] argv, String path, String[] envp) { try { if (argv.length == 0) return -1; String[] cmds = WindowsHelpers.processCommandArgs(this, program, argv, path); return childResult(createProcess("aspawn", cmds[0], cmds[1], null, null, null, null, envp), overlay); } catch (Exception e) { return -1; } } private WindowsLibC wlibc() { return (WindowsLibC) libc(); } /** * @param overlay is P_OVERLAY if true and P_NOWAIT if false * @param command full command string * @param program program to be invoked * @param path is path to be searched when needed (delimited by ; on windows) * * @return the pid */ public int spawn(boolean overlay, String command, String program, String path, String[] envp) { if (command == null) return -1; String[] cmds = WindowsHelpers.processCommandLine(this, command, program, path); return childResult(createProcess("spawn", cmds[0], cmds[1], null, null, null, null, envp), overlay); } private int childResult(WindowsChildRecord child, boolean overlay) { if (child == null) return -1; if (overlay) { IntByReference exitCode = new IntByReference(); WindowsLibC libc = (WindowsLibC) libc(); HANDLE handle = child.getProcess(); libc.WaitForSingleObject(handle, WindowsLibC.INFINITE); libc.GetExitCodeProcess(handle, exitCode); libc.CloseHandle(handle); System.exit(exitCode.getValue()); } return child.getPid(); } private static Errno mapErrorToErrno(int error) { Errno errno = errorToErrnoMapper.get(error); if (errno == null) { errno = __UNKNOWN_CONSTANT__; } return errno; } private static final int STARTF_USESTDHANDLES = 0x00000100; // Used by spawn and aspawn (Note: See fixme below...envp not hooked up yet) private WindowsChildRecord createProcess(String callingMethodName, String command, String program, WindowsSecurityAttributes securityAttributes, HANDLE input, HANDLE output, HANDLE error, String[] envp) { if (command == null && program == null) { handler.error(EFAULT, callingMethodName, "no command or program specified"); return null; } if (securityAttributes == null) { securityAttributes = new WindowsSecurityAttributes(getRuntime()); } WindowsStartupInfo startupInfo = new WindowsStartupInfo(getRuntime()); startupInfo.setFlags(STARTF_USESTDHANDLES); startupInfo.setStandardInput(input != null ? input : wlibc().GetStdHandle(WindowsLibC.STD_INPUT_HANDLE)); startupInfo.setStandardOutput(output != null ? output : wlibc().GetStdHandle(WindowsLibC.STD_OUTPUT_HANDLE)); startupInfo.setStandardError(error != null ? input : wlibc().GetStdHandle(WindowsLibC.STD_ERROR_HANDLE)); int creationFlags = WindowsLibC.NORMAL_PRIORITY_CLASS | WindowsLibC.CREATE_UNICODE_ENVIRONMENT; WindowsProcessInformation processInformation = new WindowsProcessInformation(getRuntime()); // FIXME: Convert envp into useful wideEnv Pointer wideEnv = null; byte[] programW = WindowsHelpers.toWString(program); byte[] cwd = WindowsHelpers.toWString(WindowsHelpers.escapePath(handler.getCurrentWorkingDirectory().toString()) +"\\"); ByteBuffer commandW = ByteBuffer.wrap(WindowsHelpers.toWString(command)); boolean returnValue = wlibc().CreateProcessW(programW, commandW, securityAttributes, securityAttributes, securityAttributes.getInheritHandle() ? 1: 0, creationFlags, wideEnv, cwd, startupInfo, processInformation); if (!returnValue) return null; wlibc().CloseHandle(processInformation.getThread()); // TODO: On winnt reverse sign of pid return new WindowsChildRecord(processInformation.getProcess(), processInformation.getPid()); } public static final PointerConverter PASSWD = new PointerConverter() { public Object fromNative(Object arg, FromNativeContext ctx) { throw new RuntimeException("no support for native passwd"); } }; } jnr-posix-3.0.12/src/main/java/jnr/posix/WindowsProcessInformation.java000066400000000000000000000013561252174056300261710ustar00rootroot00000000000000/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jnr.posix; /** * * @author enebo */ public class WindowsProcessInformation extends jnr.ffi.Struct { final Pointer hProcess = new Pointer(); final Pointer hThread = new Pointer(); final Unsigned32 dwProcessId = new Unsigned32(); final Unsigned32 dwThreadId = new Unsigned32(); public WindowsProcessInformation(jnr.ffi.Runtime runtime) { super(runtime); } public HANDLE getThread() { return new HANDLE(hThread.get()); } public HANDLE getProcess() { return new HANDLE(hProcess.get()); } public int getPid() { return dwProcessId.intValue(); } } jnr-posix-3.0.12/src/main/java/jnr/posix/WindowsSecurityAttributes.java000066400000000000000000000012401252174056300262130ustar00rootroot00000000000000package jnr.posix; import jnr.ffi.*; /** * */ public class WindowsSecurityAttributes extends jnr.ffi.Struct { public final Unsigned32 length = new Unsigned32(); public final Pointer securityDescriptor = new Pointer(); public final WBOOL inheritHandle = new WBOOL(); public WindowsSecurityAttributes(jnr.ffi.Runtime runtime) { super(runtime); // This seems like the sensible defaults for this. length.set(Struct.size(this)); inheritHandle.set(true); } public long getLength() { return length.get(); } public boolean getInheritHandle() { return inheritHandle.get(); } } jnr-posix-3.0.12/src/main/java/jnr/posix/WindowsStartupInfo.java000066400000000000000000000034401252174056300246170ustar00rootroot00000000000000/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jnr.posix; /** * * @author enebo */ public class WindowsStartupInfo extends jnr.ffi.Struct { public final Unsigned32 cb = new Unsigned32(); public final Pointer lpReserved = new Pointer(); //new UTF8String(); public final Pointer lpDesktop = new Pointer(); //UTF8String(); public final Pointer lpTitle = new Pointer(); //new UTF8String(); public final Unsigned32 dwX = new Unsigned32(); public final Unsigned32 dwY = new Unsigned32(); public final Unsigned32 dwXSize = new Unsigned32(); public final Unsigned32 dwYSize = new Unsigned32(); public final Unsigned32 dwXCountChars = new Unsigned32(); public final Unsigned32 dwYCountChars = new Unsigned32(); public final Unsigned32 dwFillAttribute = new Unsigned32(); public final Unsigned32 dwFlags = new Unsigned32(); public final Unsigned16 wShowWindow = new Unsigned16(); public final Unsigned16 cbReserved2 = new Unsigned16(); public final Pointer lpReserved2 = new Pointer(); public final Pointer standardInput = new Pointer(); public final Pointer standardOutput = new Pointer(); public final Pointer standardError = new Pointer(); public WindowsStartupInfo(jnr.ffi.Runtime runtime) { super(runtime); } public void setFlags(int value) { dwFlags.set(value); } public void setStandardInput(HANDLE standardInput) { this.standardInput.set(standardInput.toPointer()); } public void setStandardOutput(HANDLE standardOutput) { this.standardOutput.set(standardOutput.toPointer()); } public void setStandardError(HANDLE standardError) { this.standardError.set(standardError.toPointer()); } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/000077500000000000000000000000001252174056300210775ustar00rootroot00000000000000jnr-posix-3.0.12/src/main/java/jnr/posix/util/Chmod.java000066400000000000000000000074211252174056300230000ustar00rootroot00000000000000package jnr.posix.util; import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * Honor semantics of chmod as best we can in pure Java. Note, this uses reflection to be * more tolerant of different Java versions. */ public class Chmod { private static final boolean CHMOD_API_AVAILABLE; private static final Method setWritable; private static final Method setReadable; private static final Method setExecutable; static { boolean apiAvailable = false; Method setWritableVar = null; Method setReadableVar = null; Method setExecutableVar = null; try { setWritableVar = File.class.getMethod("setWritable", new Class[] {Boolean.TYPE, Boolean.TYPE}); setReadableVar = File.class.getMethod("setReadable", new Class[] {Boolean.TYPE, Boolean.TYPE}); setExecutableVar = File.class.getMethod("setExecutable", new Class[] {Boolean.TYPE, Boolean.TYPE}); apiAvailable = true; } catch (Exception e) { // failed to load methods, no chmod API available } setWritable = setWritableVar; setReadable = setReadableVar; setExecutable = setExecutableVar; CHMOD_API_AVAILABLE = apiAvailable; } public static int chmod(File file, String mode) { if (CHMOD_API_AVAILABLE) { // fast version char other = '0'; if (mode.length() >= 1) { other = mode.charAt(mode.length() - 1); } //char group = mode.charAt(mode.length() - 2); char user = '0'; if (mode.length() >= 3) { user = mode.charAt(mode.length() - 3); } //char setuidgid = mode.charAt(mode.length() - 3); // group and setuid/gid are ignored, no way to do them fast. Should we fall back on slow? if (!setPermissions(file, other, false)) return -1; if (!setPermissions(file, user, true)) return -1; return 0; } else { // slow version try { Process chmod = Runtime.getRuntime().exec("/bin/chmod " + mode + " " + file.getAbsolutePath()); chmod.waitFor(); return chmod.exitValue(); } catch (IOException ioe) { // FIXME: ignore? } catch (InterruptedException ie) { // FIXME: ignore? } } return -1; } private static boolean setPermissions(File file, char permChar, boolean userOnly) { int permValue = Character.digit(permChar, 8); try { if ((permValue & 1) != 0) { setExecutable.invoke(file, new Object[] {Boolean.TRUE, Boolean.valueOf(userOnly)}); } else { setExecutable.invoke(file, new Object[] {Boolean.FALSE, Boolean.valueOf(userOnly)}); } if ((permValue & 2) != 0) { setWritable.invoke(file, new Object[] {Boolean.TRUE, Boolean.valueOf(userOnly)}); } else { setWritable.invoke(file, new Object[] {Boolean.FALSE, Boolean.valueOf(userOnly)}); } if ((permValue & 4) != 0) { setReadable.invoke(file, new Object[] {Boolean.TRUE, Boolean.valueOf(userOnly)}); } else { setReadable.invoke(file, new Object[] {Boolean.FALSE, Boolean.valueOf(userOnly)}); } return true; } catch (IllegalAccessException iae) { // ignore, return false below } catch (InvocationTargetException ite) { // ignore, return false below } return false; } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/DefaultPOSIXHandler.java000066400000000000000000000040071252174056300254500ustar00rootroot00000000000000package jnr.posix.util; import jnr.constants.platform.Errno; import jnr.posix.POSIXHandler; import java.io.File; import java.io.InputStream; import java.io.PrintStream; import java.util.Arrays; import java.util.IllegalFormatException; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /** * A POSIXHandler with reasonable default behavior. */ public class DefaultPOSIXHandler implements POSIXHandler { public void error(Errno error, String extraData) { throw new RuntimeException("native error " + error.description() + " " + extraData); } public void error(Errno error, String methodName, String extraData) { throw new RuntimeException("native error calling " + methodName + ": " + error.description() + " " + extraData); } public void unimplementedError(String methodName) { throw new IllegalStateException(methodName + " is not implemented in jnr-posix"); } public void warn(WARNING_ID id, String message, Object... data) { String msg; try { msg = String.format(message, data); } catch (IllegalFormatException e) { msg = message + " " + Arrays.toString(data); } Logger.getLogger("jnr-posix").log(Level.WARNING, msg); } public boolean isVerbose() { return false; } public File getCurrentWorkingDirectory() { return new File("."); } public String[] getEnv() { String[] envp = new String[System.getenv().size()]; int i = 0; for (Map.Entry pair : System.getenv().entrySet()) { envp[i++] = new StringBuilder(pair.getKey()).append("=").append(pair.getValue()).toString(); } return envp; } public InputStream getInputStream() { return System.in; } public PrintStream getOutputStream() { return System.out; } public int getPID() { return 0; } public PrintStream getErrorStream() { return System.err; } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/ExecIt.java000066400000000000000000000141531252174056300231270ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * Copyright (C) 2008 JRuby Community * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ package jnr.posix.util; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import jnr.posix.POSIXHandler; public class ExecIt { protected final POSIXHandler handler; /** Creates a new instance of ShellLauncher */ public ExecIt(POSIXHandler handler) { this.handler = handler; } public int runAndWait(String... args) throws IOException, InterruptedException { return runAndWait(handler.getOutputStream(), args); } public int runAndWait(OutputStream output, String... args) throws IOException, InterruptedException { return runAndWait(output, handler.getErrorStream(), args); } public int runAndWait(OutputStream output, OutputStream error, String... args) throws IOException, InterruptedException { Process process = run(args); handleStreams(process, handler.getInputStream(), output, error); return process.waitFor(); } public Process run(String... args) throws IOException { File cwd = handler.getCurrentWorkingDirectory(); return Runtime.getRuntime().exec(args, handler.getEnv(), cwd); } private static class StreamPumper extends Thread { private InputStream in; private OutputStream out; private boolean onlyIfAvailable; private volatile boolean quit; private final Object waitLock = new Object(); StreamPumper(InputStream in, OutputStream out, boolean avail) { this.in = in; this.out = out; this.onlyIfAvailable = avail; } public void run() { byte[] buf = new byte[1024]; int numRead; boolean hasReadSomething = false; try { while (!quit) { // The problem we trying to solve below: STDIN in Java is blocked and // non-interruptible, so if we invoke read on it, we might never be able to // interrupt such thread. So, we use in.available() to see if there is any // input ready, and only then read it. But this approach can't tell whether // the end of stream reached or not, so we might end up looping right at the // end of the stream. Well, at least, we can improve the situation by checking // if some input was ever available, and if so, not checking for available // anymore, and just go to read. if (onlyIfAvailable && !hasReadSomething) { if (in.available() == 0) { synchronized (waitLock) { waitLock.wait(10); } continue; } else { hasReadSomething = true; } } if ((numRead = in.read(buf)) == -1) { break; } out.write(buf, 0, numRead); } } catch (Exception e) { } finally { if (onlyIfAvailable) { // We need to close the out, since some // processes would just wait for the stream // to be closed before they process its content, // and produce the output. E.g.: "cat". try { out.close(); } catch (IOException ioe) {} } } } public void quit() { this.quit = true; synchronized (waitLock) { waitLock.notify(); } } } private void handleStreams(Process p, InputStream in, OutputStream out, OutputStream err) throws IOException { InputStream pOut = p.getInputStream(); InputStream pErr = p.getErrorStream(); OutputStream pIn = p.getOutputStream(); StreamPumper t1 = new StreamPumper(pOut, out, false); StreamPumper t2 = new StreamPumper(pErr, err, false); StreamPumper t3 = new StreamPumper(in, pIn, true); t1.start(); t2.start(); t3.start(); try { t1.join(); } catch (InterruptedException ie) {} try { t2.join(); } catch (InterruptedException ie) {} t3.quit(); try { err.flush(); } catch (IOException io) {} try { out.flush(); } catch (IOException io) {} try { pIn.close(); } catch (IOException io) {} try { pOut.close(); } catch (IOException io) {} try { pErr.close(); } catch (IOException io) {} } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/FieldAccess.java000066400000000000000000000014471252174056300241150ustar00rootroot00000000000000/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jnr.posix.util; import java.lang.reflect.Field; /** * * @author nicksieger */ public class FieldAccess { public static Field getProtectedField(Class klass, String fieldName) { Field field = null; try { field = klass.getDeclaredField(fieldName); field.setAccessible(true); } catch (Exception e) { } return field; } public static Object getProtectedFieldValue(Class klass, String fieldName, Object instance) { try { Field f = getProtectedField(klass, fieldName); return f.get(instance); } catch (Exception e) { throw new IllegalArgumentException(e); } } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/Finder.java000066400000000000000000000112531252174056300231530ustar00rootroot00000000000000package jnr.posix.util; import java.io.File; import java.util.*; import jnr.posix.FileStat; import jnr.posix.POSIX; public class Finder { private static final Collection EXECUTABLE_EXTENSIONS = Collections.unmodifiableSet(new HashSet(Arrays.asList(".exe", ".com", ".cmd", ".bat"))); public static String findFileInPath(POSIX posix, String name, String path) { if (path == null || path.length() == 0) path = System.getenv("PATH"); // MRI sets up a bogus path which seems like it would violate security // if nothing else since if I don't have /usr/bin in my path but I end // up executing it anyways??? Returning original name and hoping for // best. if (path == null || path.length() == 0) return name; return findFileCommon(posix, name, path, true); } public static String findFileCommon(POSIX posix, String name, String path, boolean executableOnly) { // No point looking for nothing... if (name == null || name.length() == 0) return name; int length = name.length(); boolean isAbsolute = false; boolean isPath = false; int i = 0; if (Platform.IS_WINDOWS) { if (length > 1 && Character.isLetter(name.charAt(0)) && name.charAt(1) == ':') { i = 2; isAbsolute = true; } int extensionIndex = -1; char c = name.charAt(i); if (i == '/' || i == '\\') { i++; c = name.charAt(i); isAbsolute = true; } // Is this a partial path and does it contain an explicit // file extension? for (; i < length; i++) { switch (c) { case '/': case '\\': isPath = true; extensionIndex = -1; break; case '.': extensionIndex = i - 1; break; } c = name.charAt(i); } if (extensionIndex >= 0 && !EXECUTABLE_EXTENSIONS.contains(name.substring(extensionIndex).toLowerCase())) { extensionIndex = -1; } if (!executableOnly) { if (isAbsolute) return name; } else if (isPath) { if (extensionIndex >= 0) return name; if (executableOnly) { return addExtension(name); } else if (new File(name).exists()) { return name; } return null; } String[] paths = path.split(File.pathSeparator); for (int p = 0; p < paths.length; p++) { String currentPath = paths[p]; int currentPathLength = currentPath.length(); if (currentPath == null || currentPathLength == 0) continue; if (currentPath.charAt(0) == '~' && (currentPathLength == 1 || (currentPathLength > 1 && (currentPath.charAt(1) == '/' || currentPath.charAt(1) == '\\')))) { String home = System.getenv("HOME"); if (home != null) { currentPath = home + (currentPathLength == 1 ? "" : currentPath.substring(1)); } } if (!currentPath.endsWith("/") && !currentPath.endsWith("\\")) { currentPath += "\\"; } String filename = currentPath + name; if (Platform.IS_WINDOWS) filename = filename.replace('/', '\\'); if (Platform.IS_WINDOWS && executableOnly && extensionIndex == -1) { String extendedFilename = addExtension(filename); if (extendedFilename != null) return extendedFilename; continue; } try { FileStat stat = posix.stat(filename); if (!executableOnly || (!stat.isDirectory() && stat.isExecutable())) return filename; } catch (Throwable t) {} } } return null; } public static String addExtension(String path) { for (String extension : EXECUTABLE_EXTENSIONS) { String newPath = path + extension; if (new File(newPath).exists()) return newPath; } return null; } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/Java5ProcessMaker.java000066400000000000000000000113771252174056300252400ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ package jnr.posix.util; import jnr.posix.POSIXHandler; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; public class Java5ProcessMaker implements ProcessMaker { private final ProcessBuilder builder; private final POSIXHandler handler; public Java5ProcessMaker(POSIXHandler handler, String... command) { this.handler = handler; builder = new ProcessBuilder(command); } public Java5ProcessMaker(POSIXHandler handler) { this.handler = handler; builder = new ProcessBuilder(); } public List command() { return builder.command(); } public ProcessMaker command(List command) { builder.command(command); return this; } public ProcessMaker command(String... command) { builder.command(command); return this; } public File directory() { return builder.directory(); } public ProcessMaker directory(File dir) { builder.directory(dir); return this; } public Map environment() { return builder.environment(); } public ProcessMaker environment(String[] envLines) { envIntoProcessBuilder(builder, envLines); return this; } public ProcessMaker inheritIO() { handler.unimplementedError("inheritIO"); return this; } public Redirect redirectError() { return Redirect.PIPE; // only option on Java 5/6 } public ProcessMaker redirectError(File file) { handler.unimplementedError("redirectError"); return this; } public ProcessMaker redirectError(Redirect destination) { handler.unimplementedError("redirectError"); return this; } public boolean redirectErrorStream() { return false; } public ProcessMaker redirectErrorStream(boolean redirectErrorStream) { handler.unimplementedError("redirectErrorStream"); return this; } public Redirect redirectInput() { return Redirect.PIPE; // only option on Java 5/6 } public ProcessMaker redirectInput(File file) { handler.unimplementedError("redirectInput"); return this; } public ProcessMaker redirectInput(Redirect source) { handler.unimplementedError("redirectInput"); return this; } public Redirect redirectOutput() { return Redirect.PIPE; // only option on Java 5/6 } public ProcessMaker redirectOutput(File file) { handler.unimplementedError("redirectOutput"); return this; } public ProcessMaker redirectOutput(Redirect destination) { handler.unimplementedError("redirectOutput"); return this; } public Process start() throws IOException { return builder.start(); } private static void envIntoProcessBuilder(ProcessBuilder pb, String[] env) { if (env == null) return; pb.environment().clear(); for (String envLine : env) { if (envLine.indexOf(0) != -1) { envLine = envLine.replaceFirst("\u0000.*", ""); } int index = envLine.indexOf('='); if (index != -1) { pb.environment().put( envLine.substring(0, index), envLine.substring(index + 1)); } } } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/JavaCrypt.java000066400000000000000000000402701252174056300236500ustar00rootroot00000000000000package jnr.posix.util; public class JavaCrypt { private static final int ITERATIONS = 16; private static final int[] con_salt = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 0, 0, 0, 0}; private static final boolean[] shifts2 = {false, false, true, true, true, true, true, true, false, true, true, true, true, true, true, false}; private static final int[][] skb = {{0, 16, 536870912, 536870928, 65536, 65552, 536936448, 536936464, 2048, 2064, 536872960, 536872976, 67584, 67600, 536938496, 536938512, 32, 48, 536870944, 536870960, 65568, 65584, 536936480, 536936496, 2080, 2096, 536872992, 536873008, 67616, 67632, 536938528, 536938544, 524288, 524304, 537395200, 537395216, 589824, 589840, 537460736, 537460752, 526336, 526352, 537397248, 537397264, 591872, 591888, 537462784, 537462800, 524320, 524336, 537395232, 537395248, 589856, 589872, 537460768, 537460784, 526368, 526384, 537397280, 537397296, 591904, 591920, 537462816, 537462832}, {0, 33554432, 8192, 33562624, 2097152, 35651584, 2105344, 35659776, 4, 33554436, 8196, 33562628, 2097156, 35651588, 2105348, 35659780, 1024, 33555456, 9216, 33563648, 2098176, 35652608, 2106368, 35660800, 1028, 33555460, 9220, 33563652, 2098180, 35652612, 2106372, 35660804, 268435456, 301989888, 268443648, 301998080, 270532608, 304087040, 270540800, 304095232, 268435460, 301989892, 268443652, 301998084, 270532612, 304087044, 270540804, 304095236, 268436480, 301990912, 268444672, 301999104, 270533632, 304088064, 270541824, 304096256, 268436484, 301990916, 268444676, 301999108, 270533636, 304088068, 270541828, 304096260}, {0, 1, 262144, 262145, 16777216, 16777217, 17039360, 17039361, 2, 3, 262146, 262147, 16777218, 16777219, 17039362, 17039363, 512, 513, 262656, 262657, 16777728, 16777729, 17039872, 17039873, 514, 515, 262658, 262659, 16777730, 16777731, 17039874, 17039875, 134217728, 134217729, 134479872, 134479873, 150994944, 150994945, 151257088, 151257089, 134217730, 134217731, 134479874, 134479875, 150994946, 150994947, 151257090, 151257091, 134218240, 134218241, 134480384, 134480385, 150995456, 150995457, 151257600, 151257601, 134218242, 134218243, 134480386, 134480387, 150995458, 150995459, 151257602, 151257603}, {0, 1048576, 256, 1048832, 8, 1048584, 264, 1048840, 4096, 1052672, 4352, 1052928, 4104, 1052680, 4360, 1052936, 67108864, 68157440, 67109120, 68157696, 67108872, 68157448, 67109128, 68157704, 67112960, 68161536, 67113216, 68161792, 67112968, 68161544, 67113224, 68161800, 131072, 1179648, 131328, 1179904, 131080, 1179656, 131336, 1179912, 135168, 1183744, 135424, 1184000, 135176, 1183752, 135432, 1184008, 67239936, 68288512, 67240192, 68288768, 67239944, 68288520, 67240200, 68288776, 67244032, 68292608, 67244288, 68292864, 67244040, 68292616, 67244296, 68292872}, {0, 268435456, 65536, 268500992, 4, 268435460, 65540, 268500996, 536870912, 805306368, 536936448, 805371904, 536870916, 805306372, 536936452, 805371908, 1048576, 269484032, 1114112, 269549568, 1048580, 269484036, 1114116, 269549572, 537919488, 806354944, 537985024, 806420480, 537919492, 806354948, 537985028, 806420484, 4096, 268439552, 69632, 268505088, 4100, 268439556, 69636, 268505092, 536875008, 805310464, 536940544, 805376000, 536875012, 805310468, 536940548, 805376004, 1052672, 269488128, 1118208, 269553664, 1052676, 269488132, 1118212, 269553668, 537923584, 806359040, 537989120, 806424576, 537923588, 806359044, 537989124, 806424580}, {0, 134217728, 8, 134217736, 1024, 134218752, 1032, 134218760, 131072, 134348800, 131080, 134348808, 132096, 134349824, 132104, 134349832, 1, 134217729, 9, 134217737, 1025, 134218753, 1033, 134218761, 131073, 134348801, 131081, 134348809, 132097, 134349825, 132105, 134349833, 33554432, 167772160, 33554440, 167772168, 33555456, 167773184, 33555464, 167773192, 33685504, 167903232, 33685512, 167903240, 33686528, 167904256, 33686536, 167904264, 33554433, 167772161, 33554441, 167772169, 33555457, 167773185, 33555465, 167773193, 33685505, 167903233, 33685513, 167903241, 33686529, 167904257, 33686537, 167904265}, {0, 256, 524288, 524544, 16777216, 16777472, 17301504, 17301760, 16, 272, 524304, 524560, 16777232, 16777488, 17301520, 17301776, 2097152, 2097408, 2621440, 2621696, 18874368, 18874624, 19398656, 19398912, 2097168, 2097424, 2621456, 2621712, 18874384, 18874640, 19398672, 19398928, 512, 768, 524800, 525056, 16777728, 16777984, 17302016, 17302272, 528, 784, 524816, 525072, 16777744, 16778000, 17302032, 17302288, 2097664, 2097920, 2621952, 2622208, 18874880, 18875136, 19399168, 19399424, 2097680, 2097936, 2621968, 2622224, 18874896, 18875152, 19399184, 19399440}, {0, 67108864, 262144, 67371008, 2, 67108866, 262146, 67371010, 8192, 67117056, 270336, 67379200, 8194, 67117058, 270338, 67379202, 32, 67108896, 262176, 67371040, 34, 67108898, 262178, 67371042, 8224, 67117088, 270368, 67379232, 8226, 67117090, 270370, 67379234, 2048, 67110912, 264192, 67373056, 2050, 67110914, 264194, 67373058, 10240, 67119104, 272384, 67381248, 10242, 67119106, 272386, 67381250, 2080, 67110944, 264224, 67373088, 2082, 67110946, 264226, 67373090, 10272, 67119136, 272416, 67381280, 10274, 67119138, 272418, 67381282}}; private static final int[][] SPtrans = {{8520192, 131072, -2139095040, -2138963456, 8388608, -2147352064, -2147352576, -2139095040, -2147352064, 8520192, 8519680, -2147483136, -2139094528, 8388608, 0, -2147352576, 131072, -2147483648, 8389120, 131584, -2138963456, 8519680, -2147483136, 8389120, -2147483648, 512, 131584, -2138963968, 512, -2139094528, -2138963968, 0, 0, -2138963456, 8389120, -2147352576, 8520192, 131072, -2147483136, 8389120, -2138963968, 512, 131584, -2139095040, -2147352064, -2147483648, -2139095040, 8519680, -2138963456, 131584, 8519680, -2139094528, 8388608, -2147483136, -2147352576, 0, 131072, 8388608, -2139094528, 8520192, -2147483648, -2138963968, 512, -2147352064}, {268705796, 0, 270336, 268697600, 268435460, 8196, 268443648, 270336, 8192, 268697604, 4, 268443648, 262148, 268705792, 268697600, 4, 262144, 268443652, 268697604, 8192, 270340, 268435456, 0, 262148, 268443652, 270340, 268705792, 268435460, 268435456, 262144, 8196, 268705796, 262148, 268705792, 268443648, 270340, 268705796, 262148, 268435460, 0, 268435456, 8196, 262144, 268697604, 8192, 268435456, 270340, 268443652, 268705792, 8192, 0, 268435460, 4, 268705796, 270336, 268697600, 268697604, 262144, 8196, 268443648, 268443652, 4, 268697600, 270336}, {1090519040, 16842816, 64, 1090519104, 1073807360, 16777216, 1090519104, 65600, 16777280, 65536, 16842752, 1073741824, 1090584640, 1073741888, 1073741824, 1090584576, 0, 1073807360, 16842816, 64, 1073741888, 1090584640, 65536, 1090519040, 1090584576, 16777280, 1073807424, 16842752, 65600, 0, 16777216, 1073807424, 16842816, 64, 1073741824, 65536, 1073741888, 1073807360, 16842752, 1090519104, 0, 16842816, 65600, 1090584576, 1073807360, 16777216, 1090584640, 1073741824, 1073807424, 1090519040, 16777216, 1090584640, 65536, 16777280, 1090519104, 65600, 16777280, 0, 1090584576, 1073741888, 1090519040, 1073807424, 64, 16842752}, {1049602, 67109888, 2, 68158466, 0, 68157440, 67109890, 1048578, 68158464, 67108866, 67108864, 1026, 67108866, 1049602, 1048576, 67108864, 68157442, 1049600, 1024, 2, 1049600, 67109890, 68157440, 1024, 1026, 0, 1048578, 68158464, 67109888, 68157442, 68158466, 1048576, 68157442, 1026, 1048576, 67108866, 1049600, 67109888, 2, 68157440, 67109890, 0, 1024, 1048578, 0, 68157442, 68158464, 1024, 67108864, 68158466, 1049602, 1048576, 68158466, 2, 67109888, 1049602, 1048578, 1049600, 68157440, 67109890, 1026, 67108864, 67108866, 68158464}, {33554432, 16384, 256, 33571080, 33570824, 33554688, 16648, 33570816, 16384, 8, 33554440, 16640, 33554696, 33570824, 33571072, 0, 16640, 33554432, 16392, 264, 33554688, 16648, 0, 33554440, 8, 33554696, 33571080, 16392, 33570816, 256, 264, 33571072, 33571072, 33554696, 16392, 33570816, 16384, 8, 33554440, 33554688, 33554432, 16640, 33571080, 0, 16648, 33554432, 256, 16392, 33554696, 256, 0, 33571080, 33570824, 33571072, 264, 16384, 16640, 33570824, 33554688, 264, 8, 16648, 33570816, 33554440}, {536870928, 524304, 0, 537397248, 524304, 2048, 536872976, 524288, 2064, 537397264, 526336, 536870912, 536872960, 536870928, 537395200, 526352, 524288, 536872976, 537395216, 0, 2048, 16, 537397248, 537395216, 537397264, 537395200, 536870912, 2064, 16, 526336, 526352, 536872960, 2064, 536870912, 536872960, 526352, 537397248, 524304, 0, 536872960, 536870912, 2048, 537395216, 524288, 524304, 537397264, 526336, 16, 537397264, 526336, 524288, 536872976, 536870928, 537395200, 526352, 0, 2048, 536870928, 536872976, 537397248, 537395200, 2064, 16, 537395216}, {4096, 128, 4194432, 4194305, 4198529, 4097, 4224, 0, 4194304, 4194433, 129, 4198400, 1, 4198528, 4198400, 129, 4194433, 4096, 4097, 4198529, 0, 4194432, 4194305, 4224, 4198401, 4225, 4198528, 1, 4225, 4198401, 128, 4194304, 4225, 4198400, 4198401, 129, 4096, 128, 4194304, 4198401, 4194433, 4225, 4224, 0, 128, 4194305, 1, 4194432, 0, 4194433, 4194432, 4224, 129, 4096, 4198529, 4194304, 4198528, 1, 4097, 4198529, 4194305, 4198528, 4198400, 4097}, {136314912, 136347648, 32800, 0, 134250496, 2097184, 136314880, 136347680, 32, 134217728, 2129920, 32800, 2129952, 134250528, 134217760, 136314880, 32768, 2129952, 2097184, 134250496, 136347680, 134217760, 0, 2129920, 134217728, 2097152, 134250528, 136314912, 2097152, 32768, 136347648, 32, 2097152, 32768, 134217760, 136347680, 32800, 134217728, 0, 2129920, 136314912, 134250528, 134250496, 2097184, 136347648, 32, 2097184, 134250496, 136347680, 2097152, 136314880, 134217760, 2129920, 32800, 134250528, 136314880, 32, 136347648, 2129952, 0, 134217728, 136314912, 32768, 2129952}}; private static final int[] cov_2char = {46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}; private static final int byteToUnsigned(byte b) { return b & 255; } private static int fourBytesToInt(byte[] b, int offset) { int value; value = byteToUnsigned(b[offset++]); value |= (byteToUnsigned(b[offset++]) << 8); value |= (byteToUnsigned(b[offset++]) << 16); value |= (byteToUnsigned(b[offset++]) << 24); return value; } private static final void intToFourBytes(int iValue, byte[] b, int offset) { b[offset++] = (byte) ((iValue) & 255); b[offset++] = (byte) ((iValue >>> 8) & 255); b[offset++] = (byte) ((iValue >>> 16) & 255); b[offset++] = (byte) ((iValue >>> 24) & 255); } private static final void PERM_OP(int a, int b, int n, int m, int[] results) { int t; t = ((a >>> n) ^ b) & m; a ^= t << n; b ^= t; results[0] = a; results[1] = b; } private static final int HPERM_OP(int a, int n, int m) { int t; t = ((a << (16 - n)) ^ a) & m; a = a ^ t ^ (t >>> (16 - n)); return a; } private static int[] des_set_key(byte[] key) { int[] schedule = new int[ITERATIONS * 2]; int c = fourBytesToInt(key, 0); int d = fourBytesToInt(key, 4); int[] results = new int[2]; PERM_OP(d, c, 4, 252645135, results); d = results[0]; c = results[1]; c = HPERM_OP(c, -2, -859045888); d = HPERM_OP(d, -2, -859045888); PERM_OP(d, c, 1, 1431655765, results); d = results[0]; c = results[1]; PERM_OP(c, d, 8, 16711935, results); c = results[0]; d = results[1]; PERM_OP(d, c, 1, 1431655765, results); d = results[0]; c = results[1]; d = (((d & 255) << 16) | (d & 65280) | ((d & 16711680) >>> 16) | ((c & -268435456) >>> 4)); c &= 268435455; int s; int t; int j = 0; for (int i = 0; i < ITERATIONS; i++) { if (shifts2[i]) { c = (c >>> 2) | (c << 26); d = (d >>> 2) | (d << 26); } else { c = (c >>> 1) | (c << 27); d = (d >>> 1) | (d << 27); } c &= 268435455; d &= 268435455; s = skb[0][(c) & 63] | skb[1][((c >>> 6) & 3) | ((c >>> 7) & 60)] | skb[2][((c >>> 13) & 15) | ((c >>> 14) & 48)] | skb[3][((c >>> 20) & 1) | ((c >>> 21) & 6) | ((c >>> 22) & 56)]; t = skb[4][(d) & 63] | skb[5][((d >>> 7) & 3) | ((d >>> 8) & 60)] | skb[6][(d >>> 15) & 63] | skb[7][((d >>> 21) & 15) | ((d >>> 22) & 48)]; schedule[j++] = ((t << 16) | (s & 65535)) & -1; s = ((s >>> 16) | (t & -65536)); s = (s << 4) | (s >>> 28); schedule[j++] = s & -1; } return schedule; } private static final int D_ENCRYPT(int L, int R, int S, int E0, int E1, int[] s) { int t; int u; int v; v = R ^ (R >>> 16); u = v & E0; v = v & E1; u = (u ^ (u << 16)) ^ R ^ s[S]; t = (v ^ (v << 16)) ^ R ^ s[S + 1]; t = (t >>> 4) | (t << 28); L ^= SPtrans[1][(t) & 63] | SPtrans[3][(t >>> 8) & 63] | SPtrans[5][(t >>> 16) & 63] | SPtrans[7][(t >>> 24) & 63] | SPtrans[0][(u) & 63] | SPtrans[2][(u >>> 8) & 63] | SPtrans[4][(u >>> 16) & 63] | SPtrans[6][(u >>> 24) & 63]; return L; } private static final int[] body(int[] schedule, int Eswap0, int Eswap1) { int left = 0; int right = 0; int t = 0; for (int j = 0; j < 25; j++) { for (int i = 0; i < ITERATIONS * 2; i += 4) { left = D_ENCRYPT(left, right, i, Eswap0, Eswap1, schedule); right = D_ENCRYPT(right, left, i + 2, Eswap0, Eswap1, schedule); } t = left; left = right; right = t; } t = right; right = (left >>> 1) | (left << 31); left = (t >>> 1) | (t << 31); left &= -1; right &= -1; int[] results = new int[2]; PERM_OP(right, left, 1, 1431655765, results); right = results[0]; left = results[1]; PERM_OP(left, right, 8, 16711935, results); left = results[0]; right = results[1]; PERM_OP(right, left, 2, 858993459, results); right = results[0]; left = results[1]; PERM_OP(left, right, 16, 65535, results); left = results[0]; right = results[1]; PERM_OP(right, left, 4, 252645135, results); right = results[0]; left = results[1]; int[] out = new int[2]; out[0] = left; out[1] = right; return out; } public static final CharSequence crypt(CharSequence original, CharSequence salt) { char[] buffer = new char[]{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; char charZero = salt.charAt(0); char charOne = salt.charAt(1); buffer[0] = charZero; buffer[1] = charOne; int Eswap0 = con_salt[charZero & 127]; int Eswap1 = con_salt[charOne & 127] << 4; byte[] key = new byte[8]; for (int i = 0; i < key.length; i++) { key[i] = (byte) 0; } for (int i = 0; i < key.length && i < original.length(); i++) { int iChar = original.charAt(i) & 255; key[i] = (byte) (iChar << 1); } int[] schedule = des_set_key(key); int[] out = body(schedule, Eswap0, Eswap1); byte[] b = new byte[9]; intToFourBytes(out[0], b, 0); intToFourBytes(out[1], b, 4); b[8] = 0; for (int i = 2, y = 0, u = 128; i < 13; i++) { for (int j = 0, c = 0; j < 6; j++) { c <<= 1; if (((int) b[y] & u) != 0) { c |= 1; } u >>>= 1; if (u == 0) { y++; u = 128; } buffer[i] = (char) cov_2char[c]; } } return new String(buffer); } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/MethodName.java000066400000000000000000000014611252174056300237650ustar00rootroot00000000000000package jnr.posix.util; public class MethodName { private static final int CLIENT_CODE_STACK_INDEX; static { // Finds out the index of "this code" in the returned stack trace - funny but it differs in JDK 1.5 and 1.6 int i = 0; for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { i++; if (ste.getClassName().equals(MethodName.class.getName())) { break; } } CLIENT_CODE_STACK_INDEX = i; } public static String getMethodName() { return Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX].getMethodName(); } public static String getCallerMethodName() { return Thread.currentThread().getStackTrace()[CLIENT_CODE_STACK_INDEX + 1].getMethodName(); } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/Platform.java000066400000000000000000000130501252174056300235250ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix.util; import java.util.HashMap; import java.util.Map; public class Platform { public static final String OS_NAME = System.getProperty("os.name"); public static final String OS_NAME_LC = OS_NAME.toLowerCase(); // Generic Windows designation private static final String WINDOWS = "windows"; // For Windows 95, 98... Would these platforms actually work? private static final String WINDOWS_9X = "windows 9"; // TODO: Windows ME? private static final String WINDOWS_NT = "nt"; private static final String WINDOWS_20X = "windows 2"; private static final String WINDOWS_XP = "windows xp"; private static final String WINDOWS_SERVER = "server"; private static final String WINDOWS_VISTA = "vista"; private static final String WINDOWS_7 = "windows 7"; private static final String MAC_OS = "mac os"; private static final String DARWIN = "darwin"; private static final String FREEBSD = "freebsd"; private static final String OPENBSD = "openbsd"; private static final String LINUX = "linux"; private static final String SOLARIS = "sunos"; // TODO: investigate supported platforms for OpenJDK7? public static final boolean IS_WINDOWS = OS_NAME_LC.indexOf(WINDOWS) != -1; public static final boolean IS_WINDOWS_9X = OS_NAME_LC.indexOf(WINDOWS_9X) > -1; public static final boolean IS_WINDOWS_NT = IS_WINDOWS && OS_NAME_LC.indexOf(WINDOWS_NT) > -1; public static final boolean IS_WINDOWS_20X = OS_NAME_LC.indexOf(WINDOWS_20X) > -1; public static final boolean IS_WINDOWS_XP = OS_NAME_LC.indexOf(WINDOWS_XP) > -1; public static final boolean IS_WINDOWS_VISTA = IS_WINDOWS && OS_NAME_LC.indexOf(WINDOWS_VISTA) > -1; public static final boolean IS_WINDOWS_SERVER = IS_WINDOWS && OS_NAME_LC.indexOf(WINDOWS_SERVER) > -1; public static final boolean IS_WINDOWS_7 = IS_WINDOWS && OS_NAME_LC.indexOf(WINDOWS_7) > -1; public static final boolean IS_MAC = OS_NAME_LC.startsWith(MAC_OS) || OS_NAME_LC.startsWith(DARWIN); public static final boolean IS_FREEBSD = OS_NAME_LC.startsWith(FREEBSD); public static final boolean IS_OPENBSD = OS_NAME_LC.startsWith(OPENBSD); public static final boolean IS_LINUX = OS_NAME_LC.startsWith(LINUX); public static final boolean IS_SOLARIS = OS_NAME_LC.startsWith(SOLARIS); public static final boolean IS_BSD = IS_MAC || IS_FREEBSD || IS_OPENBSD; public static final String envCommand() { if (IS_WINDOWS) { if (IS_WINDOWS_9X) { return "command.com /c set"; } else if (IS_WINDOWS_NT || IS_WINDOWS_20X || IS_WINDOWS_XP || IS_WINDOWS_SERVER || IS_WINDOWS_VISTA || IS_WINDOWS_7) { return "cmd.exe /c set"; } } return "env"; } public static final boolean IS_32_BIT = "32".equals(getProperty("sun.arch.data.model", "32")); public static final boolean IS_64_BIT = "64".equals(getProperty("sun.arch.data.model", "64")); public static final String ARCH; static { String arch = System.getProperty("os.arch"); if (arch.equals("amd64")) arch = "x86_64"; ARCH = arch; } public static final Map OS_NAMES = new HashMap(); static { OS_NAMES.put("Mac OS X", DARWIN); OS_NAMES.put("Darwin", DARWIN); OS_NAMES.put("Linux", LINUX); } public static String getOSName() { String theOSName = OS_NAMES.get(OS_NAME); return theOSName == null ? OS_NAME : theOSName; } /** * An extension over System.getProperty method. * Handles security restrictions, and returns the default * value if the access to the property is restricted. * @param property The system property name. * @param defValue The default value. * @return The value of the system property, * or the default value. */ public static String getProperty(String property, String defValue) { try { return System.getProperty(property, defValue); } catch (SecurityException se) { return defValue; } } } jnr-posix-3.0.12/src/main/java/jnr/posix/util/ProcessMaker.java000066400000000000000000000071341252174056300243450ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ package jnr.posix.util; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; /** * Abstract interface for building a process on various JVM versions. */ public interface ProcessMaker { public static class Redirect { public static final Redirect INHERIT = new Redirect(Type.INHERIT); public static final Redirect PIPE = new Redirect(Type.PIPE); private enum Type { APPEND, INHERIT, PIPE, READ, WRITE } private final Type type; private final File file; private Redirect(Type type) { this(type, null); } private Redirect(Type type, File file) { this.type = type; this.file = file; } public static Redirect appendTo(File file) { return new Redirect(Type.APPEND, file); } public static Redirect from(File file) { return new Redirect(Type.READ, file); } public static Redirect to(File file) { return new Redirect(Type.WRITE, file); } public File file() { return file; } public Type type() { return type; } } public List command(); public ProcessMaker command(List command); public ProcessMaker command(String... command); public File directory(); public ProcessMaker directory(File dir); public Map environment(); public ProcessMaker environment(String[] envLines); public ProcessMaker inheritIO(); public Redirect redirectError(); public ProcessMaker redirectError(File file); public ProcessMaker redirectError(Redirect destination); public boolean redirectErrorStream(); public ProcessMaker redirectErrorStream(boolean redirectErrorStream); public Redirect redirectInput(); public ProcessMaker redirectInput(File file); public ProcessMaker redirectInput(Redirect source); public Redirect redirectOutput(); public ProcessMaker redirectOutput(File file); public ProcessMaker redirectOutput(Redirect destination); public Process start() throws IOException; } jnr-posix-3.0.12/src/main/java/jnr/posix/util/WindowsHelpers.java000066400000000000000000000356151252174056300247310ustar00rootroot00000000000000/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jnr.posix.util; import jnr.ffi.*; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; import jnr.posix.POSIX; /** * * @author enebo */ public class WindowsHelpers { static final jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getSystemRuntime(); static final int WORDSIZE = jnr.ffi.Runtime.getSystemRuntime().addressSize(); public static byte[] toWPath(String path) { boolean absolute = new File(path).isAbsolute(); if (absolute) { path = "//?/" + path; } return toWString(path); } public static byte[] toWString(String string) { if (string == null) return null; string += (char) 0; try { return string.getBytes("UTF-16LE"); } catch (UnsupportedEncodingException e) { return null; // JVM mandates this encoding. Not reached } } // FIXME: This does not work and I am unsure if it is because I am violating something // CreateProcess requires OR because there are weird requirements in how env needs to be // setup for CreateProcess (e.g. =C:=C:/ vars). public static Pointer createWideEnv(String[] envp) { if (envp == null) return null; byte[] marker = {0}; int envLength = envp.length; // Allocate pointer for env pointer entries plus last \0\0 marker Pointer result = Memory.allocateDirect(runtime, WORDSIZE * (envLength + 1)); for (int i = 0; i < envLength; i++) { byte[] bytes = toWString(envp[i]); Pointer envElement = Memory.allocateDirect(runtime, bytes.length + 1); envElement.put(0, bytes, 0, bytes.length); envElement.put(bytes.length, marker, 0, marker.length); result.putPointer(i * WORDSIZE, envElement); } Pointer nullMarker = Memory.allocateDirect(runtime, marker.length); nullMarker.put(0, marker, 0, marker.length); result.putPointer(WORDSIZE * envLength, nullMarker); return result; } // Windows cmd strings have various escaping: // 1. <>|^ can all be escaped with ^ (e.g. ^<) // 2. \s\t must be quoted if not already // 3. Any arguments with double quotes must be escaped with a double // quote around whole cmd private static void joinSingleArgv(StringBuilder buffer, String arg, boolean quote, boolean escape) { int backslashCount = 0; int start = 0; if (quote) buffer.append('"'); for (int i = 0; i < arg.length(); i++) { char c = arg.charAt(i); switch(c) { case '\\': backslashCount++; break; case '"': { buffer.append(arg.substring(start, i)); for (int j = 0; j < backslashCount + 1; j++) { buffer.append('\\'); } backslashCount = 0; start = i; } case '<': case '>': case '|': case '^': { if (escape && !quote) { buffer.append(arg.substring(start, i)); buffer.append('^'); start = i; break; } } default: { backslashCount = 0; break; } } } buffer.append(arg.substring(start)); if (quote) buffer.append('"'); } public static String joinArgv(String command, String[] argv, boolean escape) { StringBuilder buffer = new StringBuilder(); if (command != null) { buffer.append(command); buffer.append(' '); } int last_index = argv.length - 1; for (int i = 0; i <= last_index; i++) { joinSingleArgv(buffer, argv[i], quotable(argv[i]), escape); if (i != last_index) buffer.append(' '); // Add space between arguments } return buffer.toString(); } public static boolean quotable(String value) { if (value == null) return false; StringTokenizer toker = new StringTokenizer(value, " \t\"'"); toker.nextToken(); // We know a string with no delimeters will return self return toker.hasMoreTokens(); } public static boolean isBatch(String value) { if (value == null) return false; int length = value.length(); if (length < 5) return false; String end = value.substring(length - 4); return end.equalsIgnoreCase(".bat") || end.equalsIgnoreCase(".cmd"); } public static String[] processCommandLine(POSIX posix, String command, String program, String path) { String shell = null; if (program != null) { String fullPath = Finder.findFileInPath(posix, program, path); shell = fullPath == null ? program : fullPath.replace('/', '\\'); } else { // Strip off leading whitespace command = command.substring(firstNonWhitespaceIndex(command)); // FIXME: Ruby first looks for RUBYSHELL, but this only applies for // JRuby (I doubt Jython wants to honor that env). We need a generic // hook for other envs to look for? shell = System.getenv("COMSPEC"); boolean notHandledYet = true; if (shell != null) { boolean commandDotCom = isCommandDotCom(shell); if (hasBuiltinSpecialNeeds(command) || isInternalCommand(command, commandDotCom)) { String quote = commandDotCom ? "\"" : ""; command = shell + " /c " + quote + command + quote; notHandledYet = false; } } if (notHandledYet) { char firstChar = command.charAt(0); char quote = firstChar == '"' ? firstChar : (firstChar == '\'' ? firstChar : (char) 0); int commandLength = command.length(); int i = quote == 0 ? 0 : 1; for(;; i++) { if (i == commandLength) { shell = command; break; } char c = command.charAt(i); if (c == quote) { shell = command.substring(1, i); break; } if (quote != 0) continue; if (Character.isSpaceChar(c) || isFunnyChar(c)) { shell = command.substring(0, i); break; } } shell = Finder.findFileInPath(posix, shell, path); if (shell == null) { shell = command.substring(0, i); } else { if (!shell.contains(" ")) quote = 0; shell.replace('/', '\\'); } } } return new String[] { command, shell }; } public static String[] processCommandArgs(POSIX posix, String program, String[] argv, String path) { if (program == null || program.length() == 0) program = argv[0]; boolean addSlashC = false; boolean isNotBuiltin = false; boolean notHandledYet = true; String shell = System.getenv("COMSPEC"); String command = null; if (shell != null) { boolean commandDotCom = isCommandDotCom(shell); if (isInternalCommand(program, commandDotCom)) { isNotBuiltin = !commandDotCom; program = shell; addSlashC = true; notHandledYet = false; } } if (notHandledYet) { command = Finder.findFileInPath(posix, program, path); if (command != null) { program = command.replace('/', '\\'); } else if (program.contains("/")) { command = program.replace('/', '\\'); program = command; } } if (addSlashC || isBatch(program)) { if (addSlashC) { command = program + " /c "; } else { String[] newArgv = new String[argv.length - 1]; System.arraycopy(argv, 1, newArgv, 0, argv.length - 1); argv = newArgv; } if (argv.length > 0) { command = WindowsHelpers.joinArgv(command, argv, isNotBuiltin); } program = addSlashC ? shell : null; } else { command = WindowsHelpers.joinArgv(null, argv, false); } return new String[] { command, program }; } private static boolean isFunnyChar(char c) { return c == '<' || c == '>' || c == '|' || c == '*' || c == '?' || c == '"'; } private static boolean hasBuiltinSpecialNeeds(String value) { int length = value.length(); char quote = '\0'; for (int i = 0; i < length; i++) { char c = value.charAt(i); switch (c) { case '\'': case '\"': if (quote == '\0') { quote = c; } else if (quote == c) { quote = '\0'; } break; case '>': case '<': case '|': case '\n': if (quote != '\0') return true; break; case '%': // %FOO% check if (i + 1 < length) { i += 1; char c2 = value.charAt(i); if (c2 != ' ' && !Character.isLetter(c2)) break; for (int j = i; j < length; j++) { c2 = value.charAt(j); if (c2 != ' ' && !Character.isLetterOrDigit(c2)) break; } if (c2 == '%') return true; } break; } } return false; } private static int firstNonWhitespaceIndex(String value) { int length = value.length(); int i = 0; for (; i < length && Character.isSpaceChar(value.charAt(i)); i++) {} return i; } public static String escapePath(String path) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < path.length(); i++) { char c = path.charAt(i); buf.append(c); if (c == '\\') buf.append(c); } return buf.toString() + "\\\\"; } private final static String COMMAND_DOT_COM = "command.com"; private final static int CDC_LENGTH = COMMAND_DOT_COM.length(); private enum InternalType { SHELL, COMMAND, BOTH }; private static Map INTERNAL_COMMANDS = new HashMap() {{ put("assoc", InternalType.COMMAND); put("break", InternalType.BOTH); put("call", InternalType.BOTH); put("cd", InternalType.BOTH); put("chcp", InternalType.SHELL); put("chdir", InternalType.BOTH); put("cls", InternalType.BOTH); put("color", InternalType.COMMAND); put("copy", InternalType.BOTH); put("ctty", InternalType.SHELL); put("date", InternalType.BOTH); put("del", InternalType.BOTH); put("dir", InternalType.BOTH); put("echo", InternalType.BOTH); put("endlocal", InternalType.COMMAND); put("erase", InternalType.BOTH); put("exit", InternalType.BOTH); put("for", InternalType.BOTH); put("ftype", InternalType.COMMAND); put("goto", InternalType.BOTH); put("if", InternalType.BOTH); put("lfnfor", InternalType.SHELL); put("lh", InternalType.SHELL); put("lock", InternalType.SHELL); put("md", InternalType.BOTH); put("mkdir", InternalType.BOTH); put("move", InternalType.COMMAND); put("path", InternalType.BOTH); put("pause", InternalType.BOTH); put("popd", InternalType.COMMAND); put("prompt", InternalType.BOTH); put("pushd", InternalType.COMMAND); put("rd", InternalType.BOTH); put("rem", InternalType.BOTH); put("ren", InternalType.BOTH); put("rename", InternalType.BOTH); put("rmdir", InternalType.BOTH); put("set", InternalType.BOTH); put("setlocal", InternalType.COMMAND); put("shift", InternalType.BOTH); put("start", InternalType.COMMAND); put("time", InternalType.BOTH); put("title", InternalType.COMMAND); put("truename", InternalType.SHELL); put("type", InternalType.BOTH); put("unlock", InternalType.SHELL); put("ver", InternalType.BOTH); put("verify", InternalType.BOTH); put("vol", InternalType.BOTH); }}; private static boolean isDirectorySeparator(char value) { return value == '/' || value == '\\'; } private static boolean isCommandDotCom(String command) { int length = command.length(); int i = length - CDC_LENGTH; return i == 0 || i > 0 && isDirectorySeparator(command.charAt(i - 1)) && command.regionMatches(true, i, COMMAND_DOT_COM, 0, CDC_LENGTH); } private static boolean isInternalCommand(String command, boolean hasCommandDotCom) { assert command != null && !Character.isSpaceChar(command.charAt(0)) : "Spaces should have been stripped off already"; int length = command.length(); StringBuilder buf = new StringBuilder(); int i = 0; char c = 0; for (; i < length; i++) { c = command.charAt(i); if (!Character.isLetter(c)) break; buf.append(Character.toLowerCase(c)); } if (i < length) { if (c == '.' && i + 1 < length) i++; switch (command.charAt(i)) { case '<': case '>': case '|': return true; case '\0': case ' ': case '\t': case '\n': break; default: return false; } } InternalType kindOf = INTERNAL_COMMANDS.get(buf.toString()); return kindOf == InternalType.BOTH || (hasCommandDotCom ? kindOf == InternalType.COMMAND : kindOf == InternalType.SHELL); } } jnr-posix-3.0.12/src/test/000077500000000000000000000000001252174056300153015ustar00rootroot00000000000000jnr-posix-3.0.12/src/test/java/000077500000000000000000000000001252174056300162225ustar00rootroot00000000000000jnr-posix-3.0.12/src/test/java/jnr/000077500000000000000000000000001252174056300170135ustar00rootroot00000000000000jnr-posix-3.0.12/src/test/java/jnr/posix/000077500000000000000000000000001252174056300201555ustar00rootroot00000000000000jnr-posix-3.0.12/src/test/java/jnr/posix/CryptTest.java000066400000000000000000000016301252174056300227610ustar00rootroot00000000000000package jnr.posix; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import java.util.Arrays; /** * Created by headius on 3/24/15. */ public class CryptTest { @Before public void before() { posix = POSIXFactory.getPOSIX(); } @Test public void testCrypt() { String str1 = "blahblahblah"; String salt1 = "saltysalty"; CharSequence result1 = posix.crypt(str1, salt1); Assert.assertNotNull(result1); byte[] str1bytes = Arrays.copyOfRange(str1.getBytes(), 0, str1.length() + 1); byte[] salt1bytes = Arrays.copyOfRange(salt1.getBytes(), 0, salt1.length() + 1); byte[] result2 = posix.crypt(str1bytes, salt1bytes); Assert.assertNotNull(result2); String result2str = new String(result2, 0, result2.length - 1); Assert.assertEquals(result1, result2str); } private POSIX posix; } jnr-posix-3.0.12/src/test/java/jnr/posix/DTableSizeTest.java000066400000000000000000000012561252174056300236520ustar00rootroot00000000000000package jnr.posix; import jnr.posix.util.Platform; import org.junit.Test; import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeTrue; public class DTableSizeTest { @Test public void testGetDtableSize() throws Exception { if (!Platform.IS_WINDOWS) { // FIXME: I have seen window's impl so this can be impld POSIX posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); assumeTrue("getdtablesize only supported on native posix", posix.isNative()); int dtablesize = posix.getdtablesize(); assertThat(dtablesize, not(-1)); } } } jnr-posix-3.0.12/src/test/java/jnr/posix/DummyPOSIXHandler.java000066400000000000000000000031351252174056300242360ustar00rootroot00000000000000/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jnr.posix; import jnr.constants.platform.Errno; import java.io.File; import java.io.InputStream; import java.io.PrintStream; /** * * @author wayne */ public class DummyPOSIXHandler implements POSIXHandler { public void error(Errno error, String extraData) { throw new UnsupportedOperationException("error: " + error); } public void error(Errno error, String methodName, String extraData) { throw new UnsupportedOperationException("error: " + error); } public void unimplementedError(String methodName) { throw new UnsupportedOperationException("unimplemented method: " + methodName); } public void warn(WARNING_ID id, String message, Object... data) { throw new UnsupportedOperationException("warning: " + id); } public boolean isVerbose() { return true; } public File getCurrentWorkingDirectory() { return new File("/tmp"); } public String[] getEnv() { throw new UnsupportedOperationException("Not supported yet."); } public InputStream getInputStream() { throw new UnsupportedOperationException("Not supported yet."); } public PrintStream getOutputStream() { throw new UnsupportedOperationException("Not supported yet."); } public int getPID() { throw new UnsupportedOperationException("Not supported yet."); } public PrintStream getErrorStream() { throw new UnsupportedOperationException("Not supported yet."); } } jnr-posix-3.0.12/src/test/java/jnr/posix/FileStatTest.java000066400000000000000000000065551252174056300234060ustar00rootroot00000000000000 package jnr.posix; import jnr.posix.util.FieldAccess; import jnr.posix.util.Platform; import org.junit.*; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.lang.reflect.Field; import static org.junit.Assert.*; public class FileStatTest { public FileStatTest() { } private static POSIX posix; @BeforeClass public static void setUpClass() throws Exception { posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { } @After public void tearDown() { } // TODO add test methods here. // The methods must be annotated with annotation @Test. For example: // // @Test // public void hello() {} @Test public void filestat() throws Throwable { File f = File.createTempFile("stat", null); try { FileStat st = posix.stat(f.getAbsolutePath()); assertNotNull("posix.stat failed", st); FileStat stat = posix.allocateStat(); int result = posix.stat(f.getAbsolutePath(), stat); assertNotNull("posix.stat failed", st); assertEquals(0, result); } finally { f.delete(); } } @Test public void filestatInt() throws Throwable { // Windows does not store fd in FileDescriptor so this test wll not work if (jnr.ffi.Platform.getNativePlatform().isUnix()) { File f = File.createTempFile("stat", null); try { FileInputStream fis = new FileInputStream(f); FileDescriptor desc = fis.getFD(); int fd = -1; try { Field fdField = FieldAccess.getProtectedField(FileDescriptor.class, "fd"); fd = fdField.getInt(desc); } catch (SecurityException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } FileStat stat = posix.allocateStat(); int result = posix.fstat(fd, stat); assertTrue(fd > 2); // should not be stdin, stdout, stderr assertEquals(0, result); } finally { f.delete(); } } } @Test public void structStatSize() throws Throwable { if (Platform.IS_SOLARIS) { jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getSystemRuntime(); if (Platform.IS_32_BIT) { assertEquals("struct size is wrong", 144, new SolarisFileStat32.Layout(runtime).size()); } else { assertEquals("struct size is wrong", 128, new SolarisFileStat64.Layout(runtime).size()); } } if (Platform.IS_SOLARIS) { File f = File.createTempFile("stat", null); try { FileStat st = posix.stat(f.getAbsolutePath()); if (Platform.IS_32_BIT) { assertSame("incorrect stat instance returned", SolarisFileStat32.class, st.getClass()); } else { assertSame("incorrect stat instance returned", SolarisFileStat64.class, st.getClass()); } } finally { f.delete(); } } } } jnr-posix-3.0.12/src/test/java/jnr/posix/FileTest.java000066400000000000000000000252001252174056300225360ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Fcntl; import jnr.constants.platform.Errno; import jnr.constants.platform.OpenFlags; import org.junit.BeforeClass; import org.junit.Test; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; public class FileTest { private static POSIX posix; @BeforeClass public static void setUpClass() throws Exception { posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); } @Test public void utimeTest() throws Throwable { File f = File.createTempFile("utime", null); long oldTime = posix.stat(f.getAbsolutePath()).mtime(); Thread.sleep(2000); int rval = posix.utimes(f.getAbsolutePath(), null, null); assertEquals("utime did not return 0", 0, rval); long newTime = posix.stat(f.getAbsolutePath()).mtime(); f.delete(); assertTrue("mtime failed", newTime > oldTime); } @Test public void futimeTest() throws Throwable { File f = File.createTempFile("jnr-posix-futime", "tmp"); long oldTime = posix.stat(f.getAbsolutePath()).mtime(); Thread.sleep(2000); int fd = posix.open(f.getAbsolutePath(), OpenFlags.O_RDWR.intValue(), 0666); int rval = posix.futimes(fd, null, null); assertEquals("futime did not return 0", 0, rval); long newTime = posix.stat(f.getAbsolutePath()).mtime(); f.delete(); assertTrue("mtime failed", newTime > oldTime); } @Test public void linkTest() throws Throwable { File f1 = File.createTempFile("utime", null); File f2 = new File(f1.getAbsolutePath() + "link"); int rval = posix.link(f1.getAbsolutePath(), f2.getAbsolutePath()); assertEquals("link did not return 0", 0, rval); assertTrue("Link was not created", f2.exists()); f1.delete(); f2.delete(); } @Test public void mkdirRelativeTest() throws Throwable { File dir = new File("tmp"); int rval = posix.mkdir(dir.getPath(), 0); assertEquals("mkdir did not return 0", 0, rval); assertTrue("Directory was not created", dir.exists()); dir.delete(); } @Test public void mkdirAbsoluteTest() throws Throwable { File dir = new File("tmp"); int rval = posix.mkdir(dir.getAbsolutePath(), 0); assertEquals("mkdir did not return 0", 0, rval); assertTrue("Directory was not created", dir.exists()); dir.delete(); } @Test public void flockTest() throws Throwable { File tmp = File.createTempFile("flockTest", "tmp"); RandomAccessFile raf = new RandomAccessFile(tmp, "rw"); RandomAccessFile raf2 = new RandomAccessFile(tmp, "rw"); FileChannel fc = raf.getChannel(); FileChannel fc2 = raf2.getChannel(); FileDescriptor FD = JavaLibCHelper.getDescriptorFromChannel(fc); FileDescriptor FD2 = JavaLibCHelper.getDescriptorFromChannel(fc2); int fd = JavaLibCHelper.getfdFromDescriptor(FD); int fd2 = JavaLibCHelper.getfdFromDescriptor(FD2); assertEquals(0, posix.flock(fd, 1)); // LOCK_SH assertEquals(0, posix.flock(fd, 8)); // LOCK_UN assertEquals(0, posix.flock(fd, 2)); // LOCK_EX assertEquals(-1, posix.flock(fd2, 2 | 4)); // LOCK_EX | LOCK_NB assertEquals(0, posix.flock(fd, 8)); // LOCK_UN } @Test public void dupTest() throws Throwable { File tmp = File.createTempFile("dupTest", "tmp"); RandomAccessFile raf = new RandomAccessFile(tmp, "rw"); FileChannel fileChannel = raf.getChannel(); int fileDescriptor = JavaLibCHelper.getfdFromDescriptor(JavaLibCHelper.getDescriptorFromChannel(fileChannel)); byte[] outContent = "foo".getBytes(); FileDescriptor newFileDescriptor = JavaLibCHelper.toFileDescriptor(posix.dup(fileDescriptor)); new FileOutputStream(JavaLibCHelper.toFileDescriptor(fileDescriptor)).write(outContent); raf.seek(0); byte[] inContent = new byte[outContent.length]; new FileInputStream(newFileDescriptor).read(inContent, 0, 3); assertArrayEquals(inContent, outContent); } @Test public void dup2Test() throws Throwable { File tmp = File.createTempFile("dupTest", "tmp"); RandomAccessFile raf = new RandomAccessFile(tmp, "rw"); int oldFd = JavaLibCHelper.getfdFromDescriptor(JavaLibCHelper.getDescriptorFromChannel(new RandomAccessFile(tmp, "rw").getChannel())); int newFd = JavaLibCHelper.getfdFromDescriptor(JavaLibCHelper.getDescriptorFromChannel(raf.getChannel())); byte[] outContent = "foo".getBytes(); FileDescriptor newFileDescriptor = JavaLibCHelper.toFileDescriptor(posix.dup2(oldFd, newFd)); new FileOutputStream(JavaLibCHelper.toFileDescriptor(oldFd)).write(outContent); raf.seek(0); byte[] inContent = new byte[outContent.length]; new FileInputStream(newFileDescriptor).read(inContent, 0, 3); assertArrayEquals(inContent, outContent); } @Test public void fcntlDupfdTest() throws Throwable { File tmp = File.createTempFile("fcntlTest", "tmp"); RandomAccessFile raf = new RandomAccessFile(tmp, "rw"); int fd = JavaLibCHelper.getfdFromDescriptor(JavaLibCHelper.getDescriptorFromChannel(raf.getChannel())); byte[] outContent = "foo".getBytes(); int newFd = posix.fcntl(fd, Fcntl.F_DUPFD); new FileOutputStream(JavaLibCHelper.toFileDescriptor(fd)).write(outContent); raf.seek(0); byte[] inContent = new byte[outContent.length]; new FileInputStream(JavaLibCHelper.toFileDescriptor(newFd)).read(inContent, 0, 3); assertArrayEquals(inContent, outContent); } @Test public void fcntlDupfdWithArgTest() throws Throwable { File tmp = File.createTempFile("dupTest", "tmp"); int oldFd = JavaLibCHelper.getfdFromDescriptor(JavaLibCHelper.getDescriptorFromChannel( new RandomAccessFile(tmp, "rw").getChannel())); int newFd = JavaLibCHelper.getfdFromDescriptor(JavaLibCHelper.getDescriptorFromChannel( new RandomAccessFile(tmp, "rw").getChannel())); byte[] outContent = "foo".getBytes(); int dupFd = posix.fcntl(oldFd, Fcntl.F_DUPFD, newFd); new FileOutputStream(JavaLibCHelper.toFileDescriptor(newFd)).write(outContent); byte[] inContent = new byte[outContent.length]; new FileInputStream(JavaLibCHelper.toFileDescriptor(dupFd)).read(inContent, 0, 3); assertTrue(dupFd > newFd); assertArrayEquals(inContent, outContent); } @Test public void closeTest() throws Throwable { File tmp = File.createTempFile("closeTest", "tmp"); int fd = JavaLibCHelper.getfdFromDescriptor(JavaLibCHelper.getDescriptorFromChannel(new RandomAccessFile(tmp, "rw").getChannel())); int result; result = posix.close(fd); assertEquals(0, result); result = posix.close(fd); assertEquals(-1, result); assertEquals(Errno.EBADF.intValue(), posix.errno()); } @Test public void unlinkTest() throws Throwable { File tmp = File.createTempFile("unlinkTest", "tmp"); RandomAccessFile raf = new RandomAccessFile(tmp, "rw"); raf.write("hello".getBytes()); int res = posix.unlink(tmp.getCanonicalPath()); assertEquals(0, res); assertFalse(tmp.exists()); raf.write("world".getBytes()); raf.seek(0); byte[] actual = new byte[10]; raf.read(actual); assertArrayEquals("helloworld".getBytes(), actual); } @Test public void openTest() throws Throwable { int fd = posix.open("pom.xml", 0, 0666); assertNotEquals(-1, fd); int result = posix.close(fd); assertEquals(0, result); result = posix.close(fd); assertEquals(-1, result); } @Test public void writeTest() throws Throwable { String str = "To thine own self be true"; File tmp = File.createTempFile("writeTest", "tmp"); ByteBuffer buffer = ByteBuffer.wrap(str.getBytes()); int fd = posix.open(tmp.getAbsolutePath(), 1, 066); posix.write(fd, buffer, str.length()); posix.close(fd); RandomAccessFile raf = new RandomAccessFile(tmp, "rw"); assertEquals(raf.readLine(), new String(buffer.array())); posix.unlink(tmp.getAbsolutePath()); } @Test public void pwriteTest() throws Throwable { String str = "Now is the winter of our discontent"; File tmp = File.createTempFile("pwriteTest", "tmp"); RandomAccessFile raf = new RandomAccessFile(tmp, "rw"); raf.write(str.getBytes()); raf.close(); String str2 = "summer"; ByteBuffer buffer = ByteBuffer.wrap(str2.getBytes()); int fd = posix.open(tmp.getAbsolutePath(), 1, 066); posix.pwrite(fd, buffer, str2.length(), 11); posix.close(fd); raf = new RandomAccessFile(tmp, "r"); assertEquals(raf.readLine(), "Now is the summer of our discontent"); posix.unlink(tmp.getAbsolutePath()); } @Test public void ftruncateTest() throws Throwable { String str = "Beware the Jabberwock, my son!"; File tmp = File.createTempFile("ftruncateTest", "tmp"); RandomAccessFile raf = new RandomAccessFile(tmp, "rw"); raf.write(str.getBytes()); raf.close(); int fd = posix.open(tmp.getAbsolutePath(), OpenFlags.O_RDWR.intValue(), 0666); posix.ftruncate(fd, 21); posix.lseek(fd, 0, 0); byte[] buf = new byte[21]; int read = posix.read(fd, buf, 31); assertEquals(21, read); assertArrayEquals(buf, "Beware the Jabberwock".getBytes()); } @Test public void fcntlTest() throws Throwable { int[] fds = new int[2]; int ret = posix.pipe(fds); assertEquals(0, ret); int flags = posix.fcntlInt(fds[0], Fcntl.F_GETFD, 0); posix.fcntlInt(fds[0], Fcntl.F_SETFD, flags | 1); // FD_CLOEXEC assertEquals(1, posix.fcntlInt(fds[0], Fcntl.F_GETFD, 0)); } @Test public void fchmodTest() throws IOException { File tmp = File.createTempFile("jnr-posix-chmod-test", "tmp"); int fd = posix.open(tmp.getAbsolutePath(), OpenFlags.O_RDWR.intValue(), 0600); assertEquals("chmod: ", 0, posix.fchmod(fd, 0)); assertEquals("chmod: ", 0, posix.fchmod(fd, 0777)); tmp.delete(); } } jnr-posix-3.0.12/src/test/java/jnr/posix/GroupTest.java000066400000000000000000000040661252174056300227620ustar00rootroot00000000000000 package jnr.posix; import java.util.ArrayList; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; /** * */ public class GroupTest { public GroupTest() { } private static POSIX posix; @BeforeClass public static void setUpClass() throws Exception { posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { } @After public void tearDown() { } // TODO add test methods here. // The methods must be annotated with annotation @Test. For example: // // @Test // public void hello() {} @Test public void getgrnam() { if (jnr.ffi.Platform.getNativePlatform().isUnix()) { final String LOGIN = "nogroup"; Group grp = posix.getgrnam(LOGIN); assertNotNull(grp); assertEquals("Login name not equal", LOGIN, grp.getName()); } } @Test public void nonExistantGroupReturnsNull() { if (jnr.ffi.Platform.getNativePlatform().isUnix()) { final String LOGIN = "dkjhfjkdsfhjksdhfsdjkhfsdkjhfdskj"; assertNull("getpwnam for non-existant group should return null", posix.getgrnam(LOGIN)); } } @Test public void getgrent() { if (jnr.ffi.Platform.getNativePlatform().isUnix()) { ArrayList grps = new ArrayList(); while (true) { Group grp = posix.getgrent(); if (grp == null) { break; } grps.add(grp); } for (Group grp : grps) { assertNotNull(grp.getName()); assertNotNull(grp.getPassword()); assertNotNull(grp.getGID()); for (String member : grp.getMembers()) { assertNotNull(member); } } } } } jnr-posix-3.0.12/src/test/java/jnr/posix/HANDLETest.java000066400000000000000000000035561252174056300226240ustar00rootroot00000000000000package jnr.posix; import jnr.posix.util.Platform; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; public class HANDLETest { private static POSIX posix; @BeforeClass public static void setUpClass() throws Exception { posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); } @Test public void invalidHandleIsInValid() { assertFalse(HANDLE.valueOf(-1L).isValid()); } private static WindowsLibC kernel32() { return (WindowsLibC) posix.libc(); } // This really is matching pipe for the sake of these unit tests but in general the // in/out/error handle will vary depending on how a CLI supplies it: // http://stackoverflow.com/questions/9021916/how-do-i-check-if-my-delphi-console-app-is-redirected-to-a-file-or-pipe private boolean isValidHandleType(HANDLE handle) { int type = kernel32().GetFileType(handle); return type == WindowsLibC.FILE_TYPE_DISK || type == WindowsLibC.FILE_TYPE_PIPE; } @Test public void stdinHandle() { if (Platform.IS_WINDOWS) { assertTrue(kernel32().GetStdHandle(WindowsLibC.STD_INPUT_HANDLE).isValid()); assertTrue(isValidHandleType(kernel32().GetStdHandle(WindowsLibC.STD_INPUT_HANDLE))); } } @Test public void stdoutHandle() { if (Platform.IS_WINDOWS) { assertTrue(kernel32().GetStdHandle(WindowsLibC.STD_OUTPUT_HANDLE).isValid()); assertTrue(isValidHandleType(kernel32().GetStdHandle(WindowsLibC.STD_OUTPUT_HANDLE))); } } @Test public void stderrHandle() { if (Platform.IS_WINDOWS) { assertTrue(kernel32().GetStdHandle(WindowsLibC.STD_ERROR_HANDLE).isValid()); assertTrue(isValidHandleType(kernel32().GetStdHandle(WindowsLibC.STD_ERROR_HANDLE))); } } } jnr-posix-3.0.12/src/test/java/jnr/posix/IDTest.java000066400000000000000000000046411252174056300221610ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; public class IDTest { public IDTest() { } private static POSIX nativePosix, javaPosix; @BeforeClass public static void setUpClass() throws Exception { nativePosix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); javaPosix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), false); } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { } @After public void tearDown() { } @Test public void getuid() { assertEquals("Java posix did not return same value as native posix", nativePosix.getuid(), javaPosix.getuid()); } @Test public void getgid() { assertEquals("Java posix did not return same value as native posix", nativePosix.getgid(), javaPosix.getgid()); } }jnr-posix-3.0.12/src/test/java/jnr/posix/IOTest.java000066400000000000000000000146641252174056300222020ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.*; import jnr.posix.util.Platform; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.io.File; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; /** * Created by headius on 5/31/14. */ public class IOTest { private static POSIX posix; @BeforeClass public static void setUpClass() throws Exception { posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); } @Test public void testOpenReadWrite() throws Throwable { File tmp = File.createTempFile("IOTest", "testOpen"); int fd = posix.open(tmp.getPath(), OpenFlags.O_RDWR.intValue(), 0666); Assert.assertTrue(fd > 0); byte[] hello = "hello".getBytes(); int written = posix.write(fd, hello, 5); Assert.assertEquals(5, written); byte[] buf = new byte[5]; posix.lseek(fd, 0, 0); // no jnr-constants for SEEK_SET int read = posix.read(fd, buf, 5); Assert.assertEquals(5, read); Assert.assertArrayEquals(hello, buf); byte[] goodbye = "goodbye".getBytes(); written = posix.pwrite(fd, goodbye, 7, 3); Assert.assertEquals(7, written); Assert.assertEquals(5, posix.lseek(fd, 0, 1)); // SEEK_CUR byte[] bye = new byte[3]; read = posix.pread(fd, bye, 3, 7); Assert.assertEquals(3, read); Assert.assertEquals(5, posix.lseek(fd, 0, 1)); // SEEK_CUR Assert.assertArrayEquals("bye".getBytes(), bye); } @Test public void testPipe() throws Throwable { int[] fds = {0, 0}; int ret = posix.pipe(fds); Assert.assertTrue(ret >= 0); Assert.assertTrue(fds[0] > 0); Assert.assertTrue(fds[1] > 0); byte[] hello = "hello".getBytes(); int written = posix.write(fds[1], hello, 5); Assert.assertEquals(5, written); byte[] buf = new byte[5]; int read = posix.read(fds[0], buf, 5); Assert.assertEquals(5, read); Assert.assertArrayEquals(buf, hello); } @Test public void testSocketPair() throws Throwable { if (!Platform.IS_WINDOWS) { int[] fds = {0, 0}; int ret = posix.socketpair(AddressFamily.AF_UNIX.intValue(), Sock.SOCK_STREAM.intValue(), 0, fds); Assert.assertTrue(ret >= 0); Assert.assertTrue(fds[0] > 0); Assert.assertTrue(fds[1] > 0); byte[] hello = "hello".getBytes(); int written = posix.write(fds[1], hello, 5); Assert.assertEquals(5, written); byte[] buf = new byte[5]; int read = posix.read(fds[0], buf, 5); Assert.assertEquals(5, read); Assert.assertArrayEquals(buf, hello); hello = "goodbye".getBytes(); written = posix.write(fds[0], hello, 7); Assert.assertEquals(7, written); buf = new byte[7]; read = posix.read(fds[1], buf, 7); Assert.assertEquals(7, read); Assert.assertArrayEquals(buf, hello); } } @Test public void testSendRecvMsg_NoControl() throws Throwable { if (!Platform.IS_WINDOWS) { int[] fds = {0, 0}; int ret = posix.socketpair(AddressFamily.AF_UNIX.intValue(), Sock.SOCK_STREAM.intValue(), 0, fds); Assert.assertTrue(ret >= 0); Assert.assertTrue(fds[0] > 0); Assert.assertTrue(fds[1] > 0); MsgHdr outMessage = posix.allocateMsgHdr(); String data = "does this work?"; byte[] dataBytes = data.getBytes(); ByteBuffer[] outIov = new ByteBuffer[1]; outIov[0] = ByteBuffer.allocateDirect(dataBytes.length); outIov[0].put(dataBytes); outIov[0].flip(); outMessage.setIov(outIov); int sendStatus = posix.sendmsg(fds[0], outMessage, 0); Assert.assertTrue(sendStatus == dataBytes.length); // ---------------- MsgHdr inMessage = posix.allocateMsgHdr(); ByteBuffer[] inIov = new ByteBuffer[1]; inIov[0] = ByteBuffer.allocateDirect(1024); inMessage.setIov(inIov); int recvStatus = posix.recvmsg(fds[1], inMessage, 0); Assert.assertTrue(recvStatus == dataBytes.length); for (int i = 0; i < recvStatus; ++i) { Assert.assertEquals(dataBytes[i], outIov[0].get(i)); } } } @Test public void testSendRecvMsg_WithControl() throws Throwable { if (!Platform.IS_WINDOWS) { int[] fds = {0, 0}; int ret = posix.socketpair(AddressFamily.AF_UNIX.intValue(), Sock.SOCK_STREAM.intValue(), 0, fds); String data = "does this work?"; byte[] dataBytes = data.getBytes(); Assert.assertTrue(ret >= 0); Assert.assertTrue(fds[0] > 0); Assert.assertTrue(fds[1] > 0); MsgHdr outMessage = posix.allocateMsgHdr(); ByteBuffer[] outIov = new ByteBuffer[1]; outIov[0] = ByteBuffer.allocateDirect(dataBytes.length); outIov[0].put(dataBytes); outIov[0].flip(); outMessage.setIov(outIov); CmsgHdr outControl = outMessage.allocateControl(4); outControl.setLevel(SocketLevel.SOL_SOCKET.intValue()); outControl.setType(0x01); ByteBuffer fdBuf = ByteBuffer.allocateDirect(4); fdBuf.order(ByteOrder.nativeOrder()); fdBuf.putInt(0, fds[0]); outControl.setData(fdBuf); int sendStatus = posix.sendmsg(fds[0], outMessage, 0); Assert.assertTrue(sendStatus == dataBytes.length); // ---------------- MsgHdr inMessage = posix.allocateMsgHdr(); ByteBuffer[] inIov = new ByteBuffer[1]; inIov[0] = ByteBuffer.allocateDirect(1024); inMessage.setIov(inIov); inMessage.allocateControl(4); int recvStatus = posix.recvmsg(fds[1], inMessage, 0); Assert.assertTrue(recvStatus == dataBytes.length); ByteBuffer inFdBuf = inMessage.getControls()[0].getData(); inFdBuf.order(ByteOrder.nativeOrder()); int fd = inFdBuf.getInt(); Assert.assertTrue(fd != 0); for (int i = 0; i < recvStatus; ++i) { Assert.assertEquals(dataBytes[i], outIov[0].get(i)); } } } } jnr-posix-3.0.12/src/test/java/jnr/posix/JavaFileStatTest.java000066400000000000000000000010461252174056300241760ustar00rootroot00000000000000/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jnr.posix; import jnr.posix.JavaFileStat; import junit.framework.TestCase; import jnr.posix.util.Platform; /** * * @author nicksieger */ public class JavaFileStatTest extends TestCase { public void testSetup() { JavaFileStat fs = new JavaFileStat(null, null); if (Platform.IS_WINDOWS) { fs.setup("c:/"); } else { fs.setup("/"); } assertFalse(fs.isSymlink()); } } jnr-posix-3.0.12/src/test/java/jnr/posix/JavaPOSIXTest.java000066400000000000000000000043421252174056300233670ustar00rootroot00000000000000/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package jnr.posix; import java.io.File; import java.io.IOException; import jnr.posix.JavaPOSIX; import jnr.posix.POSIX; import jnr.posix.Passwd; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; /** * * @author wayne */ public class JavaPOSIXTest { POSIX posix; public JavaPOSIXTest() { } @BeforeClass public static void setUpClass() throws Exception { } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { posix = new JavaPOSIX(new DummyPOSIXHandler()); } @After public void tearDown() { posix = null; } // TODO add test methods here. // The methods must be annotated with annotation @Test. For example: // // @Test // public void hello() {} @Test public void uid() { } @Test public void lstatTest() throws IOException { File file = createTestFile(); FileStat stat = posix.lstat(file.getName()); assertTrue(stat.isEmpty()); stat = posix.allocateStat(); int ret = posix.lstat(file.getPath(), stat); assertTrue(ret >= 0); assertTrue(stat.isEmpty()); file.delete(); stat = posix.allocateStat(); ret = posix.lstat(file.getPath(), stat); assertTrue(ret < 0); } @Test public void chmodTest() throws IOException { File file = createTestFile(); assertEquals("chmod: ", 0, posix.chmod(file.getName(), 0)); assertEquals("chmod: ", 0, posix.chmod(file.getName(), 0777)); file.delete(); } @Test public void getpwuid() { Passwd pwd = posix.getpwuid(posix.getuid()); assertNotNull("getpwuid failed", pwd); } @Test public void isNative() { assertFalse("JavaPOSIX isNative should be false", posix.isNative()); } private File createTestFile() throws IOException { // create a tmp file String fName = "test.dat"; File file = new File(fName); file.createNewFile(); return file; } } jnr-posix-3.0.12/src/test/java/jnr/posix/POSIXFactoryTest.java000066400000000000000000000007161252174056300241160ustar00rootroot00000000000000package jnr.posix; import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.*; public class POSIXFactoryTest { @Test public void testGetNativePOSIX() throws Throwable { POSIX posix = POSIXFactory.getNativePOSIX(); assertTrue(posix.isNative()); } @Test public void testGetJavaPOSIX() throws Throwable { POSIX posix = POSIXFactory.getJavaPOSIX(); assertFalse(posix.isNative()); } } jnr-posix-3.0.12/src/test/java/jnr/posix/PasswdTest.java000066400000000000000000000036651252174056300231330ustar00rootroot00000000000000 package jnr.posix; import jnr.posix.util.Platform; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; /** * */ public class PasswdTest { public PasswdTest() { } private static POSIX posix; private static Class passwdClass; @BeforeClass public static void setUpClass() throws Exception { if (Platform.IS_MAC) { passwdClass = MacOSPasswd.class; } else if (Platform.IS_LINUX) { passwdClass = LinuxPasswd.class; } else if (Platform.IS_FREEBSD) { passwdClass = FreeBSDPasswd.class; } else if (Platform.IS_OPENBSD) { passwdClass = OpenBSDPasswd.class; } else if (Platform.IS_SOLARIS) { passwdClass = SolarisPasswd.class; } else if (Platform.IS_WINDOWS) { // Skipping these tests for windows } else { throw new IllegalArgumentException("Platform not supported"); } posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { } @After public void tearDown() { } @Test public void getpwnam() { if (jnr.ffi.Platform.getNativePlatform().isUnix()) { final String LOGIN = "root"; Passwd pwd = posix.getpwnam(LOGIN); assertNotNull(pwd); assertEquals("Login name not equal", LOGIN, pwd.getLoginName()); assertTrue(pwd.getClass().equals(passwdClass)); } } @Test public void nonExistantUserReturnsNull() { if (jnr.ffi.Platform.getNativePlatform().isUnix()) { final String LOGIN = "dkjhfjkdsfhjksdhfsdjkhfsdkjhfdskj"; assertNull("getpwnam for non-existant user should return null", posix.getpwnam(LOGIN)); } } } jnr-posix-3.0.12/src/test/java/jnr/posix/ProcessTest.java000066400000000000000000000016321252174056300233000ustar00rootroot00000000000000package jnr.posix; import com.kenai.jffi.Platform; import jnr.constants.platform.Sysconf; import jnr.posix.util.FieldAccess; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.nio.channels.Pipe; import java.util.Arrays; import java.util.Collections; import static org.junit.Assert.*; public class ProcessTest { private static POSIX posix; @BeforeClass public static void setUpClass() throws Exception { posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); } @Test public void times() { long hz = posix.sysconf(Sysconf._SC_CLK_TCK); if (Platform.getPlatform().getOS() == Platform.OS.DARWIN) { assertEquals("incorrect HZ value", 100L, hz); } } @Test public void getcwd() { String propCwd = System.getProperty("user.dir"); assertEquals(propCwd, posix.getcwd()); } } jnr-posix-3.0.12/src/test/java/jnr/posix/SignalTest.java000066400000000000000000000035241252174056300231010ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.Signal; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import java.util.concurrent.atomic.AtomicBoolean; public class SignalTest { private static POSIX posix; private static POSIX javaPosix; @BeforeClass public static void setupClass() throws Exception { posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); javaPosix = new JavaPOSIX(new DummyPOSIXHandler()); } private static void waitUntilTrue(AtomicBoolean var, long maxWait) { long start = System.currentTimeMillis(); while (!var.get() && (System.currentTimeMillis() - start) < maxWait) { try { Thread.sleep(1); } catch (InterruptedException e) { break; } } } @Test public void testBasicSignal() { Signal s = Signal.SIGHUP; final AtomicBoolean fired = new AtomicBoolean(false); posix.signal(s, new SignalHandler() { public void handle(int signal) { fired.set(true); } }); posix.kill(posix.getpid(), s.intValue()); waitUntilTrue(fired, 200); Assert.assertTrue(fired.get()); } @Test public void testJavaSignal() { Signal s = Signal.SIGHUP; final AtomicBoolean fired = new AtomicBoolean(false); javaPosix.signal(s, new SignalHandler() { public void handle(int signal) { fired.set(true); } }); // have to use native here; no abstraction for kill in pure Java // TODO: sun.misc.Signal.raise can be used to kill current pid posix.kill(posix.getpid(), s.intValue()); waitUntilTrue(fired, 200); Assert.assertTrue(fired.get()); } } jnr-posix-3.0.12/src/test/java/jnr/posix/SpawnTest.java000066400000000000000000000201101252174056300227420ustar00rootroot00000000000000package jnr.posix; import jnr.constants.platform.windows.OpenFlags; import jnr.ffi.Library; import jnr.ffi.Platform; import jnr.ffi.annotations.Out; import org.junit.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collections; import java.util.List; import static org.junit.Assert.*; import static jnr.posix.SpawnFileAction.*; public class SpawnTest { private static POSIX posix; private static LibC libc; private static final List emptyEnv = Arrays.asList(new String[0]); private static final List emptyActions = Arrays.asList(new SpawnFileAction[0]); public static interface LibC { int pipe(@Out int[] fds); } @BeforeClass public static void setUpClass() throws Exception { if (Platform.getNativePlatform().isUnix()) { posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true); libc = Library.loadLibrary(LibC.class, "c"); } } @Test public void validPid() { if (Platform.getNativePlatform().isUnix()) { long pid = -1; try { pid = posix.posix_spawnp("true", emptyActions, Arrays.asList("true"), emptyEnv); assertTrue(pid != -1); } finally { if (pid != -1) posix.libc().waitpid((int) pid, null, 0); } } } private static void closePipe(int[] fds) { posix.libc().close(fds[0]); posix.libc().close(fds[1]); } private static void killChild(long pid) { if (pid > 0) { posix.libc().kill((int) pid, 9); posix.libc().waitpid((int) pid, null, 0); } } @Test public void outputPipe() { if (Platform.getNativePlatform().isUnix()) { int[] outputPipe = { -1, -1 }; long pid = -1; try { assertFalse(libc.pipe(outputPipe) < 0); assertNotSame(-1, outputPipe[0]); assertNotSame(-1, outputPipe[1]); List actions = Arrays.asList(dup(outputPipe[1], 1)); pid = posix.posix_spawnp("echo", actions, Arrays.asList("echo", "bar"), emptyEnv); assertTrue(pid != -1); // close the write side of the output pipe, so read() will return immediately once the process has exited posix.libc().close(outputPipe[1]); ByteBuffer output = ByteBuffer.allocate(100); long nbytes = posix.libc().read(outputPipe[0], output, output.remaining()); assertEquals(4L, nbytes); output.position((int) nbytes).flip(); byte[] bytes = new byte[output.remaining()]; output.get(bytes); assertEquals("bar", new String(bytes).trim()); } finally { closePipe(outputPipe); killChild(pid); } } } @Test public void inputPipe() { if (Platform.getNativePlatform().isUnix()) { int[] outputPipe = { -1, -1 }; int[] inputPipe = { -1, -1 }; long pid = -1; try { assertFalse(libc.pipe(outputPipe) < 0); assertFalse(libc.pipe(inputPipe) < 0); assertNotSame(-1, outputPipe[0]); assertNotSame(-1, outputPipe[1]); assertNotSame(-1, inputPipe[0]); assertNotSame(-1, inputPipe[1]); List actions = Arrays.asList(dup(inputPipe[0], 0), dup(outputPipe[1], 1)); pid = posix.posix_spawnp("cat", actions, Arrays.asList("cat", "-"), emptyEnv); assertTrue(pid != -1); posix.libc().close(inputPipe[0]); assertEquals(3, posix.libc().write(inputPipe[1], ByteBuffer.wrap("foo".getBytes(Charset.forName("US-ASCII"))), 3)); posix.libc().close(inputPipe[1]); // send EOF to process // close the write side of the output pipe, so read() will return immediately once the process has exited posix.libc().close(outputPipe[1]); ByteBuffer output = ByteBuffer.allocate(100); long nbytes = posix.libc().read(outputPipe[0], output, output.remaining()); assertEquals(3L, nbytes); output.position((int) nbytes).flip(); byte[] bytes = new byte[output.remaining()]; output.get(bytes); assertEquals("foo", new String(bytes).trim()); } finally { closePipe(outputPipe); closePipe(inputPipe); killChild(pid); } } } /* FIXME: Does not pass on seemingly only travis @Test public void inputFile() throws IOException { if (Platform.getNativePlatform().isUnix()) { File inputFile = File.createTempFile("foo", null); FileOutputStream inputStream = new FileOutputStream(inputFile); inputStream.write("foo".getBytes("US-ASCII")); inputStream.close(); int[] outputPipe = { -1, -1 }; long pid = -1; try { assertFalse(libc.pipe(outputPipe) < 0); assertNotSame(-1, outputPipe[0]); assertNotSame(-1, outputPipe[1]); List actions = Arrays.asList(open(inputFile.getAbsolutePath(), 0, OpenFlags.O_RDONLY.intValue(), 0444), dup(outputPipe[1], 1), close(outputPipe[0])); pid = posix.posix_spawnp("cat", actions, Arrays.asList("cat", "-"), emptyEnv); assertTrue(pid != -1); // close the write side of the output pipe, so read() will return immediately once the process has exited posix.libc().close(outputPipe[1]); ByteBuffer output = ByteBuffer.allocate(100); long nbytes = posix.libc().read(outputPipe[0], output, output.remaining()); assertEquals(3L, nbytes); output.position((int) nbytes).flip(); byte[] bytes = new byte[output.remaining()]; output.get(bytes); assertEquals("foo", new String(bytes).trim()); } finally { closePipe(outputPipe); killChild(pid); } } } */ @Test public void closeInput() throws IOException { if (Platform.getNativePlatform().isUnix()) { int[] outputPipe = { -1, -1 }; int[] inputPipe = { -1, -1 }; long pid = -1; try { assertFalse(libc.pipe(outputPipe) < 0); assertFalse(libc.pipe(inputPipe) < 0); assertNotSame(-1, outputPipe[0]); assertNotSame(-1, outputPipe[1]); assertNotSame(-1, inputPipe[0]); assertNotSame(-1, inputPipe[1]); List actions = Arrays.asList(dup(outputPipe[1], 1), open("/dev/null", 2, OpenFlags.O_WRONLY.intValue(), 0444), close(inputPipe[0]), close(inputPipe[1])); pid = posix.posix_spawnp("cat", actions, Arrays.asList("cat", "/dev/fd/" + inputPipe[0]), emptyEnv); assertTrue(pid != -1); assertEquals(3, posix.libc().write(inputPipe[1], ByteBuffer.wrap("foo".getBytes(Charset.forName("US-ASCII"))), 3)); posix.libc().close(inputPipe[1]); // send EOF to process // close the write side of the output pipe, so read() will return immediately once the process has exited posix.libc().close(outputPipe[1]); // Output from the process on stdout should be empty ByteBuffer output = ByteBuffer.allocate(100); long nbytes = posix.libc().read(outputPipe[0], output, output.remaining()); assertEquals(0L, nbytes); } finally { closePipe(inputPipe); closePipe(outputPipe); killChild(pid); } } } } jnr-posix-3.0.12/src/test/java/jnr/posix/util/000077500000000000000000000000001252174056300211325ustar00rootroot00000000000000jnr-posix-3.0.12/src/test/java/jnr/posix/util/DefaultPOSIXHandlerTest.java000066400000000000000000000012571252174056300263470ustar00rootroot00000000000000package jnr.posix.util; import jnr.posix.POSIX; import jnr.posix.POSIXFactory; import org.junit.Test; import java.lang.management.ManagementFactory; import java.util.logging.Level; import java.util.logging.Logger; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; public class DefaultPOSIXHandlerTest { @Test public void testGetPid() throws Exception { int pid_from_jmx = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]); POSIX posix = POSIXFactory.getPOSIX(new DefaultPOSIXHandler(), true); int pid = posix.getpid(); assertThat(pid, equalTo(pid_from_jmx)); } } jnr-posix-3.0.12/src/test/java/jnr/posix/util/PlatformTest.java000066400000000000000000000045051252174056300244250ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix.util; import jnr.posix.util.Platform; import junit.framework.TestCase; /* * Admittedly, there is a lot of cyclomatic complexity and system-dependent * testing going on here. At least it's in one place. */ public class PlatformTest extends TestCase { public void testBitLengths() { assertTrue("Not 32 or 64-bit platform?", Platform.IS_32_BIT ^ Platform.IS_64_BIT); } public void testEnvCommand() { String command =Platform.envCommand(); if (Platform.IS_WINDOWS_9X) { assertEquals("Fails on Windows 95/98", "command.com /c set", command); } if (Platform.IS_WINDOWS & !Platform.IS_WINDOWS_9X) { assertEquals("Fails on Windows other than 95/98", "cmd.exe /c set", command); } if (!Platform.IS_WINDOWS) { assertEquals("Fails on Non-Windows platform", "env", command); } } } jnr-posix-3.0.12/src/test/java/jnr/posix/util/WindowsHelpersTest.java000066400000000000000000000043111252174056300256110ustar00rootroot00000000000000/***** BEGIN LICENSE BLOCK ***** * Version: CPL 1.0/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Common Public * License Version 1.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.eclipse.org/legal/cpl-v10.html * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the CPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the CPL, the GPL or the LGPL. ***** END LICENSE BLOCK *****/ /** * $Id: $ */ package jnr.posix.util; import jnr.posix.util.WindowsHelpers; import junit.framework.TestCase; public class WindowsHelpersTest extends TestCase { public void testIsBatch() { assertTrue(WindowsHelpers.isBatch("file.bat")); assertTrue(WindowsHelpers.isBatch("FILE.BAT")); assertTrue(WindowsHelpers.isBatch("file.cmd")); assertTrue(WindowsHelpers.isBatch("FILE.CMD")); assertFalse(WindowsHelpers.isBatch("file.exe")); assertFalse(WindowsHelpers.isBatch("file.txt")); assertFalse(WindowsHelpers.isBatch("file")); assertFalse(WindowsHelpers.isBatch("filebat")); assertFalse(WindowsHelpers.isBatch("filecmd")); assertFalse(WindowsHelpers.isBatch("")); assertFalse(WindowsHelpers.isBatch(null)); } }