71 {
72 m_filesize = 0;
73 m_fileid = 0;
74 m_diskid = 1;
75 m_chksum = 1;
76 m_nevents = 0;
77 m_filesize = 0;
78 m_expno = g_expno;
79 m_runno = g_runno;
80 m_fileid = fileid;
81 bool available = false;
82 for (int i = 0; i < ndisks; i++) {
83 struct statvfs statfs;
84 char filename[1024];
85 sprintf(filename, "%s%02d", dir.c_str(), m_diskid);
86 statvfs(filename, &statfs);
87 float usage = 1 - ((float)statfs.f_bfree / statfs.f_blocks);
88 if (usage < 0.7) {
89 sprintf(filename, "%s%02d/storage/full_flag", dir.c_str(), m_diskid);
90 std::ifstream fin(filename);
91 int flag = 0;
92 fin >> flag;
93 if (flag != 1) {
94 available = true;
95 std::cout << "[DEBUG] disk : " << m_diskid << " is available" << std::endl;
96 break;
97 }
98 fin.close();
99 std::cout << "[DEBUG] disk : " << m_diskid << " is still full" << std::endl;
100 } else {
101 sprintf(filename, "%s%02d/storage/full_flag", dir.c_str(), m_diskid);
102 std::ofstream fout(filename);
103 fout << 1;
104 fout.close();
105 B2WARNING("disk-" << m_diskid << " is full " << usage);
106 }
107 m_diskid++;
108 if (m_diskid > ndisks) m_diskid = 1;
109 }
110 if (!available) {
111 B2FATAL("No disk available for writing");
112 exit(1);
113 }
114 std::string filedir = dir + StringUtil::form("%02d/storage/%4.4d/%5.5d/",
115 m_diskid, m_expno, m_runno);
116 system(("mkdir -p " + filedir).c_str());
117 m_filename = StringUtil::form("%s.%4.4d.%5.5d.%s.f%5.5d.sroot",
118 m_runtype.c_str(), m_expno, m_runno, m_host.c_str(), m_fileid);
119 m_path = filedir + m_filename;
120 m_file = ::open(m_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0664);
121 if (g_diskid > 0 && g_diskid != m_diskid) {
122 B2FATAL("disk-" << m_diskid << " is already full! Terminating process..");
123 exit(1);
124 }
125 g_diskid = m_diskid;
126 if (m_file < 0) {
127 B2FATAL("Failed to open file : " << m_path);
128 exit(1);
129 }
130 try {
131 m_db->connect();
132 m_db->execute("insert into %s (name, path, host, label, expno, runno, fileno, nevents, chksum, size) "
133 "values ('%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0);",
134 g_table, m_filename.c_str(), m_path.c_str(), m_host.c_str(),
135 m_runtype.c_str(), m_expno, m_runno, m_fileid);
137 B2WARNING(e.what());
138 }
139 write(g_streamerinfo, g_streamersize, true);
140 B2INFO("New file " << m_path << " is opened");
141
142 return m_id;
143 }