how to do file lock on share folder(synchronization)

S

swc

Hi,

I have a xml file on a shared folder(network, on NAS), which can be write
and read from any CE device that's attached to it.
I used WNetAddConnection3() to connect to the drive, which uses the SMB
protocol. This protocol supposely have a function
to do a lock on the file, but I can't find a API to do this. I have search
the newsgroups, but I can't find any for CE.

Any knows how to do this?

Also, how can I detect if the network connected? couldn't find anything on
that either.

I am using a custom CE device, Windows CE 5.0,
using .NET CF (2.x).

Thanks,
 
P

Paul G. Tobey [eMVP]

I have a xml file on a shared folder(network, on NAS), which can be write
and read from any CE device that's attached to it.
I used WNetAddConnection3() to connect to the drive, which uses the SMB
protocol. This protocol supposely have a function
to do a lock on the file, but I can't find a API to do this. I have search
the newsgroups, but I can't find any for CE.

Any knows how to do this?

Be more specific about what you mean. "a lock on the file" isn't
descriptive enough. What do you wish to prevent or what happens when you
"lock" this file? If you just want to prevent other writers, you should be
able to open the file with no sharing enabled and I would think that the
network file system would do the right thing and prevent other machines from
accessing the file.
Also, how can I detect if the network connected? couldn't find anything on
that either.

When you say "connected"? You can tell if the network itself is hooked up
and, as far as Windows CE can tell, working by using network calls like
GetAdaptersInfo(), which OpenNETCF's Smart Device Framework already has
wrapped for you. You want to see if media state connected is set. Even if
that happens, however, there's no way to tell that your file open of a
network shared file will work, other than attempting it. That's pretty much
the way that all network stuff works...

Paul T.
 
S

swc

"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> wrote in message news:%23%[email protected]...
Be more specific about what you mean. "a lock on the file" isn't
descriptive enough. What do you wish to prevent or what happens when you
"lock" this file? If you just want to prevent other writers, you should
be able to open the file with no sharing enabled and I would think that
the network file system would do the right thing and prevent other
machines from accessing the file.

yes, that's what I meant. since the file can be read and write to by any
machine,
I need to actually have a lock to prevent any access to it by any other
machine,
when I upate the file, I need to a read, modify and write operation on the
file,
a simple open with no share will not work, because the read is done by .net
XmlDocument.load(), which closes the file before the modify and write
operation.

Before I read this response, I was thinking about create a no share file
open using another file, and check this file before accessing another file,
but that's kind of a hack. I was hopefully there is a more "elegant" or
"real" solution.
Like in the unix world, they have flock();(at least after googling, that's
what it looks like)
(and yes, I'm not a network programmer. pretty much just learn to connect to
network drives a few weeks ago)

When you say "connected"? You can tell if the network itself is hooked up
and, as far as Windows CE can tell, working by using network calls like
GetAdaptersInfo(), which OpenNETCF's Smart Device Framework already has
wrapped for you. You want to see if media state connected is set. Even
if that happens, however, there's no way to tell that your file open of a
network shared file will work, other than attempting it. That's pretty
much the way that all network stuff works...

I am worrying the connection got disconnected, like someone yank the cable
or power off the network device(NAS) while the application is running.
So I want to check it before I attempt a access to it and prevent any
problem as much as possible.
 
P

Paul G. Tobey [eMVP]

When you open the document for write with no sharing, that should prevent
anyone else from opening it, while you have it open. If someone else
already has it open for writing with no sharing, your attempt to open it for
writing should fail. Isn't that happening now? I suppose that you could
move away from XmlDocument and use something lower-level where you control
when the file is open. So, Load closes a stream that you pass it, when it's
done loading from that? I suppose that you could load the content of the
file into a String, keeping the file open, and pass the string to
XmlDocument.LoadXml(). I don't think there's any way to somehow create a
cross-platform synchronization, other than by a shared file.

On the network question, I suppose that you could try pinging a server that
you know will always be present and, if the ping fails, you don't try and if
it succeeds you do. Even if you do that, however, you still have to arrange
for sensible behavior on a network failure, as the network could be
disconnected between ping and the actual connection or, if it's a lengthy
process, during the process, rather than just before it starts. All of your
network code should assume that it will, at times, block for up to several
minutes. If it's code running in the same thread as the user interface, you
need to move it to a different thread or use asynchronous network I/O.
Ditto file I/O when the file might be across the network.

Paul T.

swc said:
"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> wrote in message news:%23%[email protected]...
Be more specific about what you mean. "a lock on the file" isn't
descriptive enough. What do you wish to prevent or what happens when you
"lock" this file? If you just want to prevent other writers, you should
be able to open the file with no sharing enabled and I would think that
the network file system would do the right thing and prevent other
machines from accessing the file.

yes, that's what I meant. since the file can be read and write to by any
machine,
I need to actually have a lock to prevent any access to it by any other
machine,
when I upate the file, I need to a read, modify and write operation on the
file,
a simple open with no share will not work, because the read is done by
.net
XmlDocument.load(), which closes the file before the modify and write
operation.

Before I read this response, I was thinking about create a no share file
open using another file, and check this file before accessing another
file,
but that's kind of a hack. I was hopefully there is a more "elegant" or
"real" solution.
Like in the unix world, they have flock();(at least after googling, that's
what it looks like)
(and yes, I'm not a network programmer. pretty much just learn to connect
to network drives a few weeks ago)

When you say "connected"? You can tell if the network itself is hooked
up and, as far as Windows CE can tell, working by using network calls
like GetAdaptersInfo(), which OpenNETCF's Smart Device Framework already
has wrapped for you. You want to see if media state connected is set.
Even if that happens, however, there's no way to tell that your file open
of a network shared file will work, other than attempting it. That's
pretty much the way that all network stuff works...

I am worrying the connection got disconnected, like someone yank the cable
or power off the network device(NAS) while the application is running.
So I want to check it before I attempt a access to it and prevent any
problem as much as possible.

 
S

swc

"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT
com> wrote in message news:[email protected]...
When you open the document for write with no sharing, that should prevent
anyone else from opening it, while you have it open. If someone else
already has it open for writing with no sharing, your attempt to open it
for writing should fail. Isn't that happening now? I suppose that you
could move away from XmlDocument and use something lower-level where you
control when the file is open. So, Load closes a stream that you pass it,
when it's done loading from that? I suppose that you could load the
content of the file into a String, keeping the file open, and pass the
string to XmlDocument.LoadXml(). I don't think there's any way to somehow
create a cross-platform synchronization, other than by a shared file.

That's good idea. thanks for the suggestion.

On the network question, I suppose that you could try pinging a server
that you know will always be present and, if the ping fails, you don't try
and if it succeeds you do. Even if you do that, however, you still have
to arrange for sensible behavior on a network failure, as the network
could be disconnected between ping and the actual connection or, if it's a
lengthy process, during the process, rather than just before it starts.
All of your network code should assume that it will, at times, block for
up to several minutes. If it's code running in the same thread as the
user interface, you need to move it to a different thread or use
asynchronous network I/O. Ditto file I/O when the file might be across the
network.




Paul T.

swc said:
"Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam
DOT com> wrote in message
I have a xml file on a shared folder(network, on NAS), which can be
write and read from any CE device that's attached to it.
I used WNetAddConnection3() to connect to the drive, which uses the SMB
protocol. This protocol supposely have a function
to do a lock on the file, but I can't find a API to do this. I have
search the newsgroups, but I can't find any for CE.

Any knows how to do this?

Be more specific about what you mean. "a lock on the file" isn't
descriptive enough. What do you wish to prevent or what happens when
you "lock" this file? If you just want to prevent other writers, you
should be able to open the file with no sharing enabled and I would
think that the network file system would do the right thing and prevent
other machines from accessing the file.

yes, that's what I meant. since the file can be read and write to by any
machine,
I need to actually have a lock to prevent any access to it by any other
machine,
when I upate the file, I need to a read, modify and write operation on
the file,
a simple open with no share will not work, because the read is done by
.net
XmlDocument.load(), which closes the file before the modify and write
operation.

Before I read this response, I was thinking about create a no share file
open using another file, and check this file before accessing another
file,
but that's kind of a hack. I was hopefully there is a more "elegant" or
"real" solution.
Like in the unix world, they have flock();(at least after googling,
that's what it looks like)
(and yes, I'm not a network programmer. pretty much just learn to connect
to network drives a few weeks ago)

Also, how can I detect if the network connected? couldn't find anything
on that either.

When you say "connected"? You can tell if the network itself is hooked
up and, as far as Windows CE can tell, working by using network calls
like GetAdaptersInfo(), which OpenNETCF's Smart Device Framework already
has wrapped for you. You want to see if media state connected is set.
Even if that happens, however, there's no way to tell that your file
open of a network shared file will work, other than attempting it.
That's pretty much the way that all network stuff works...

I am worrying the connection got disconnected, like someone yank the
cable
or power off the network device(NAS) while the application is running.
So I want to check it before I attempt a access to it and prevent any
problem as much as possible.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top