2 * Copyright 2006 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.mythtv;
20 import java.io.IOException;
21 import java.text.SimpleDateFormat;
22 import java.util.HashMap;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.wamblee.io.SimpleProcess;
28 import org.wamblee.io.DirectoryMonitor.Listener;
33 public class LinkStructure implements Listener {
35 private static final Log LOG = LogFactory.getLog(LinkStructure.class);
37 private String _monitorDir;
39 private File _linkDir;
41 private RecordingDatabase _database;
43 private SimpleDateFormat _format;
45 private Map<File,Recording> _recordings;
47 public LinkStructure(String aMonitorDir, File aLinkDir,
48 RecordingDatabase aDatabase) {
49 _monitorDir = aMonitorDir + "/";
52 _database = aDatabase;
53 _format = new SimpleDateFormat("yyyy-MM-dd-HH:mm");
54 _recordings = new HashMap<File,Recording>();
57 private void deleteDir(File aFile) {
58 for (File file: aFile.listFiles()) {
59 if ( file.isDirectory()) {
62 LOG.info("File deleted " + file + ": " + file.delete());
69 * @see org.wamblee.io.DirectoryMonitor.Listener#fileChanged(java.io.File)
71 public void fileChanged(File aFile) {
72 LOG.debug("file changed " + aFile);
74 // Re-assess file type
75 Recording recording = _recordings.get(aFile);
76 LOG.debug("Recording changed " + recording);
77 recording.setFilesize(aFile.length());
78 _database.update(recording);
79 String dir = getDirectory(recording);
80 FileType type = getFileType(aFile);
81 String path = dir + "/" + getFilename(recording, type);
83 if (exists(dir + "/" + getFilename(recording, type))) {
87 for (FileType t : FileType.values()) {
88 rmlink(dir + "/" + getFilename(recording, t));
90 createSymLink(_monitorDir + aFile.getName(), dir + "/"
91 + getFilename(recording, type));
98 * @see org.wamblee.io.DirectoryMonitor.Listener#fileCreated(java.io.File)
100 public void fileCreated(File aFile) {
101 LOG.debug("file created " + aFile);
102 Recording recording = _database.findRecording(aFile.getName());
103 _recordings.put(aFile, recording);
104 LOG.info("New recording detected " + aFile + " "
107 recording.setFilesize(aFile.length());
108 _database.update(recording);
109 String dir = getDirectory(recording);
111 createSymLink(_monitorDir + aFile.getName(), dir + "/"
112 + getFilename(recording, getFileType(aFile)));
118 * @see org.wamblee.io.DirectoryMonitor.Listener#fileDeleted(java.io.File)
120 public void fileDeleted(File aFile) {
121 LOG.debug("file deleted " + aFile);
122 Recording recording = _recordings.get(aFile);
123 _recordings.remove(recording);
124 // mythtv will remove the recording from its database itself.
125 LOG.info("recording deleted " + recording);
126 String dir = getDirectory(recording);
127 for (FileType t: FileType.values()) {
128 rmlink(dir + "/" + getFilename(recording, t));
133 private String getDirectory(Recording aRecording) {
134 return aRecording.getTitle().replaceAll("/", "-");
137 private FileType getFileType(File aFile) {
138 SimpleProcess process = new SimpleProcess(new File(_monitorDir), new String[] {
139 "file", aFile.getName() });
142 if (process.getStdout().contains("RIFF")) {
147 } catch (IOException e) {
148 LOG.error("Determining filetype for " + aFile + " failed", e);
153 private String getFilename(Recording aRecording, FileType aType) {
154 return (_format.format(aRecording.getProgstart()) + "-"
155 + aRecording.getSubtitle() + "-"
156 + aRecording.getChannel().getName() + "."
157 + aType.toString().toLowerCase()).replaceAll("/", "-");
160 private boolean exists(String aPath) {
161 LOG.debug("exists " + aPath);
162 return new File(_linkDir, aPath).exists();
165 private void rmlink(String aPath) {
166 LOG.debug("rmlink " + aPath);
167 File link = new File(_linkDir, aPath);
168 //if ( !link.exists()) {
171 if (!link.delete()) {
172 LOG.warn("Delete failed: " + aPath);
174 LOG.info("Removed link " + link);
178 private void mkdir(String aDir) {
179 LOG.debug("mkdir " + aDir);
180 File dir = new File(_linkDir, aDir);
181 if ( dir.isDirectory()) {
185 LOG.warn("Could not create directory path: " + aDir);
187 LOG.info("Created directory " + dir);
191 private void rmdir(String aDir) {
192 LOG.debug("rmdir " + aDir);
193 File dir = new File(_linkDir, aDir);
195 LOG.warn("Directory not deleted (still recordings left): " + aDir);
197 LOG.info("Directory " + dir + " deleted.");
201 private void createSymLink(String aTarget, String aSource) {
203 SimpleProcess process = new SimpleProcess(_linkDir, new String[] {
204 "ln", "-s", aTarget, aSource });
206 LOG.info("Created symlink " + aSource + " -> " + aTarget);
207 } catch (IOException e) {
209 "Could not create symlink: " + aTarget + " <- " + aSource,