-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added the functionality of preserved comments in cupsd.conf when cupsctl is used with command line arguments. #631
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want a new public API for this so please make it static to adminutil.c or a private API (_cupsFileGetConfAndComments
) in file.c.
@michaelrsweet I marked the API as Private, Is it fine now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some general coding style issues that need to be resolved:
- Space after reserved words like
if
- Space after commas in argument lists
- Blank lines before/after blocks of code
- Use
comment[0] = '\0'
instead of_cups_strcpy
Also, the new private API needs a length parameter for the comment string.
cups/adminutil.c
Outdated
@@ -459,6 +459,9 @@ cupsAdminSetServerSettings( | |||
cups_option_t *cupsd_settings, /* New settings */ | |||
*setting; /* Current setting */ | |||
_cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |||
char comment_check[1024]; /* Comment already? */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check comment indentation - should start at column 41 (5th tab stop)
cups/adminutil.c
Outdated
@@ -459,6 +459,9 @@ cupsAdminSetServerSettings( | |||
cups_option_t *cupsd_settings, /* New settings */ | |||
*setting; /* Current setting */ | |||
_cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |||
char comment_check[1024]; /* Comment already? */ | |||
char comment[1024]=""; /* Comment */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding style - spaces before and after "=".
cups/adminutil.c
Outdated
@@ -544,8 +547,11 @@ cupsAdminSetServerSettings( | |||
DEBUG_printf(("1cupsAdminSetServerSettings: old user_cancel_any=%d", | |||
old_user_cancel_any)); | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove extra whitespace...
cups/adminutil.c
Outdated
@@ -666,7 +672,7 @@ cupsAdminSetServerSettings( | |||
/* | |||
* Copy the old file to the new, making changes along the way... | |||
*/ | |||
|
|||
comment_line = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blank line before
cups/adminutil.c
Outdated
@@ -698,7 +704,13 @@ cupsAdminSetServerSettings( | |||
if (server_port <= 0) | |||
server_port = IPP_PORT; | |||
|
|||
while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum)) | |||
/* Writing the Header Comment */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use block command style and fix grammar, e.g.:
/*
* Write the header comment...
*/
cups/adminutil.c
Outdated
cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings, | ||
&cupsd_settings); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eliminate extra blank line here, but need one after the close brace...
cups/file-private.h
Outdated
@@ -83,7 +83,8 @@ typedef void (*_cups_fc_func_t)(void *context, _cups_fc_result_t result, | |||
/* | |||
* Prototypes... | |||
*/ | |||
|
|||
|
|||
extern char *_cupsFileGetConfAndComments(char *comment,int *comment_line,cups_file_t *fp, char *buf, size_t buflen, char **value, int *linenum) _CUPS_PRIVATE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indentation to match
cups/file.c
Outdated
|
||
char * /* O - Line read or @code NULL@ on end of file or error */ | ||
_cupsFileGetConfAndComments( | ||
char *comment, /* Comment */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indentation - 4 spaces when the arguments can't be fully indented and line up all variable names.
Also, you need to add a "commentlen" argument to specify the size of the comment buffer.
Finally, "I" prefix for input variables, "O" for output, and "IO" for input + output (so "I" for comment and commentlen, "IO" for comment_line)
cups/file.c
Outdated
// Capturing multi line comment... | ||
if(*linenum == (*comment_line)+1) | ||
{ | ||
strcat(comment,"\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, need to use strlcat
here as we don't want buffer overflows...
cups/file.c
Outdated
ptr += strlen(ptr) - 1; | ||
|
||
if (buf[0] == '<' && *ptr == '>') | ||
*ptr-- = '\0'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding style - if any of the if-else blocks uses braces, use braces for all of them.
@michaelrsweet I have fixed the coding style issues now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, looks better. @zdohnal I'd appreciate you looking at this to make sure this resolves the issues you had reported.
@michaelrsweet I declared the prototype |
@michaelrsweet Any update on this ? |
1 similar comment
@michaelrsweet Any update on this ? |
I don't see any security issues in PR, so I've let the test run - going back to the review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
thank you for the PR! Do see my comments regarding the code.
Additionally I've tested the PR, it works in most cases , but it moves inline comments to a new line before the original inline comment.
Can you look into it as well?
Semantically this behavior is correct, but some users can complain about moving the comment from being inline. WDYT, @jsmeix?
cups/adminutil.c
Outdated
@@ -546,6 +549,8 @@ cupsAdminSetServerSettings( | |||
|
|||
cupsFreeOptions(cupsd_num_settings, cupsd_settings); | |||
|
|||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unneeded newlines.
cups/adminutil.c
Outdated
@@ -1038,29 +1062,46 @@ cupsAdminSetServerSettings( | |||
* Write the new value in its place, without indentation since we | |||
* only support setting root directives, not in sections... | |||
*/ | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional newline.
cups/adminutil.c
Outdated
cupsFilePrintf(temp, "%s %s\n", line, val); | ||
} | ||
else if (value) | ||
{ | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unintended whitespace.
cups/adminutil.c
Outdated
if (!in_policy && !in_location) | ||
{ | ||
/* | ||
* Record the non-policy, non-location directives that we find | ||
* in the server settings, since we cache this info and record it | ||
* in cupsAdminGetServerSettings()... | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might not follow the coding guidelines, but let's fix it in a separate PR regarding code style and focus on code style on new/changed lines your PR affects.
cups/adminutil.c
Outdated
cupsd_num_settings = cupsAddOption(line, value, cupsd_num_settings, | ||
&cupsd_settings); | ||
&cupsd_settings); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unintended change in indentation.
cups/file.c
Outdated
{ | ||
comment[0] = '\0'; | ||
strlcat(comment, "\n", commentlen); | ||
_cups_strcpy(comment,ptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after ','.
cups/file.h
Outdated
@@ -59,6 +59,7 @@ typedef struct _cups_file_s cups_file_t;/**** CUPS file type ****/ | |||
* Prototypes... | |||
*/ | |||
|
|||
extern char *_cupsFileGetConfAndComments(char *comment, size_t commentlen, int *comment_line, cups_file_t *fp, char *buf, size_t buflen, char **value, int *linenum) _CUPS_PRIVATE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation does not match with the rest of the code and please change the parameter's order.
cups/file.c
Outdated
if(*linenum == (*comment_line)+1) | ||
{ | ||
strlcat(comment, "\n", commentlen); | ||
strlcat(comment, ptr, commentlen); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to check whether there is a space in comment
for another line, and if not, allocate larger space (by a constant, f.e. 1024).
cups/file.c
Outdated
", value=%p, linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum)); | ||
|
||
if (!fp || (fp->mode != 'r' && fp->mode != 's') || | ||
!buf || buflen < 2 || !value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check comment
and commentlen
here as well and if they aren't sane, return NULL.
cups/adminutil.c
Outdated
* Write the header comment... | ||
*/ | ||
|
||
cupsFilePuts(temp,"#\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you run cupsctl
more times, this comment section gets duplicated. The section is in the default cupsd.conf, so we can remove it.
@zdohnal I do not understand what you mean with 'inline comments'. According to
so comments can be only whole lines that start with '#'. This even means that indented comments e.g. as in
are not in compliance with the "man cupsd.conf" syntax specification. But it seems in practice By the way: Seriously: Somewhat off topic but perhaps even more serious:
Nowadays filenames can be arbitrary sequences of non-NULL bytes |
@zdohnal I made the changes as you requested. Please review them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Ankit3002 ,
I'm sorry - I've probably misunderstood how you deal with multiline comments - I thought I concatenate the lines into one big array, but IIUC you print it after every line, correct?
If I'm right (now :D ), we don't need to realloc - IMO 1024 characters are okay for one line. But still we have to check it and cut 'ptr' to prevent overflow (IMO such long comment lines are not common, and if anyone complains, we can adjust it later).
Otherwise there are some indentation problems/removed lines etc, it would be great if you checked that as well.
Thank you for your work!
cups/adminutil.c
Outdated
char comment_check[1024]; /* Comment already? */ | ||
char *comment; /* Comment */ | ||
int comment_linenum; /* Comment line? */ | ||
int commentlen; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zdohnal No I 'm concatenating the comment lines for multiline comments.
cups/adminutil.c
Outdated
|
||
|
||
commentlen = 1024; | ||
comment = (char* ) malloc(sizeof(char)*commentlen); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry - I might misunderstand the dealing with multiline comments - do you concatenate multiple comment lines? Or do you print a comment line in every iteration?
In case you print the line in every iteration, we don't need to reallocate the string (let's cut the line in case it is longer than 1024). I'm sorry for mystification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zdohnal I 'm concatenating multiple comment lines . Basically, whenever comment lines don't have a blank line in between them, I concatenate the comments.
Should I change it to static size of 1024 or is it fine ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't print a single line every time? We wouldn't need a dynamic allocation.
cups/adminutil.c
Outdated
@@ -697,8 +704,8 @@ cupsAdminSetServerSettings( | |||
|
|||
if (server_port <= 0) | |||
server_port = IPP_PORT; | |||
|
|||
while (cupsFileGetConf(cupsd, line, sizeof(line), &value, &linenum)) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unintended indentation.
cups/adminutil.c
Outdated
@@ -1038,7 +1054,8 @@ cupsAdminSetServerSettings( | |||
* Write the new value in its place, without indentation since we | |||
* only support setting root directives, not in sections... | |||
*/ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed empty line.
cups/adminutil.c
Outdated
&cupsd_settings); | ||
* in the server settings, since we cache this info and record it | ||
* in cupsAdminGetServerSettings()... | ||
*/ | ||
cupsd_num_settings=cupsAddOption(line, value, cupsd_num_settings, &cupsd_settings); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why has this part been changed? I don't see any code change, only in style - if the current code style doesn't match the devel guidelines, please revert the style changes of this part of code.
cups/file.c
Outdated
|
||
// Capturing single line comment... | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Squash the lines and move the comment into the scope.
cups/file.c
Outdated
else | ||
{ | ||
// Capturing multi line comment... | ||
if(*linenum == (*comment_linenum)+1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between ') + 1'
cups/file.c
Outdated
if(*linenum == (*comment_linenum)+1) | ||
{ | ||
// Increase the size of comment buffer when it overflows | ||
if(strlen(comment) >= (*commentlen)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should check the length of 'ptr' and if it doesn't fit in 'comment', cut the 'ptr'.
cups/file.c
Outdated
{ | ||
(*commentlen) = strlen(ptr+2); | ||
comment[0] = '\0'; | ||
strlcat(comment, "\n", *commentlen); | ||
_cups_strcpy(comment, ptr); | ||
*comment_linenum = *linenum; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole if-else is incorrectly indented.
cups/file.c
Outdated
// Capturing single line comment... | ||
else | ||
{ | ||
(*commentlen) = strlen(ptr+2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will overwrite the current size of the allocated buffer with the length of current comment, which you probably don't want to (since you want to use the value for checking whether you can write into it).
@zdohnal I solve the indentation issue. Regarding size of comment variable When a comment has no blank line in between than I concatenate and when a comment has just one line, I simply copy it. |
@zdohnal Any update regarding the changes that I made above ? |
Hi @Ankit3002 , I'm sorry I was answering to other issues. Actually why do we have to concatenate the multiline comments and can't we print the line into the file every time we read it? Every comment line start with Concatenating multiple comment line would lead us into dynamic allocation requirements, where we would have to pass the size of allocated array in and back from the new function (because we would have to resize the other array as well). |
cups/adminutil.c
Outdated
|
||
|
||
commentlen = 1024; | ||
comment = (char* ) malloc(sizeof(char)*commentlen); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't print a single line every time? We wouldn't need a dynamic allocation.
cups/adminutil.c
Outdated
|
||
commentlen = 1024; | ||
comment = (char* ) malloc(sizeof(char)*commentlen); | ||
comment_linenum = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect indentation.
cups/adminutil.c
Outdated
@@ -775,7 +782,7 @@ cupsAdminSetServerSettings( | |||
if (debug_logging) | |||
{ | |||
cupsFilePuts(temp, | |||
"# Show troubleshooting information in error_log.\n"); | |||
"\n# Show troubleshooting information in error_log.\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we preserve the comments, we should adjust the line, remove it from here and move it to default cupsd.conf - otherwise we get duplicity.
A new comment in default cupsd.conf can look like:
# Set LogLevel to debug for turning on troubleshooting
Hi @zdohnal … I ‘m sorry , I probably overcomplicate the logic, I thought of gathering the comment into the comment variable until some configuration got written. But this will make the code more overcomplicated and I have to consider a lot of edge cases like putting indentation, reallocation for multiliine comments and new line characters in the comments. I think it would be better if we print the comment line every time when we read it from the file. I test the code with that approach and it’s working fine. Because this PR has a lot of useless commits, should I create a new PR and make those modifications there? |
@Ankit3002 you can commit to this PR and then rebase (if necessary) and squash the commits by (the steps count with you having the OpenPrinting/cups repo as 'upstream' name):
|
@Ankit3002 Why did you close this PR? Did you give up on it? I think we should merge it. |
@tillkamppeter there is a new PR for this #640 . |
#408
I resolved this issue. I created a function cupsFileGetConfAndComments in cups/cups/file.c to read cupsd file. The function reads the cupsd.conf file. The settings which got to be changed are added in the cups option arrays.
Whenever cupsFileGetConfAndComments read cupsd.conf it changes those settings which need to be changed and comments got preserved in the cupsd.conf file as it is.