穏やかにファイルを削除する

鉄次 尾形
鉄次 尾形Perl Programmer at Gaiax Co. Ltd.
OGATA Tetsuji (@xtetsuji)
2016/05/19 Gaiax
https://flic.kr/p/pAQruL
• / / OGATA Tetsuji
• @xtetsuji
• Blog: http://post.tetsuji.jp/
• RND
• Qiita:Team
•
•
•
• …
•
• 5 10
•
•
•
穏やかにファイルを削除する
•
• 👺 man rm
•
)
• rm
•
•
•
• rm -rf tmp_foo
•
• I/O
•
• I/O
• I/O
$ strace rm moge.pl
execve("/bin/rm", ["rm", "moge.pl"], [/* 52 vars */]) = 0
brk(0) = 0x12c1000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1fd3000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=41893, ...}) = 0
mmap(NULL, 41893, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd9d1fc8000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY) = 3
read(3, "177ELF21100000000030>010000360100000"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1599536, ...}) = 0
mmap(NULL, 3713112, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fd9d1a2b000
mprotect(0x7fd9d1bac000, 2097152, PROT_NONE) = 0
mmap(0x7fd9d1dac000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x181000) = 0x7fd9d1dac000
mmap(0x7fd9d1db1000, 18520, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1db1000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1fc7000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1fc6000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1fc5000
arch_prctl(ARCH_SET_FS, 0x7fd9d1fc6700) = 0
mprotect(0x7fd9d1dac000, 16384, PROT_READ) = 0
mprotect(0x60d000, 4096, PROT_READ) = 0
mprotect(0x7fd9d1fd5000, 4096, PROT_READ) = 0
munmap(0x7fd9d1fc8000, 41893) = 0
brk(0) = 0x12c1000
brk(0x12e2000) = 0x12e2000
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2508736, ...}) = 0
mmap(NULL, 2508736, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd9d17c6000
close(3) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 opost isig icanon echo ...}) = 0
newfstatat(AT_FDCWD, "moge.pl", {st_mode=S_IFREG|0755, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0
geteuid() = 1000
newfstatat(AT_FDCWD, "moge.pl", {st_mode=S_IFREG|0755, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0
faccessat(AT_FDCWD, "moge.pl", W_OK) = 0
unlinkat(AT_FDCWD, "moge.pl", 0) = 0
lseek(0, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek)
close(0) = 0
close(1) = 0
close(2) = 0
exit_group(0) = ?
•
• find sleep
http://ja.stackoverflow.com/questions/7304/%E5%86%8D%E5%B8%B0%E7%9A%84%E3%81%AB
%E3%82%86%E3%81%A3%E3%81%8F%E3%82%8A%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB
%E3%81%A0%E3%81%91%E3%82%92%E5%89%8A%E9%99%A4%E3%81%99%E3%82%8B
%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89
http://blog.layer8.sh/ja/2011/12/16/%E5%A4%A7%E9%87%8F%E3%81%AE
%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E3%82%86%E3%81%A3%E3%81%8F
%E3%82%8A%E7%9B%AE%E3%81%AB%E5%89%8A%E9%99%A4%E3%81%99/
http://unix.stackexchange.com/questions/106133/why-is-rm-slow
• find xargs
• rsync --delete empty/ garbage/
•
•
•
•
•
• gentle_unlink Perl
•
•
•
•
$ gentle_unlink --help
Usage:
gentle_unlink remove_file_list.txt
ls -1 *.log | gentle_unlink --timeout=600 --progress --interval=2
gentle_unlink [--timeout=SECONDS] [--progress] [--interval=SECONDS] 
[--flexible] [--dry-run]
• --progress rsync
• --interval
• --timeout
• --flexible
• --dry-run
gentle_unlink
•
•
find $DIR -type f
• --progress
•
• --timeout cron
DEMO?
穏やかにファイルを削除する
#!/usr/bin/perl
use strict;
use warnings;
use Carp qw(croak);
use Term::ANSIColor qw(colored);
use Getopt::Long qw(:config posix_default no_ignore_case bundling auto_help);
use Time::HiRes qw(time usleep ualarm);
USR1
$SIG{USR1} = &guess_current_status;
kill -USR1 %1 OK
# for USR1
sub guess_current_status {
my $signal = shift;
printf colored("dry-run mode. all delete operations are fake.n", "blue") if $dry_run;
printf "process id: %dn", $$;
printf "delete file count: %sn", comma($delete_count);
printf "delete size total: %s bytesn", comma($delete_size_total);
printf "process run times: %s secondn", comma(int(time - $process_start_time));
printf "interval=%d timeout=%sn", $interval, $timeout;
}
use constant DEBUG => $ENV{DEBUG};
use constant MAX_TIME_HIRES_BIT => 31;
use constant UNLINK_CHUNK_NUM => 20;
use constant COLOR_WARNING => "yellow";
use constant COLOR_ERROR => "red";
use constant COLOR_INFO => "green";
$SIG{USR1} = &guess_current_status;
GetOptions(
my %opt,
"timeout|t=f", "progress|p", "interval|i=f", "flexible|f", "dry-run|n"
);
my $timeout = $opt{timeout} || 0;
my $progress = $opt{progress};
my $interval = $opt{interval} || 0;
my $dry_run = $opt{"dry-run"};
my $flexible = $opt{flexible};
my $delete_count = 0;
my $delete_size_total = 0;
my $timeout_flag;
my $preserve_interval_flag;
my $process_start_time = time;
eval
local $@;
eval {
local $SIG{ALRM} = $timeout > 0 ? sub { $timeout_flag = 1; } : "DEFAULT";
# alarm (10^{-6}) 32 alarm
my $alarm_cancel_cb; # alram 0 ualarm 0
my $timeout_microsecond = $timeout * 10**6;
if ( bit($timeout_microsecond) < MAX_TIME_HIRES_BIT ) {
ualarm($timeout_microsecond);
$alarm_cancel_cb = sub { ualarm 0; };
} else {
alarm(int($timeout));
$alarm_cancel_cb = sub { alarm 0; };
}
gentle_unlink();
$alarm_cancel_cb->();
};
if ( $@ ) {
print colored("e=$@n", COLOR_ERROR);
}
sub gentle_unlink {
my @file_chunk;
my $start_time = time;
while (<>) {
chomp;
print "> $_n" if DEBUG;
push @file_chunk, $_;
if ( @file_chunk > UNLINK_CHUNK_NUM ) {
unlink_chunk(@file_chunk);
@file_chunk = ();
$preserve_interval_flag = 1;
}
} continue {
if ( $timeout_flag ) {
progress_printf(colored("timeoutn", COLOR_WARNING));
last;
}
if ( $interval && $preserve_interval_flag ) {
progress_printf(colored("interval %f seconds.n", COLOR_INFO), $interval );
usleep $interval * 10**6;
$preserve_interval_flag = 0;
}
}
if ( @file_chunk ) {
unlink_chunk(@file_chunk);
}
my $end_time = time;
progress_printf("%f seconds, %s bytes, total %d files deleted.n",
$end_time - $start_time, comma($delete_size_total), $delete_count);
progress_printf(colored("dry-run mode. all delete operations are fake.n", "blue")) if
$dry_run;
}
sub unlink_chunk {
my @files = @_;
my @nowhere_files = grep { !-f } @files;
if ( @nowhere_files ) {
if ( $flexible ) {
progress_printf(colored(
join( "", map { qq(file "$_" is not foundn) } @nowhere_files ),
COLOR_WARNING
)); # progress_printf colored
} else {
croak colored("unlink_chunk gives lost filename.", COLOR_ERROR);
}
}
progress_printf(">>> dry-run moden") if $dry_run;
progress_printf("%4d: deleting... %s (%s bytes)n",
++$delete_count, $_, comma(-s $_)) for @files;
$delete_size_total += -s $_ for @files;
return if $dry_run;
local $!;
my $chunk_num = @files;
my $delete_num = unlink @files;
if ( $chunk_num != $delete_num ) {
print colored("maybe delete failed.n" . "w=$!", COLOR_ERROR);
croak colored("delete failure error", COLOR_ERROR) if !$flexible;
}
}
sub comma {
my $number = shift;
$number =~ s/(?<=d)(?=(?:ddd)+(?!d))/,/g;
return $number;
}
sub bit {
my $integer = shift;
return 0 if $integer == 0;
return( log($integer) / log(2) );
}
穏やかにファイルを削除する
穏やかにファイルを削除する
•
• cp --reflink=always
•
•
穏やかにファイルを削除する
1 of 34

Recommended

PHP Tutorial (funtion) by
PHP Tutorial (funtion)PHP Tutorial (funtion)
PHP Tutorial (funtion)Tinnakorn Puttha
818 views61 slides
PHP 1 by
PHP 1PHP 1
PHP 1Mohammed Abutabikh
354 views24 slides
SecureSocial - Authentication for Play Framework by
SecureSocial - Authentication for Play FrameworkSecureSocial - Authentication for Play Framework
SecureSocial - Authentication for Play Frameworkjaliss
4.5K views25 slides
Pre-Bootcamp introduction to Elixir by
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPaweł Dawczak
128 views175 slides
Php by
PhpPhp
PhpLinh Tran
537 views13 slides
Flow of events during Media Player creation in Android by
Flow of events during Media Player creation in AndroidFlow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidSomenath Mukhopadhyay
960 views8 slides

More Related Content

What's hot

Create Custom Post Type Plugin by
Create Custom Post Type PluginCreate Custom Post Type Plugin
Create Custom Post Type PluginJan Wilson
10K views56 slides
Nouveau document texte by
Nouveau document texteNouveau document texte
Nouveau document texteSai Ef
932 views64 slides
Yy by
YyYy
Yyyygh
995 views64 slides
201412 seccon2014 オンライン予選(英語) write-up by
201412 seccon2014 オンライン予選(英語) write-up201412 seccon2014 オンライン予選(英語) write-up
201412 seccon2014 オンライン予選(英語) write-up恵寿 東
357 views9 slides
C99.php by
C99.phpC99.php
C99.phpveng33k
1.5K views68 slides
Gabriele Lana - The Magic of Elixir by
Gabriele Lana - The Magic of ElixirGabriele Lana - The Magic of Elixir
Gabriele Lana - The Magic of ElixirCodemotion
525 views59 slides

What's hot(20)

Create Custom Post Type Plugin by Jan Wilson
Create Custom Post Type PluginCreate Custom Post Type Plugin
Create Custom Post Type Plugin
Jan Wilson10K views
Nouveau document texte by Sai Ef
Nouveau document texteNouveau document texte
Nouveau document texte
Sai Ef932 views
Yy by yygh
YyYy
Yy
yygh995 views
201412 seccon2014 オンライン予選(英語) write-up by 恵寿 東
201412 seccon2014 オンライン予選(英語) write-up201412 seccon2014 オンライン予選(英語) write-up
201412 seccon2014 オンライン予選(英語) write-up
恵寿 東357 views
C99.php by veng33k
C99.phpC99.php
C99.php
veng33k1.5K views
Gabriele Lana - The Magic of Elixir by Codemotion
Gabriele Lana - The Magic of ElixirGabriele Lana - The Magic of Elixir
Gabriele Lana - The Magic of Elixir
Codemotion525 views
The Ring programming language version 1.5.4 book - Part 51 of 185 by Mahmoud Samir Fayed
The Ring programming language version 1.5.4 book - Part 51 of 185The Ring programming language version 1.5.4 book - Part 51 of 185
The Ring programming language version 1.5.4 book - Part 51 of 185
Drupal 8 in action, the route to the method by juanolalla
Drupal 8 in action, the route to the methodDrupal 8 in action, the route to the method
Drupal 8 in action, the route to the method
juanolalla803 views
The Ring programming language version 1.6 book - Part 48 of 189 by Mahmoud Samir Fayed
The Ring programming language version 1.6 book - Part 48 of 189The Ring programming language version 1.6 book - Part 48 of 189
The Ring programming language version 1.6 book - Part 48 of 189
Climbing the Abstract Syntax Tree (PHP South Africa 2017) by James Titcumb
Climbing the Abstract Syntax Tree (PHP South Africa 2017)Climbing the Abstract Syntax Tree (PHP South Africa 2017)
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
James Titcumb266 views
ECMAScript 6 new features by GephenSG
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new features
GephenSG646 views
R57shell by ady36
R57shellR57shell
R57shell
ady363.3K views
Frege is a Haskell for the JVM by jwausle
Frege is a Haskell for the JVMFrege is a Haskell for the JVM
Frege is a Haskell for the JVM
jwausle200 views
Dip Your Toes in the Sea of Security (PHP South Africa 2017) by James Titcumb
Dip Your Toes in the Sea of Security (PHP South Africa 2017)Dip Your Toes in the Sea of Security (PHP South Africa 2017)
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
James Titcumb306 views
A Taste of Python - Devdays Toronto 2009 by Jordan Baker
A Taste of Python - Devdays Toronto 2009A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009
Jordan Baker1.3K views
Hangman Game Programming in C (coding) by hasan0812
Hangman Game Programming in C (coding)Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)
hasan081211.7K views
Solr integration in Magento Enterprise by Tobias Zander
Solr integration in Magento EnterpriseSolr integration in Magento Enterprise
Solr integration in Magento Enterprise
Tobias Zander2K views

Similar to 穏やかにファイルを削除する

Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011 by
Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011
Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011John Ford
8.7K views75 slides
Stop Monkeys Fall by
Stop Monkeys FallStop Monkeys Fall
Stop Monkeys FallHajime Morrita
784 views20 slides
Gazelle - Plack Handler for performance freaks #yokohamapm by
Gazelle - Plack Handler for performance freaks #yokohamapmGazelle - Plack Handler for performance freaks #yokohamapm
Gazelle - Plack Handler for performance freaks #yokohamapmMasahiro Nagano
13.1K views26 slides
Perl5i by
Perl5iPerl5i
Perl5iMarcos Rebelo
510 views24 slides
Czzawk by
CzzawkCzzawk
Czzawk宗志 陈
974 views56 slides
Bag of tricks by
Bag of tricksBag of tricks
Bag of tricksbrian d foy
4.4K views32 slides

Similar to 穏やかにファイルを削除する(20)

Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011 by John Ford
Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011
Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011
John Ford8.7K views
Gazelle - Plack Handler for performance freaks #yokohamapm by Masahiro Nagano
Gazelle - Plack Handler for performance freaks #yokohamapmGazelle - Plack Handler for performance freaks #yokohamapm
Gazelle - Plack Handler for performance freaks #yokohamapm
Masahiro Nagano13.1K views
Bag of tricks by brian d foy
Bag of tricksBag of tricks
Bag of tricks
brian d foy4.4K views
Itsecteam shell by ady36
Itsecteam shellItsecteam shell
Itsecteam shell
ady361.6K views
Yy by yygh
YyYy
Yy
yygh346 views
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011 by Masahiro Nagano
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Masahiro Nagano2.6K views
Notes for SQLite3 Usage by William Lee
Notes for SQLite3 UsageNotes for SQLite3 Usage
Notes for SQLite3 Usage
William Lee1.6K views
Serhii Korolenko - Passing Security By by NoNameCon
Serhii Korolenko - Passing Security BySerhii Korolenko - Passing Security By
Serhii Korolenko - Passing Security By
NoNameCon313 views
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016) by James Titcumb
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
James Titcumb542 views
Top 10 php classic traps confoo by Damien Seguy
Top 10 php classic traps confooTop 10 php classic traps confoo
Top 10 php classic traps confoo
Damien Seguy132 views
Top 10 php classic traps DPC 2020 by Damien Seguy
Top 10 php classic traps DPC 2020Top 10 php classic traps DPC 2020
Top 10 php classic traps DPC 2020
Damien Seguy122 views
Build a custom (micro)framework with ZF2 Components (as building blocks) by Corley S.r.l.
Build a custom (micro)framework with ZF2 Components (as building blocks)Build a custom (micro)framework with ZF2 Components (as building blocks)
Build a custom (micro)framework with ZF2 Components (as building blocks)
Corley S.r.l.1.8K views
Climbing the Abstract Syntax Tree (CodeiD PHP Odessa 2017) by James Titcumb
Climbing the Abstract Syntax Tree (CodeiD PHP Odessa 2017)Climbing the Abstract Syntax Tree (CodeiD PHP Odessa 2017)
Climbing the Abstract Syntax Tree (CodeiD PHP Odessa 2017)
James Titcumb95 views

More from 鉄次 尾形

Perl入学式 2018年度の報告 by
Perl入学式 2018年度の報告Perl入学式 2018年度の報告
Perl入学式 2018年度の報告鉄次 尾形
1.3K views26 slides
【公開版】AWS基礎 for 新卒エンジニア by
【公開版】AWS基礎 for 新卒エンジニア【公開版】AWS基礎 for 新卒エンジニア
【公開版】AWS基礎 for 新卒エンジニア鉄次 尾形
2.1K views68 slides
俺のこだわりデスクのその後 by
俺のこだわりデスクのその後俺のこだわりデスクのその後
俺のこだわりデスクのその後鉄次 尾形
585 views26 slides
Hokkaido.pmと7年 by
Hokkaido.pmと7年Hokkaido.pmと7年
Hokkaido.pmと7年鉄次 尾形
1.6K views19 slides
WordPress 運用を支える Perl by
WordPress 運用を支える PerlWordPress 運用を支える Perl
WordPress 運用を支える Perl鉄次 尾形
1.4K views52 slides
障害対応とその防止策 by
障害対応とその防止策障害対応とその防止策
障害対応とその防止策鉄次 尾形
753 views41 slides

More from 鉄次 尾形(20)

Perl入学式 2018年度の報告 by 鉄次 尾形
Perl入学式 2018年度の報告Perl入学式 2018年度の報告
Perl入学式 2018年度の報告
鉄次 尾形1.3K views
【公開版】AWS基礎 for 新卒エンジニア by 鉄次 尾形
【公開版】AWS基礎 for 新卒エンジニア【公開版】AWS基礎 for 新卒エンジニア
【公開版】AWS基礎 for 新卒エンジニア
鉄次 尾形2.1K views
俺のこだわりデスクのその後 by 鉄次 尾形
俺のこだわりデスクのその後俺のこだわりデスクのその後
俺のこだわりデスクのその後
鉄次 尾形585 views
WordPress 運用を支える Perl by 鉄次 尾形
WordPress 運用を支える PerlWordPress 運用を支える Perl
WordPress 運用を支える Perl
鉄次 尾形1.4K views
障害対応とその防止策 by 鉄次 尾形
障害対応とその防止策障害対応とその防止策
障害対応とその防止策
鉄次 尾形753 views
働きやすい社内を目指す!二酸化炭素計測ツール by 鉄次 尾形
働きやすい社内を目指す!二酸化炭素計測ツール働きやすい社内を目指す!二酸化炭素計測ツール
働きやすい社内を目指す!二酸化炭素計測ツール
鉄次 尾形968 views
Perl ウェブ開発の中世〜CGI と Plack の間〜 by 鉄次 尾形
Perl ウェブ開発の中世〜CGI と Plack の間〜Perl ウェブ開発の中世〜CGI と Plack の間〜
Perl ウェブ開発の中世〜CGI と Plack の間〜
鉄次 尾形8.7K views
コードの動的生成のお話 by 鉄次 尾形
コードの動的生成のお話コードの動的生成のお話
コードの動的生成のお話
鉄次 尾形698 views
三角関数の加法定理はなぜ難しいのか by 鉄次 尾形
三角関数の加法定理はなぜ難しいのか三角関数の加法定理はなぜ難しいのか
三角関数の加法定理はなぜ難しいのか
鉄次 尾形1.1K views
Perlで生きる10年 by 鉄次 尾形
Perlで生きる10年Perlで生きる10年
Perlで生きる10年
鉄次 尾形606 views
穏やかにファイルを削除する続き by 鉄次 尾形
穏やかにファイルを削除する続き穏やかにファイルを削除する続き
穏やかにファイルを削除する続き
鉄次 尾形769 views
日本全国ぶらりPerl旅 by 鉄次 尾形
日本全国ぶらりPerl旅日本全国ぶらりPerl旅
日本全国ぶらりPerl旅
鉄次 尾形695 views
最近のメールまわりのお仕事のお話 by 鉄次 尾形
最近のメールまわりのお仕事のお話最近のメールまわりのお仕事のお話
最近のメールまわりのお仕事のお話
鉄次 尾形1K views
再帰、漸化式、差分方程式とアルゴリズム Gx#20 by 鉄次 尾形
再帰、漸化式、差分方程式とアルゴリズム   Gx#20再帰、漸化式、差分方程式とアルゴリズム   Gx#20
再帰、漸化式、差分方程式とアルゴリズム Gx#20
鉄次 尾形2.9K views
超小規模環境のMySQL #mysqlcasual by 鉄次 尾形
超小規模環境のMySQL #mysqlcasual超小規模環境のMySQL #mysqlcasual
超小規模環境のMySQL #mysqlcasual
鉄次 尾形4.7K views
mod_perlプログラマーがYAPCで語るレガシー開発論 by 鉄次 尾形
mod_perlプログラマーがYAPCで語るレガシー開発論mod_perlプログラマーがYAPCで語るレガシー開発論
mod_perlプログラマーがYAPCで語るレガシー開発論
鉄次 尾形6.7K views
東京近郊のPerlコミュニティのご紹介 #lcs2015 by 鉄次 尾形
東京近郊のPerlコミュニティのご紹介 #lcs2015東京近郊のPerlコミュニティのご紹介 #lcs2015
東京近郊のPerlコミュニティのご紹介 #lcs2015
鉄次 尾形1.4K views
4月16日だからFoursquare 位置情報とライフログ by 鉄次 尾形
4月16日だからFoursquare 位置情報とライフログ4月16日だからFoursquare 位置情報とライフログ
4月16日だからFoursquare 位置情報とライフログ
鉄次 尾形2.3K views
設定ファイルに使われそうなデータ形式 by 鉄次 尾形
設定ファイルに使われそうなデータ形式設定ファイルに使われそうなデータ形式
設定ファイルに使われそうなデータ形式
鉄次 尾形7.6K views

Recently uploaded

Scaling Knowledge Graph Architectures with AI by
Scaling Knowledge Graph Architectures with AIScaling Knowledge Graph Architectures with AI
Scaling Knowledge Graph Architectures with AIEnterprise Knowledge
50 views15 slides
virtual reality.pptx by
virtual reality.pptxvirtual reality.pptx
virtual reality.pptxG036GaikwadSnehal
18 views15 slides
Democratising digital commerce in India-Report by
Democratising digital commerce in India-ReportDemocratising digital commerce in India-Report
Democratising digital commerce in India-ReportKapil Khandelwal (KK)
20 views161 slides
Vertical User Stories by
Vertical User StoriesVertical User Stories
Vertical User StoriesMoisés Armani Ramírez
17 views16 slides
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Safe Software
317 views86 slides
GDSC CTU First Meeting Party by
GDSC CTU First Meeting PartyGDSC CTU First Meeting Party
GDSC CTU First Meeting PartyNational Yang Ming Chiao Tung University
11 views25 slides

Recently uploaded(20)

Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software317 views
Unit 1_Lecture 2_Physical Design of IoT.pdf by StephenTec
Unit 1_Lecture 2_Physical Design of IoT.pdfUnit 1_Lecture 2_Physical Design of IoT.pdf
Unit 1_Lecture 2_Physical Design of IoT.pdf
StephenTec15 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays33 views
The Forbidden VPN Secrets.pdf by Mariam Shaba
The Forbidden VPN Secrets.pdfThe Forbidden VPN Secrets.pdf
The Forbidden VPN Secrets.pdf
Mariam Shaba20 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson126 views
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc72 views
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views
SAP Automation Using Bar Code and FIORI.pdf by Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf

穏やかにファイルを削除する

  • 1. OGATA Tetsuji (@xtetsuji) 2016/05/19 Gaiax https://flic.kr/p/pAQruL
  • 2. • / / OGATA Tetsuji • @xtetsuji • Blog: http://post.tetsuji.jp/ • RND
  • 7. )
  • 9. • • • rm -rf tmp_foo •
  • 11. $ strace rm moge.pl execve("/bin/rm", ["rm", "moge.pl"], [/* 52 vars */]) = 0 brk(0) = 0x12c1000 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1fd3000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=41893, ...}) = 0 mmap(NULL, 41893, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd9d1fc8000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY) = 3 read(3, "177ELF21100000000030>010000360100000"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0755, st_size=1599536, ...}) = 0 mmap(NULL, 3713112, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fd9d1a2b000 mprotect(0x7fd9d1bac000, 2097152, PROT_NONE) = 0 mmap(0x7fd9d1dac000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x181000) = 0x7fd9d1dac000 mmap(0x7fd9d1db1000, 18520, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1db1000 close(3) = 0 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1fc7000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1fc6000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9d1fc5000 arch_prctl(ARCH_SET_FS, 0x7fd9d1fc6700) = 0 mprotect(0x7fd9d1dac000, 16384, PROT_READ) = 0 mprotect(0x60d000, 4096, PROT_READ) = 0 mprotect(0x7fd9d1fd5000, 4096, PROT_READ) = 0 munmap(0x7fd9d1fc8000, 41893) = 0 brk(0) = 0x12c1000 brk(0x12e2000) = 0x12e2000 open("/usr/lib/locale/locale-archive", O_RDONLY) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=2508736, ...}) = 0 mmap(NULL, 2508736, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd9d17c6000 close(3) = 0 ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 opost isig icanon echo ...}) = 0 newfstatat(AT_FDCWD, "moge.pl", {st_mode=S_IFREG|0755, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 geteuid() = 1000 newfstatat(AT_FDCWD, "moge.pl", {st_mode=S_IFREG|0755, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 faccessat(AT_FDCWD, "moge.pl", W_OK) = 0 unlinkat(AT_FDCWD, "moge.pl", 0) = 0 lseek(0, 0, SEEK_CUR) = -1 ESPIPE (Illegal seek) close(0) = 0 close(1) = 0 close(2) = 0 exit_group(0) = ?
  • 16. • find xargs • rsync --delete empty/ garbage/ •
  • 19. $ gentle_unlink --help Usage: gentle_unlink remove_file_list.txt ls -1 *.log | gentle_unlink --timeout=600 --progress --interval=2 gentle_unlink [--timeout=SECONDS] [--progress] [--interval=SECONDS] [--flexible] [--dry-run]
  • 20. • --progress rsync • --interval • --timeout • --flexible • --dry-run
  • 21. gentle_unlink • • find $DIR -type f • --progress • • --timeout cron
  • 22. DEMO?
  • 24. #!/usr/bin/perl use strict; use warnings; use Carp qw(croak); use Term::ANSIColor qw(colored); use Getopt::Long qw(:config posix_default no_ignore_case bundling auto_help); use Time::HiRes qw(time usleep ualarm);
  • 25. USR1 $SIG{USR1} = &guess_current_status; kill -USR1 %1 OK # for USR1 sub guess_current_status { my $signal = shift; printf colored("dry-run mode. all delete operations are fake.n", "blue") if $dry_run; printf "process id: %dn", $$; printf "delete file count: %sn", comma($delete_count); printf "delete size total: %s bytesn", comma($delete_size_total); printf "process run times: %s secondn", comma(int(time - $process_start_time)); printf "interval=%d timeout=%sn", $interval, $timeout; }
  • 26. use constant DEBUG => $ENV{DEBUG}; use constant MAX_TIME_HIRES_BIT => 31; use constant UNLINK_CHUNK_NUM => 20; use constant COLOR_WARNING => "yellow"; use constant COLOR_ERROR => "red"; use constant COLOR_INFO => "green"; $SIG{USR1} = &guess_current_status; GetOptions( my %opt, "timeout|t=f", "progress|p", "interval|i=f", "flexible|f", "dry-run|n" ); my $timeout = $opt{timeout} || 0; my $progress = $opt{progress}; my $interval = $opt{interval} || 0; my $dry_run = $opt{"dry-run"}; my $flexible = $opt{flexible}; my $delete_count = 0; my $delete_size_total = 0; my $timeout_flag; my $preserve_interval_flag; my $process_start_time = time;
  • 27. eval local $@; eval { local $SIG{ALRM} = $timeout > 0 ? sub { $timeout_flag = 1; } : "DEFAULT"; # alarm (10^{-6}) 32 alarm my $alarm_cancel_cb; # alram 0 ualarm 0 my $timeout_microsecond = $timeout * 10**6; if ( bit($timeout_microsecond) < MAX_TIME_HIRES_BIT ) { ualarm($timeout_microsecond); $alarm_cancel_cb = sub { ualarm 0; }; } else { alarm(int($timeout)); $alarm_cancel_cb = sub { alarm 0; }; } gentle_unlink(); $alarm_cancel_cb->(); }; if ( $@ ) { print colored("e=$@n", COLOR_ERROR); }
  • 28. sub gentle_unlink { my @file_chunk; my $start_time = time; while (<>) { chomp; print "> $_n" if DEBUG; push @file_chunk, $_; if ( @file_chunk > UNLINK_CHUNK_NUM ) { unlink_chunk(@file_chunk); @file_chunk = (); $preserve_interval_flag = 1; } } continue { if ( $timeout_flag ) { progress_printf(colored("timeoutn", COLOR_WARNING)); last; } if ( $interval && $preserve_interval_flag ) { progress_printf(colored("interval %f seconds.n", COLOR_INFO), $interval ); usleep $interval * 10**6; $preserve_interval_flag = 0; } } if ( @file_chunk ) { unlink_chunk(@file_chunk); } my $end_time = time; progress_printf("%f seconds, %s bytes, total %d files deleted.n", $end_time - $start_time, comma($delete_size_total), $delete_count); progress_printf(colored("dry-run mode. all delete operations are fake.n", "blue")) if $dry_run; }
  • 29. sub unlink_chunk { my @files = @_; my @nowhere_files = grep { !-f } @files; if ( @nowhere_files ) { if ( $flexible ) { progress_printf(colored( join( "", map { qq(file "$_" is not foundn) } @nowhere_files ), COLOR_WARNING )); # progress_printf colored } else { croak colored("unlink_chunk gives lost filename.", COLOR_ERROR); } } progress_printf(">>> dry-run moden") if $dry_run; progress_printf("%4d: deleting... %s (%s bytes)n", ++$delete_count, $_, comma(-s $_)) for @files; $delete_size_total += -s $_ for @files; return if $dry_run; local $!; my $chunk_num = @files; my $delete_num = unlink @files; if ( $chunk_num != $delete_num ) { print colored("maybe delete failed.n" . "w=$!", COLOR_ERROR); croak colored("delete failure error", COLOR_ERROR) if !$flexible; } }
  • 30. sub comma { my $number = shift; $number =~ s/(?<=d)(?=(?:ddd)+(?!d))/,/g; return $number; } sub bit { my $integer = shift; return 0 if $integer == 0; return( log($integer) / log(2) ); }