Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
isoonline
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Cederqvist
isoonline
Commits
24dbc847
Commit
24dbc847
authored
21 years ago
by
Per Cederqvist
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit.
parent
ef03d66e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
optimize.sql
+4
-0
4 additions, 0 deletions
optimize.sql
scanner.py
+118
-0
118 additions, 0 deletions
scanner.py
with
122 additions
and
0 deletions
optimize.sql
0 → 100644
+
4
−
0
View file @
24dbc847
optimize
table
base
;
optimize
table
file
;
optimize
table
media
;
optimize
table
contents
;
This diff is collapsed.
Click to expand it.
scanner.py
0 → 100644
+
118
−
0
View file @
24dbc847
#!/usr/bin/env python
# Scan filesystem for new files to back up.
import
os
import
sys
import
md5
import
sha
import
MySQLdb
class
file_hash
:
def
__init__
(
self
,
filename
):
fp
=
file
(
filename
,
"
rb
"
)
md
=
md5
.
new
()
sh
=
sha
.
new
()
while
1
:
chunk
=
fp
.
read
()
if
chunk
==
""
:
break
md
.
update
(
chunk
)
sh
.
update
(
chunk
)
fp
.
close
()
self
.
md5
=
md
.
hexdigest
()
self
.
sha1
=
sh
.
hexdigest
()
class
xreadchunks
:
def
__init__
(
self
,
file
,
delimiter
):
self
.
__file
=
file
self
.
__buf
=
""
self
.
__pos
=
0
self
.
__delim
=
delimiter
def
__getitem__
(
self
,
key
):
if
self
.
__pos
!=
key
:
raise
RuntimeError
while
1
:
ix
=
self
.
__buf
.
find
(
self
.
__delim
)
if
ix
!=
-
1
:
ret
=
self
.
__buf
[:
ix
]
self
.
__buf
=
self
.
__buf
[
ix
+
len
(
self
.
__delim
):]
self
.
__pos
+=
1
return
ret
fill
=
self
.
__file
.
read
(
8192
)
if
fill
==
""
:
# We lose the last line unless it terminated by a delimiter.
raise
IndexError
self
.
__buf
+=
fill
def
scan
(
DBH
,
dir_id
):
cursor
=
DBH
.
cursor
()
cursor
.
execute
(
"
SELECT dir_name FROM base
"
"
WHERE dir_id = %d
"
%
(
dir_id
,
))
dir_name
=
cursor
.
fetchone
()[
0
]
find
=
os
.
popen
(
"
find
"
+
dir_name
+
"
-type f
"
+
"
-printf
'
%TY-%Tm-%Td %TT %s %P
\\
0
'"
,
"
r
"
)
for
line
in
xreadchunks
(
find
,
"
\0
"
):
mtime
=
line
[
0
:
19
]
[
filesize
,
filename
]
=
line
[
20
:].
split
(
"
"
,
2
)
cursor
.
execute
(
"
LOCK TABLES file WRITE
"
)
cursor
.
execute
(
"
SELECT count(*) FROM file
"
"
WHERE filename = %s
"
"
AND dir_id = %s
"
"
AND mtime = %s
"
"
AND size = %s
"
,
(
filename
,
dir_id
,
mtime
,
filesize
))
count
=
cursor
.
fetchone
()[
0
]
if
count
==
0
:
h
=
file_hash
(
os
.
path
.
join
(
dir_name
,
filename
))
cursor
.
execute
(
"
INSERT INTO file
"
"
(filename, dir_id, mtime, size,
"
"
md5sum, sha1sum, verified, broken)
"
"
VALUES
"
"
(%s, %s, %s, %s,
"
"
%s, %s, NOW(), %s)
"
,
(
filename
,
dir_id
,
mtime
,
filesize
,
h
.
md5
,
h
.
sha1
,
0
))
cursor
.
execute
(
"
UNLOCK TABLES
"
)
cursor
.
close
()
def
scan_all
(
DBH
):
outer
=
DBH
.
cursor
()
inner
=
DBH
.
cursor
()
outer
.
execute
(
"
SELECT dir_id, first_scanned FROM base
"
"
WHERE active = 1
"
)
for
[
dir_id
,
first_scanned
]
in
outer
.
fetchall
():
scan
(
DBH
,
dir_id
)
if
first_scanned
==
None
:
inner
.
execute
(
"
UPDATE base
"
"
SET first_scanned = NOW(), last_scanned = NOW()
"
"
WHERE dir_id = %s
"
,
(
dir_id
,
))
else
:
inner
.
execute
(
"
UPDATE base
"
"
SET last_scanned = NOW()
"
"
WHERE dir_id = %s
"
,
(
dir_id
,
))
inner
.
close
()
outer
.
close
()
def
main
():
DBH
=
MySQLdb
.
connect
(
db
=
'
isoonline
'
)
scan_all
(
DBH
)
if
__name__
==
'
__main__
'
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment