FOSSology  4.7.1
Open Source License Compliance by Open Source Software
ReuserUtils.cc
1 /*
2  SPDX-License-Identifier: GPL-2.0-only
3  Author: Dietmar Helmut Leher <helmut.leher.ext@vaillant-group.com>
4  SPDX-FileCopyrightText: © 2026 Vaillant GmbH
5 */
6 
7 #include "ReuserUtils.hpp"
8 
9 #include <cstdlib>
10 #include <cstdio>
11 
12 using namespace fo;
13 
15 {
17 }
18 
20 {
21  char* commitHash = fo_sysconfig(AGENT_NAME, "COMMIT_HASH");
22  char* version = fo_sysconfig(AGENT_NAME, "VERSION");
23 
24  if (!commitHash || !version)
25  {
26  LOG_FATAL("Reuser: fo_sysconfig returned NULL for VERSION or COMMIT_HASH.");
27  bail(1);
28  }
29 
30  char* revision = nullptr;
31  if (asprintf(&revision, "%s.%s", version, commitHash) < 0)
32  {
33  LOG_FATAL("Reuser: asprintf failed allocating revision string.");
34  bail(1);
35  }
36 
37  int agentId = fo_GetAgentKey(dbManager.getConnection(),
38  AGENT_NAME, 0, revision, AGENT_DESC);
39  free(revision);
40 
41  if (agentId <= 0)
42  bail(1);
43 
44  return agentId;
45 }
46 
47 int writeARS(const ReuserState& state, int arsId, int uploadId, int success,
49 {
50  return fo_WriteARS(dbManager.getConnection(), arsId, uploadId,
51  state.getAgentId(), AGENT_ARS, nullptr, success);
52 }
53 
54 void bail(int exitval)
55 {
56  fo_scheduler_disconnect(exitval);
57  exit(exitval);
58 }
59 
60 bool processUploadId(const ReuserState& /*state*/, int uploadId,
61  ReuserDatabaseHandler& databaseHandler)
62 {
63  int groupId = fo_scheduler_groupID();
64  int userId = fo_scheduler_userID();
65 
66  auto reusedUploads = databaseHandler.getReusedUploads(uploadId, groupId);
67 
68  for (const auto& triple : reusedUploads)
69  {
70  ItemTreeBounds boundsReused;
71  if (!databaseHandler.getParentItemBounds(triple.reusedUploadId,
72  boundsReused))
73  {
74  LOG_WARNING("Reuser: could not determine parent bounds for reused"
75  " upload %d, skipping.", triple.reusedUploadId);
76  continue;
77  }
78 
79  if (triple.reuseMode & REUSE_ENHANCED)
80  {
81  if (!databaseHandler.processEnhancedUploadReuse(
82  uploadId, triple.reusedUploadId,
83  groupId, triple.reusedGroupId, userId))
84  return false;
85  }
86  else
87  {
88  if (!databaseHandler.processUploadReuse(
89  uploadId, triple.reusedUploadId,
90  groupId, triple.reusedGroupId, userId))
91  return false;
92  }
93 
94  // Failures are logged but do not abort the overall reuse run, matching
95  // the original PHP behaviour where their return values were also not checked.
96  if (triple.reuseMode & REUSE_MAIN)
97  {
98  if (!databaseHandler.reuseMainLicense(uploadId, groupId,
99  triple.reusedUploadId, triple.reusedGroupId))
100  LOG_WARNING("Reuser: reuseMainLicense failed for upload %d"
101  " (reused %d), continuing.",
102  uploadId, triple.reusedUploadId);
103  }
104 
105  if (triple.reuseMode & REUSE_CONF)
106  {
107  if (!databaseHandler.reuseConfSettings(uploadId, triple.reusedUploadId))
108  LOG_WARNING("Reuser: reuseConfSettings failed for upload %d"
109  " (reused %d), continuing.",
110  uploadId, triple.reusedUploadId);
111  }
112 
113  if (triple.reuseMode & REUSE_COPYRIGHT)
114  {
115  if (!databaseHandler.reuseCopyrights(uploadId, triple.reusedUploadId,
116  userId))
117  LOG_WARNING("Reuser: reuseCopyrights failed for upload %d"
118  " (reused %d), continuing.",
119  uploadId, triple.reusedUploadId);
120  }
121  }
122  return true;
123 }
bool processUploadId(const CompatibilityState &state, int uploadId, CompatibilityDatabaseHandler &databaseHandler, int groupId)
CompatibilityState getState(DbManager &dbManager, CompatibilityCliOptions &&cliOptions)
Create a new state for the current agent based on CliOptions.
int writeARS(const CompatibilityState &state, int arsId, int uploadId, int success, DbManager &dbManager)
int queryAgentId(DbManager &dbManager)
Query and register the agent id. Bails on failure.
void bail(int exitval)
Disconnect scheduler and exit.
Database handler for the reuser agent.
virtual bool processUploadReuse(int uploadId, int reusedUploadId, int groupId, int reusedGroupId, int userId)
virtual bool getParentItemBounds(int uploadId, ItemTreeBounds &out)
Fetch the parent item bounds for a given upload.
virtual bool reuseMainLicense(int uploadId, int groupId, int reusedUploadId, int reusedGroupId)
virtual bool processEnhancedUploadReuse(int uploadId, int reusedUploadId, int groupId, int reusedGroupId, int userId)
virtual bool reuseCopyrights(int uploadId, int reusedUploadId, int userId)
virtual std::vector< ReuseTriple > getReusedUploads(int uploadId, int groupId)
Return the list of uploads that should be reused for uploadId.
virtual bool reuseConfSettings(int uploadId, int reusedUploadId)
Holds the runtime state of the reuser agent (agent id).
Definition: ReuserState.hpp:15
DB wrapper for agents.
FUNCTION int fo_WriteARS(PGconn *pgConn, int ars_pk, int upload_pk, int agent_pk, const char *tableName, const char *ars_status, int ars_success)
Write ars record.
Definition: libfossagent.c:214
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
Definition: libfossagent.c:158
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
int fo_scheduler_groupID()
Gets the id of the group that created the job that the agent is running.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
fo namespace holds the FOSSology library functions.
Bounds of an item within an uploadtree table.
Definition: ReuserTypes.hpp:14