Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-ds-flatfs
Commits
bcc4dcb5
Unverified
Commit
bcc4dcb5
authored
Apr 13, 2020
by
Steven Allen
Committed by
GitHub
Apr 13, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #80 from ipfs/feat/clean-remove
cleanup putMany implementation
parents
c1f339f4
bc42aaa6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
35 deletions
+42
-35
flatfs.go
flatfs.go
+42
-35
No files found.
flatfs.go
View file @
bcc4dcb5
...
...
@@ -537,42 +537,43 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
return
ErrClosed
}
var
dirsToSync
[]
string
files
:=
make
(
map
[
*
os
.
File
]
*
op
,
len
(
data
))
ops
:=
make
(
map
[
*
os
.
File
]
int
,
len
(
data
))
type
putManyOp
struct
{
key
datastore
.
Key
file
*
os
.
File
dstPath
string
srcPath
string
}
var
(
dirsToSync
=
make
(
map
[
string
]
struct
{},
len
(
data
))
files
=
make
([]
putManyOp
,
0
,
len
(
data
))
closed
int
removed
int
)
defer
func
()
{
for
fi
:=
range
files
{
val
:=
ops
[
fi
]
switch
val
{
case
0
:
_
=
fi
.
Close
()
fallthrough
case
1
:
_
=
os
.
Remove
(
fi
.
Name
())
for
closed
<
len
(
files
)
{
files
[
closed
]
.
file
.
Close
()
closed
++
}
for
removed
<
len
(
files
)
{
_
=
os
.
Remove
(
files
[
removed
]
.
srcPath
)
removed
++
}
}()
closer
:=
func
()
error
{
for
fi
:=
range
files
{
if
ops
[
fi
]
!=
0
{
continue
}
for
closed
<
len
(
files
)
{
fi
:=
files
[
closed
]
.
file
if
fs
.
sync
{
if
err
:=
syncFile
(
fi
);
err
!=
nil
{
return
err
}
}
if
err
:=
fi
.
Close
();
err
!=
nil
{
return
err
}
// signify closed
ops
[
fi
]
=
1
closed
++
}
return
nil
}
...
...
@@ -582,7 +583,7 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
if
err
:=
fs
.
makeDirNoSync
(
dir
);
err
!=
nil
{
return
err
}
dirsToSync
=
append
(
dirsToSync
,
dir
)
dirsToSync
[
dir
]
=
struct
{}{}
tmp
,
err
:=
fs
.
tempFile
()
...
...
@@ -604,16 +605,17 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
return
err
}
// Do this _first_ so we close it if writing fails.
files
=
append
(
files
,
putManyOp
{
key
:
key
,
file
:
tmp
,
dstPath
:
path
,
srcPath
:
tmp
.
Name
(),
})
if
_
,
err
:=
tmp
.
Write
(
value
);
err
!=
nil
{
return
err
}
files
[
tmp
]
=
&
op
{
typ
:
opRename
,
path
:
path
,
tmp
:
tmp
.
Name
(),
key
:
key
,
}
}
// Now we sync everything
...
...
@@ -624,19 +626,24 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
}
// move files to their proper places
for
fi
,
op
:=
range
files
{
done
,
err
:=
fs
.
doWriteOp
(
op
)
for
_
,
pop
:=
range
files
{
done
,
err
:=
fs
.
doWriteOp
(
&
op
{
typ
:
opRename
,
key
:
pop
.
key
,
tmp
:
pop
.
srcPath
,
path
:
pop
.
dstPath
,
})
if
err
!=
nil
{
return
err
}
else
if
done
{
// signify removed
ops
[
fi
]
=
2
}
else
if
!
done
{
_
=
os
.
Remove
(
pop
.
file
.
Name
())
}
removed
++
}
// now sync the dirs for those files
if
fs
.
sync
{
for
_
,
dir
:=
range
dirsToSync
{
for
dir
:=
range
dirsToSync
{
if
err
:=
syncDir
(
dir
);
err
!=
nil
{
return
err
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment