-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBInterface.pm
164 lines (123 loc) · 3.05 KB
/
DBInterface.pm
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
package DBInterface;
use strict;
use warnings;
use Errno qw(ENOTDIR EIO ENOENT ENOANO);
use File::Basename;
use WebService::Dropbox;
use DateTime::Format::Strptime;
sub new
{
my ($class, $args) = @_;
my $object = {
dropbox => $args->{dropbox},
uid => $args->{uid},
gid => $args->{gid},
mode => $args->{mode},
blocksize => 1024,
};
bless($object, $class);
return $object;
}
sub dropbox_to_unix_time
{
my $dropbox_time = $_[0];
unless (defined $dropbox_time) {
return 0;
}
my $time_pattern = "%a, %d %b %Y %H:%M:%S %z";
my $strp = DateTime::Format::Strptime->new(pattern => $time_pattern);
return $strp->parse_datetime($dropbox_time)->epoch;
}
sub getattr
{
my ($self, $filepath) = @_;
my $file_info = $self->{dropbox}->metadata($filepath);
unless (defined $file_info) {
return -ENOENT();
}
my $block_size = $self->{blocksize};
my $size = $file_info->{bytes};
my $blocks = 1 + $size / $block_size;
my $file_time = dropbox_to_unix_time($file_info->{modified});
my $mode = $self->{mode};
$mode |= ($file_info->{is_dir} ? 0040000 : 0100000);
(0, # device
0, # inode (ignored by FUSE)
$mode, # Unix mode
1, # hard links
$self->{uid}, # UID
$self->{gid}, # GID
0, # rdev (for special files only)
$size, # file size in bytes
$file_time, # atime
$file_time, # mtime
$file_time, # ctime
$block_size, # block size in bytes
$blocks # "blocks" on disk
);
}
sub getdir
{
my ($self, $directory) = @_;
my $dir_info = $self->{dropbox}->metadata($directory);
unless (defined($dir_info) || $dir_info->{is_dir}) {
return ('.', ENOTDIR());
}
my @dir_entries = map {
my @components = fileparse($_->{path});
$components[0]
} @{ $dir_info->{contents} };
('.', '..', @dir_entries, 0);
}
sub mkdir
{
my ($self, $directory, $mode) = @_;
$self->{dropbox}->create_folder($directory) or return -EIO();
return 0;
}
sub unlink
{
my ($self, $path) = @_;
$self->{dropbox}->delete($path) or return -ENOENT();
return 0;
}
*rmdir = \&unlink;
sub rename
{
my ($self, $old_path, $new_path) = @_;
$self->{dropbox}->move($old_path, $new_path) or return -ENOENT();
return 0;
}
sub read
{
my ($self, $path, $size, $offset) = @_;
my $block = "";
my $end_byte = $offset + $size - 1;
my $response_code = sub {
$block .= $_[0];
};
my $files_opts = {
headers => [ 'Range' => "bytes=$offset-$end_byte" ],
};
$self->{dropbox}->files($path, $response_code, {}, $files_opts) or return -EIO();
return $block;
}
sub statfs
{
my $self = $_[0];
my $account_info = $self->{dropbox}->account_info() or return -ENOANO();
my $blocksize = $self->{blocksize};
my $total_blocks = 1 + $account_info->{quota_info}->{quota} / $blocksize;
my $used_blocks = 1 + $account_info->{quota_info}->{normal} / $blocksize;
my $free_blocks = $total_blocks - $used_blocks;
my $db_uid = $account_info->{uid};
(
255, # File name len
$db_uid, # Total inode count in FS
$db_uid / 2, # Total free inodes in FS
$total_blocks, # Total blocks in FS
$free_blocks, # Total free blocks in FS
$blocksize # Block size in bytes
);
}
1;