PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > How to get to know if file is currently being written by another app?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to get to know if file is currently being written by another app?

Réponse
 
LinkBack Outils de la discussion
Vieux 10/11/2007, 15h46   #1
alexandis@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to get to know if file is currently being written by another app?

I run some remote binary with php script in background. The binary
creates some big files - a lot of files, they are being written 2
days, one after another, by chunks.

I output window where I display a list of these files by another php
script. I must show only those of them that are COMPLETELY DONE. How
can I do this?

I don't have access to binary sources.

OS is Solaris.

  Réponse avec citation
Vieux 10/11/2007, 16h27   #2
zdzisio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by anotherapp?

alexandis@gmail.com pisze:
> I run some remote binary with php script in background. The binary
> creates some big files - a lot of files, they are being written 2
> days, one after another, by chunks.
>
> I output window where I display a list of these files by another php
> script. I must show only those of them that are COMPLETELY DONE. How
> can I do this?
>
> I don't have access to binary sources.
>
> OS is Solaris.
>


lsof |grep $filename

z.
  Réponse avec citation
Vieux 11/11/2007, 09h47   #3
alexandis@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

it's some external util, there's no such available from command
line... should i ask admin to install it on Solaris?

  Réponse avec citation
Vieux 11/11/2007, 20h17   #4
Aaron Saray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

On Nov 10, 9:46 am, alexan...@gmail.com wrote:
> I run some remote binary with php script in background. The binary
> creates some big files - a lot of files, they are being written 2
> days, one after another, by chunks.
>
> I output window where I display a list of these files by another php
> script. I must show only those of them that are COMPLETELY DONE. How
> can I do this?
>
> I don't have access to binary sources.
>
> OS is Solaris.


What if you checked out the 'flock' functions of PHP? You might be
able to lock the file, then check to see if they're locked by the
other script... etc.

  Réponse avec citation
Vieux 13/11/2007, 17h01   #5
saul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

In article <1194812238.516514.122610@k79g2000hse.googlegroups .com>,
102degrees@102degrees.com says...
> On Nov 10, 9:46 am, alexan...@gmail.com wrote:
> > I run some remote binary with php script in background. The binary
> > creates some big files - a lot of files, they are being written 2
> > days, one after another, by chunks.
> >
> > I output window where I display a list of these files by another php
> > script. I must show only those of them that are COMPLETELY DONE. How
> > can I do this?
> >
> > I don't have access to binary sources.
> >
> > OS is Solaris.

>
> What if you checked out the 'flock' functions of PHP? You might be
> able to lock the file, then check to see if they're locked by the
> other script... etc.
>
>


Didnt I read somewhere that flock is unreliable?
Any info on that would be appreciated.

saul
  Réponse avec citation
Vieux 13/11/2007, 19h01   #6
NC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

On Nov 13, 9:01 am, saul <s...@dummy.com> wrote:
> In article <1194812238.516514.122...@k79g2000hse.googlegroups .com>,
> 102degr...@102degrees.com says...
>
>
> > What if you checked out the 'flock' functions of PHP? You might be
> > able to lock the file, then check to see if they're locked by the
> > other script... etc.

>
> Didnt I read somewhere that flock is unreliable?


In threaded environments (which usually means Windows)

> Any info on that would be appreciated.


http://www.php.net/flock

Cheers,
NC

  Réponse avec citation
Vieux 14/11/2007, 14h08   #7
alexandis@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

Flock doesn't fit me. Flock is used to make lock FROM WITHIN php
script.
And in my case I have external binaries, that create some output
files, writing them piece by piece.

And what i need to do is to CHECK if some file in filesystem is being
written in the moment or not.

The only approach i can see is to check modification date of the file
and if file has not been modified quite a while - suppose that it's
already done. But this approach is not reliable...

  Réponse avec citation
Vieux 14/11/2007, 16h15   #8
NC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

On Nov 14, 6:08 am, alexan...@gmail.com wrote:
>
> Flock doesn't fit me. Flock is used to make lock
> FROM WITHIN php script.


Indeed. So if a PHP script can obtain an exclusive (writer's) lock on
a file, it means that no other process has that lock, meaning that the
file is not open for writing at the moment...

Cheers,
NC

  Réponse avec citation
Vieux 14/11/2007, 18h20   #9
alexandis@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

Wow, if things are like that - I will give it a try... Thanks.

  Réponse avec citation
Vieux 14/11/2007, 20h55   #10
alexandis@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

i tried to open file in write mode and oops! now it's zero... maybe i
should have used w+...
Is it correct and safe like following?

if ($handle = opendir($sourcefile_dir)) {
while (false !== ($filename = readdir($handle))) {
if ($filename != "." && $filename != ".." && is_file($sourcefile_dir.
$filename)) {
$fp = fopen($sourcefile_dir.$filename, "w+");
if (flock($fp, LOCK_EX)) {
?>
Outputting file info
<?php
flock($fp, LOCK_UN);
}
fclose($fp);
}
}
closedir($handle);
}
?>

  Réponse avec citation
Vieux 14/11/2007, 21h52   #11
alexandis@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

damned, no! again zero! But i even didn't start to write into file!
SO i should open it as for reading?

  Réponse avec citation
Vieux 15/11/2007, 01h07   #12
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by another app?

>> Flock doesn't fit me. Flock is used to make lock
>> FROM WITHIN php script.

>
>Indeed. So if a PHP script can obtain an exclusive (writer's) lock on
>a file, it means that no other process has that lock, meaning that the
>file is not open for writing at the moment...


flock() is advisory, meaning that it won't stop someone else from
reading or writing, unless that someone else is obtaining locks
before doing the reading or writing. *Mandatory* locking has great
potential for evil (e.g. lock some critical system files, then go
to sleep indefinitely, and anyone trying to log in ends up waiting
for you to let go of the locks).

  Réponse avec citation
Vieux 15/11/2007, 22h45   #13
NC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by anotherapp?

On Nov 14, 12:55 pm, alexan...@gmail.com wrote:
>
> i tried to open file in write mode and oops!
> now it's zero... maybe i should have used w+...


Nope; "a"...

Cheers,
NC
  Réponse avec citation
Vieux 08/12/2007, 19h41   #14
alexandis@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by anotherapp?

I now had chance to check this.
At least the following DIDN'T WORK - even being-written files were
shown:

while (false !== ($filename = readdir($handle))) {
if ($filename != "." && $filename != "..") {
$fp = fopen($resultsourcefile_dir.$filename, "a
+");
if (flock($fp, LOCK_EX)) {
flock($fp, LOCK_UN);
//display the file
}
}
}

p.s. I had to use "a+", not "a", because I also read from the file
inside the loop. But if it doesn't matter, right? Maybe it's just a
matter of OS...
  Réponse avec citation
Vieux 09/12/2007, 00h00   #15
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get to know if file is currently being written by anotherapp?

alexandis@gmail.com wrote:
> I now had chance to check this.
> At least the following DIDN'T WORK - even being-written files were
> shown:
>
> while (false !== ($filename = readdir($handle))) {
> if ($filename != "." && $filename != "..") {
> $fp = fopen($resultsourcefile_dir.$filename, "a
> +");
> if (flock($fp, LOCK_EX)) {
> flock($fp, LOCK_UN);
> //display the file
> }
> }
> }
>
> p.s. I had to use "a+", not "a", because I also read from the file
> inside the loop. But if it doesn't matter, right? Maybe it's just a
> matter of OS...
>


This will work if the other application also uses flock(). But if it
doesn't, there isn't any real way you can tell.

The bottom line is - if the other application doesn't "play nice",
there's not much your application can do.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,25912 seconds with 23 queries