@@ -1293,6 +1293,11 @@ int flb_tail_file_append(char *path, struct stat *st, int mode,
12931293 goto error ;
12941294 }
12951295
1296+ /* Avoid leaking the temporary name; flb_tail_file_name_dup() will set it again */
1297+ flb_free (file -> name );
1298+ file -> name = NULL ;
1299+ file -> name_len = 0 ;
1300+
12961301 /*
12971302 * Duplicate string into 'file' structure, the called function
12981303 * take cares to resolve real-name of the file in case we are
@@ -2069,14 +2074,13 @@ int flb_tail_file_to_event(struct flb_tail_file *file)
20692074}
20702075
20712076/*
2072- * Given an open file descriptor, return the filename. This function is a
2073- * bit slow and it aims to be used only when a file is rotated.
2077+ * Internal implementation: Given an open file descriptor, return the filename.
2078+ * This function is a bit slow and it aims to be used only when a file is rotated.
20742079 *
20752080 * This is used to detect the new file path after an open handle has been
2076- * rotated/moved. Requires an open file descriptor and should only be called
2077- * when keep_file_handle is enabled.
2081+ * rotated/moved. Requires an open file descriptor.
20782082 */
2079- char * flb_tail_file_name (struct flb_tail_file * file )
2083+ static char * flb_tail_file_name_internal (struct flb_tail_file * file )
20802084{
20812085 int ret ;
20822086 char * buf ;
@@ -2169,6 +2173,41 @@ char *flb_tail_file_name(struct flb_tail_file *file)
21692173 return buf ;
21702174}
21712175
2176+ /*
2177+ * Public wrapper: Get the file name from a file descriptor.
2178+ * This function handles opening/closing the file handle as needed.
2179+ * If the handle is closed, opens it temporarily and closes it after getting the name.
2180+ *
2181+ * Note: When keep_file_handle is false, this function still needs to work for
2182+ * resolving symlinks during file initialization, but it should NOT be used for
2183+ * log rotation detection (which requires persistent open handles).
2184+ */
2185+ char * flb_tail_file_name (struct flb_tail_file * file )
2186+ {
2187+ int ret ;
2188+ int fd_was_opened = FLB_FALSE ;
2189+ char * result ;
2190+
2191+ /* If handle is closed, open it temporarily */
2192+ if (file -> fd == -1 ) {
2193+ ret = flb_tail_file_ensure_open_handle (file );
2194+ if (ret != 0 ) {
2195+ return NULL ;
2196+ }
2197+ fd_was_opened = FLB_TRUE ;
2198+ }
2199+
2200+ /* Call the internal implementation */
2201+ result = flb_tail_file_name_internal (file );
2202+
2203+ /* Close handle if we opened it in this function */
2204+ if (fd_was_opened ) {
2205+ flb_tail_file_close_handle (file );
2206+ }
2207+
2208+ return result ;
2209+ }
2210+
21722211int flb_tail_file_name_dup (char * path , struct flb_tail_file * file )
21732212{
21742213 file -> name = flb_strdup (path );
@@ -2204,15 +2243,6 @@ int flb_tail_file_rotated(struct flb_tail_file *file)
22042243 struct stat st ;
22052244 struct flb_tail_config * ctx = file -> config ;
22062245
2207- /*
2208- * This function should only be called when keep_file_handle is enabled,
2209- * as it requires an open file descriptor for rotation detection.
2210- */
2211- if (ctx -> keep_file_handle == FLB_FALSE ) {
2212- flb_plg_error (ctx -> ins , "flb_tail_file_rotated() called with keep_file_handle=false" );
2213- return -1 ;
2214- }
2215-
22162246 /* Get the new file name */
22172247 name = flb_tail_file_name (file );
22182248 if (!name ) {
0 commit comments