DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::oks::OksFile Class Reference

Provides interface to the OKS XML schema and data files. More...

#include <file.hpp>

Collaboration diagram for dunedaq::oks::OksFile:
[legend]

Classes

struct  SortByName

Public Types

enum  Mode { ReadOnly , ReadWrite }
enum  FileStatus { FileNotModified , FileModified , FileWasNotSaved , FileRemoved }
typedef std::map< const std::string *, OksFile *, SortByNameMap
typedef std::unordered_set< OksFile *, oks::hash_file_ptr, oks::equal_file_ptrSet
typedef std::unordered_map< OksFile *, Set, oks::hash_file_ptr, oks::equal_file_ptrIMap

Public Member Functions

void lock ()
 Lock OKS file.
void unlock ()
 Unlock OKS file.
void add_include_file (const std::string &name)
 Add include file.
void remove_include_file (const std::string &name)
 Remove include file.
void rename_include_file (const std::string &from, const std::string &to)
 Rename include file.
void add_comment (const std::string &text, const std::string &author)
 Add new comment to file.
void modify_comment (const std::string &creation_time, const std::string &text, const std::string &author)
void remove_comment (const std::string &creation_time)
 Modify existing comment.
const std::map< std::string, oks::Comment * > & get_comments () const
void set_logical_name (const std::string &name)
 Set logical name of file.
void set_type (const std::string &type)
 Set file type.
const std::string & get_short_file_name () const
const std::string & get_full_file_name () const
bool is_repository_file () const
 Get information about repository, the file belongs to.
const std::string & get_repository_name () const
 Get name of file inside repository.
const std::string & get_well_formed_name () const
 Get well-formed file name.
const std::string & get_lock_file () const
const std::string & get_logical_name () const
const std::string & get_type () const
const std::string & get_oks_format () const
long get_number_of_items () const
long get_size () const
const std::string & get_created_by () const
const boost::posix_time::ptime get_creation_time () const
const std::string & get_created_on () const
const std::string & get_last_modified_by () const
const boost::posix_time::ptime get_last_modification_time () const
const std::string & get_last_modified_on () const
bool is_locked () const
 Return lock status of OKS file.
bool is_updated () const
 Return update status of OKS file.
bool is_read_only () const
 Return read-only status of OKS file.
const std::list< std::string > & get_include_files () const
 Get directly include files.
void get_all_include_files (const OksKernel *kernel, std::set< OksFile * > &out)
 Get all include files.
FileStatus get_status_of_file () const
 Return update status of file.
void update_status_of_file (bool update_local=true, bool update_repository=true)
 Update status of file.
bool get_lock_string (std::string &info) const
 Return lock status of OKS file and if the file is locked, get information string about process which locked the file.
bool compare (const char *name) const
 Compare with another file.
const OksFileget_parent () const
 Return parent including given file.
OksFilecheck_parent (const OksFile *parent_h)
 Set given parent, if this file is not yet included.
 ~OksFile () noexcept

Static Public Member Functions

static void set_nolock_mode (bool nl)
static bool compare (const char *file1_name, const char *file2_name)
 Compare two files.

Private Member Functions

 OksFile (const std::string &, const std::string &, const std::string &, const std::string &, OksKernel *)
OksFileoperator= (const OksFile &)
 OksFile (const OksFile &f)
 OksFile (std::shared_ptr< OksXmlInputStream >, const std::string &, const std::string &, OksKernel *)
void write (OksXmlOutputStream &)
void set_updated ()
void rename (const std::string &short_name, const std::string &full_name)
void rename (const std::string &full_name)
void create_lock_name ()
void init_lock_name () const
void check_repository ()
std::string make_repository_name () const
oks::Commentget_comment (const std::string &creation_time) noexcept
void clear_comments () noexcept
std::list< std::string >::iterator find_include_file (const std::string &)

Private Attributes

std::string p_short_name
std::string p_full_name
std::string p_repository_name
std::string p_logical_name
std::string p_type
std::string p_oks_format
long p_number_of_items
long p_size
std::string p_created_by
boost::posix_time::ptime p_creation_time
std::string p_created_on
std::string p_last_modified_by
boost::posix_time::ptime p_last_modification_time
std::string p_last_modified_on
Mode p_open_mode
std::shared_ptr< boost::interprocess::file_lock > p_lock
std::string p_lock_file_name
bool p_is_updated
bool p_is_read_only
std::list< std::string > p_list_of_include_files
time_t p_last_modified
time_t p_repository_last_modified
std::map< std::string, oks::Comment * > p_comments
bool p_is_on_disk
const OksFilep_included_by
OksKernelp_kernel

Static Private Attributes

static bool p_nolock_mode = false
static const char xml_file_header [] = "<?xml version=\"1.0\" encoding=\"ASCII\"?>"
static const char xml_schema_file_dtd []
static const char xml_data_file_dtd []
static const char xml_info_tag [] = "info"
static const char xml_include_tag [] = "include"
static const char xml_file_tag [] = "file"
static const char xml_comments_tag [] = "comments"
static const char xml_comment_tag [] = "comment"

Friends

class OksKernel
class OksClass
class OksObject
struct OksLoadObjectsJob

Detailed Description

Provides interface to the OKS XML schema and data files.

An object of this class is used to read/write header part of OKS XML file, includes and comments. It allows to set update lock on file creating .oks-lock-$filename file: see lock() and unlock() methods. Also, the class provides methods to get many other properties of the file, e.g. status, access mode, repository information, number of stored items, size, etc.

Definition at line 345 of file file.hpp.

Member Typedef Documentation

◆ IMap

Definition at line 375 of file file.hpp.

◆ Map

typedef std::map<const std::string *, OksFile *, SortByName> dunedaq::oks::OksFile::Map

Definition at line 372 of file file.hpp.

◆ Set

Definition at line 374 of file file.hpp.

Member Enumeration Documentation

◆ FileStatus

Enumerator
FileNotModified 
FileModified 
FileWasNotSaved 
FileRemoved 

Definition at line 359 of file file.hpp.

◆ Mode

Enumerator
ReadOnly 
ReadWrite 

Definition at line 354 of file file.hpp.

354 {
355 ReadOnly,
357 };

Constructor & Destructor Documentation

◆ ~OksFile()

dunedaq::oks::OksFile::~OksFile ( )
noexcept

Definition at line 295 of file file.cpp.

296{
298}
void clear_comments() noexcept
Definition file.cpp:285

◆ OksFile() [1/3]

dunedaq::oks::OksFile::OksFile ( const std::string & s,
const std::string & ln,
const std::string & ft,
const std::string & ff,
OksKernel * k )
private

Definition at line 257 of file file.cpp.

257 :
258 p_short_name (s),
259 p_full_name (s),
260 p_logical_name (ln),
261 p_type (ft),
262 p_oks_format (ff),
264 p_size (0),
266 p_creation_time (boost::posix_time::second_clock::universal_time()),
269 p_lock (),
270 p_is_updated (false),
271 p_is_read_only (true),
272 p_last_modified (0),
274 p_is_on_disk (false),
275 p_included_by (0),
276 p_kernel (k)
277{
282}
std::shared_ptr< boost::interprocess::file_lock > p_lock
Definition file.hpp:759
std::string p_oks_format
Definition file.hpp:749
time_t p_repository_last_modified
Definition file.hpp:765
std::string p_created_by
Definition file.hpp:752
std::string p_short_name
Definition file.hpp:744
boost::posix_time::ptime p_last_modification_time
Definition file.hpp:756
boost::posix_time::ptime p_creation_time
Definition file.hpp:753
std::string p_logical_name
Definition file.hpp:747
std::string p_last_modified_by
Definition file.hpp:755
OksKernel * p_kernel
Definition file.hpp:769
std::string p_full_name
Definition file.hpp:745
std::string p_type
Definition file.hpp:748
void create_lock_name()
Definition file.cpp:959
void check_repository()
Definition file.cpp:920
const OksFile * p_included_by
Definition file.hpp:768
std::string p_last_modified_on
Definition file.hpp:757
std::string p_created_on
Definition file.hpp:754
static std::string & get_host_name()
Get hostname of given process.
Definition kernel.cpp:434
static std::string & get_user_name()
Get username of given process.
Definition kernel.cpp:473

◆ OksFile() [2/3]

dunedaq::oks::OksFile::OksFile ( const OksFile & f)
inlineprivate

Definition at line 793 of file file.hpp.

◆ OksFile() [3/3]

dunedaq::oks::OksFile::OksFile ( std::shared_ptr< OksXmlInputStream > xmls,
const std::string & sp,
const std::string & fp,
OksKernel * k )
private

Definition at line 349 of file file.cpp.

349 :
350 p_short_name (sp),
351 p_full_name (fp),
353 p_creation_time (boost::posix_time::not_a_date_time),
354 p_last_modification_time (boost::posix_time::not_a_date_time),
355 p_lock (),
356 p_is_updated (false),
357 p_is_read_only (true),
358 p_last_modified (0),
360 p_is_on_disk (true),
361 p_included_by (0),
362 p_kernel (k)
363{
364 const char * fname = "OksFile::OksFile()";
365
366// create_lock_name();
368
369
370 // check file header
371
372 try {
373
374 // skip an xml file header
375
376 {
377 const char * tag_start = xmls->get_tag_start();
378
379 if(!oks::cmp_str4(tag_start, "?xml")) {
380 xmls->throw_unexpected_tag(tag_start, "?xml");
381 }
382 }
383
384 // skip possible tag attributes
385
386 while(true) {
387 OksXmlAttribute attr(*xmls);
388
389 // check for close of tag
390
391 if(
392 oks::cmp_str1(attr.name(), ">") ||
393 oks::cmp_str1(attr.name(), "/") ||
394 oks::cmp_str1(attr.name(), "?")
395 ) { break; }
396
397 // check for start of info tag
398
399 if(!oks::cmp_str7(attr.name(), "version") && !oks::cmp_str8(attr.name(), "encoding")) {
400 if(!p_kernel->get_silence_mode()) {
401 Oks::warning_msg(fname) << " Unexpected attribute \'" << attr.name() << "\' in the xml file header. Either \'version\' or \'encoding\' is expected.\n";
402 }
403 }
404 }
405
406 }
407 catch (oks::exception & e) {
408 throw oks::FailedRead("an xml file header", e);
409 }
410 catch (std::exception & e) {
411 throw oks::FailedRead("an xml file header", e.what());
412 }
413
414
415 // skip document type declaration and comments
416
417 {
418 while(true) {
419 try {
420 const char * dtd = xmls->get_tag();
421 const char dtd_start[] = "<!DOCTYPE ";
422 if(!strncmp(dtd_start, dtd, sizeof(dtd_start) - 1)) {
423 break;
424 }
425 }
426 catch (oks::exception & e) {
427 throw oks::FailedRead("DTD section", e);
428 }
429 catch (std::exception & e) {
430 throw oks::FailedRead("DTD section", e.what());
431 }
432 }
433 }
434
435
436 // skip
437
438 try {
439 xmls->store_position();
440
441 const char * a_tag = xmls->get_tag();
442
443 const char xml_stylesheet_start[] = "<?xml-stylesheet";
444 if(strncmp(xml_stylesheet_start, a_tag, sizeof(xml_stylesheet_start) - 1)) {
445 xmls->restore_position();
446 }
447 }
448 catch (oks::exception & e) {
449 throw oks::FailedRead("an xml-stylesheet tag or an oks tag", e);
450 }
451 catch (std::exception & e) {
452 throw oks::FailedRead("an xml-stylesheet tag or an oks tag", e.what());
453 }
454
455 // read start-of-tag
456
457 try {
458 xmls->get_tag_start();
459 }
460 catch (oks::exception & e) {
461 throw oks::FailedRead("tag", e);
462 }
463 catch (std::exception & e) {
464 throw oks::FailedRead("tag", e.what());
465 }
466
467
468 // read info tag
469
470 try {
471 const char * tag_start = xmls->get_tag_start();
472
473 if(!oks::cmp_str4(tag_start, xml_info_tag)) {
474 xmls->throw_unexpected_tag(tag_start, xml_info_tag);
475 }
476 }
477 catch (oks::exception & e) {
478 throw oks::FailedRead("<info> tag", e);
479 }
480 catch (std::exception & e) {
481 throw oks::FailedRead("<info> tag", e.what());
482 }
483
484
485 std::string oks_version;
486
487
488 // read class attributes
489
490 try {
491 while(true) {
492 OksXmlAttribute attr(*xmls);
493
494 // check for close of tag
495
496 if(oks::cmp_str1(attr.name(), ">") || oks::cmp_str1(attr.name(), "/")) { break; }
497
498 // check for known info' attributes
499
500 else if(oks::cmp_str4 (attr.name(), "name")) p_logical_name.assign(attr.value(), attr.value_len());
501 else if(oks::cmp_str4 (attr.name(), "type")) p_type.assign(attr.value(), attr.value_len());
502 else if(oks::cmp_str10(attr.name(), "oks-format")) p_oks_format.assign(attr.value(), attr.value_len());
503 else if(oks::cmp_str10(attr.name(), "created-by")) p_created_by.assign(attr.value(), attr.value_len());
504 else if(oks::cmp_str10(attr.name(), "created-on")) p_created_on.assign(attr.value(), attr.value_len());
505 else if(oks::cmp_str11(attr.name(), "oks-version")) oks_version.assign(attr.value(), attr.value_len());
506 else if(oks::cmp_str12(attr.name(), "num-of-items")) p_number_of_items = atol(attr.value());
507 else if(oks::cmp_str13(attr.name(), "creation-time")) p_creation_time = oks::str2time(attr.value(), attr.value_len(), p_full_name.c_str());
508 else if(oks::cmp_str15(attr.name(), "num-of-includes")) continue; // ignore this value
509 else if(oks::cmp_str16(attr.name(), "last-modified-by")) p_last_modified_by.assign(attr.value(), attr.value_len());
510 else if(oks::cmp_str16(attr.name(), "last-modified-on")) p_last_modified_on.assign(attr.value(), attr.value_len());
511 else if(!strcmp(attr.name(), "last-modification-time")) p_last_modification_time = oks::str2time(attr.value(), attr.value_len(), p_full_name.c_str());
512 else {
513 p_oks_format.clear();
514 xmls->throw_unexpected_tag(attr.name(), "info\'s tags");
515 }
516 }
517 }
518 catch(oks::exception & e) {
519 throw oks::FailedRead("<info> tag attribute", e);
520 }
521 catch (std::exception & e) {
522 throw oks::FailedRead("<info> tag attribute", e.what());
523 }
524
525 // read include and comments sections
526 while(true) {
527 xmls->store_position();
528
529 bool read_include = false;
530
531 try {
532 const char * tag_start = xmls->get_tag_start();
533 if(oks::cmp_str7(tag_start, xml_include_tag)) { read_include = true; }
534 else if(oks::cmp_str8(tag_start, xml_comments_tag)) { read_include = false; }
535 else { xmls->restore_position(); return; }
536 }
537 catch (oks::exception & e) {
538 throw oks::FailedRead("a tag", e);
539 }
540 catch (std::exception & e) {
541 throw oks::FailedRead("a tag", e.what());
542 }
543
544 if(read_include) {
545 while(true) {
546 try {
547
548 {
549 const char * tag_start = xmls->get_tag_start();
550 if(!oks::cmp_str4(tag_start, xml_file_tag)) {
551 if(!oks::cmp_str8(tag_start, "/include")) { xmls->throw_unexpected_tag(tag_start + 1, xml_file_tag); }
552 break;
553 }
554 }
555
556 while(true) {
557 OksXmlAttribute attr(*xmls);
558
559 // check for close of tag
560 if(oks::cmp_str1(attr.name(), ">") || oks::cmp_str1(attr.name(), "/")) { break; }
561
562 // check for known info' attributes
563 else if(oks::cmp_str4(attr.name(), "path")) p_list_of_include_files.push_back(std::string(attr.value(),attr.value_len()));
564 else {
565 p_oks_format.clear();
566 xmls->throw_unexpected_tag(attr.name(), "path");
567 }
568 }
569
570 }
571 catch (oks::exception & e) {
572 throw oks::FailedRead("<file> tag", e);
573 }
574 catch (std::exception & e) {
575 throw oks::FailedRead("<file> tag", e.what());
576 }
577 }
578 }
579 else {
580 while(true) {
581 try {
582
583 {
584 const char * tag_start = xmls->get_tag_start();
585 if(!oks::cmp_str7(tag_start, xml_comment_tag)) {
586 if(!oks::cmp_str9(tag_start, "/comments")) { xmls->throw_unexpected_tag(tag_start, xml_comment_tag); }
587 return;
588 }
589 }
590
591 std::unique_ptr<oks::Comment> comment( new oks::Comment() );
592 std::string creation_time;
593
594 while(true) {
595 OksXmlAttribute attr(*xmls);
596
597 // check for close of tag
598 if(oks::cmp_str1(attr.name(), ">") || oks::cmp_str1(attr.name(), "/")) { break; }
599
600 // check for known info' attributes
601 else if(oks::cmp_str4 (attr.name(), "text")) comment->p_text.assign(attr.value(), attr.value_len());
602 else if(oks::cmp_str6 (attr.name(), "author")) comment->p_author.assign(attr.value(), attr.value_len());
603 else if(oks::cmp_str10(attr.name(), "created-by")) comment->p_created_by.assign(attr.value(), attr.value_len());
604 else if(oks::cmp_str10(attr.name(), "created-on")) comment->p_created_on.assign(attr.value(), attr.value_len());
605 else if(oks::cmp_str13(attr.name(), "creation-time")) creation_time.assign(attr.value(), attr.value_len());
606 else {
607 p_oks_format.clear();
608 xmls->throw_unexpected_tag(attr.name(), "comment\'s tags");
609 }
610 }
611
612 comment->validate(creation_time);
613
614 if(p_comments.find(creation_time) != p_comments.end()) {
615 std::ostringstream text;
616 text << "The comment created at \'" << creation_time << "\' was already defined in this file";
617 throw std::runtime_error(text.str().c_str());
618 }
619
620 p_comments[creation_time] = comment.release();
621
622 }
623 catch (oks::exception & e) {
624 throw oks::FailedRead("<comment>", e);
625 }
626 catch (std::exception & e) {
627 throw oks::FailedRead("<comment>", e.what());
628 }
629 }
630 }
631 }
632}
static const char xml_comments_tag[]
Definition file.hpp:777
std::map< std::string, oks::Comment * > p_comments
Definition file.hpp:766
std::list< std::string > p_list_of_include_files
Definition file.hpp:763
static const char xml_file_tag[]
Definition file.hpp:776
static const char xml_comment_tag[]
Definition file.hpp:778
static const char xml_include_tag[]
Definition file.hpp:775
static const char xml_info_tag[]
Definition file.hpp:774
static std::ostream & warning_msg(const char *)
Definition kernel.cpp:568
bool cmp_str8(const char *s1, const char s2[9])
Definition cstring.hpp:66
bool cmp_str1(const char *s1, const char s2[2])
Definition cstring.hpp:14
bool cmp_str4(const char *s1, const char s2[5])
Definition cstring.hpp:34
bool cmp_str6(const char *s1, const char s2[7])
Definition cstring.hpp:50
bool cmp_str10(const char *s1, const char s2[11])
Definition cstring.hpp:78
bool cmp_str9(const char *s1, const char s2[10])
Definition cstring.hpp:74
boost::posix_time::ptime str2time(const char *value, size_t len, const char *file_name=nullptr)
const char _empty_str[]
Definition file.cpp:347
bool cmp_str13(const char *s1, const char s2[14])
Definition cstring.hpp:90
bool cmp_str16(const char *s1, const char s2[17])
Definition cstring.hpp:102
bool cmp_str15(const char *s1, const char s2[16])
Definition cstring.hpp:98
bool cmp_str11(const char *s1, const char s2[12])
Definition cstring.hpp:82
bool cmp_str12(const char *s1, const char s2[13])
Definition cstring.hpp:86
bool cmp_str7(const char *s1, const char s2[8])
Definition cstring.hpp:58

Member Function Documentation

◆ add_comment()

void dunedaq::oks::OksFile::add_comment ( const std::string & text,
const std::string & author )

Add new comment to file.

The method allows to add text description to the file.

Parameters
textthe description; must not be empty
authorthe author's description, e.g. first and last names, the e-mail address, etc.
Exceptions
Incase of problems (e.g. empty text) the method throws oks::exception.

Definition at line 674 of file file.cpp.

675{
676 try {
677 lock();
678 }
679 catch(oks::exception& ex) {
680 throw oks::FailedAddComment(*this, ex);
681 }
682
683 boost::posix_time::ptime now = boost::posix_time::second_clock::universal_time();
684 std::string creation_time = boost::posix_time::to_iso_string(now);
685
686 try {
687 std::unique_ptr<oks::Comment> comment( new oks::Comment(text, author) );
688 comment->validate();
689
690 oks::Comment *& x(p_comments[creation_time]);
691
692 if(x == 0)
693 {
694 x = comment.release();
695 }
696 else
697 {
698 x->p_text += "\n-----\n";
699 x->p_text.append(text);
700 }
701 }
702 catch(std::exception & ex) {
703 throw oks::FailedAddComment(*this, ex.what());
704 }
705
706 p_is_updated = true;
707}
void lock()
Lock OKS file.
Definition file.cpp:1017
static int64_t now()

◆ add_include_file()

void dunedaq::oks::OksFile::add_include_file ( const std::string & name)

Add include file.

The method adds include file to given one. To load included file it is necessary to save the file and load it again.

Parameters
namename of include file to be added
Exceptions
Incase of problems (e.g. cannot lock file) the method throws oks::exception.

Definition at line 1199 of file file.cpp.

1200{
1201 // check if the file was already included
1202 if(find_include_file(s) != p_list_of_include_files.end()) { return; }
1203
1204 // lock file
1205 try {
1206 lock();
1207 }
1208 catch(oks::exception& ex) {
1209 throw oks::FailedAddInclude(*this, s, ex);
1210 }
1211
1212 // try to find path to include and load it
1213 try {
1214 std::string path = p_kernel->get_file_path(s, this);
1215 p_kernel->k_load_file(path, true, this, 0);
1216 }
1217 catch(std::exception& ex) {
1218 throw oks::FailedAddInclude(*this, s, ex.what());
1219 }
1220
1221 // add include and mark file as updated
1222 p_list_of_include_files.push_back(s);
1223 p_is_updated = true;
1224}
std::list< std::string >::iterator find_include_file(const std::string &)
Definition file.cpp:1177

◆ check_parent()

OksFile * dunedaq::oks::OksFile::check_parent ( const OksFile * parent_h)

Set given parent, if this file is not yet included.

Parameters
parent_hparent file
Returns
this file.

Definition at line 1188 of file file.cpp.

1189{
1190 if(p_included_by == 0 && parent_h != 0) {
1191 p_included_by = parent_h;
1192 }
1193
1194 return this;
1195}

◆ check_repository()

void dunedaq::oks::OksFile::check_repository ( )
private

Definition at line 920 of file file.cpp.

921{
922// if(size_t len = OksKernel::get_repository_root().size()) {
923// if(p_full_name.size() > len && p_full_name.substr(0, len) == OksKernel::get_repository_root()) {
924// p_repository = GlobalRepository;
925// p_repository_name = p_full_name.substr(len + 1);
926// TLOG_DEBUG( 3 ) << "file \'" << p_full_name << "\' comes from user repository [\'" << p_repository_name << "\']";
927// return;
928// }
929// }
930
931 const std::string& s(p_kernel->get_user_repository_root());
932 if(size_t len = s.size()) {
933 if(p_full_name.size() > len && p_full_name.substr(0, len) == s) {
934// p_repository = UserRepository;
935 p_repository_name = p_full_name.substr(len + 1);
936
937 TLOG_DEBUG( 3 ) << "file \'" << p_full_name << "\' comes from user repository [\'" << p_repository_name << "\']" ;
938 return;
939
940// std::string repository_file = OksKernel::get_repository_root() + '/' + p_repository_name;
941//
942// struct stat buf;
943// if(stat(repository_file.c_str(), &buf) == 0) {
944// TLOG_DEBUG( 3 ) << "file \'" << p_full_name << "\' comes from user repository [\'" << p_repository_name << "\']" ;
945// return;
946// }
947// else {
948// TLOG_DEBUG( 3 ) << "file \'" << p_full_name << "\' is found in user repository [\'" << p_repository_name << "\'] but does not exist in global one [\'" << OksKernel::get_repository_root() << "\']" ;
949// }
950 }
951 }
952
953 TLOG_DEBUG( 3 ) << "file \'" << p_full_name << "\' is not in a repository" ;
954// p_repository = NoneRepository;
955 p_repository_name.clear();
956}
std::string p_repository_name
Definition file.hpp:746
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112

◆ clear_comments()

void dunedaq::oks::OksFile::clear_comments ( )
privatenoexcept

Definition at line 285 of file file.cpp.

286{
287 for(std::map<std::string, oks::Comment *>::iterator i = p_comments.begin(); i != p_comments.end(); ++i) {
288 delete i->second;
289 }
290
291 p_comments.clear();
292}

◆ compare() [1/2]

bool dunedaq::oks::OksFile::compare ( const char * file1_name,
const char * file2_name )
static

Compare two files.

Parameters
file1_namename of first file
file2_namename of second file
Returns
false => files are different; true => files are equal
Exceptions
Incase of problems the method throws oks::exception.

Definition at line 1519 of file file.cpp.

1520{
1521 std::ifstream f1(file1_name);
1522 std::ifstream f2(file2_name);
1523
1524 if (!f1)
1525 {
1526 throw oks::FileCompareError(file1_name, file2_name, "cannot open file");
1527 }
1528
1529 if (!f2)
1530 {
1531 throw oks::FileCompareError(file2_name, file1_name, "cannot open file");
1532 }
1533
1534 std::string line1;
1535 std::string line2;
1536
1537 while (true)
1538 {
1539 if (f1.eof() && f2.eof())
1540 {
1541 return true;
1542 }
1543
1544 std::getline(f1, line1);
1545 std::getline(f2, line2);
1546
1547 if ((line1 != line2) && (is_not_info(line1) || is_not_info(line2)))
1548 {
1549 return false;
1550 }
1551 }
1552
1553 return true;
1554}
static bool is_not_info(const std::string &line)
Definition file.cpp:1504

◆ compare() [2/2]

bool dunedaq::oks::OksFile::compare ( const char * name) const
inline

Compare with another file.

Parameters
namename of file to compare with
Returns
false => files are different; true => files are equal
Exceptions
Incase of problems the method throws oks::exception.

Definition at line 713 of file file.hpp.

713{ return compare(p_full_name.c_str(), name); }
static bool compare(const char *file1_name, const char *file2_name)
Compare two files.
Definition file.cpp:1519

◆ create_lock_name()

void dunedaq::oks::OksFile::create_lock_name ( )
private

Definition at line 959 of file file.cpp.

960{
962
963 std::string::size_type idx = p_lock_file_name.find_last_of('/');
964 if(idx == std::string::npos) p_lock_file_name = "./";
965 else p_lock_file_name.erase(idx+1);
966
967 const char _prefix_str[] = ".oks-lock-";
968 p_lock_file_name.append(_prefix_str, sizeof(_prefix_str)-1);
969
970 idx = p_full_name.find_last_of('/');
971 std::string::size_type len(p_full_name.size());
972 if(idx == std::string::npos) {
973 idx = 0;
974 }
975 else {
976 idx++;
977 len -= idx;
978 }
979
980 p_lock_file_name.append(p_full_name, idx, len);
981 p_lock_file_name.append(".txt");
982}
std::string p_lock_file_name
Definition file.hpp:760

◆ find_include_file()

std::list< std::string >::iterator dunedaq::oks::OksFile::find_include_file ( const std::string & s)
private

Definition at line 1177 of file file.cpp.

1178{
1179 std::list<std::string>::iterator i = p_list_of_include_files.begin();
1180
1181 for(;i != p_list_of_include_files.end(); ++i) {if(*i == s) break;}
1182
1183 return i;
1184}

◆ get_all_include_files()

void dunedaq::oks::OksFile::get_all_include_files ( const OksKernel * kernel,
std::set< OksFile * > & out )

Get all include files.

Parameters
kernelOKS kernel
outset of all files included explicitly and implicitly by given file

Definition at line 1471 of file file.cpp.

1472{
1473 const OksFile::Map & schema_files = kernel->schema_files();
1474 const OksFile::Map & data_files = kernel->data_files();
1475
1476 for(std::list<std::string>::iterator i = p_list_of_include_files.begin(); i != p_list_of_include_files.end(); ++i) {
1477 std::string s = kernel->get_file_path(*i, this);
1478 if(s.size()) {
1479 OksFile::Map::const_iterator j = data_files.find(&s);
1480 if(j != data_files.end()) {
1481 if(out.find(j->second) != out.end()) { continue; }
1482 out.insert(j->second);
1483 }
1484 else {
1485 j = schema_files.find(&s);
1486 if(j != schema_files.end()) {
1487 if(out.find(j->second) != out.end()) { continue; }
1488 out.insert(j->second);
1489 }
1490 else {
1491 continue;
1492 }
1493 }
1494
1495 j->second->get_all_include_files(kernel, out);
1496 }
1497 }
1498}
std::map< const std::string *, OksFile *, SortByName > Map
Definition file.hpp:372
FELIX Initialization std::string initerror FELIX queue timed out

◆ get_comment()

oks::Comment * dunedaq::oks::OksFile::get_comment ( const std::string & creation_time)
privatenoexcept

Definition at line 710 of file file.cpp.

711{
712 std::map<std::string, oks::Comment *>::const_iterator i = p_comments.find(creation_time);
713 return (i == p_comments.end()) ? 0: i->second;
714}

◆ get_comments()

const std::map< std::string, oks::Comment * > & dunedaq::oks::OksFile::get_comments ( ) const
inline

Get all comments of file.

Definition at line 494 of file file.hpp.

494{return p_comments;}

◆ get_created_by()

const std::string & dunedaq::oks::OksFile::get_created_by ( ) const
inline

Return name of user who created this file.

Definition at line 588 of file file.hpp.

588{return p_created_by;}

◆ get_created_on()

const std::string & dunedaq::oks::OksFile::get_created_on ( ) const
inline

Return name of host where the file was created.

Definition at line 598 of file file.hpp.

598{return p_created_on;}

◆ get_creation_time()

const boost::posix_time::ptime dunedaq::oks::OksFile::get_creation_time ( ) const
inline

Return timestamp when file was created.

Definition at line 593 of file file.hpp.

593{return p_creation_time;}

◆ get_full_file_name()

const std::string & dunedaq::oks::OksFile::get_full_file_name ( ) const
inline

Return real file name (replacing all software links)

Definition at line 528 of file file.hpp.

528{return p_full_name;}

◆ get_include_files()

const std::list< std::string > & dunedaq::oks::OksFile::get_include_files ( ) const
inline

Get directly include files.

Returns
list of file names included by given file

Definition at line 645 of file file.hpp.

◆ get_last_modification_time()

const boost::posix_time::ptime dunedaq::oks::OksFile::get_last_modification_time ( ) const
inline

Return timestamp when file was modified last time using OKS tools.

Definition at line 608 of file file.hpp.

◆ get_last_modified_by()

const std::string & dunedaq::oks::OksFile::get_last_modified_by ( ) const
inline

Return name of user who modified the file last time using OKS tools.

Definition at line 603 of file file.hpp.

603{return p_last_modified_by;}

◆ get_last_modified_on()

const std::string & dunedaq::oks::OksFile::get_last_modified_on ( ) const
inline

Return name of host where the file was modified last time using OKS tools.

Definition at line 613 of file file.hpp.

613{return p_last_modified_on;}

◆ get_lock_file()

const std::string & dunedaq::oks::OksFile::get_lock_file ( ) const
inline

Definition at line 570 of file file.hpp.

570{ init_lock_name(); return p_lock_file_name; }
void init_lock_name() const
Definition file.hpp:803

◆ get_lock_string()

bool dunedaq::oks::OksFile::get_lock_string ( std::string & info) const

Return lock status of OKS file and if the file is locked, get information string about process which locked the file.

The method method also works for files locked by external process contrary to the is_locked() method.

Parameters
infoout parameter containing info about process locked this file
Returns
true, if the file is locked and false otherwise

Definition at line 989 of file file.cpp.

990{
992
993 lock_file_contents.clear();
994
995 const char * lock_name = p_lock_file_name.c_str();
996 struct stat buf;
997
998 if(stat(lock_name, &buf) == 0) {
999 std::ifstream f(lock_name);
1000
1001 if(f.good()) {
1002 std::getline(f, lock_file_contents);
1003 return true;
1004 }
1005 else {
1006 lock_file_contents = "unknown [cannot read lock file \'";
1007 lock_file_contents += p_lock_file_name;
1008 lock_file_contents += "\']";
1009 return true;
1010 }
1011 }
1012
1013 return false;
1014}

◆ get_logical_name()

const std::string & dunedaq::oks::OksFile::get_logical_name ( ) const
inline

Definition at line 571 of file file.hpp.

571{return p_logical_name;}

◆ get_number_of_items()

long dunedaq::oks::OksFile::get_number_of_items ( ) const
inline

Return number of classes in schema file, or number of objects in data file.

Definition at line 578 of file file.hpp.

578{return p_number_of_items;}

◆ get_oks_format()

const std::string & dunedaq::oks::OksFile::get_oks_format ( ) const
inline

Definition at line 573 of file file.hpp.

573{return p_oks_format;}

◆ get_parent()

const OksFile * dunedaq::oks::OksFile::get_parent ( ) const
inline

Return parent including given file.

Returns
parent file; can be NULL.

Definition at line 722 of file file.hpp.

722{ return p_included_by; }

◆ get_repository_name()

const std::string & dunedaq::oks::OksFile::get_repository_name ( ) const
inline

Get name of file inside repository.

For example, if TDAQ_DB_REPOSITORY="/db/v15", and the file name is "/db/v15/common/params.data.xml", the file's repository name will be "common/params.data.xml".

Definition at line 555 of file file.hpp.

555{return p_repository_name;}

◆ get_short_file_name()

const std::string & dunedaq::oks::OksFile::get_short_file_name ( ) const
inline

Definition at line 523 of file file.hpp.

523{return p_short_name;}

◆ get_size()

long dunedaq::oks::OksFile::get_size ( ) const
inline

Return length of file in bytes.

Definition at line 583 of file file.hpp.

583{return p_size;}

◆ get_status_of_file()

OksFile::FileStatus dunedaq::oks::OksFile::get_status_of_file ( ) const

Return update status of file.

The file can be in one of the following states:

  • NotModified - file is not modified
  • FileModified - file is modified in memory
  • FileWasNotSaved - file was created, but not saved yet (only exists in memory)
  • FileRemoved - file was removed by external process after load in memory
  • FileRepositoryModified - repository file was modified (given file comes from user repository)
  • FileRepositoryRemoved - repository file was removed (given file comes from user repository)

Definition at line 1418 of file file.cpp.

1419{
1420 if(p_is_on_disk == false) {
1421 return FileWasNotSaved;
1422 }
1423 else {
1424 struct stat buf;
1425
1426 if(stat(p_full_name.c_str(), &buf) != 0) {
1427 return FileRemoved;
1428 }
1429
1430 if(buf.st_mtime != p_last_modified) {
1431 return FileModified;
1432 }
1433
1434// if(get_repository() == UserRepository) {
1435// std::string full_repository_name = make_repository_name();
1436//
1437// if(!full_repository_name.empty()) {
1438// if(stat(full_repository_name.c_str(), &buf) != 0) {
1439// return FileRepositoryRemoved;
1440// }
1441//
1442// if(buf.st_mtime != p_repository_last_modified) {
1443// return FileRepositoryModified;
1444// }
1445// }
1446// }
1447 }
1448
1449 return FileNotModified;
1450}

◆ get_type()

const std::string & dunedaq::oks::OksFile::get_type ( ) const
inline

Definition at line 572 of file file.hpp.

572{return p_type;}

◆ get_well_formed_name()

const std::string & dunedaq::oks::OksFile::get_well_formed_name ( ) const
inline

Get well-formed file name.

In particular, strip repository root prefix.

Definition at line 565 of file file.hpp.

566 {
567 return (!p_repository_name.empty() ? p_repository_name : p_full_name);
568 }

◆ init_lock_name()

void dunedaq::oks::OksFile::init_lock_name ( ) const
inlineprivate

Definition at line 803 of file file.hpp.

803{ if(p_lock_file_name.empty()) const_cast<OksFile*>(this)->create_lock_name(); }
OksFile(const std::string &, const std::string &, const std::string &, const std::string &, OksKernel *)
Definition file.cpp:257

◆ is_locked()

bool dunedaq::oks::OksFile::is_locked ( ) const
inline

Return lock status of OKS file.

Returns
true, if the file is locked by given process and false otherwise

Definition at line 621 of file file.hpp.

621{return (p_lock != nullptr);}

◆ is_read_only()

bool dunedaq::oks::OksFile::is_read_only ( ) const
inline

Return read-only status of OKS file.

Returns
true, if the file is read-only for given process and false otherwise

Definition at line 637 of file file.hpp.

637{return p_is_read_only;}

◆ is_repository_file()

bool dunedaq::oks::OksFile::is_repository_file ( ) const

Get information about repository, the file belongs to.

If the global database repository is set (e.g. TDAQ_DB_REPOSITORY is defined) and the file is stored on it, the method returns GlobalRepository. If the user database repository is set (e.g. TDAQ_DB_USER_REPOSITORY is defined) and the file is stored on it, the method returns UserRepository. Otherwise file does not belong to any repository and the method returns NoneRepository.

The file's repository can be changed using OksKernel::checkout() and OksKernel::release() methods: they move file between global and user repositories. If file is removed in global repository, while it is stored on the user one, its repository should be changed using unset_repository() method.

Definition at line 914 of file file.cpp.

915{
916 return (!p_kernel->get_user_repository_root().empty());
917}

◆ is_updated()

bool dunedaq::oks::OksFile::is_updated ( ) const
inline

Return update status of OKS file.

Returns
true, if the file is updated by given process and false otherwise

Definition at line 629 of file file.hpp.

629{return p_is_updated;}

◆ lock()

void dunedaq::oks::OksFile::lock ( )

Lock OKS file.

The method locks OKS file: for file .../file.xml the lock file .../.oks-file.xml is created. The lock file contains information about process locking it and remains exclusive while process created it is alive.

Exceptions
Incase of problems (e.g. file was already locked) the method throws oks::exception.

Definition at line 1017 of file file.cpp.

1018{
1019 if (p_nolock_mode) {
1020 // Nasty hack to allow code generation on the fly without locking
1021 // file. Just return without locking!
1022 return;
1023 }
1024
1026
1027 if (p_lock != nullptr)
1028 {
1029 return;
1030 }
1031
1032
1033 // check that the file is not read-only
1034
1035 {
1036 struct stat buf;
1037
1038 if (stat(p_full_name.c_str(), &buf) == 0)
1039 {
1040 if (OksKernel::check_read_only(this) == true)
1041 {
1042 throw oks::FileLockError(*this, true, "file is read-only");
1043 }
1044 }
1045 }
1046
1047
1048 // check lock and report problem if it exists and cannot be reset
1049
1050 {
1051 std::string lock_file_contents;
1052
1053 if (get_lock_string(lock_file_contents))
1054 {
1055 try
1056 {
1057 boost::interprocess::file_lock lock(p_lock_file_name.c_str());
1058
1059 if (lock.try_lock() == false)
1060 {
1061 std::ostringstream text;
1062 text << "file is already locked by \"" << lock_file_contents << '\"';
1063 throw oks::FileLockError(*this, true, text.str());
1064 }
1065 else
1066 {
1067 try
1068 {
1069 lock.unlock();
1070 }
1071 catch (const boost::interprocess::interprocess_exception& ex)
1072 {
1073 std::ostringstream text;
1074 text << "boost::interprocess::unlock() failed: \"" << ex.what() << '\"';
1075 throw oks::FileLockError(*this, false, text.str());
1076 }
1077
1078 if (!p_kernel->get_silence_mode())
1079 {
1080 Oks::warning_msg("OksFile::lock()") << "Remove obsolete lock of file \'" << p_full_name << "\' created by \n\"" << lock_file_contents << "\"\n";
1081 }
1082
1083 if (unlink(p_lock_file_name.c_str()) != 0)
1084 {
1085 std::ostringstream text;
1086 text << "failed to remove lock file \'" << p_lock_file_name << "\':\n" << oks::strerror(errno);
1087 throw oks::FileLockError(*this, false, text.str());
1088 }
1089 }
1090 }
1091 catch (const boost::interprocess::interprocess_exception& ex)
1092 {
1093 std::ostringstream text;
1094 text << "boost::interprocess::try_lock() failed: \"" << ex.what() << '\"';
1095 throw oks::FileLockError(*this, false, text.str());
1096 }
1097 }
1098 }
1099
1100
1101 // write lock file
1102
1103 {
1104 std::ofstream f(p_lock_file_name.c_str());
1105
1106 if (!f.good())
1107 {
1108 std::ostringstream text;
1109 text << "failed to create lock file \'" << p_lock_file_name << '\'';
1110 throw oks::FileLockError(*this, true, text.str());
1111 }
1112
1113 try
1114 {
1115 boost::posix_time::ptime now(boost::posix_time::second_clock::universal_time());
1116 f << "process " << getpid() << " on " << OksKernel::get_host_name() << " started by " << OksKernel::get_user_name() << " at " << boost::posix_time::to_simple_string(now) << " (UTC)" << std::endl;
1117 f.flush();
1118 f.close();
1119 }
1120 catch (std::exception& ex)
1121 {
1122 std::ostringstream text;
1123 text << "failed to write lock file \'" << p_lock_file_name << "\': " << ex.what();
1124 throw oks::FileLockError(*this, true, text.str());
1125 }
1126 }
1127
1128
1129 try
1130 {
1131 p_lock.reset(new boost::interprocess::file_lock(p_lock_file_name.c_str()));
1132
1133 if (p_lock->try_lock() == false)
1134 {
1135 throw std::runtime_error("file is locked by another process");
1136 }
1137 }
1138 catch (const std::exception& ex)
1139 {
1140 std::ostringstream text;
1141 text << "boost::interprocess::try_lock() failed: \"" << ex.what() << '\"';
1142 p_lock.reset();
1143 throw oks::FileLockError(*this, false, text.str());
1144 }
1145}
bool get_lock_string(std::string &info) const
Return lock status of OKS file and if the file is locked, get information string about process which ...
Definition file.cpp:989
static bool p_nolock_mode
Definition file.hpp:742
static bool check_read_only(OksFile *f)
Check if the OKS file is read-only.
Definition kernel.cpp:1537
const std::string strerror(int error)
Convert C error number to string.
Definition kernel.cpp:119

◆ make_repository_name()

std::string dunedaq::oks::OksFile::make_repository_name ( ) const
private

Definition at line 1383 of file file.cpp.

1384{
1385 std::string name;
1386
1387 if(!OksKernel::get_repository_root().empty()) {
1389 }
1390
1391 return name;
1392}
const std::string & get_repository_name() const
Get name of file inside repository.
Definition file.hpp:555
static const std::string & get_repository_root()
Get OKS repository root.
Definition kernel.cpp:309

◆ modify_comment()

void dunedaq::oks::OksFile::modify_comment ( const std::string & creation_time,
const std::string & text,
const std::string & author )

/brief Modify existing comment.

The method allows to modify already existing comment.

/param creation_time the time-stamp when comment was created ( returned by get_comments() ) /param text the description; must not be empty /param author the author's description, e.g. first and last names, the e-mail address, etc.

Exceptions
Incase of problems (e.g. empty text, cannot find comment created at given time) the method throws oks::exception.

Definition at line 717 of file file.cpp.

718{
719 if(oks::Comment * comment = get_comment(creation_time)) {
720 try {
721 lock();
722 }
723 catch(oks::exception& ex) {
724 throw oks::FailedChangeComment(*this, creation_time, ex);
725 }
726
727 std::string saved_author(comment->p_author);
728 std::string saved_text(comment->p_text);
729
730 comment->p_author = author;
731 comment->p_text = text;
732
733 try {
734 comment->validate();
735 }
736 catch(std::exception& ex) {
737 comment->p_author = saved_author;
738 comment->p_text = saved_text;
739 throw oks::FailedChangeComment(*this, creation_time, ex.what());
740 }
741
742 p_is_updated = true;
743 }
744 else {
745 throw oks::FailedChangeComment(*this, creation_time, "cannot find comment");
746 }
747}
oks::Comment * get_comment(const std::string &creation_time) noexcept
Definition file.cpp:710

◆ operator=()

OksFile & dunedaq::oks::OksFile::operator= ( const OksFile & f)
private

Definition at line 301 of file file.cpp.

302{
303 if (&f != this)
304 {
307
308 p_short_name = f.p_short_name;
309 p_full_name = f.p_full_name;
310// p_repository = f.p_repository;
311 p_repository_name = f.p_repository_name;
312 p_logical_name = f.p_logical_name;
313 p_type = f.p_type;
314 p_oks_format = f.p_oks_format;
315 p_number_of_items = f.p_number_of_items;
316 p_size = f.p_size;
317 p_created_by = f.p_created_by;
318 p_creation_time = f.p_creation_time;
319 p_created_on = f.p_created_on;
320 p_last_modified_by = f.p_last_modified_by;
321 p_last_modification_time = f.p_last_modification_time;
322 p_last_modified_on = f.p_last_modified_on;
323 p_open_mode = f.p_open_mode;
324 p_is_updated = f.p_is_updated;
325 p_lock = f.p_lock;
326 p_is_read_only = f.p_is_read_only;
327 p_lock_file_name = f.p_lock_file_name;
328 p_list_of_include_files = f.p_list_of_include_files;
329 p_last_modified = f.p_last_modified;
330 p_repository_last_modified = f.p_repository_last_modified;
331 p_is_on_disk = f.p_is_on_disk;
332 p_included_by = f.p_included_by;
333 p_kernel = f.p_kernel;
334
335 for (const auto& i : f.p_comments)
336 {
337 p_comments[i.first] = new oks::Comment(*i.second);
338 }
339 }
340
341 return *this;
342}

◆ remove_comment()

void dunedaq::oks::OksFile::remove_comment ( const std::string & creation_time)

Modify existing comment.

The method allows to erase already existing comment.

Parameters
creation_timethe timestamp when comment was created ( returned by get_comments() )
Exceptions
Incase of problems (e.g. cannot find comment created at given time, cannot lock file) the method throws oks::exception.

Definition at line 751 of file file.cpp.

752{
753 if(oks::Comment * comment = get_comment(creation_time)) {
754 try {
755 lock();
756 }
757 catch(oks::exception& ex) {
758 throw oks::FailedRemoveComment(*this, creation_time, ex);
759 }
760
761 delete comment;
762 p_comments.erase(creation_time);
763 p_is_updated = true;
764 }
765 else {
766 throw oks::FailedRemoveComment(*this, creation_time, "cannot find comment");
767 }
768}

◆ remove_include_file()

void dunedaq::oks::OksFile::remove_include_file ( const std::string & name)

Remove include file.

The method removes include file from given one. The name of the file to be removed has to be exactly the same as passed to the add_include_file() or as it appears in the include section of OKS xml file. To unload included file it is necessary to save the file and load it again.

Parameters
namename of include file to be removed
Exceptions
Incase of problems (e.g. cannot lock file, include does not exist) the method throws oks::exception.

Definition at line 1228 of file file.cpp.

1229{
1230 // search included file
1231 std::list<std::string>::iterator i = find_include_file(s);
1232
1233 // check if the file was included
1234 if(i == p_list_of_include_files.end()) {
1235 throw oks::FailedRemoveInclude(*this, s, "there is no such include file");
1236 }
1237
1238 // lock file
1239 try {
1240 lock();
1241 }
1242 catch(oks::exception& ex) {
1243 throw oks::FailedRemoveInclude(*this, s, ex);
1244 }
1245
1246 // remove include and mark file as updated
1247 p_list_of_include_files.erase(i);
1248 p_is_updated = true;
1249
1250 // close file, if it is not referenced by others
1251 p_kernel->k_close_dangling_includes();
1252}

◆ rename() [1/2]

void dunedaq::oks::OksFile::rename ( const std::string & full_name)
private

Definition at line 1463 of file file.cpp.

1464{
1465 p_full_name = full_name;
1468}
void update_status_of_file(bool update_local=true, bool update_repository=true)
Update status of file.
Definition file.cpp:1396

◆ rename() [2/2]

void dunedaq::oks::OksFile::rename ( const std::string & short_name,
const std::string & full_name )
private

Definition at line 1454 of file file.cpp.

1455{
1456 p_short_name = short_name;
1457 p_full_name = full_name;
1460}

◆ rename_include_file()

void dunedaq::oks::OksFile::rename_include_file ( const std::string & from,
const std::string & to )

Rename include file.

The method renames already included file. To re-load new file it is necessary to save the file and load it again.

Parameters
fromexact name of already included file
tonew name of include
Exceptions
Incase of problems (e.g. cannot lock file, no such include file) the method throws oks::exception.

Definition at line 1256 of file file.cpp.

1257{
1258 if(old_s == new_s) return;
1259
1260 std::list<std::string>::iterator i1 = find_include_file(old_s);
1261 std::list<std::string>::iterator i2 = find_include_file(new_s);
1262
1263 if(i1 == p_list_of_include_files.end()) {
1264 throw oks::FailedRenameInclude(*this, old_s, new_s, "there is no such include file");
1265 }
1266
1267 if(i2 != p_list_of_include_files.end()) {
1268 throw oks::FailedRenameInclude(*this, old_s, new_s, "file with new name is already included");
1269 }
1270
1271 try {
1272 lock();
1273 }
1274 catch(oks::exception& ex) {
1275 throw oks::FailedRenameInclude(*this, old_s, new_s, ex);
1276 }
1277
1278 (*i1) = new_s;
1279 p_is_updated = true;
1280}

◆ set_logical_name()

void dunedaq::oks::OksFile::set_logical_name ( const std::string & name)

Set logical name of file.

The logical name can be associated by user with the file, e.g. to describe its purpose.

Parameters
namethe logical name
Exceptions
Incase of problems (e.g. cannot lock file) the method throws oks::exception.

Definition at line 1284 of file file.cpp.

1285{
1286 if(s == p_logical_name) return;
1287
1288 try {
1289 lock();
1290 }
1291 catch(oks::exception& ex) {
1292 throw oks::FileChangeError(*this, "set logical name", ex);
1293 }
1294
1295 p_logical_name = s;
1296 p_is_updated = true;
1297}

◆ set_nolock_mode()

void dunedaq::oks::OksFile::set_nolock_mode ( bool nl)
inlinestatic

Definition at line 390 of file file.hpp.

390{p_nolock_mode = nl;}

◆ set_type()

void dunedaq::oks::OksFile::set_type ( const std::string & type)

Set file type.

The type can be associated by user with the file, e.g. to describe its contents.

Parameters
namethe type
Exceptions
Incase of problems (e.g. cannot lock file) the method throws oks::exception.

Definition at line 1301 of file file.cpp.

1302{
1303 if(s == p_type) return;
1304
1305 try {
1306 lock();
1307 }
1308 catch(oks::exception& ex) {
1309 throw oks::FileChangeError(*this, "set type", ex);
1310 }
1311
1312 p_type = s;
1313 p_is_updated = true;
1314}

◆ set_updated()

void dunedaq::oks::OksFile::set_updated ( )
inlineprivate

Definition at line 798 of file file.hpp.

798{p_is_updated = true;} // in-memory

◆ unlock()

void dunedaq::oks::OksFile::unlock ( )

Unlock OKS file.

The method removes lock created by lock() method. check_repository

Exceptions
Incase of problems (e.g. cannot unlink lock file) the method throws oks::exception.

Definition at line 1148 of file file.cpp.

1149{
1150 if (p_lock == nullptr)
1151 return;
1152
1153 try
1154 {
1155 p_lock->unlock();
1156 }
1157 catch (const boost::interprocess::interprocess_exception& ex)
1158 {
1159 std::ostringstream text;
1160 text << "boost::interprocess::unlock() failed: \"" << ex.what() << '\"';
1161 p_lock.reset();
1162 throw oks::FileLockError(*this, false, text.str());
1163 }
1164
1165 p_lock.reset();
1166
1167 if (unlink(p_lock_file_name.c_str()) != 0 && errno != ENOENT)
1168 {
1169 std::ostringstream text;
1170 text << "failed to remove lock file \'" << p_lock_file_name << "\':\n" << oks::strerror(errno);
1171 throw oks::FileLockError(*this, false, text.str());
1172 }
1173}

◆ update_status_of_file()

void dunedaq::oks::OksFile::update_status_of_file ( bool update_local = true,
bool update_repository = true )

Update status of file.

Parameters
update_localif true, update last-modified timestamp of local file
update_repositoryif true, update last-modified timestamp of repository file

Definition at line 1396 of file file.cpp.

1397{
1398 struct stat buf;
1399
1400 if(update_local == true && p_is_on_disk == true) {
1401
1402 if(stat(p_full_name.c_str(), &buf) == 0) {
1403 p_last_modified = buf.st_mtime;
1404 }
1405 }
1406
1407 if(update_repository == true /*&& get_repository() == UserRepository*/) {
1408 std::string full_repository_name = make_repository_name();
1409
1410 if(!full_repository_name.empty() && stat(full_repository_name.c_str(), &buf) == 0) {
1411 p_repository_last_modified = buf.st_mtime;
1412 }
1413 }
1414}
std::string make_repository_name() const
Definition file.cpp:1383

◆ write()

void dunedaq::oks::OksFile::write ( OksXmlOutputStream & xmls)
private

Definition at line 772 of file file.cpp.

773{
774 const static std::string __data("data");
775 const static std::string __schema("schema");
776
777 try
778 {
779 const char __oks_data[] = "oks-data";
780 const char __oks_schema[] = "oks-schema";
781
782 const char * id(__oks_data);
783 long id_len(sizeof(__oks_data) - 1);
784 const char * dtd(xml_data_file_dtd);
785 long dtd_len(sizeof(xml_data_file_dtd) - 1);
786
787 if (p_oks_format != __data)
788 {
789 if (p_oks_format == __schema)
790 {
791 id = __oks_schema;
792 id_len = sizeof(__oks_schema) - 1;
794 dtd_len = sizeof(xml_schema_file_dtd) - 1;
795 }
796 else
797 {
798 throw std::runtime_error("bad oks format");
799 }
800 }
801
802 {
803 const char __hdr_start[] = "\n\n<!-- ";
804 const char __hdr_end[] = " version 2.2 -->\n\n\n";
805
806 std::string header(xml_file_header, sizeof(xml_file_header) - 1);
807 header.append(__hdr_start, sizeof(__hdr_start) - 1);
808 header.append(id, id_len);
809 header.append(__hdr_end, sizeof(__hdr_end) - 1);
810 header.append(dtd, dtd_len);
811
812 xmls.put_raw(header.c_str(), header.size());
813 xmls.put_raw('\n');
814 xmls.put_raw('\n');
815 }
816
817 xmls.put_start_tag(id, id_len);
818
819 xmls.put_raw('>');
820 xmls.put_raw('\n');
821 xmls.put_raw('\n');
822
823 // "creation-time" may not be filled
824
825 std::string created_c_str;
826 if (!p_creation_time.is_not_a_date_time())
827 {
828 created_c_str = boost::posix_time::to_iso_string(p_creation_time);
829 }
830
831 // get last-modified information
832
833 if (p_repository_name.empty())
834 {
835 p_last_modification_time = boost::posix_time::second_clock::universal_time();
838 }
839
840 std::string last_modified_c_str = boost::posix_time::to_iso_string(p_last_modification_time);
841
842 xmls.put_start_tag(xml_info_tag, sizeof(xml_info_tag) - 1);
843 xmls.put_attribute("name", sizeof("name") - 1, p_logical_name.c_str());
844 xmls.put_attribute("type", sizeof("type") - 1, p_type.c_str());
845 xmls.put_attribute("num-of-items", sizeof("num-of-items") - 1, p_number_of_items);
846 xmls.put_attribute("oks-format", sizeof("oks-format") - 1, p_oks_format.c_str());
847 xmls.put_attribute("oks-version", sizeof("oks-version") - 1, OksKernel::GetVersion());
848 xmls.put_attribute("created-by", sizeof("created-by") - 1, p_created_by.c_str());
849 xmls.put_attribute("created-on", sizeof("created-on") - 1, p_created_on.c_str());
850 if (!created_c_str.empty())
851 xmls.put_attribute("creation-time", sizeof("creation-time") - 1, created_c_str.c_str());
852 if (p_repository_name.empty())
853 {
854 xmls.put_attribute("last-modified-by", sizeof("last-modified-by") - 1, p_last_modified_by.c_str());
855 xmls.put_attribute("last-modified-on", sizeof("last-modified-on") - 1, p_last_modified_on.c_str());
856 xmls.put_attribute("last-modification-time", sizeof("last-modification-time") - 1, last_modified_c_str.c_str());
857 }
858 xmls.put_end_tag();
859
860 if (!p_list_of_include_files.empty())
861 {
862 xmls.put_raw('\n');
863 xmls.put_start_tag(xml_include_tag, sizeof(xml_include_tag) - 1);
864
865 xmls.put_raw('>');
866 xmls.put_raw('\n');
867
868 for (const auto& i : p_list_of_include_files)
869 {
870 xmls.put_raw(' ');
871 xmls.put_start_tag(xml_file_tag, sizeof(xml_file_tag) - 1);
872 xmls.put_attribute("path", sizeof("path") - 1, i.c_str());
873 xmls.put_end_tag();
874 }
875
876 xmls.put_last_tag(xml_include_tag, sizeof(xml_include_tag) - 1);
877 xmls.put_raw('\n');
878 }
879
880 if (!p_comments.empty())
881 {
882 xmls.put_start_tag(xml_comments_tag, sizeof(xml_comments_tag) - 1);
883
884 xmls.put_raw('>');
885 xmls.put_raw('\n');
886
887 for (const auto& i : p_comments)
888 {
889 xmls.put_raw(' ');
890 xmls.put_start_tag(xml_comment_tag, sizeof(xml_comment_tag) - 1);
891 xmls.put_attribute("creation-time", sizeof("creation-time") - 1, i.first.c_str());
892 xmls.put_attribute("created-by", sizeof("created-by") - 1, i.second->p_created_by.c_str());
893 xmls.put_attribute("created-on", sizeof("created-on") - 1, i.second->p_created_on.c_str());
894 xmls.put_attribute("author", sizeof("author") - 1, i.second->p_author.c_str());
895 xmls.put_attribute("text", sizeof("text") - 1, i.second->p_text.c_str());
896 xmls.put_end_tag();
897 }
898
899 xmls.put_last_tag(xml_comments_tag, sizeof(xml_comments_tag) - 1);
900 xmls.put_raw('\n');
901 }
902
903 xmls.put_raw('\n');
904 }
905 catch (std::exception& ex)
906 {
907 throw oks::FailedSave("oks-file", p_full_name, ex.what());
908 }
909
910 p_is_on_disk = true;
911}
static const char xml_schema_file_dtd[]
Definition file.hpp:772
static const char xml_data_file_dtd[]
Definition file.hpp:773
static const char xml_file_header[]
Definition file.hpp:771
static const char * GetVersion()
Get OKS version. The method returns string containing CVS tag and date of OKS build.
Definition kernel.cpp:302

◆ OksClass

friend class OksClass
friend

Definition at line 348 of file file.hpp.

◆ OksKernel

friend class OksKernel
friend

Definition at line 347 of file file.hpp.

◆ OksLoadObjectsJob

friend struct OksLoadObjectsJob
friend

Definition at line 350 of file file.hpp.

◆ OksObject

friend class OksObject
friend

Definition at line 349 of file file.hpp.

Member Data Documentation

◆ p_comments

std::map<std::string, oks::Comment *> dunedaq::oks::OksFile::p_comments
private

Definition at line 766 of file file.hpp.

◆ p_created_by

std::string dunedaq::oks::OksFile::p_created_by
private

Definition at line 752 of file file.hpp.

◆ p_created_on

std::string dunedaq::oks::OksFile::p_created_on
private

Definition at line 754 of file file.hpp.

◆ p_creation_time

boost::posix_time::ptime dunedaq::oks::OksFile::p_creation_time
private

Definition at line 753 of file file.hpp.

◆ p_full_name

std::string dunedaq::oks::OksFile::p_full_name
private

Definition at line 745 of file file.hpp.

◆ p_included_by

const OksFile* dunedaq::oks::OksFile::p_included_by
private

Definition at line 768 of file file.hpp.

◆ p_is_on_disk

bool dunedaq::oks::OksFile::p_is_on_disk
private

Definition at line 767 of file file.hpp.

◆ p_is_read_only

bool dunedaq::oks::OksFile::p_is_read_only
private

Definition at line 762 of file file.hpp.

◆ p_is_updated

bool dunedaq::oks::OksFile::p_is_updated
private

Definition at line 761 of file file.hpp.

◆ p_kernel

OksKernel* dunedaq::oks::OksFile::p_kernel
private

Definition at line 769 of file file.hpp.

◆ p_last_modification_time

boost::posix_time::ptime dunedaq::oks::OksFile::p_last_modification_time
private

Definition at line 756 of file file.hpp.

◆ p_last_modified

time_t dunedaq::oks::OksFile::p_last_modified
private

Definition at line 764 of file file.hpp.

◆ p_last_modified_by

std::string dunedaq::oks::OksFile::p_last_modified_by
private

Definition at line 755 of file file.hpp.

◆ p_last_modified_on

std::string dunedaq::oks::OksFile::p_last_modified_on
private

Definition at line 757 of file file.hpp.

◆ p_list_of_include_files

std::list<std::string> dunedaq::oks::OksFile::p_list_of_include_files
private

Definition at line 763 of file file.hpp.

◆ p_lock

std::shared_ptr<boost::interprocess::file_lock> dunedaq::oks::OksFile::p_lock
private

Definition at line 759 of file file.hpp.

◆ p_lock_file_name

std::string dunedaq::oks::OksFile::p_lock_file_name
private

Definition at line 760 of file file.hpp.

◆ p_logical_name

std::string dunedaq::oks::OksFile::p_logical_name
private

Definition at line 747 of file file.hpp.

◆ p_nolock_mode

bool dunedaq::oks::OksFile::p_nolock_mode = false
staticprivate

Definition at line 742 of file file.hpp.

◆ p_number_of_items

long dunedaq::oks::OksFile::p_number_of_items
private

Definition at line 750 of file file.hpp.

◆ p_oks_format

std::string dunedaq::oks::OksFile::p_oks_format
private

Definition at line 749 of file file.hpp.

◆ p_open_mode

Mode dunedaq::oks::OksFile::p_open_mode
private

Definition at line 758 of file file.hpp.

◆ p_repository_last_modified

time_t dunedaq::oks::OksFile::p_repository_last_modified
private

Definition at line 765 of file file.hpp.

◆ p_repository_name

std::string dunedaq::oks::OksFile::p_repository_name
private

Definition at line 746 of file file.hpp.

◆ p_short_name

std::string dunedaq::oks::OksFile::p_short_name
private

Definition at line 744 of file file.hpp.

◆ p_size

long dunedaq::oks::OksFile::p_size
private

Definition at line 751 of file file.hpp.

◆ p_type

std::string dunedaq::oks::OksFile::p_type
private

Definition at line 748 of file file.hpp.

◆ xml_comment_tag

const char dunedaq::oks::OksFile::xml_comment_tag = "comment"
staticprivate

Definition at line 778 of file file.hpp.

◆ xml_comments_tag

const char dunedaq::oks::OksFile::xml_comments_tag = "comments"
staticprivate

Definition at line 777 of file file.hpp.

◆ xml_data_file_dtd

const char dunedaq::oks::OksFile::xml_data_file_dtd
staticprivate

Definition at line 773 of file file.hpp.

◆ xml_file_header

const char dunedaq::oks::OksFile::xml_file_header = "<?xml version=\"1.0\" encoding=\"ASCII\"?>"
staticprivate

Definition at line 771 of file file.hpp.

◆ xml_file_tag

const char dunedaq::oks::OksFile::xml_file_tag = "file"
staticprivate

Definition at line 776 of file file.hpp.

◆ xml_include_tag

const char dunedaq::oks::OksFile::xml_include_tag = "include"
staticprivate

Definition at line 775 of file file.hpp.

◆ xml_info_tag

const char dunedaq::oks::OksFile::xml_info_tag = "info"
staticprivate

Definition at line 774 of file file.hpp.

◆ xml_schema_file_dtd

const char dunedaq::oks::OksFile::xml_schema_file_dtd
staticprivate

Definition at line 772 of file file.hpp.


The documentation for this class was generated from the following files: