+ /* No tracefile rotation, a single file to unlink and re-create. */
+ ret = utils_unlink_stream_file(stream->chan->pathname, stream->name,
+ tracefile_size, 0, stream->uid, stream->gid, 0);
+ if (ret < 0 && errno != ENOENT) {
+ return LTTCOMM_CONSUMERD_FATAL;
+ }
+ return LTTCOMM_CONSUMERD_SUCCESS;
+}
+
+static
+int consumer_clear_stream_files(struct lttng_consumer_stream *stream)
+{
+ int ret;
+ uint64_t tracefile_size = stream->chan->tracefile_size;
+
+ /*
+ * If stream is sent over to a relay daemon, there are no local files
+ * to unlink.
+ */
+ if (stream->net_seq_idx != (uint64_t) -1ULL) {
+ return LTTCOMM_CONSUMERD_SUCCESS;
+ }
+
+ ret = close(stream->out_fd);
+ if (ret < 0) {
+ PERROR("Closing tracefile");
+ return LTTCOMM_CONSUMERD_FATAL;
+ }
+ stream->out_fd = -1;
+ stream->out_fd_offset = 0;
+ stream->tracefile_size_current = 0;
+
+ /*
+ * Re-creation of the index file takes care of clearing its
+ * content for non-tracefile-rotation streams.
+ * Rotation streams need to explicitly unlink each index file.
+ * We put the stream file, but keep the stream->index_file value
+ * as indication whether the stream has index (non-NULL) before
+ * overwriting it with an index creation.
+ */
+ if (stream->index_file) {
+ lttng_index_file_put(stream->index_file);
+ }
+
+ if (tracefile_size > 0) {
+ /* Tracefile rotation. */
+ ret = consumer_unlink_stream_files_rotation(stream);
+ } else {
+ ret = consumer_unlink_stream_files(stream);
+ }
+ if (ret != LTTCOMM_CONSUMERD_SUCCESS) {
+ return ret;
+ }
+
+ /* Create new files. */
+ ret = utils_create_stream_file(stream->chan->pathname, stream->name,
+ tracefile_size, 0, stream->uid, stream->gid, 0);
+ if (ret < 0) {
+ return LTTCOMM_CONSUMERD_FATAL;
+ }
+ stream->out_fd = ret;
+
+ if (stream->index_file) {
+ stream->index_file = lttng_index_file_create(stream->chan->pathname,
+ stream->name, stream->uid, stream->gid, tracefile_size,
+ 0, CTF_INDEX_MAJOR, CTF_INDEX_MINOR);
+ if (!stream->index_file) {
+ return LTTCOMM_CONSUMERD_FATAL;
+ }
+ }
+
+ return LTTCOMM_CONSUMERD_SUCCESS;
+}
+
+static
+int consumer_clear_stream(struct lttng_consumer_stream *stream)
+{
+ int ret;
+
+ ret = consumer_flush_buffer(stream, 1);
+ if (ret < 0) {
+ ERR("Failed to flush stream %" PRIu64 " during channel clear",
+ stream->key);
+ ret = LTTCOMM_CONSUMERD_FATAL;
+ goto error;
+ }
+
+ ret = consumer_clear_buffer(stream);
+ if (ret < 0) {
+ ERR("Failed to clear stream %" PRIu64 " during channel clear",
+ stream->key);
+ ret = LTTCOMM_CONSUMERD_FATAL;
+ goto error;
+ }
+
+ ret = consumer_clear_stream_files(stream);
+ if (ret != LTTCOMM_CONSUMERD_SUCCESS) {
+ ERR("Failed to clear stream %" PRIu64 " files during channel clear",
+ stream->key);
+ goto error;
+ }
+ ret = LTTCOMM_CONSUMERD_SUCCESS;
+error:
+ return ret;
+}
+
+static
+int consumer_clear_unmonitored_channel(struct lttng_consumer_channel *channel)
+{
+ int ret;
+ struct lttng_consumer_stream *stream;