Skip to contentSkip to conversation

arecord: Remove only regular files

PATCH In review Alexander Volkov 3 participants 4 messages Sep 21, 2015 – Sep 23, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arecord: Remove only regular files

arecord removes a file before writing into it. It's not appropriate in some cases. For example, if you a pass a symlink to a file, then the symlink will be removed while the user expects to record into the symlink's target. Another case is recording into the device file. Some modems provide a tty device file as a voice device. And it's not possible to write into it under root with arecord, because it removes the device file.

So check the type of a file before writing into it and remove only regular files.

aplay/aplay.c +5 -1
index 459f7dd..1b2cdfc 100644
@@ -2929,6 +2929,7 @@ static void capture(char *orig_name)
 	char *name = orig_name;	/* current filename */
 	char namebuf[PATH_MAX+1];
 	off64_t count, rest;		/* number of bytes to capture */
+	struct stat statbuf;
 
 	/* get number of bytes to capture */
 	count = calc_count();
@@ -2973,7 +2974,10 @@ static void capture(char *orig_name)
 			}
 			
 			/* open a new file */
-			remove(name);
+			if (!lstat(name, &statbuf)) {
+				if (S_ISREG(statbuf.st_mode))
+					remove(name);
+			}
 			fd = safe_open(name);
 			if (fd < 0) {
 				perror(name);
Signature · 1 line
2.4.0
#1 lore raw
Quoted 28 lines · Alexander Volkov

arecord removes a file before writing into it. It's not appropriate in some cases. For example, if you a pass a symlink to a file, then the symlink will be removed while the user expects to record into the symlink's target. Another case is recording into the device file. Some modems provide a tty device file as a voice device. And it's not possible to write into it under root with arecord, because it removes the device file.

So check the type of a file before writing into it and remove only regular files.

aplay/aplay.c +1 -0
index 459f7dd..1b2cdfc 100644
@@ -2929,6 +2929,7 @@ static void capture(char *orig_name)
        char *name = orig_name; /* current filename */
        char namebuf[PATH_MAX+1];
        off64_t count, rest;            /* number of bytes to capture */
+       struct stat statbuf;
 
        /* get number of bytes to capture */
        count = calc_count();
@@ -2973,7 +2974,10 @@ static void capture(char *orig_name)
                        }

aplay/aplay.c static void capture(char *orig_name) · line 2983

Quoted 10 lines
aplay/aplay.c +4 -1
 
                        /* open a new file */
-                       remove(name);
+                       if (!lstat(name, &statbuf)) {
+                               if (S_ISREG(statbuf.st_mode))
+                                       remove(name);
+                       }
                        fd = safe_open(name);
                        if (fd < 0) {
                                perror(name);

Would it work to just open with O_TRUNC?

-- Arun

#2 lore raw

22.09.2015 16:24, Arun Raghavan пишет:

Quoted 41 lines · Alexander Volkov
Quoted 28 lines

arecord removes a file before writing into it. It's not appropriate in some cases. For example, if you a pass a symlink to a file, then the symlink will be removed while the user expects to record into the symlink's target. Another case is recording into the device file. Some modems provide a tty device file as a voice device. And it's not possible to write into it under root with arecord, because it removes the device file.

So check the type of a file before writing into it and remove only regular files.

aplay/aplay.c +1 -0
index 459f7dd..1b2cdfc 100644
@@ -2929,6 +2929,7 @@ static void capture(char *orig_name)
         char *name = orig_name; /* current filename */
         char namebuf[PATH_MAX+1];
         off64_t count, rest;            /* number of bytes to capture */
+       struct stat statbuf;
 
         /* get number of bytes to capture */
         count = calc_count();
@@ -2973,7 +2974,10 @@ static void capture(char *orig_name)
                         }

aplay/aplay.c static void capture(char *orig_name) · line 2983

Quoted 10 lines
aplay/aplay.c +4 -1
 
                         /* open a new file */
-                       remove(name);
+                       if (!lstat(name, &statbuf)) {
+                               if (S_ISREG(statbuf.st_mode))
+                                       remove(name);
+                       }
                         fd = safe_open(name);
                         if (fd < 0) {
                                 perror(name);

Would it work to just open with O_TRUNC?

Quoted 1 line

-- Arun

I guess that "remove" is used here to exclude mutual access to the file. We can record to the created file while some program may read the removed file. Using O_TRUNC will break this use case.

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#3 lore raw
Quoted 43 lines · Александр Волков

22.09.2015 16:24, Arun Raghavan пишет:

Quoted 41 lines · Alexander Volkov
Quoted 28 lines

arecord removes a file before writing into it. It's not appropriate in some cases. For example, if you a pass a symlink to a file, then the symlink will be removed while the user expects to record into the symlink's target. Another case is recording into the device file. Some modems provide a tty device file as a voice device. And it's not possible to write into it under root with arecord, because it removes the device file.

So check the type of a file before writing into it and remove only regular files.

aplay/aplay.c +1 -0
index 459f7dd..1b2cdfc 100644
@@ -2929,6 +2929,7 @@ static void capture(char *orig_name)
         char *name = orig_name; /* current filename */
         char namebuf[PATH_MAX+1];
         off64_t count, rest;            /* number of bytes to capture */
+       struct stat statbuf;
 
         /* get number of bytes to capture */
         count = calc_count();
@@ -2973,7 +2974,10 @@ static void capture(char *orig_name)
                         }

aplay/aplay.c static void capture(char *orig_name) · line 2983

Quoted 10 lines
aplay/aplay.c +4 -1
 
                         /* open a new file */
-                       remove(name);
+                       if (!lstat(name, &statbuf)) {
+                               if (S_ISREG(statbuf.st_mode))
+                                       remove(name);
+                       }
                         fd = safe_open(name);
                         if (fd < 0) {
                                 perror(name);

Would it work to just open with O_TRUNC?

Quoted 5 lines
Quoted 1 line

-- Arun

I guess that "remove" is used here to exclude mutual access to the file. We can record to the created file while some program may read the removed file.
Using O_TRUNC will break this use case.

Yeah, it's a good argument. I applied your patch as is now.

thanks,

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel