MCPcopy Create free account
hub / github.com/apache/trafficserver / psi_include

Function psi_include

example/plugins/c-api/thread_pool/psi.cc:457–530  ·  view source on GitHub ↗

------------------------------------------------------------------------- psi_include Read file to include. Copy its content into an iobuffer. This is the function doing blocking calls and called by the plugin's threads Input: data continuation for the current transaction Output : data->psi_buffer contains the file content data->psi_success 0 if include failed, 1 if

Source from the content-addressed store, hash-verified

455 1 if success
456 -------------------------------------------------------------------------*/
457static int
458psi_include(TSCont contp, void *edata ATS_UNUSED)
459{
460#define BUFFER_SIZE 1024
461 ContData *data;
462 TSFile filep;
463 char inc_file[PSI_PATH_MAX_SIZE + PSI_FILENAME_MAX_SIZE];
464
465 /* We manipulate plugin continuation data from a separate thread.
466 Grab mutex to avoid concurrent access */
467 TSMutexLock(TSContMutexGet(contp));
468 data = contDataGet(contp);
469 TSAssert(data->magic == MAGIC_ALIVE);
470
471 if (!data->psi_buffer) {
472 data->psi_buffer = TSIOBufferCreate();
473 data->psi_reader = TSIOBufferReaderAlloc(data->psi_buffer);
474 }
475
476 /* For security reason, we do not allow to include files that are
477 not in the directory <plugin_path>/include.
478 Also include file cannot contain any path. */
479 snprintf(inc_file, sizeof(inc_file), "%s/%s", psi_directory, _basename(data->psi_filename));
480
481 /* Read the include file and copy content into iobuffer */
482 if ((filep = TSfopen(inc_file, "r")) != nullptr) {
483 Dbg(dbg_ctl, "Reading include file %s", inc_file);
484
485 char buf[BUFFER_SIZE];
486 while (TSfgets(filep, buf, BUFFER_SIZE) != nullptr) {
487 int64_t len, ndone, ntodo;
488
489 len = strlen(buf);
490 ndone = 0;
491 ntodo = len;
492 while (ntodo > 0) {
493 /* TSIOBufferStart allocates more blocks if required */
494 TSIOBufferBlock block = TSIOBufferStart(data->psi_buffer);
495 int64_t avail;
496 char *ptr_block = TSIOBufferBlockWriteStart(block, &avail);
497 int64_t towrite = MIN(ntodo, avail);
498
499 memcpy(ptr_block, buf + ndone, towrite);
500 TSIOBufferProduce(data->psi_buffer, towrite);
501 ntodo -= towrite;
502 ndone += towrite;
503 }
504 }
505 TSfclose(filep);
506 data->psi_success = 1;
507 if (ts_log) {
508 TSTextLogObjectWrite(ts_log, "Successfully included file: %s", inc_file);
509 }
510 } else {
511 data->psi_success = 0;
512 if (ts_log) {
513 TSTextLogObjectWrite(ts_log, "Failed to include file: %s", inc_file);
514 }

Callers

nothing calls this directly

Calls 15

TSMutexLockFunction · 0.85
TSContMutexGetFunction · 0.85
contDataGetFunction · 0.85
TSIOBufferCreateFunction · 0.85
TSIOBufferReaderAllocFunction · 0.85
_basenameFunction · 0.85
TSfopenFunction · 0.85
TSfgetsFunction · 0.85
TSIOBufferStartFunction · 0.85
TSIOBufferProduceFunction · 0.85
TSfcloseFunction · 0.85

Tested by

no test coverage detected