Merge lp:~yavor-nikolov/pbzip2/bug-807536-excessive-output-permissions into lp:pbzip2/1.1

Proposed by Yavor Nikolov
Status: Merged
Merged at revision: 23
Proposed branch: lp:~yavor-nikolov/pbzip2/bug-807536-excessive-output-permissions
Merge into: lp:pbzip2/1.1
Diff against target: 158 lines (+65/-13)
3 files modified
ChangeLog (+2/-0)
pbzip2.cpp (+61/-11)
pbzip2.h (+2/-2)
To merge this branch: bzr merge lp:~yavor-nikolov/pbzip2/bug-807536-excessive-output-permissions
Reviewer Review Type Date Requested Status
pbzip2 development team Pending
Review via email: mp+67630@code.launchpad.net

Commit message

Merged fix for bug #807536 (excessive output permissions while compress/decompress is in progress)

Description of the change

Fixed excessive output permissions while compress/decompress is in progress (bug #807536)

To post a comment you must log in.
24. By Yavor Nikolov

Fixed excessive output permissions during compress/decompress in case of output file already existed (additional fix for bug #807536).

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ChangeLog'
--- ChangeLog 2011-07-11 18:20:53 +0000
+++ ChangeLog 2011-07-13 22:04:28 +0000
@@ -1,4 +1,6 @@
1Changes in 1.1.5 (NOT RELEASED YET)1Changes in 1.1.5 (NOT RELEASED YET)
2- Fixed excessive output permissions while compress/decompress
3 is in progress (bug #807536)
2Changes in 1.1.4 (Apr 22, 2011)4Changes in 1.1.4 (Apr 22, 2011)
3- Fixed hang on decompress with --ignore-trailing-garbage=1 when5- Fixed hang on decompress with --ignore-trailing-garbage=1 when
4 producer is interrupted on trailing garbage (bug #762464)6 producer is interrupted on trailing garbage (bug #762464)
57
=== modified file 'pbzip2.cpp'
--- pbzip2.cpp 2011-07-11 18:20:53 +0000
+++ pbzip2.cpp 2011-07-13 22:04:28 +0000
@@ -194,6 +194,9 @@
194 * - Print trailing garbage errors even when in quiet mode (bug #743635)194 * - Print trailing garbage errors even when in quiet mode (bug #743635)
195 * - Fixed hang on decompress with --ignore-trailing-garbage=1 when195 * - Fixed hang on decompress with --ignore-trailing-garbage=1 when
196 * producer is interrupted on trailing garbage (bug #762464)196 * producer is interrupted on trailing garbage (bug #762464)
197 * - Fixed excessive output permissions while compress/decompress
198 * is in progress (bug #807536)
199 *
197 *200 *
198 *201 *
199 * Specials thanks for suggestions and testing: Phillippe Welsh,202 * Specials thanks for suggestions and testing: Phillippe Welsh,
@@ -566,6 +569,53 @@
566 return (count - bytesRemaining);569 return (count - bytesRemaining);
567}570}
568571
572/*
573 * Open output file with least required privileges
574 */
575int safe_open_output(const char *path)
576{
577 return open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, FILE_MODE);
578}
579
580/*
581 * Based on bzip2.c code
582 */
583FILE *safe_fopen_output(const char *path, const char *mode)
584{
585 int fh = safe_open_output(path);
586 if (fh == -1)
587 {
588 return NULL;
589 }
590
591 FILE *fp = fdopen(fh, mode);
592 if (fp == NULL)
593 {
594 close(fh);
595 }
596
597 return fp;
598}
599
600/**
601 * Check if a given file exists.
602 *
603 * @return true if file exists and false if it doesn't
604 */
605bool check_file_exists( const char * filename )
606{
607 int hOutfile = open( filename, O_RDONLY | O_BINARY );
608
609 if ( hOutfile == -1 )
610 {
611 return false;
612 }
613 else
614 {
615 close( hOutfile );
616 return true;
617 }
618}
569619
570/*620/*
571 *********************************************************621 *********************************************************
@@ -1658,7 +1708,7 @@
1658 // write to file instead of stdout1708 // write to file instead of stdout
1659 if (OutputStdOut == 0)1709 if (OutputStdOut == 0)
1660 {1710 {
1661 hOutfile = open(OutFilename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, FILE_MODE);1711 hOutfile = safe_open_output(OutFilename);
1662 // check to see if file creation was successful1712 // check to see if file creation was successful
1663 if (hOutfile == -1)1713 if (hOutfile == -1)
1664 {1714 {
@@ -1913,7 +1963,7 @@
1913 // write to file instead of stdout1963 // write to file instead of stdout
1914 if (OutputStdOut == 0)1964 if (OutputStdOut == 0)
1915 {1965 {
1916 hOutfile = open(OutFilename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, FILE_MODE);1966 hOutfile = safe_open_output(OutFilename);
1917 // check to see if file creation was successful1967 // check to see if file creation was successful
1918 if (hOutfile == -1)1968 if (hOutfile == -1)
1919 {1969 {
@@ -2156,7 +2206,7 @@
2156 // see if we are outputting to stdout2206 // see if we are outputting to stdout
2157 if (OutputStdOut == 0)2207 if (OutputStdOut == 0)
2158 {2208 {
2159 stream = fopen(OutFilename, "wb");2209 stream = safe_fopen_output(OutFilename, "wb");
2160 if (stream == NULL)2210 if (stream == NULL)
2161 {2211 {
2162 handle_error(EF_NOQUIT, -1,2212 handle_error(EF_NOQUIT, -1,
@@ -4040,19 +4090,19 @@
4040 }4090 }
40414091
4042 // check to see if output file exists4092 // check to see if output file exists
4043 if ((force != 1) && (OutputStdOut == 0))4093 if ((OutputStdOut == 0) && check_file_exists(outFilename.c_str()))
4044 {4094 {
4045 hOutfile = open(outFilename.c_str(), O_RDONLY | O_BINARY);4095 if (force != 1)
4046 // check to see if file exists before processing
4047 if (hOutfile != -1)
4048 {4096 {
4049 fprintf(stderr, "pbzip2: *ERROR: Output file [%s] already exists! Use -f to overwrite...\n", outFilename.c_str());4097 fprintf(stderr, "pbzip2: *ERROR: Output file [%s] already exists! Use -f to overwrite...\n", outFilename.c_str());
4050 fprintf(stderr, "-------------------------------------------\n");4098 fprintf(stderr, "-------------------------------------------\n");
4051 errLevel = 1;4099 errLevel = 1;
4052 close(hOutfile);
4053 errLevel = 1;
4054 continue;4100 continue;
4055 }4101 }
4102 else
4103 {
4104 remove(outFilename.c_str());
4105 }
4056 }4106 }
40574107
4058 if (readEntireFile == 1)4108 if (readEntireFile == 1)
@@ -4125,7 +4175,7 @@
4125 // write to file instead of stdout4175 // write to file instead of stdout
4126 if (OutputStdOut == 0)4176 if (OutputStdOut == 0)
4127 {4177 {
4128 hOutfile = open(outFilename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, FILE_MODE);4178 hOutfile = safe_open_output(outFilename.c_str());
4129 // check to see if file creation was successful4179 // check to see if file creation was successful
4130 if (hOutfile == -1)4180 if (hOutfile == -1)
4131 {4181 {
41324182
=== modified file 'pbzip2.h'
--- pbzip2.h 2011-02-13 15:00:47 +0000
+++ pbzip2.h 2011-07-13 22:04:28 +0000
@@ -16,11 +16,11 @@
16#include <string>16#include <string>
17#include <cctype>17#include <cctype>
1818
19#define FILE_MODE (S_IRUSR | S_IWUSR )
20
19#ifndef WIN3221#ifndef WIN32
20#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
21#define OFF_T off_t22#define OFF_T off_t
22#else23#else
23#define FILE_MODE (S_IRUSR | S_IWUSR )
24#define OFF_T __int6424#define OFF_T __int64
25#endif25#endif
2626

Subscribers

People subscribed via source and target branches