├── .gitignore ├── .perltidyrc ├── README.md ├── Makefile ├── postmirror.sh ├── mirror.list ├── CHANGELOG ├── LICENSE └── apt-mirror /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.tar.* 3 | -------------------------------------------------------------------------------- /.perltidyrc: -------------------------------------------------------------------------------- 1 | -bl 2 | -l=500 3 | -i=4 4 | -pt=1 5 | -bt=1 6 | -sbt=1 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | apt-mirror 2 | ========== 3 | 4 | See: http://apt-mirror.github.com 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | VERSION := 0.5.1 2 | DIST := apt-mirror CHANGELOG LICENSE Makefile mirror.list postmirror.sh README.md .perltidyrc 3 | BASE_PATH := /var/spool/apt-mirror 4 | PREFIX ?= /usr/local 5 | 6 | all: 7 | 8 | dist: apt-mirror-$(VERSION).tar.xz 9 | 10 | install: 11 | install -m 755 -D apt-mirror $(DESTDIR)$(PREFIX)/bin/apt-mirror 12 | mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1/ 13 | pod2man apt-mirror > $(DESTDIR)$(PREFIX)/share/man/man1/apt-mirror.1 14 | if test ! -f $(DESTDIR)/etc/apt/mirror.list; then install -m 644 -D mirror.list $(DESTDIR)/etc/apt/mirror.list; fi 15 | mkdir -p $(DESTDIR)$(BASE_PATH)/mirror 16 | mkdir -p $(DESTDIR)$(BASE_PATH)/skel 17 | mkdir -p $(DESTDIR)$(BASE_PATH)/var 18 | 19 | %.tar.bz2: $(DIST) 20 | tar -c --exclude-vcs --transform="s@^@$*/@" $^ | bzip2 -cz9 > $@ 21 | 22 | %.tar.gz: $(DIST) 23 | tar -c --exclude-vcs --transform="s@^@$*/@" $^ | gzip -cn9 > $@ 24 | 25 | %.tar.xz: $(DIST) 26 | tar -c --exclude-vcs --transform="s@^@$*/@" $^ | xz -cz9 > $@ 27 | 28 | clean: 29 | rm -f *.tar.* 30 | 31 | .PHONY: all clean dist install 32 | -------------------------------------------------------------------------------- /postmirror.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | ## Anything in this file gets run AFTER the mirror has been run. 4 | ## Put your custom post mirror operations in here (like rsyncing the installer 5 | ## files and running clean.sh automatically)! 6 | 7 | ## Example of grabbing the extra installer files from Ubuntu (Note: rsync needs 8 | ## to be installed and in the path for this example to work correctly) 9 | 10 | #rsync --recursive --times --links --hard-links --delete --delete-after rsync://mirror.anl.gov/ubuntu/dists/trusty/main/debian-installer /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/dists/trusty/main/ 11 | #rsync --recursive --times --links --hard-links --delete --delete-after rsync://mirror.anl.gov/ubuntu/dists/trusty/main/dist-upgrader-all/ /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/dists/trusty/main/ 12 | #rsync --recursive --times --links --hard-links --delete --delete-after rsync://mirror.anl.gov/ubuntu/dists/trusty/main/installer-amd64/ /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/dists/trusty/main/ 13 | #rsync --recursive --times --links --hard-links --delete --delete-after rsync://mirror.anl.gov/ubuntu/dists/trusty/main/installer-i386/ /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/dists/trusty/main/ 14 | -------------------------------------------------------------------------------- /mirror.list: -------------------------------------------------------------------------------- 1 | set base_path /var/spool/apt-mirror 2 | set mirror_path $base_path/mirror 3 | set skel_path $base_path/skel 4 | set var_path $base_path/var 5 | set postmirror_script $var_path/postmirror.sh 6 | set defaultarch i386 7 | set run_postmirror 0 8 | set nthreads 20 9 | set _tilde 0 10 | # Use --unlink with wget (for use with hardlinked directories) 11 | set unlink 1 12 | set use_proxy off 13 | set http_proxy 127.0.0.1:3128 14 | set proxy_user user 15 | set proxy_password password 16 | 17 | deb http://archive.ubuntu.com/ubuntu precise main restricted universe multiverse 18 | deb http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse 19 | deb http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse 20 | deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse 21 | 22 | #deb-src http://archive.ubuntu.com/ubuntu precise main restricted universe multiverse 23 | #deb-src http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse 24 | #deb-src http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse 25 | #deb-src http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse 26 | 27 | clean http://archive.ubuntu.com/ubuntu 28 | 29 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | 0.5.1 (2014-04-12) 2 | * Fix find-translation-files bug. 3 | * Fix skel (indexes) updates in a hardlinked mirror 4 | * Added proxy support 5 | * Also mirror xz compressed files (fixes #25). 6 | 7 | 0.5.0 (2014-02-22) 8 | * Removed restrictions when parsing deb-$arch (Launchpad bug #913837) 9 | new arch's no longer need to be manually added 10 | via an apt-mirror source change. 11 | * Code cleaned up with perltidy and a .perltidyrc added 12 | * Changed to warn() instead of die() when sources go missing 13 | so mirroring of other sources continues. 14 | * Download all provided translation files instead of trying to download 15 | translation files based on a hard-coded language list. (Debian bug #725762) 16 | * Fix typos (e.g. Debian bug #736904 and Launchpad bug #1211097). 17 | * Mirror Contents-*.gz for each component. (Debian bug #647301) 18 | * Mirror InRelease files (fixes #3). 19 | 20 | 0.4.9 21 | * Added support for downloading of i18n files. 22 | * Fix regex problem: arm overrides armhf. Thanks to hawken. 23 | * Add support for HTTPS with basic HTTP authentication. Thanks to Pali 24 | * Improve lock file handling to avoid false "apt-mirror is already running" 25 | messages. Thanks to Ivan Borzenkov 26 | * Create directories after config is parsed. 27 | Thanks to Rob Verduijn 28 | * Support armhf, kfreebsd-* and s390x arch. Thanks Benjamin Drung 29 | * Fix braindead build environments. Thanks Jeremy T. Bouse 30 | * Add a shebang line to clean.sh. Thanks Benjamin Drung 31 | * debian/postrm: Clean-up auto-generated files on purge. Thanks 32 | Jeremy T. Bouse 33 | 34 | 0.4.8 35 | * Fix for superfluous errors when mirroring a Flat repo 36 | * Fixed support for repositories with no components. 37 | * Reduced the number of tries wget will try before it fails to 5 38 | * Fixed loop if wget was not installed, would not die() 39 | * fix stale wget caching due to bad proxy configs 40 | 41 | 0.4.7 42 | * Lots of typos fixed 43 | * apt-mirror will now try to create needed directories if they do not exist 44 | * applied patch for closedir() errors 45 | * added mirroring examples of multiple architectures to the debian mirror.list 46 | * fixed manpage description of mirror.list 47 | 48 | 0.4.6 49 | 50 | * minor cleanup to the documentation 51 | * applied patch from Steve Kowalik ( stevenk@ubuntu.com ) to 52 | allow user:pass@host ( http and ftp authenticated ) 53 | and port numbers in the url host:8080 etc 54 | * Added ability to run a shell script after apt-mirror completes the 55 | mirroring configuration values "run_postmirror" 0/1 56 | and "postmirror_script" /path/to/script.sh 57 | * added rate_limit option ( closes sf.net bug #1729402 ) 58 | a value of 25 is equal to 25 Bytes/Second 59 | a value of 25k is equal to 25 Kilobytes/Second 60 | a value of 25m is equal to 25 Megabyte/Second 61 | 62 | 0.4.5 63 | 64 | * added mipsel, armel, and lpia arch support 65 | * fixed the tilde replace code so it works with 2 or more tilde 66 | in the package names 67 | * default _tilde option set to 0 68 | * minor updates to the manpage wordings 69 | * new _stat function to cache and speed up parsing 70 | * download size now in human readable numbers ( KiB,MiB,GiB,etc ) 71 | 72 | 0.4.4 73 | old changes documented in the debian/changelog 74 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /apt-mirror: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | =pod 4 | 5 | =head1 NAME 6 | 7 | apt-mirror - apt sources mirroring tool 8 | 9 | =head1 SYNOPSIS 10 | 11 | apt-mirror [configfile] 12 | 13 | =head1 DESCRIPTION 14 | 15 | A small and efficient tool that lets you mirror a part of or 16 | the whole Debian GNU/Linux distribution or any other apt sources. 17 | 18 | Main features: 19 | * It uses a config similar to APT's F 20 | * It's fully pool compliant 21 | * It supports multithreaded downloading 22 | * It supports multiple architectures at the same time 23 | * It can automatically remove unneeded files 24 | * It works well on an overloaded Internet connection 25 | * It never produces an inconsistent mirror including while mirroring 26 | * It works on all POSIX compliant systems with Perl and wget 27 | 28 | =head1 COMMENTS 29 | 30 | apt-mirror uses F as a configuration file. 31 | By default it is tuned to official Debian or Ubuntu mirrors. Change 32 | it for your needs. 33 | 34 | After you setup the configuration file you may run as root: 35 | 36 | # su - apt-mirror -c apt-mirror 37 | 38 | Or uncomment the line in F to enable daily mirror updates. 39 | 40 | =head1 FILES 41 | 42 | F 43 | Main configuration file 44 | 45 | F 46 | Cron configuration template 47 | 48 | F 49 | Mirror places here 50 | 51 | F 52 | Place for temporarily downloaded indexes 53 | 54 | F 55 | Log files placed here. URLs and MD5 checksums also here. 56 | 57 | =head1 CONFIGURATION EXAMPLES 58 | 59 | The mirror.list configuration supports many options, the file is well commented explaining each option. 60 | Here are some sample mirror configuration lines showing the various supported ways: 61 | 62 | Normal: 63 | deb http://example.com/debian stable main contrib non-free 64 | 65 | Arch Specific: (many other architectures are supported) 66 | deb-powerpc http://example.com/debian stable main contrib non-free 67 | 68 | HTTP and FTP Auth or non-standard port: 69 | deb http://user:pass@example.com:8080/debian stable main contrib non-free 70 | 71 | HTTPS with sending Basic HTTP authentication information (plaintext username and password) for all requests: 72 | (this was default behaviour of Wget 1.10.2 and prior and is needed for some servers with new version of Wget) 73 | set auth_no_challenge 1 74 | deb https://user:pass@example.com:443/debian stable main contrib non-free 75 | 76 | HTTPS without checking certificate: 77 | set no_check_certificate 1 78 | deb https://example.com:443/debian stable main contrib non-free 79 | 80 | Source Mirroring: 81 | deb-src http://example.com/debian stable main contrib non-free 82 | 83 | =head1 AUTHORS 84 | 85 | Dmitry N. Hramtsov Ehdn@nsu.ruE 86 | Brandon Holtsclaw Eme@brandonholtsclaw.comE 87 | 88 | =cut 89 | 90 | use warnings; 91 | use strict; 92 | use File::Copy; 93 | use File::Compare; 94 | use File::Path; 95 | use File::Basename; 96 | use Fcntl qw(:flock); 97 | 98 | my $config_file; 99 | 100 | my %config_variables = ( 101 | "defaultarch" => `dpkg --print-architecture 2>/dev/null` || 'i386', 102 | "nthreads" => 20, 103 | "base_path" => '/var/spool/apt-mirror', 104 | "mirror_path" => '$base_path/mirror', 105 | "skel_path" => '$base_path/skel', 106 | "var_path" => '$base_path/var', 107 | "cleanscript" => '$var_path/clean.sh', 108 | "_contents" => 1, 109 | "_autoclean" => 0, 110 | "_tilde" => 0, 111 | "limit_rate" => '100m', 112 | "run_postmirror" => 1, 113 | "auth_no_challenge" => 0, 114 | "no_check_certificate" => 0, 115 | "unlink" => 0, 116 | "postmirror_script" => '$var_path/postmirror.sh', 117 | "use_proxy" => 'off', 118 | "http_proxy" => '', 119 | "proxy_user" => '', 120 | "proxy_password" => '' 121 | ); 122 | 123 | my @config_binaries = (); 124 | my @config_sources = (); 125 | 126 | my @index_urls; 127 | my @childrens = (); 128 | my %skipclean = (); 129 | my %clean_directory = (); 130 | 131 | ###################################################################################### 132 | ## Setting up $config_file variable 133 | 134 | $config_file = "/etc/apt/mirror.list"; # Default value 135 | if ( $_ = shift ) 136 | { 137 | die("apt-mirror: invalid config file specified") unless -f $_; 138 | $config_file = $_; 139 | } 140 | 141 | chomp $config_variables{"defaultarch"}; 142 | 143 | ###################################################################################### 144 | ## Common subroutines 145 | 146 | sub round_number 147 | { 148 | my $n = shift; 149 | my $minus = $n < 0 ? '-' : ''; 150 | $n = abs($n); 151 | $n = int( ( $n + .05 ) * 10 ) / 10; 152 | $n .= '.0' unless $n =~ /\./; 153 | $n .= '0' if substr( $n, ( length($n) - 1 ), 1 ) eq '.'; 154 | chop $n if $n =~ /\.\d\d0$/; 155 | return "$minus$n"; 156 | } 157 | 158 | sub format_bytes 159 | { 160 | my $bytes = shift; 161 | my $bytes_out = '0'; 162 | my $size_name = 'bytes'; 163 | my $KiB = 1024; 164 | my $MiB = 1024 * 1024; 165 | my $GiB = 1024 * 1024 * 1024; 166 | 167 | if ( $bytes >= $KiB ) 168 | { 169 | $bytes_out = $bytes / $KiB; 170 | $size_name = 'KiB'; 171 | if ( $bytes >= $MiB ) 172 | { 173 | $bytes_out = $bytes / $MiB; 174 | $size_name = 'MiB'; 175 | if ( $bytes >= $GiB ) 176 | { 177 | $bytes_out = $bytes / $GiB; 178 | $size_name = 'GiB'; 179 | } 180 | } 181 | $bytes_out = round_number($bytes_out); 182 | } 183 | else 184 | { 185 | $bytes_out = $bytes; 186 | $size_name = 'bytes'; 187 | } 188 | 189 | return "$bytes_out $size_name"; 190 | } 191 | 192 | sub get_variable 193 | { 194 | my $value = $config_variables{ shift @_ }; 195 | my $count = 16; 196 | while ( $value =~ s/\$(\w+)/$config_variables{$1}/xg ) 197 | { 198 | die("apt-mirror: too many substitution while evaluating variable") if ( $count-- ) < 0; 199 | } 200 | return $value; 201 | } 202 | 203 | sub lock_aptmirror 204 | { 205 | open( LOCK_FILE, '>', get_variable("var_path") . "/apt-mirror.lock" ); 206 | my $lock = flock( LOCK_FILE, LOCK_EX | LOCK_NB ); 207 | if ( !$lock ) 208 | { 209 | die("apt-mirror is already running, exiting"); 210 | } 211 | } 212 | 213 | sub unlock_aptmirror 214 | { 215 | close(LOCK_FILE); 216 | unlink( get_variable("var_path") . "/apt-mirror.lock" ); 217 | } 218 | 219 | sub download_urls 220 | { 221 | my $stage = shift; 222 | my @urls; 223 | my $i = 0; 224 | my $pid; 225 | my $nthreads = get_variable("nthreads"); 226 | my @args = (); 227 | local $| = 1; 228 | 229 | @urls = @_; 230 | $nthreads = @urls if @urls < $nthreads; 231 | 232 | if ( get_variable("auth_no_challenge") == 1 ) { push( @args, "--auth-no-challenge" ); } 233 | if ( get_variable("no_check_certificate") == 1 ) { push( @args, "--no-check-certificate" ); } 234 | if ( get_variable("unlink") == 1 ) { push( @args, "--unlink" ); } 235 | if ( length( get_variable("use_proxy") ) && ( get_variable("use_proxy") eq 'yes' || get_variable("use_proxy") eq 'on' ) ) 236 | { 237 | if ( length( get_variable("http_proxy") ) ) { push( @args, "-e use_proxy=yes" ); push( @args, "-e http_proxy=" . get_variable("http_proxy") ); } 238 | if ( length( get_variable("proxy_user") ) ) { push( @args, "-e proxy_user=" . get_variable("proxy_user") ); } 239 | if ( length( get_variable("proxy_password") ) ) { push( @args, "-e proxy_password=" . get_variable("proxy_password") ); } 240 | } 241 | print "Downloading " . scalar(@urls) . " $stage files using $nthreads threads...\n"; 242 | 243 | while ( scalar @urls ) 244 | { 245 | my @part = splice( @urls, 0, int( @urls / $nthreads ) ); 246 | open URLS, ">" . get_variable("var_path") . "/$stage-urls.$i" or die("apt-mirror: can't write to intermediate file ($stage-urls.$i)"); 247 | foreach (@part) { print URLS "$_\n"; } 248 | close URLS or die("apt-mirror: can't close intermediate file ($stage-urls.$i)"); 249 | 250 | $pid = fork(); 251 | 252 | die("apt-mirror: can't do fork in download_urls") if !defined($pid); 253 | 254 | if ( $pid == 0 ) 255 | { 256 | exec 'wget', '--no-cache', '--limit-rate=' . get_variable("limit_rate"), '-t', '5', '-r', '-N', '-l', 'inf', '-o', get_variable("var_path") . "/$stage-log.$i", '-i', get_variable("var_path") . "/$stage-urls.$i", @args; 257 | 258 | # shouldn't reach this unless exec fails 259 | die("\n\nCould not run wget, please make sure its installed and in your path\n\n"); 260 | } 261 | 262 | push @childrens, $pid; 263 | $i++; 264 | $nthreads--; 265 | } 266 | 267 | print "Begin time: " . localtime() . "\n[" . scalar(@childrens) . "]... "; 268 | while ( scalar @childrens ) 269 | { 270 | my $dead = wait(); 271 | @childrens = grep { $_ != $dead } @childrens; 272 | print "[" . scalar(@childrens) . "]... "; 273 | } 274 | print "\nEnd time: " . localtime() . "\n\n"; 275 | } 276 | 277 | ## Parse config 278 | 279 | open CONFIG, "<$config_file" or die("apt-mirror: can't open config file ($config_file)"); 280 | while () 281 | { 282 | next if /^\s*#/; 283 | next unless /\S/; 284 | my @config_line = split; 285 | my $config_line = shift @config_line; 286 | 287 | if ( $config_line eq "set" ) 288 | { 289 | $config_variables{ $config_line[0] } = $config_line[1]; 290 | next; 291 | } 292 | 293 | if ( $config_line eq "deb" ) 294 | { 295 | push @config_binaries, [ get_variable("defaultarch"), @config_line ]; 296 | next; 297 | } 298 | 299 | if ( $config_line eq "deb-src" ) 300 | { 301 | push @config_sources, [@config_line]; 302 | next; 303 | } 304 | 305 | if ( $config_line =~ /deb-([\w\-]+)/ ) 306 | { 307 | push @config_binaries, [ $1, @config_line ]; 308 | next; 309 | } 310 | 311 | if ( $config_line eq "skip-clean" ) 312 | { 313 | $config_line[0] =~ s[^(\w+)://][]; 314 | $config_line[0] =~ s[/$][]; 315 | $config_line[0] =~ s[~][%7E]g if get_variable("_tilde"); 316 | $skipclean{ $config_line[0] } = 1; 317 | next; 318 | } 319 | 320 | if ( $config_line eq "clean" ) 321 | { 322 | $config_line[0] =~ s[^(\w+)://][]; 323 | $config_line[0] =~ s[/$][]; 324 | $config_line[0] =~ s[~][%7E]g if get_variable("_tilde"); 325 | $clean_directory{ $config_line[0] } = 1; 326 | next; 327 | } 328 | 329 | die("apt-mirror: invalid line in config file ($.: $config_line ...)"); 330 | } 331 | close CONFIG; 332 | 333 | die("Please explicitly specify 'defaultarch' in mirror.list") unless get_variable("defaultarch"); 334 | 335 | ###################################################################################### 336 | ## Create the 3 needed directories if they don't exist yet 337 | my @needed_directories = ( get_variable("mirror_path"), get_variable("skel_path"), get_variable("var_path") ); 338 | foreach my $needed_directory (@needed_directories) 339 | { 340 | unless ( -d $needed_directory ) 341 | { 342 | mkdir($needed_directory) or die("apt-mirror: can't create $needed_directory directory"); 343 | } 344 | } 345 | # 346 | ####################################################################################### 347 | 348 | lock_aptmirror(); 349 | 350 | ###################################################################################### 351 | ## Skel download 352 | 353 | my %urls_to_download = (); 354 | my ( $url, $arch ); 355 | 356 | sub remove_double_slashes 357 | { 358 | local $_ = shift; 359 | while (s[/\./][/]g) { } 360 | while (s[(? ) 496 | { 497 | chomp $line; 498 | if ($checksums) 499 | { 500 | if ( $line =~ /^ +(.*)$/ ) 501 | { 502 | my @parts = split( / +/, $1 ); 503 | if ( @parts == 3 ) 504 | { 505 | my ( $sha1, $size, $filename ) = @parts; 506 | if ( $filename =~ m{^$component/i18n/Translation-[^./]*\.bz2$} ) 507 | { 508 | add_url_to_download( $dist_uri . $filename, $size ); 509 | } 510 | } 511 | else 512 | { 513 | warn("Malformed checksum line \"$1\" in $release_uri"); 514 | } 515 | } 516 | else 517 | { 518 | $checksums = 0; 519 | } 520 | } 521 | if ( not $checksums ) 522 | { 523 | if ( $line eq "SHA256:" ) 524 | { 525 | $checksums = 1; 526 | } 527 | } 528 | } 529 | } 530 | 531 | sub process_translation_index 532 | { 533 | # Extract all translation files from the dists/$DIST/$COMPONENT/i18n/Index 534 | # file. Fall back to parsing dists/$DIST/Release if i18n/Index is not found. 535 | 536 | my $dist_uri = remove_double_slashes(shift); 537 | my $component = shift; 538 | my ( $base_uri, $index_uri, $index_path, $line ) = ''; 539 | 540 | $base_uri = $dist_uri . $component . "/i18n/"; 541 | $index_uri = $base_uri . "Index"; 542 | $index_path = get_variable("skel_path") . "/" . sanitise_uri($index_uri); 543 | 544 | unless ( open STREAM, "<$index_path" ) 545 | { 546 | find_translation_files_in_release( $dist_uri, $component ); 547 | return; 548 | } 549 | 550 | my $checksums = 0; 551 | while ( $line = ) 552 | { 553 | chomp $line; 554 | if ($checksums) 555 | { 556 | if ( $line =~ /^ +(.*)$/ ) 557 | { 558 | my @parts = split( / +/, $1 ); 559 | if ( @parts == 3 ) 560 | { 561 | my ( $sha1, $size, $filename ) = @parts; 562 | if ( $filename =~ /.*\/([^\/]*)$/ ) { 563 | warn("Removing path from Index line \"$line\" in $index_uri"); 564 | $filename = $1; 565 | } 566 | add_url_to_download( $base_uri . $filename, $size ); 567 | } 568 | else 569 | { 570 | warn("Malformed checksum line \"$1\" in $index_uri"); 571 | } 572 | } 573 | else 574 | { 575 | $checksums = 0; 576 | } 577 | } 578 | if ( not $checksums ) 579 | { 580 | if ( $line eq "SHA256:" or $line eq "SHA1:" or $line eq "MD5Sum:" ) 581 | { 582 | $checksums = 1; 583 | } 584 | } 585 | } 586 | 587 | close STREAM; 588 | } 589 | 590 | print "Processing translation indexes: ["; 591 | 592 | foreach (@config_binaries) 593 | { 594 | my ( $arch, $uri, $distribution, @components ) = @{$_}; 595 | print "T"; 596 | if (@components) 597 | { 598 | $url = $uri . "/dists/" . $distribution . "/"; 599 | 600 | my $component; 601 | foreach $component (@components) 602 | { 603 | process_translation_index( $url, $component ); 604 | } 605 | } 606 | } 607 | 608 | print "]\n\n"; 609 | 610 | push( @index_urls, sort keys %urls_to_download ); 611 | download_urls( "translation", sort keys %urls_to_download ); 612 | 613 | foreach ( keys %urls_to_download ) 614 | { 615 | s[^(\w+)://][]; 616 | s[~][%7E]g if get_variable("_tilde"); 617 | $skipclean{$_} = 1; 618 | } 619 | 620 | ###################################################################################### 621 | ## Main download preparations 622 | 623 | %urls_to_download = (); 624 | 625 | open FILES_ALL, ">" . get_variable("var_path") . "/ALL" or die("apt-mirror: can't write to intermediate file (ALL)"); 626 | open FILES_NEW, ">" . get_variable("var_path") . "/NEW" or die("apt-mirror: can't write to intermediate file (NEW)"); 627 | open FILES_MD5, ">" . get_variable("var_path") . "/MD5" or die("apt-mirror: can't write to intermediate file (MD5)"); 628 | 629 | my %stat_cache = (); 630 | 631 | sub _stat 632 | { 633 | my ($filename) = shift; 634 | return @{ $stat_cache{$filename} } if exists $stat_cache{$filename}; 635 | my @res = stat($filename); 636 | $stat_cache{$filename} = \@res; 637 | return @res; 638 | } 639 | 640 | sub clear_stat_cache 641 | { 642 | %stat_cache = (); 643 | } 644 | 645 | sub need_update 646 | { 647 | my $filename = shift; 648 | my $size_on_server = shift; 649 | 650 | my ( undef, undef, undef, undef, undef, undef, undef, $size ) = _stat($filename); 651 | 652 | return 1 unless ($size); 653 | return 0 if $size_on_server == $size; 654 | return 1; 655 | } 656 | 657 | sub remove_spaces($) 658 | { 659 | my $hashref = shift; 660 | foreach ( keys %{$hashref} ) 661 | { 662 | while ( substr( $hashref->{$_}, 0, 1 ) eq ' ' ) 663 | { 664 | substr( $hashref->{$_}, 0, 1 ) = ''; 665 | } 666 | } 667 | } 668 | 669 | sub process_index_gz 670 | { 671 | my $uri = shift; 672 | my $index = shift; 673 | my ( $path, $package, $mirror, $files ) = ''; 674 | 675 | $path = sanitise_uri($uri); 676 | local $/ = "\n\n"; 677 | $mirror = get_variable("mirror_path") . "/" . $path; 678 | 679 | if ( $index =~ s/\.gz$// ) 680 | { 681 | system("gunzip < $path/$index.gz > $path/$index"); 682 | } 683 | 684 | unless ( open STREAM, "<$path/$index" ) 685 | { 686 | warn("apt-mirror: can't open index in process_index_gz"); 687 | return; 688 | } 689 | 690 | while ( $package = ) 691 | { 692 | local $/ = "\n"; 693 | chomp $package; 694 | my ( undef, %lines ) = split( /^([\w\-]+:)/m, $package ); 695 | 696 | $lines{"Directory:"} = "" unless defined $lines{"Directory:"}; 697 | chomp(%lines); 698 | remove_spaces( \%lines ); 699 | 700 | if ( exists $lines{"Filename:"} ) 701 | { # Packages index 702 | $skipclean{ remove_double_slashes( $path . "/" . $lines{"Filename:"} ) } = 1; 703 | print FILES_ALL remove_double_slashes( $path . "/" . $lines{"Filename:"} ) . "\n"; 704 | print FILES_MD5 $lines{"MD5sum:"} . " " . remove_double_slashes( $path . "/" . $lines{"Filename:"} ) . "\n"; 705 | if ( need_update( $mirror . "/" . $lines{"Filename:"}, $lines{"Size:"} ) ) 706 | { 707 | print FILES_NEW remove_double_slashes( $uri . "/" . $lines{"Filename:"} ) . "\n"; 708 | add_url_to_download( $uri . "/" . $lines{"Filename:"}, $lines{"Size:"} ); 709 | } 710 | } 711 | else 712 | { # Sources index 713 | foreach ( split( /\n/, $lines{"Files:"} ) ) 714 | { 715 | next if $_ eq ''; 716 | my @file = split; 717 | die("apt-mirror: invalid Sources format") if @file != 3; 718 | $skipclean{ remove_double_slashes( $path . "/" . $lines{"Directory:"} . "/" . $file[2] ) } = 1; 719 | print FILES_ALL remove_double_slashes( $path . "/" . $lines{"Directory:"} . "/" . $file[2] ) . "\n"; 720 | print FILES_MD5 $file[0] . " " . remove_double_slashes( $path . "/" . $lines{"Directory:"} . "/" . $file[2] ) . "\n"; 721 | if ( need_update( $mirror . "/" . $lines{"Directory:"} . "/" . $file[2], $file[1] ) ) 722 | { 723 | print FILES_NEW remove_double_slashes( $uri . "/" . $lines{"Directory:"} . "/" . $file[2] ) . "\n"; 724 | add_url_to_download( $uri . "/" . $lines{"Directory:"} . "/" . $file[2], $file[1] ); 725 | } 726 | } 727 | } 728 | } 729 | 730 | close STREAM; 731 | } 732 | 733 | print "Processing indexes: ["; 734 | 735 | foreach (@config_sources) 736 | { 737 | my ( $uri, $distribution, @components ) = @{$_}; 738 | print "S"; 739 | if (@components) 740 | { 741 | my $component; 742 | foreach $component (@components) 743 | { 744 | process_index_gz( $uri, "/dists/$distribution/$component/source/Sources.gz" ); 745 | } 746 | } 747 | else 748 | { 749 | process_index_gz( $uri, "/$distribution/Sources.gz" ); 750 | } 751 | } 752 | 753 | foreach (@config_binaries) 754 | { 755 | my ( $arch, $uri, $distribution, @components ) = @{$_}; 756 | print "P"; 757 | if (@components) 758 | { 759 | my $component; 760 | foreach $component (@components) 761 | { 762 | process_index_gz( $uri, "/dists/$distribution/$component/binary-$arch/Packages.gz" ); 763 | process_index_gz( $uri, "/dists/$distribution/$component/debian-installer/binary-$arch/Packages.gz" ); 764 | } 765 | } 766 | else 767 | { 768 | process_index_gz( $uri, "/$distribution/Packages.gz" ); 769 | } 770 | } 771 | 772 | clear_stat_cache(); 773 | 774 | print "]\n\n"; 775 | 776 | close FILES_ALL; 777 | close FILES_NEW; 778 | close FILES_MD5; 779 | 780 | ###################################################################################### 781 | ## Main download 782 | 783 | chdir get_variable("mirror_path") or die("apt-mirror: can't chdir to mirror"); 784 | 785 | my $need_bytes = 0; 786 | foreach ( values %urls_to_download ) 787 | { 788 | $need_bytes += $_; 789 | } 790 | 791 | my $size_output = format_bytes($need_bytes); 792 | 793 | print "$size_output will be downloaded into archive.\n"; 794 | 795 | download_urls( "archive", sort keys %urls_to_download ); 796 | 797 | ###################################################################################### 798 | ## Copy skel to main archive 799 | 800 | sub copy_file 801 | { 802 | my ( $from, $to ) = @_; 803 | my $dir = dirname($to); 804 | return unless -f $from; 805 | mkpath($dir) unless -d $dir; 806 | if ( get_variable("unlink") == 1 ) 807 | { 808 | if ( compare( $from, $to ) != 0 ) { unlink($to); } 809 | } 810 | unless ( copy( $from, $to ) ) 811 | { 812 | warn("apt-mirror: can't copy $from to $to"); 813 | return; 814 | } 815 | my ( $atime, $mtime ) = ( stat($from) )[ 8, 9 ]; 816 | utime( $atime, $mtime, $to ) or die("apt-mirror: can't utime $to"); 817 | } 818 | 819 | foreach (@index_urls) 820 | { 821 | die("apt-mirror: invalid url in index_urls") unless s[^(\w+)://][]; 822 | copy_file( get_variable("skel_path") . "/" . sanitise_uri("$_"), get_variable("mirror_path") . "/" . sanitise_uri("$_") ); 823 | copy_file( get_variable("skel_path") . "/" . sanitise_uri("$_"), get_variable("mirror_path") . "/" . sanitise_uri("$_") ) if (s/\.gz$//); 824 | copy_file( get_variable("skel_path") . "/" . sanitise_uri("$_"), get_variable("mirror_path") . "/" . sanitise_uri("$_") ) if (s/\.bz2$//); 825 | copy_file( get_variable("skel_path") . "/" . sanitise_uri("$_"), get_variable("mirror_path") . "/" . sanitise_uri("$_") ) if (s/\.xz$//); 826 | } 827 | 828 | ###################################################################################### 829 | ## Make cleaning script 830 | 831 | my ( @rm_dirs, @rm_files ) = (); 832 | my $unnecessary_bytes = 0; 833 | 834 | sub process_symlink 835 | { 836 | return 1; # symlinks are always needed 837 | } 838 | 839 | sub process_file 840 | { 841 | my $file = shift; 842 | $file =~ s[~][%7E]g if get_variable("_tilde"); 843 | return 1 if $skipclean{$file}; 844 | push @rm_files, sanitise_uri($file); 845 | my ( undef, undef, undef, undef, undef, undef, undef, $size, undef, undef, undef, undef, $blocks ) = stat($file); 846 | $unnecessary_bytes += $blocks * 512; 847 | return 0; 848 | } 849 | 850 | sub process_directory 851 | { 852 | my $dir = shift; 853 | my $is_needed = 0; 854 | return 1 if $skipclean{$dir}; 855 | opendir( my $dir_h, $dir ) or die "apt-mirror: can't opendir $dir: $!"; 856 | foreach ( grep { !/^\.$/ && !/^\.\.$/ } readdir($dir_h) ) 857 | { 858 | my $item = $dir . "/" . $_; 859 | $is_needed |= process_directory($item) if -d $item && !-l $item; 860 | $is_needed |= process_file($item) if -f $item; 861 | $is_needed |= process_symlink($item) if -l $item; 862 | } 863 | closedir $dir_h; 864 | push @rm_dirs, $dir unless $is_needed; 865 | return $is_needed; 866 | } 867 | 868 | chdir get_variable("mirror_path") or die("apt-mirror: can't chdir to mirror"); 869 | 870 | foreach ( keys %clean_directory ) 871 | { 872 | process_directory($_) if -d $_ && !-l $_; 873 | } 874 | 875 | open CLEAN, ">" . get_variable("cleanscript") or die("apt-mirror: can't open clean script file"); 876 | 877 | my ( $i, $total ) = ( 0, scalar @rm_files ); 878 | 879 | if ( get_variable("_autoclean") ) 880 | { 881 | 882 | my $size_output = format_bytes($unnecessary_bytes); 883 | print "$size_output in $total files and " . scalar(@rm_dirs) . " directories will be freed..."; 884 | 885 | chdir get_variable("mirror_path") or die("apt-mirror: can't chdir to mirror"); 886 | 887 | foreach (@rm_files) { unlink $_; } 888 | foreach (@rm_dirs) { rmdir $_; } 889 | 890 | } 891 | else 892 | { 893 | 894 | my $size_output = format_bytes($unnecessary_bytes); 895 | print "$size_output in $total files and " . scalar(@rm_dirs) . " directories can be freed.\n"; 896 | print "Run " . get_variable("cleanscript") . " for this purpose.\n\n"; 897 | 898 | print CLEAN "#!/bin/sh\n"; 899 | print CLEAN "set -e\n\n"; 900 | print CLEAN "cd " . get_variable("mirror_path") . "\n\n"; 901 | print CLEAN "echo 'Removing $total unnecessary files [$size_output]...'\n"; 902 | foreach (@rm_files) 903 | { 904 | print CLEAN "rm -f '$_'\n"; 905 | print CLEAN "echo -n '[" . int( 100 * $i / $total ) . "\%]'\n" unless $i % 500; 906 | print CLEAN "echo -n .\n" unless $i % 10; 907 | $i++; 908 | } 909 | print CLEAN "echo 'done.'\n"; 910 | print CLEAN "echo\n\n"; 911 | 912 | $i = 0; 913 | $total = scalar @rm_dirs; 914 | print CLEAN "echo 'Removing $total unnecessary directories...'\n"; 915 | foreach (@rm_dirs) 916 | { 917 | print CLEAN "if test -d '$_'; then rmdir '$_'; fi\n"; 918 | print CLEAN "echo -n '[" . int( 100 * $i / $total ) . "\%]'\n" unless $i % 50; 919 | print CLEAN "echo -n .\n"; 920 | $i++; 921 | } 922 | print CLEAN "echo 'done.'\n"; 923 | print CLEAN "echo\n"; 924 | 925 | close CLEAN; 926 | 927 | } 928 | 929 | # Make clean script executable 930 | my $perm = ( stat get_variable("cleanscript") )[2] & 07777; 931 | chmod( $perm | 0111, get_variable("cleanscript") ); 932 | 933 | if ( get_variable("run_postmirror") ) 934 | { 935 | print "Running the Post Mirror script ...\n"; 936 | print "(" . get_variable("postmirror_script") . ")\n\n"; 937 | if ( -x get_variable("postmirror_script") ) 938 | { 939 | system( get_variable("postmirror_script") ); 940 | } 941 | else 942 | { 943 | system( '/bin/sh ' . get_variable("postmirror_script") ); 944 | } 945 | print "\nPost Mirror script has completed. See above output for any possible errors.\n\n"; 946 | } 947 | 948 | unlock_aptmirror(); 949 | --------------------------------------------------------------------------------