/
cache.cpp
666 lines (482 loc) · 14.7 KB
/
cache.cpp
1
2
3
4
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
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
64
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
91
92
93
94
95
96
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// ライセンス: GPL2
//#define _DEBUG
#include "jddebug.h"
#include "cache.h"
#include "config/globalconf.h"
#include "jdlib/miscutil.h"
#include "jdlib/miscmsg.h"
#include "dbtree/interface.h"
#include <gtkmm.h>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
// 設定ファイル
std::string CACHE::path_conf()
{
std::string home = getenv( "HOME" );
return home + "/.jdrc";
}
// セッション情報ファイル
std::string CACHE::path_session()
{
return CACHE::path_root() + "session.info";
}
// ロックファイル
std::string CACHE::path_lock()
{
return CACHE::path_root() + "JDLOCK";
}
// パスワード設定ファイル
std::string CACHE::path_passwd( const std::string& basename )
{
return CACHE::path_root() + basename + ".conf";
}
// キャッシュルートの絶対パス
std::string CACHE::path_root()
{
std::string root = MISC::remove_space( CONFIG::get_path_cacheroot() );
if( root[ root.length() -1 ] != '/' ) root = root + "/";
if( root[ 0 ] == '~' ){
std::string home = getenv( "HOME" );
root.replace( 0, 1, home );
}
return root;
}
// 板リスト
std::string CACHE::path_xml_listmain()
{
return CACHE::path_root() + "list_main.xml";
}
std::string CACHE::path_xml_listmain_bkup()
{
return CACHE::path_xml_listmain() + ".bkup";
}
// お気に入り
std::string CACHE::path_xml_favorite()
{
return CACHE::path_root() + "favorite.xml";
}
std::string CACHE::path_xml_favorite_bkup()
{
return CACHE::path_xml_favorite() + ".bkup";
}
// 外部板設定ファイル( navi2ch 互換 )
std::string CACHE::path_etcboard()
{
return CACHE::path_root() + "etc.txt";
}
// ユーザーコマンド設定ファイル
std::string CACHE::path_usrcmd()
{
return CACHE::path_root() + "usrcmd.txt";
}
// 履歴
std::string CACHE::path_xml_history()
{
return CACHE::path_root() + "history.xml";
}
// 板履歴
std::string CACHE::path_xml_history_board()
{
return CACHE::path_root() + "history_board.xml";
}
// 板移転情報
std::string CACHE::path_movetable()
{
return CACHE::path_root() + "move.info";
}
// キーボード設定
std::string CACHE::path_keyconf()
{
return CACHE::path_root() + "key.conf";
}
// マウスジェスチャ設定
std::string CACHE::path_mouseconf()
{
return CACHE::path_root() + "mouse.conf";
}
// マウスボタン設定
std::string CACHE::path_buttonconf()
{
return CACHE::path_root() + "button.conf";
}
// 板のルートパス
std::string CACHE::path_board_root( const std::string& url )
{
// http:// を取り除く
std::string url_board = DBTREE::url_boardbase( url );
#ifdef _DEBUG
std::cout << "CACHE::path_board_root " << url << " -> " << url_board << std::endl;
#endif
size_t i = url_board.find( "://" );
if( i == std::string::npos ) return std::string();
return CACHE::path_root() + url_board.substr( i + 3 );
}
std::string CACHE::path_article_summary( const std::string& url )
{
return CACHE::path_board_root( url ) + "article-summary";
}
// board情報( navi2ch互換用 )
std::string CACHE::path_board_info( const std::string& url )
{
return CACHE::path_board_root( url ) + "board.info";
}
// board情報( jd 用 )
std::string CACHE::path_jdboard_info( const std::string& url )
{
return CACHE::path_board_root( url ) + "jdboard.info";
}
std::string CACHE::path_article_info_root( const std::string& url )
{
return CACHE::path_board_root( url ) + "info/";
}
// スレの情報ファイル( navi2ch互換、実際には使用しない )
std::string CACHE::path_article_info( const std::string& url, const std::string& id )
{
std::string id_str = id;
// idに拡張子が付いてたら取る
size_t i = id.find( "." );
if( i != std::string::npos ) id_str = id.substr( 0, i );
return CACHE::path_article_info_root( url ) + id_str;
}
// スレの拡張情報ファイル
std::string CACHE::path_article_ext_info( const std::string& url, const std::string& id )
{
return CACHE::path_article_info( url, id ) + ".info";
}
std::string CACHE::path_dat( const std::string& url )
{
return CACHE::path_board_root( url ) + DBTREE::article_id( url );
}
//
// 画像キャッシュのルートパス
//
std::string CACHE::path_img_root()
{
return CACHE::path_root() + "image/";
}
//
// 画像キャッシュのinfoファイルのルートパス
//
std::string CACHE::path_img_info_root()
{
return path_img_root() + "info/";
}
//
// 書き込みログ
//
std::string CACHE::path_postlog()
{
return CACHE::path_root() + "post.log";
}
//
// 画像キャッシュファイルの名前
//
std::string CACHE::filename_img( const std::string& url )
{
std::string file = MISC::tolower_str( url );
file = MISC::replace_str( file, "http://", "" );
file = MISC::replace_str( file, "/", "-" );
file = MISC::url_encode( file.c_str(), file.length() );
file = MISC::replace_str( file, "%", "" );
return file;
}
//
// 画像キャッシュファイルのパス
//
std::string CACHE::path_img( const std::string& url )
{
return CACHE::path_img_root() + filename_img( url );
}
//
// 画像infoファイルのパス
//
std::string CACHE::path_img_info( const std::string& url )
{
return CACHE::path_img_info_root() + filename_img( url ) + ".info";
}
//
// キャッシュのルートディレクトリをmkdir
//
// 例えば "/home/hoge/.jd/" を作成
//
bool CACHE::mkdir_root()
{
std::string path_root = CACHE::path_root();
if( ! CACHE::jdmkdir( path_root ) ){
MISC::ERRMSG( "can't create " + path_root );
return false;
}
return true;
}
//
// 画像キャッシュのルートディレクトリをmkdir
//
// 例えば "/home/hoge/.jd/image/" と "/home/hoge/.jd/image/info/" を作成
//
bool CACHE::mkdir_imgroot()
{
// root
std::string path_img_root = CACHE::path_img_root();
if( ! CACHE::jdmkdir( path_img_root ) ){
MISC::ERRMSG( "can't create " + path_img_root );
return false;
}
// info ディレクトリ
std::string path_info_root = CACHE::path_img_info_root();
if( ! CACHE::jdmkdir( path_info_root ) ){
MISC::ERRMSG( "can't create " + path_info_root );
return false;
}
return true;
}
//
// キャッシュのひとつ上のディレクトリをmkdir
//
// 例えばキャッシュのルートディレクトリが "/home/hoge/.jd/hoge.2ch.net/hogeboard/" だったら
// "/home/hoge/.jd/hoge.2ch.net/" を作成する
//
bool CACHE::mkdir_parent_of_board( const std::string& url )
{
std::string path_tmp = CACHE::path_board_root( url );
size_t i = path_tmp.rfind( "/", path_tmp.length() -2 );
if( i == std::string::npos ) return false;
std::string path_parent = path_tmp.substr( 0, i );
if( ! CACHE::jdmkdir( path_parent ) ){
MISC::ERRMSG( "can't create " + path_parent );
return false;
}
return true;
}
//
// ある板のキャッシュのルートディレクトリをmkdir
//
// 例えば "/home/hoge/.jd/hoge.2ch.net/hogeboard/" と "/home/hoge/.jd/hoge.2ch.net/hogeboard/info/" を作成
//
bool CACHE::mkdir_boardroot( const std::string& url )
{
// root
std::string path_board_root = CACHE::path_board_root( url );
if( ! CACHE::jdmkdir( path_board_root ) ){
MISC::ERRMSG( "can't create " + path_board_root );
return false;
}
// info ディレクトリ
std::string path_info_root = CACHE::path_article_info_root( url );
if( ! CACHE::jdmkdir( path_info_root ) ){
MISC::ERRMSG( "can't create " + path_info_root );
return false;
}
return true;
}
size_t CACHE::load_rawdata( const std::string& path, std::string& str )
{
str.clear();
std::ifstream fin;
fin.open( path.c_str() );
if( !fin.is_open() ) return 0;
getline( fin, str, '\0' );
fin.close();
return str.length();
}
size_t CACHE::load_rawdata( const std::string& path, char* data, size_t n )
{
size_t count = 0;
std::ifstream fin;
fin.open( path.c_str() );
if( !fin.is_open() ) return 0;
fin.read( data, n );
count = fin.gcount();
fin.close();
return count;
}
bool CACHE::save_rawdata( const std::string& path, const std::string& str, bool append )
{
return save_rawdata( path, str.c_str(), str.length(), append );
}
bool CACHE::save_rawdata( const std::string& path, const char* data, size_t n, bool append )
{
std::ofstream fout;
if( append ) fout.open( path.c_str(), std::ios::app );
else fout.open( path.c_str() );
if( !fout.is_open() ){
MISC::ERRMSG( "can't open " + path );
return false;
}
fout.write( data, n );
fout.close();
return true;
}
long CACHE::is_file_exists( const std::string& path )
{
struct stat buf_stat;
if( path.empty() ) return EXIST_ERROR;
if( stat( path.c_str(), &buf_stat ) != 0 ) return EXIST_ERROR;
if( S_ISREG( buf_stat.st_mode ) ) return EXIST_FILE;
if( S_ISDIR( buf_stat.st_mode ) ) return EXIST_DIR;
return EXIST;
}
size_t CACHE::get_filesize( const std::string& path )
{
struct stat buf_stat;
if( stat( path.c_str(), &buf_stat ) != 0 ) return 0;
if( S_ISREG( buf_stat.st_mode ) ) return buf_stat.st_size;
return 0;
}
time_t CACHE::get_filemtime( const std::string& path )
{
struct stat buf_stat;
if( stat( path.c_str(), &buf_stat ) != 0 ) return 0;
if( S_ISREG( buf_stat.st_mode ) ) return buf_stat.st_mtime;
return 0;
}
//
// mkdir
//
bool CACHE::jdmkdir( const std::string& path )
{
#ifdef _DEBUG
std::cout << "CACHE::jdmkdir : path = " + path << std::endl;
#endif
if( CACHE::is_file_exists( path ) == EXIST_DIR ) return true;
std::string target = path;
if( path.find( "~/" ) == 0 ){
std::string homedir = getenv( "HOME" );
if( homedir.empty() ) return false;
target = homedir + path.substr( 2 );
}
if( target[ 0 ] != '/' ) return false;
if( target[ target.length() -1 ] != '/' ) target += "/";
#ifdef _DEBUG
std::cout << "target = " << target << std::endl;
#endif
// ルートからディレクトリがあるかチェックしていく。無ければ作る
size_t i = 0;
while( ( i = target.find( "/", i ) ) != std::string::npos ){
++i;
std::string currentdir = target.substr( 0, i );
#ifdef _DEBUG
std::cout << "mkdir " << currentdir << std::endl;
#endif
if( CACHE::is_file_exists( currentdir ) == EXIST_DIR ) continue;
if( mkdir( currentdir.c_str(), 0755 ) != 0 ){
MISC::ERRMSG( "mkdir failed " + currentdir );
return false;
}
}
return true;
}
//
// file copy
//
bool CACHE::jdcopy( const std::string& file_from, const std::string& file_to )
{
struct stat buf_stat;
if( stat( file_from.c_str(), &buf_stat ) != 0 ) return false;
#ifdef _DEBUG
std::cout << "CACHE::jdcopy : from = " << file_from << std::endl;
std::cout << "to = " << file_to << std::endl;
std::cout << "size = " << buf_stat.st_size << std::endl;
#endif
// 32Mより大きい画像はエラー出す
if( buf_stat.st_size > 32 * 1024 * 1024 ){
MISC::ERRMSG( "CACHE::jdcopy: size is too big : " + file_from );
return false;
}
char* data = (char*)malloc( sizeof( char ) * buf_stat.st_size );
load_rawdata( file_from, data, buf_stat.st_size );
save_rawdata( file_to, data, buf_stat.st_size );
free( data );
return true;
}
//
// 保存ダイアログを表示
//
// 戻り値は保存先(失敗したらempty())
//
std::string CACHE::open_save_diag( const std::string& file_from, const std::string& file_to )
{
if( file_from.empty() ) return std::string();
if( file_to.empty() ) return std::string();
Gtk::FileChooserDialog diag( "save", Gtk::FILE_CHOOSER_ACTION_SAVE );
std::string name = MISC::get_filename( file_to );
std::string dir = MISC::get_dir( file_to );
if( dir.empty() ) dir = MISC::get_dir( file_from );
#ifdef _DEBUG
std::cout << "CACHE::open_copy_diag\n";
std::cout << "from = " << file_from << std::endl;
std::cout << "dir = " << dir << std::endl;
std::cout << "name = " << name << std::endl;
#endif
diag.set_current_folder( dir );
diag.set_current_name( name );
// ボタン追加 + saveボタンをデフォルトボタンにセット
diag.add_button( Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL );
diag.add_button( Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT );
diag.set_default_response( Gtk::RESPONSE_ACCEPT );
if( diag.run() == Gtk::RESPONSE_ACCEPT ){
diag.hide();
std::string path_to = diag.get_filename();
#ifdef _DEBUG
std::cout << "to = " << path_to << std::endl;
#endif
// 既にファイルがある場合は問い合わせる
if( CACHE::is_file_exists( path_to ) == CACHE::EXIST_FILE ){
Gtk::MessageDialog mdiag( "ファイルが存在します。ファイル名を変更しますか?",
false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL );
if( mdiag.run() == Gtk::RESPONSE_OK ) return CACHE::open_save_diag( file_from, path_to );
return std::string();
}
if( CACHE::jdcopy( file_from, path_to ) ) return path_to;
}
return std::string();
}
//
// dir ディレクトリ内のレギュラーファイルのリストを取得
//
std::list< std::string > CACHE::get_filelist( const std::string& dir )
{
std::list< std::string > list_files;
#ifdef _DEBUG
std::cout << "CACHE::get_filelist " << dir << std::endl;
#endif
DIR *dirp = opendir( dir.c_str() );
if( !dirp ) return list_files;
struct dirent *direntry;
while( ( direntry = readdir( dirp ) ) ){
std::string filename = dir + direntry->d_name;
if( is_file_exists( filename ) == EXIST_FILE ){
#ifdef _DEBUG
std::cout << filename << std::endl;
#endif
list_files.push_back( direntry->d_name );
}
}
closedir( dirp );
return list_files;
}
//
// dir ディレクトリ内のレギュラーファイルの合計サイズを取得
//
int64_t CACHE::get_dirsize( const std::string& dir )
{
int64_t total_size = 0;
#ifdef _DEBUG
std::cout << "CACHE::get_dirsize " << dir << std::endl;
#endif
DIR *dirp = opendir( dir.c_str() );
if( !dirp ) return 0;
struct dirent *direntry;
while( ( direntry = readdir( dirp ) ) ){
std::string filename = dir + direntry->d_name;
total_size += CACHE::get_filesize( filename );
}
closedir( dirp );
#ifdef _DEBUG
std::cout << "size = " << total_size << std::endl;
#endif
return total_size;
}