File: StoragePosixWrite.pm

package info (click to toggle)
pgbackrest 2.57.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,344 kB
  • sloc: ansic: 127,546; xml: 19,452; perl: 12,761; pascal: 3,279; sh: 91; sql: 32; makefile: 23
file content (209 lines) | stat: -rw-r--r-- 6,928 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
####################################################################################################################################
# Posix File Write
####################################################################################################################################
package pgBackRestTest::Common::StoragePosixWrite;
use parent 'pgBackRestTest::Common::Io::Handle';

use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use English '-no_match_vars';

use Fcntl qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
use File::Basename qw(dirname);

use pgBackRestDoc::Common::Exception;
use pgBackRestDoc::Common::Log;

use pgBackRestTest::Common::Io::Handle;
use pgBackRestTest::Common::StorageBase;

####################################################################################################################################
# CONSTRUCTOR
####################################################################################################################################
sub new
{
    my $class = shift;

    # Assign function parameters, defaults, and log debug info
    my
    (
        $strOperation,
        $oDriver,
        $strName,
        $strMode,
        $strUser,
        $strGroup,
        $lTimestamp,
        $bPathCreate,
        $bAtomic,
        $bSync,
    ) =
        logDebugParam
        (
            __PACKAGE__ . '->new', \@_,
            {name => 'oDriver', trace => true},
            {name => 'strName', trace => true},
            {name => 'strMode', optional => true, trace => true},
            {name => 'strUser', optional => true, trace => true},
            {name => 'strGroup', optional => true, trace => true},
            {name => 'lTimestamp', optional => true, trace => true},
            {name => 'bPathCreate', optional => true, default => false, trace => true},
            {name => 'bAtomic', optional => true, default => false, trace => true},
            {name => 'bSync', optional => true, default => true, trace => true},
        );

    # Create the class hash
    my $self = $class->SUPER::new("'${strName}'");
    bless $self, $class;

    # Set variables
    $self->{oDriver} = $oDriver;
    $self->{strName} = $strName;
    $self->{strMode} = $strMode;
    $self->{strUser} = $strUser;
    $self->{strGroup} = $strGroup;
    $self->{lTimestamp} = $lTimestamp;
    $self->{bPathCreate} = $bPathCreate;
    $self->{bAtomic} = $bAtomic;
    $self->{bSync} = $bSync;

    # If atomic create temp filename
    if ($self->{bAtomic})
    {
        # Create temp file name
        $self->{strNameTmp} = "$self->{strName}." . $self->{oDriver}->tempExtension();
    }

    # Open file on first write to avoid creating extraneous files on error
    $self->{bOpened} = false;

    # Return from function and log return values if any
    return logDebugReturn
    (
        $strOperation,
        {name => 'self', value => $self, trace => true}
    );
}

####################################################################################################################################
# open - open the file
####################################################################################################################################
sub open
{
    my $self = shift;

    # Get the file name
    my $strFile = $self->{bAtomic} ? $self->{strNameTmp} : $self->{strName};

    # Open the file
    if (!sysopen(
        $self->{fhFile}, $strFile, O_WRONLY | O_CREAT | O_TRUNC, oct(defined($self->{strMode}) ? $self->{strMode} : '0666')))
    {
        # If the path does not exist create it if requested
        if ($OS_ERROR{ENOENT} && $self->{bPathCreate})
        {
            $self->{oDriver}->pathCreate(dirname($strFile), {bIgnoreExists => true, bCreateParent => true});
            $self->{bPathCreate} = false;
            return $self->open();
        }

        logErrorResult($OS_ERROR{ENOENT} ? ERROR_PATH_MISSING : ERROR_FILE_OPEN, "unable to open '${strFile}'", $OS_ERROR);
    }

    # Set file mode to binary
    binmode($self->{fhFile});

    # Set the owner
    $self->{oDriver}->owner($strFile, {strUser => $self->{strUser}, strGroup => $self->{strGroup}});

    # Set handle
    $self->handleWriteSet($self->{fhFile});

    # Mark file as opened
    $self->{bOpened} = true;

    return true;
}

####################################################################################################################################
# write - write data to a file
####################################################################################################################################
sub write
{
    my $self = shift;
    my $rtBuffer = shift;

    # Open file if it is not open already
    $self->open() if !$self->opened();

    return $self->SUPER::write($rtBuffer);
}

####################################################################################################################################
# close - close the file
####################################################################################################################################
sub close
{
    my $self = shift;

    if (defined($self->handle()))
    {
        # Sync the file
        if ($self->{bSync})
        {
            $self->handle()->sync();
        }

        # Close the file
        close($self->handle());
        undef($self->{fhFile});

        # Get current filename
        my $strCurrentName = $self->{bAtomic} ? $self->{strNameTmp} : $self->{strName};

        # Set the modification time
        if (defined($self->{lTimestamp}))
        {
            utime(time(), $self->{lTimestamp}, $strCurrentName)
                or logErrorResult(ERROR_FILE_WRITE, "unable to set time for '${strCurrentName}'", $OS_ERROR);
        }

        # Move the file from temp to final if atomic
        if ($self->{bAtomic})
        {
            $self->{oDriver}->move($strCurrentName, $self->{strName});
        }

        # Set result
        $self->resultSet(COMMON_IO_HANDLE, $self->{lSize});

        # Close parent
        $self->SUPER::close();
    }

    return true;
}

####################################################################################################################################
# Close the handle if it is open (in case close() was never called)
####################################################################################################################################
sub DESTROY
{
    my $self = shift;

    if (defined($self->handle()))
    {
        CORE::close($self->handle());
        undef($self->{fhFile});
    }
}

####################################################################################################################################
# Getters
####################################################################################################################################
sub handle {shift->{fhFile}}
sub opened {shift->{bOpened}}
sub name {shift->{strName}}

1;