File: fileNode.h

package info (click to toggle)
fuse-zip 0.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,776 kB
  • sloc: cpp: 5,509; sh: 2,587; makefile: 193
file content (239 lines) | stat: -rw-r--r-- 6,916 bytes parent folder | download | duplicates (2)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
////////////////////////////////////////////////////////////////////////////
//  Copyright (C) 2008-2019 by Alexander Galanin                          //
//  al@galanin.nnov.ru                                                    //
//  http://galanin.nnov.ru/~al                                            //
//                                                                        //
//  This program is free software: you can redistribute it and/or modify  //
//  it under the terms of the GNU General Public License as published by  //
//  the Free Software Foundation, either version 3 of the License, or     //
//  (at your option) any later version.                                   //
//                                                                        //
//  This program is distributed in the hope that it will be useful,       //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of        //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
//  GNU General Public License for more details.                          //
//                                                                        //
//  You should have received a copy of the GNU General Public License     //
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.//
////////////////////////////////////////////////////////////////////////////

#ifndef FILE_NODE_H
#define FILE_NODE_H

#include <memory>
#include <string>

#include <unistd.h>
#include <sys/stat.h>

#include "types.h"
#include "bigBuffer.h"
#include "dataNode.h"

class FileNode {
friend class FuseZipData;
private:
    // must not be defined
    FileNode (const FileNode &);
    FileNode &operator= (const FileNode &);

    zip_int64_t _id;
    std::shared_ptr<DataNode> _data;

    struct zip *zip;

    const char *m_comment;
    uint16_t m_commentLen;
    bool m_commentChanged;

    void parse_name();
    void readComment();
    int updateExtraFields(bool force_precise_time) const;
    int updateExternalAttributes() const;

    static const zip_int64_t ROOT_NODE_INDEX, NEW_NODE_INDEX, TMP_DIR_INDEX;
    FileNode(struct zip *zip, const char *fname, zip_int64_t id, std::shared_ptr<DataNode> data);

protected:
    static FileNode *createIntermediateDir(struct zip *zip, const char *fname);

public:
    /**
     * Create new regular file
     */
    static FileNode *createFile(struct zip *zip, const char *fname,
            uid_t owner, gid_t group, mode_t mode, dev_t dev = 0);
    /**
     * Create new symbolic link
     */
    static FileNode *createSymlink(struct zip *zip, const char *fname);
    /**
     * Create new directory for ZIP file entry
     */
    static FileNode *createDir(struct zip *zip, const char *fname,
            zip_int64_t id, uid_t owner, gid_t group, mode_t mode);
    /**
     * Create root pseudo-node for file system
     */
    static FileNode *createRootNode(struct zip *zip);
    /**
     * Create node for existing ZIP file entry
     */
    static FileNode *createNodeForZipEntry(struct zip *zip,
            const char *fname, zip_int64_t id, mode_t mode);
    /**
     * Create hardlink to another node
     */
    static FileNode *createHardlink(struct zip *zip,
            const char *fname, zip_int64_t id, FileNode *target);
    ~FileNode();

    /**
     * return a pointer to data structure
     */
    DataNode *data() {
        return _data.get();
    }
    
    /**
     * add child node to list
     */
    void appendChild (FileNode *child);

    /**
     * remove child node from list
     */
    void detachChild (FileNode *child);

    /**
     * Rename file without reparenting
     */
    void rename (const char *new_name);

    int open();
    int read(char *buf, size_t size, size_t offset);
    int write(const char *buf, size_t size, size_t offset);
    int close();

    /**
     * Invoke zip_file_add() or zip_file_replace() for file to save it.
     * Should be called only if item is needed to ba saved into zip file.
     *
     * @return 0 if success, != 0 on error
     */
    int save();

    /**
     * Save file metadata to ZIP
     * @param force_precise_time force creation of NTFS extra field
     * @return libzip error code or 0 on success
     */
    int saveMetadata (bool force_precise_time) const;

    /**
     * Save file or archive comment into ZIP
     * @return libzip error code or 0 on success
     */
    int saveComment() const;

    /**
     * Truncate file.
     *
     * @return
     *      0       If successful
     *      EBADF   If file is currently closed
     *      EIO     If insufficient memory available (because ENOMEM not
     *              listed in truncate() error codes)
     */
    int truncate(size_t offset);

    inline bool isChanged() const {
        return _data->isChanged();
    }

    inline bool isMetadataChanged() const {
        return _data->isMetadataChanged();
    }

    inline bool isTemporaryDir() const {
        return _id == TMP_DIR_INDEX;
    }

    /**
     * Change file mode
     */
    void chmod (mode_t mode);
    inline mode_t mode() const {
        return _data->mode();
    }

    inline dev_t device() const {
        return _data->device();
    }

    /**
     * set atime and mtime
     */
    void setTimes (const timespec &atime, const timespec &mtime);

    void setCTime (const timespec &ctime);

    inline struct timespec atime() const {
        return _data->atime();
    }
    inline struct timespec ctime() const {
        return _data->ctime();
    }
    inline struct timespec mtime() const {
        return _data->mtime();
    }

    /**
     * Get parent name
     */
    //TODO: rewrite without memory allocation
    inline std::string getParentName () const {
        if (name > full_name.c_str()) {
            return std::string (full_name, 0, static_cast<size_t>(name - full_name.c_str() - 1));
        } else {
            return "";
        }
    }

    /**
     * owner and group
     */
    void setUid (uid_t uid);
    void setGid (gid_t gid);
    inline uid_t uid () const {
        return _data->uid();
    }
    inline gid_t gid () const {
        return _data->gid();
    }

    zip_uint64_t size() const;

    bool present_in_zip() const { return _id >= 0; }
    zip_uint64_t id() const { return static_cast<zip_uint64_t>(_id); }

    void set_id(zip_int64_t id_) {
        _id = id_;
        // called only from FuseZipData::save, so we're don't worry about 'status' variable value
    }

    bool is_dir() const;

    bool hasComment() const { return m_comment != NULL; }
    bool isCommentChanged() const { return m_commentChanged; }
    bool setComment(const char *value, uint16_t length);
    const char *getComment() const { return m_comment; }
    uint16_t getCommentLength() const { return m_commentLen; }

    const char *name;
    std::string full_name;
    nodelist_t childs;
    FileNode *parent;
};
#endif